2018-04-25 13:00:43 +00:00
|
|
|
package header
|
|
|
|
|
|
|
|
import (
|
2018-04-25 13:39:24 +00:00
|
|
|
// "regexp"
|
2018-04-25 13:00:43 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// parse tries to return a 'T' (httpHeader) from a byte array
|
|
|
|
func Parse(b []byte) (*T, error) {
|
|
|
|
|
|
|
|
/* (1) Split by ':' */
|
|
|
|
parts := bytes.Split(b, []byte(": "))
|
|
|
|
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, fmt.Errorf("Invalid HTTP header format '%s'", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Create instance */
|
|
|
|
inst := new(T)
|
|
|
|
|
|
|
|
/* (3) Check for header name */
|
|
|
|
switch strings.ToLower(string(parts[0])) {
|
|
|
|
case "host": inst.Name = HOST
|
|
|
|
case "upgrade": inst.Name = UPGRADE
|
|
|
|
case "connection": inst.Name = CONNECTION
|
|
|
|
case "origin": inst.Name = ORIGIN
|
|
|
|
case "sec-websocket-key": inst.Name = WSKEY
|
|
|
|
case "sec-websocket-protocol": inst.Name = WSPROTOCOL
|
|
|
|
case "sec-websocket-extensions": inst.Name = WSEXTENSIONS
|
|
|
|
case "sec-websocket-version": inst.Name = WSVERSION
|
|
|
|
default: inst.Name = UNKNOWN
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:39:24 +00:00
|
|
|
/* (4) Split values */
|
|
|
|
inst.Values = bytes.Split(parts[1], []byte(", "))
|
2018-04-25 13:00:43 +00:00
|
|
|
|
|
|
|
return inst, nil
|
|
|
|
|
|
|
|
}
|