rust-websocket-lite icon indicating copy to clipboard operation
rust-websocket-lite copied to clipboard

RFC compliance for Upgrade request

Open 1tgr opened this issue 4 years ago • 0 comments

Mentioned by @bluetech on ba1f935f250b8249f2581d0620784c0f45fc737a:

https://github.com/1tgr/rust-websocket-lite/blob/ba1f935f250b8249f2581d0620784c0f45fc737a/websocket-codec/src/upgrade.rs#L118

This function is not exactly right; it will also accept e.g. Upgrade2 but it shouldn't.

To be fully pedantic, the websocket RFC says:

  1. The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword.
  2. The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token.

The HTTP RFC describes the Connection and Upgrade headers with the following syntax:

 Connection        = 1#connection-option
 connection-option = token

 Upgrade          = 1#protocol
 protocol         = protocol-name ["/" protocol-version]
 protocol-name    = token
 protocol-version = token

 token          = 1*tchar
 tchar          = (irrelevant)

 1#element => element *( OWS "," OWS element )

 OWS            = *( SP / HTAB )
                ; optional whitespace

Long story short:

for part in haystack.split(',') {
   let token = part.trim_start_matches([' ', '\t']).trim_end_matches([' ', '\t']);
   if token.eq_ignore_ascii_case(needle) {
        return true;
   }
}
return false;

1tgr avatar Jun 30 '20 15:06 1tgr