def parse_extensions(data): """Parses Sec-WebSocket-Extensions header value returns a list of common.Extension objects. Leading LWSes must be trimmed. """ state = http_header_util.ParsingState(data) extension_list = [] while True: extension = _parse_extension(state) if extension is not None: extension_list.append(extension) http_header_util.consume_lwses(state) if http_header_util.peek(state) is None: break if not http_header_util.consume_string(state, ','): raise HandshakeError( 'Failed to parse Sec-WebSocket-Extensions header: ' 'Expected a comma but found %r' % http_header_util.peek(state)) http_header_util.consume_lwses(state) if len(extension_list) == 0: raise HandshakeError( 'Sec-WebSocket-Extensions header contains no valid extension') return extension_list
def parse_extensions(data, allow_quoted_string=False): """Parses Sec-WebSocket-Extensions header value returns a list of ExtensionParameter objects. Leading LWSes must be trimmed. """ state = http_header_util.ParsingState(data) extension_list = [] while True: extension = _parse_extension(state, allow_quoted_string) if extension is not None: extension_list.append(extension) http_header_util.consume_lwses(state) if http_header_util.peek(state) is None: break if not http_header_util.consume_string(state, ','): raise ExtensionParsingException( 'Failed to parse Sec-WebSocket-Extensions header: ' 'Expected a comma but found %r' % http_header_util.peek(state)) http_header_util.consume_lwses(state) if len(extension_list) == 0: raise ExtensionParsingException('No valid extension entry found') return extension_list
def parse_token_list(data): """Parses a header value which follows 1#token and returns parsed elements as a list of strings. Leading LWSes must be trimmed. """ state = http_header_util.ParsingState(data) token_list = [] while True: token = http_header_util.consume_token(state) if token is not None: token_list.append(token) http_header_util.consume_lwses(state) if http_header_util.peek(state) is None: break if not http_header_util.consume_string(state, ','): raise HandshakeException('Expected a comma but found %r' % http_header_util.peek(state)) http_header_util.consume_lwses(state) if len(token_list) == 0: raise HandshakeException('No valid token found') return token_list
def validate_subprotocol(subprotocol, hixie): """Validate a value in subprotocol fields such as WebSocket-Protocol, Sec-WebSocket-Protocol. See - HyBi 10: Section 5.1. and 5.2.2. - HyBi 00: Section 4.1. Opening handshake - Hixie 75: Section 4.1. Handshake """ if not subprotocol: raise HandshakeException('Invalid subprotocol name: empty') if hixie: # Parameter should be in the range U+0020 to U+007E. for c in subprotocol: if not 0x20 <= ord(c) <= 0x7e: raise HandshakeException( 'Illegal character in subprotocol name: %r' % c) else: # Parameter should be encoded HTTP token. state = http_header_util.ParsingState(subprotocol) token = http_header_util.consume_token(state) rest = http_header_util.peek(state) # If |rest| is not None, |subprotocol| is not one token or invalid. If # |rest| is None, |token| must not be None because |subprotocol| is # concatenation of |token| and |rest| and is not None. if rest is not None: raise HandshakeException('Invalid non-token string in subprotocol ' 'name: %r' % rest)
def validate_subprotocol(subprotocol, hixie): """Validate a value in the Sec-WebSocket-Protocol field. See - RFC 6455: Section 4.1., 4.2.2., and 4.3. - HyBi 00: Section 4.1. Opening handshake Args: hixie: if True, checks if characters in subprotocol are in range between U+0020 and U+007E. It's required by HyBi 00 but not by RFC 6455. """ if not subprotocol: raise HandshakeException('Invalid subprotocol name: empty') if hixie: # Parameter should be in the range U+0020 to U+007E. for c in subprotocol: if not 0x20 <= ord(c) <= 0x7e: raise HandshakeException( 'Illegal character in subprotocol name: %r' % c) else: # Parameter should be encoded HTTP token. state = http_header_util.ParsingState(subprotocol) token = http_header_util.consume_token(state) rest = http_header_util.peek(state) # If |rest| is not None, |subprotocol| is not one token or invalid. If # |rest| is None, |token| must not be None because |subprotocol| is # concatenation of |token| and |rest| and is not None. if rest is not None: raise HandshakeException('Invalid non-token string in subprotocol ' 'name: %r' % rest)
def validate_subprotocol(subprotocol): """Validate a value in the Sec-WebSocket-Protocol field. See the Section 4.1., 4.2.2., and 4.3. of RFC 6455. """ if not subprotocol: raise HandshakeException('Invalid subprotocol name: empty') # Parameter should be encoded HTTP token. state = http_header_util.ParsingState(subprotocol) token = http_header_util.consume_token(state) rest = http_header_util.peek(state) # If |rest| is not None, |subprotocol| is not one token or invalid. If # |rest| is None, |token| must not be None because |subprotocol| is # concatenation of |token| and |rest| and is not None. if rest is not None: raise HandshakeException('Invalid non-token string in subprotocol ' 'name: %r' % rest)