ws/internal/http/upgrade/request/private.go

110 lines
2.2 KiB
Go
Raw Normal View History

package request
import (
"fmt"
"git.xdrm.io/go/websocket/internal/http/upgrade/request/parser/header"
"git.xdrm.io/go/websocket/internal/http/upgrade/response"
)
// 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 &InvalidRequest{"Request-Line", err.Error()}
}
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
}
// dispatch error
2018-05-01 20:14:35 +00:00
if err != nil {
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 &IncompleteRequest{"Request-Line"}
}
/* (2) Host */
if len(r.host) == 0 {
return &IncompleteRequest{"Host"}
}
/* (3) Origin */
2018-05-01 20:14:35 +00:00
if !bypassOriginPolicy && len(r.origin) == 0 {
return &IncompleteRequest{"Origin"}
}
/* (4) Connection */
if !r.hasConnection {
return &IncompleteRequest{"Connection"}
}
/* (5) Upgrade */
if !r.hasUpgrade {
return &IncompleteRequest{"Upgrade"}
}
/* (6) Sec-WebSocket-Version */
if !r.hasVersion {
return &IncompleteRequest{"Sec-WebSocket-Version"}
}
/* (7) Sec-WebSocket-Key */
if len(r.key) < 1 {
return &IncompleteRequest{"Sec-WebSocket-Key"}
}
return nil
}