2018-04-24 21:10:01 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-25 13:54:15 +00:00
|
|
|
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
|
2018-04-24 21:10:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-04-25 13:41:39 +00:00
|
|
|
// parseHeader parses any http request line
|
|
|
|
// (header and request-line)
|
|
|
|
func (r *T) parseHeader(b []byte) error {
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
/* (1) First line -> GET {uri} HTTP/{version}
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if !r.first {
|
|
|
|
|
2018-04-25 07:58:50 +00:00
|
|
|
err := r.request.Parse(b)
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-04-25 13:41:39 +00:00
|
|
|
return fmt.Errorf("Error while parsing first line: %s", err)
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.first = true
|
2018-04-25 13:41:39 +00:00
|
|
|
return nil
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-25 07:58:50 +00:00
|
|
|
|
2018-04-24 21:10:01 +00:00
|
|
|
/* (2) Other lines -> Header-Name: Header-Value
|
|
|
|
---------------------------------------------------------*/
|
2018-04-25 13:01:12 +00:00
|
|
|
/* (1) Try to parse header */
|
|
|
|
head, err := header.Parse(b)
|
|
|
|
if err != nil {
|
2018-04-25 13:41:39 +00:00
|
|
|
return fmt.Errorf("Error parsing header: %s", err)
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 13:01:12 +00:00
|
|
|
/* (2) Manage header */
|
|
|
|
switch head.Name {
|
|
|
|
case header.HOST: fmt.Printf("[host] "); printValues(head.Values)
|
|
|
|
case header.UPGRADE: fmt.Printf("[upgrade] "); printValues(head.Values)
|
|
|
|
case header.CONNECTION: fmt.Printf("[connection] "); printValues(head.Values)
|
|
|
|
case header.WSKEY: fmt.Printf("[sec-websocket-key] "); printValues(head.Values)
|
|
|
|
case header.ORIGIN: fmt.Printf("[origin] "); printValues(head.Values)
|
|
|
|
case header.WSPROTOCOL: fmt.Printf("[sec-websocket-protocol] "); printValues(head.Values)
|
|
|
|
case header.WSVERSION: fmt.Printf("[sec-websocket-version] "); printValues(head.Values)
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:41:39 +00:00
|
|
|
return nil
|
|
|
|
|
2018-04-25 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func printValues(bb [][]byte){
|
|
|
|
|
|
|
|
for i, b := range bb {
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
fmt.Printf("[ '%s'", b)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(", '%s'", b)
|
|
|
|
}
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:01:12 +00:00
|
|
|
fmt.Printf(" ]\n");
|
|
|
|
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|