108 lines
2.3 KiB
Go
108 lines
2.3 KiB
Go
package request
|
|
|
|
import (
|
|
"git.xdrm.io/gws/internal/http/upgrade/response"
|
|
"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 {
|
|
r.code = response.BAD_REQUEST
|
|
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 {
|
|
r.code = response.BAD_REQUEST
|
|
return fmt.Errorf("Error parsing header: %s", err)
|
|
}
|
|
|
|
/* (2) Manage header */
|
|
switch head.Name {
|
|
case header.HOST: err = r.extractHostPort(head.Values)
|
|
case header.ORIGIN: err = r.extractOrigin(head.Values)
|
|
case header.UPGRADE: err = r.checkUpgrade(head.Values)
|
|
case header.CONNECTION: err = r.checkConnection(head.Values)
|
|
case header.WSVERSION: err = r.checkVersion(head.Values)
|
|
case header.WSKEY: err = r.extractKey(head.Values)
|
|
case header.WSPROTOCOL: err = r.extractProtocols(head.Values)
|
|
|
|
default:
|
|
return nil
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
fmt.Printf("ERR: %s\n", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isComplete returns whether the Upgrade Request
|
|
// is complete (no missing required item)
|
|
func (r T) isComplete() error {
|
|
|
|
/* (1) Request-Line */
|
|
if !r.first {
|
|
return fmt.Errorf("Missing HTTP Request-Line");
|
|
}
|
|
|
|
/* (2) Host */
|
|
if len(r.host) == 0 {
|
|
return fmt.Errorf("Missing 'Host' header")
|
|
}
|
|
|
|
/* (3) Origin */
|
|
if !bypassOriginPolicy && len(r.origin) == 0 {
|
|
return fmt.Errorf("Missing 'Origin' header")
|
|
}
|
|
|
|
/* (4) Connection */
|
|
if !r.hasConnection {
|
|
return fmt.Errorf("Missing 'Connection' header");
|
|
}
|
|
|
|
/* (5) Upgrade */
|
|
if !r.hasUpgrade {
|
|
return fmt.Errorf("Missing 'Upgrade' header");
|
|
}
|
|
|
|
/* (6) Sec-WebSocket-Version */
|
|
if !r.hasVersion {
|
|
return fmt.Errorf("Missing 'Sec-WebSocket-Version' header");
|
|
}
|
|
|
|
/* (7) Sec-WebSocket-Key */
|
|
if len(r.key) < 1 {
|
|
return fmt.Errorf("Missing 'Sec-WebSocket-Key' header");
|
|
}
|
|
|
|
return nil
|
|
|
|
} |