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 {
|
2018-04-25 14:25:58 +00:00
|
|
|
|
|
|
|
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)
|
2018-04-25 14:25:58 +00:00
|
|
|
|
|
|
|
case header.UPGRADE: fmt.Printf("[upgrade] ")
|
2018-04-25 14:40:43 +00:00
|
|
|
err = r.checkUpgrade(head.Values)
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
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)
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
case header.WSKEY: fmt.Printf("[sec-websocket-key] ")
|
2018-04-25 15:01:51 +00:00
|
|
|
err = r.extractKey(head.Values)
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
case header.WSPROTOCOL: fmt.Printf("[sec-websocket-protocol] ")
|
2018-04-25 15:22:27 +00:00
|
|
|
err = r.extractProtocols(head.Values)
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
default:
|
|
|
|
return nil
|
2018-04-25 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 15:01:51 +00:00
|
|
|
|
2018-04-25 14:25:58 +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) }
|
2018-04-25 14:25:58 +00:00
|
|
|
}
|
|
|
|
fmt.Printf(" ]\n");
|
|
|
|
|
2018-04-25 13:41:39 +00:00
|
|
|
return nil
|
|
|
|
|
2018-04-25 13:01:12 +00:00
|
|
|
}
|
2018-04-25 15:21:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 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
|
|
|
|
|
|
|
|
}
|