ws/http/upgrade/request/private.go

78 lines
1.8 KiB
Go

package request
import (
"strconv"
"fmt"
"strings"
)
func (r *T) parseHeader(b []byte) {
/* (1) First line -> GET {uri} HTTP/{version}
---------------------------------------------------------*/
if !r.first {
/* (1) Split by ' ' */
parts := strings.Split(string(b), " ")
/* (2) Abort on failure */
if len(parts) != 3 {
return
}
/* (3) Store method */
r.httpMethod = parts[0]
/* (4) Store URI */
r.uri = parts[1]
/* (5) Fail if wrong version format */
if len(parts[2]) < len("HTTP/0") {
r.Err = fmt.Errorf("Invalid HTTP version [%s] expected [HTTP/] prefix")
return
}
/* (6) Extract version part */
versionString := parts[2][len("HTTP/"):]
version, err := strconv.ParseFloat(versionString, 32)
if err != nil {
r.Err = fmt.Errorf("Cannot parse HTTP version as float")
return
}
/* (7) Store HTTP version */
r.httpVersion = float32(version)
r.first = true
return
}
/* (2) Other lines -> Header-Name: Header-Value
---------------------------------------------------------*/
/* (1) Split by ': ' */
parts := strings.Split(string(b), ": ")
/* (2) Abort on failure */
if len(parts) != 2 {
return
}
/* (3) Match header */
switch strings.ToLower(parts[0]) {
case "host": fmt.Printf("[host] '%s'\n", parts[1])
case "upgrade": fmt.Printf("[upgrade] '%s'\n", parts[1])
case "connection": fmt.Printf("[connection] '%s'\n", parts[1])
case "sec-websocket-key": fmt.Printf("[sec-websocket-key] '%s'\n", parts[1])
case "origin": fmt.Printf("[origin] '%s'\n", parts[1])
case "sec-websocket-protocol": fmt.Printf("[sec-websocket-protocol] '%s'\n", parts[1])
case "sec-websocket-version": fmt.Printf("[sec-websocket-version] '%s'\n", parts[1])
}
}