def handshake_headers(self): """ List of headers appropriate for the upgrade handshake. """ headers = [('Host', '%s:%s' % (self.host, self.port)), ('Connection', 'Upgrade'), ('Upgrade', 'websocket'), ('Sec-WebSocket-Key', self.key.decode('utf-8')), ('Sec-WebSocket-Version', str(max(WS_VERSION)))] if self.protocols: headers.append( ('Sec-WebSocket-Protocol', ','.join(self.protocols))) if self.extra_headers: headers.extend(self.extra_headers) if not any(x for x in headers if x[0].lower() == 'origin'): scheme, url = self.url.split(":", 1) parsed = urlsplit(url, scheme="http") if parsed.hostname: self.host = parsed.hostname else: self.host = 'localhost' origin = scheme + '://' + parsed.hostname if parsed.port: origin = origin + ':' + str(parsed.port) headers.append(('Origin', origin)) return headers
def handshake_headers(self): """ List of headers appropriate for the upgrade handshake. """ headers = [ ('Host', '%s:%s' % (self.host, self.port)), ('Connection', 'Upgrade'), ('Upgrade', 'websocket'), ('Sec-WebSocket-Key', self.key.decode('utf-8')), ('Sec-WebSocket-Version', str(max(WS_VERSION))) ] if self.protocols: headers.append(('Sec-WebSocket-Protocol', ','.join(self.protocols))) if self.extra_headers: headers.extend(self.extra_headers) if not any(x for x in headers if x[0].lower() == 'origin'): scheme, url = self.url.split(":", 1) parsed = urlsplit(url, scheme="http") if parsed.hostname: self.host = parsed.hostname else: self.host = 'localhost' origin = scheme + '://' + parsed.hostname if parsed.port: origin = origin + ':' + str(parsed.port) headers.append(('Origin', origin)) return headers
def handshake_headers(self): """ List of headers appropriate for the upgrade handshake. """ headers = [ ("Host", "%s:%s" % (self.host, self.port)), ("Connection", "Upgrade"), ("Upgrade", "websocket"), ("Sec-WebSocket-Key", self.key.decode("utf-8")), ("Sec-WebSocket-Version", str(max(WS_VERSION))), ] if self.protocols: headers.append(("Sec-WebSocket-Protocol", ",".join(self.protocols))) if self.extra_headers: headers.extend(self.extra_headers) if not any(x for x in headers if x[0].lower() == "origin"): scheme, url = self.url.split(":", 1) parsed = urlsplit(url, scheme="http") if parsed.hostname: self.host = parsed.hostname else: self.host = "localhost" origin = scheme + "://" + parsed.hostname if parsed.port: origin = origin + ":" + str(parsed.port) headers.append(("Origin", origin)) return headers
def _parse_url(self): """ Parses a URL which must have one of the following forms: - ws://host[:port][path] - wss://host[:port][path] - ws+unix:///path/to/my.socket In the first two cases, the ``host`` and ``port`` attributes will be set to the parsed values. If no port is explicitely provided, it will be either 80 or 443 based on the scheme. Also, the ``resource`` attribute is set to the path segment of the URL (alongside any querystring). In addition, if the scheme is ``ws+unix``, the ``unix_socket_path`` attribute is set to the path to the Unix socket while the ``resource`` attribute is set to ``/``. """ # Python 2.6.1 and below don't parse ws or wss urls properly. netloc is empty. # See: https://github.com/Lawouach/WebSocket-for-Python/issues/59 scheme, url = self.url.split(":", 1) parsed = urlsplit(url, scheme="http") if parsed.hostname: self.host = parsed.hostname elif '+unix' in scheme: self.host = 'localhost' else: raise ValueError("Invalid hostname from: %s", self.url) if parsed.port: self.port = parsed.port if scheme == "ws": if not self.port: self.port = 80 elif scheme in ("wss", "https"): scheme = "wss" if not self.port: self.port = 443 elif scheme in ('ws+unix', 'wss+unix'): pass else: raise ValueError("Invalid scheme: %s" % scheme) if parsed.path: resource = parsed.path else: resource = "/" if '+unix' in scheme: self.unix_socket_path = resource resource = '/' if parsed.query: resource += "?" + parsed.query self.scheme = scheme self.resource = resource
def _parse_url(self): """ Parses a URL which must have one of the following forms: - ws://host[:port][path] - wss://host[:port][path] - ws+unix:///path/to/my.socket In the first two cases, the ``host`` and ``port`` attributes will be set to the parsed values. If no port is explicitely provided, it will be either 80 or 443 based on the scheme. Also, the ``resource`` attribute is set to the path segment of the URL (alongside any querystring). In addition, if the scheme is ``ws+unix``, the ``unix_socket_path`` attribute is set to the path to the Unix socket while the ``resource`` attribute is set to ``/``. """ # Python 2.6.1 and below don't parse ws or wss urls properly. netloc is empty. # See: https://github.com/Lawouach/WebSocket-for-Python/issues/59 scheme, url = self.url.split(":", 1) parsed = urlsplit(url, scheme="http") if parsed.hostname: self.host = parsed.hostname elif '+unix' in scheme: self.host = 'localhost' else: raise ValueError("Invalid hostname from: %s", self.url) if parsed.port: self.port = parsed.port if scheme == "ws": if not self.port: self.port = 80 elif scheme == "wss": if not self.port: self.port = 443 elif scheme in ('ws+unix', 'wss+unix'): pass else: raise ValueError("Invalid scheme: %s" % scheme) if parsed.path: resource = parsed.path else: resource = "/" if '+unix' in scheme: self.unix_socket_path = resource resource = '/' if parsed.query: resource += "?" + parsed.query self.scheme = scheme self.resource = resource
def _parse_url(self): if ":" not in self.url: raise ValueError("Invalid URL: %s" % self.url) # Python 2.6.1 and below don't parse ws or wss urls properly. netloc is empty. # See: https://github.com/Lawouach/WebSocket-for-Python/issues/59 scheme, url = self.url.split(":", 1) parsed = urlsplit(url, scheme="http") if parsed.hostname: self.host = parsed.hostname else: raise ValueError("Invalid hostname from: %s", self.url) if parsed.port: self.port = parsed.port if scheme == "ws": if not self.port: self.port = 80 elif scheme == "wss": if not self.port: self.port = 443 else: raise ValueError("Invalid scheme: %s" % scheme) if parsed.path: resource = parsed.path else: resource = "/" if parsed.query: resource += "?" + parsed.query self.scheme = scheme self.resource = resource
def parse_url(self, url): self.parsed_url = urlsplit(url, scheme="http") self.scheme = self.parsed_url.scheme