ws/http/upgrade/request/private.go

77 lines
1.6 KiB
Go
Raw Normal View History

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