Upgrade + Connection headers check
This commit is contained in:
parent
4bcfd9c6ab
commit
fcdcb42b62
|
@ -44,7 +44,11 @@ func (r *T) parseHeader(b []byte) error {
|
|||
err = r.extractHostPort(head.Values[0])
|
||||
|
||||
case header.UPGRADE: fmt.Printf("[upgrade] ")
|
||||
err = r.checkUpgrade(head.Values)
|
||||
|
||||
case header.CONNECTION: fmt.Printf("[connection] ")
|
||||
err = r.checkConnection(head.Values)
|
||||
|
||||
case header.WSKEY: fmt.Printf("[sec-websocket-key] ")
|
||||
case header.ORIGIN: fmt.Printf("[origin] ")
|
||||
case header.WSPROTOCOL: fmt.Printf("[sec-websocket-protocol] ")
|
||||
|
@ -92,3 +96,36 @@ func (r *T) extractHostPort(b []byte) error {
|
|||
return nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
// checkConnection checks the 'Connection' header
|
||||
// it MUST contain 'Upgrade'
|
||||
func (r T) checkConnection(bb [][]byte) error {
|
||||
|
||||
for _, b := range bb {
|
||||
|
||||
if strings.ToLower( string(b) ) == "upgrade" {
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return fmt.Errorf("Connection header must be 'Upgrade'")
|
||||
|
||||
}
|
||||
|
||||
// checkUpgrade checks the 'Upgrade' header
|
||||
// it MUST be 'websocket'
|
||||
func (r T) checkUpgrade(bb [][]byte) error {
|
||||
|
||||
if len(bb) != 1 {
|
||||
return fmt.Errorf("Upgrade header must have only 1 element")
|
||||
}
|
||||
|
||||
if strings.ToLower( string(bb[0]) ) == "websocket" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("Upgrade header must be 'websocket', got '%s'", bb[0])
|
||||
|
||||
}
|
Loading…
Reference in New Issue