Beispiel #1
0
    def _send_request(self, method, url, body=None, headers=None, timeout=None):
        p = urlparse(url)
        if p.hostname and p.hostname != self._host \
            or p.scheme == "http" and self._secure \
            or p.scheme == "https" and not self._secure \
            or p.port and p.port != self._port:
            self.fire(NotConnected())
            return

        resource = p.path
        if p.query:
            resource += "?" + p.query
        if headers is None:
            headers = Headers([])
        else:
            headers = Headers([(k, v) for k, v in headers.items()])
        # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
        if not headers.has_key("Host"):
            headers["Host"] = self._host \
                + (":" + str(self._port)) if self._port else ""
        command = "%s %s HTTP/1.1" % (method, resource)
        if body is not None:
            headers["Content-Length"] = len(body)
        message = "%s\r\n%s" % (command, headers)
        self._outstanding += 1
        if timeout is not None:
            self._timer = Timer(timeout, SocketError(ETIMEDOUT), self.channel) \
                .register(self)
        self.fire(write(message.encode('utf-8')), self._transport)
        if body is not None:
            self.fire(write(body), self._transport)
Beispiel #2
0
    def request(self, method, url, body=None, headers={}):
        host, port, path, secure = parse_url(url)

        if not self._transport.connected:
            self.fire(connect(host, port, secure))
            yield self.wait("connected", self._transport.channel)

        headers = Headers([(k, v) for k, v in headers.items()])

        # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
        if "Host" not in headers:
            headers["Host"] = "{0:s}{1:s}".format(
                host, "" if port in (80, 443) else ":{0:d}".format(port)
            )

        if body is not None:
            headers["Content-Length"] = len(body)

        command = "%s %s HTTP/1.1" % (method, path)
        message = "%s\r\n%s" % (command, headers)
        self.fire(write(message.encode('utf-8')), self._transport)
        if body is not None:
            self.fire(write(body), self._transport)

        yield (yield self.wait("response"))
Beispiel #3
0
    def _send_request(self, method, url, body=None, headers={}, timeout=None):
        p = urlparse(url)
        if p.hostname and p.hostname != self._host \
            or p.scheme == "http" and self._secure \
            or p.scheme == "https" and not self._secure \
            or p.port and p.port != self._port:
            self.fire(NotConnected())
            return

        resource = p.path
        if p.query:
            resource += "?" + p.query
        headers = Headers([(k, v) for k, v in list(headers.items())])
        # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
        if "Host" not in headers:
            headers["Host"] = self._host \
                + (":" + str(self._port)) if self._port else ""
        command = "%s %s HTTP/1.1" % (method, resource)
        if body is not None:
            headers["Content-Length"] = len(body)
        message = "%s\r\n%s" % (command, headers)
        self._outstanding += 1
        if timeout is not None:
            self._timer = Timer(timeout, SocketError(ETIMEDOUT), self.channel) \
                .register(self)
        self.fire(write(message.encode('utf-8')), self._transport)
        if body is not None:
            self.fire(write(body), self._transport)
Beispiel #4
0
    def request(self, method, url, body=None, headers=None):
        host, port, path, secure = parse_url(url)

        if not self._transport.connected:
            self.fire(connect(host, port, secure))
            yield self.wait("connected", self._transport.channel)

        headers = Headers([(k, v) for k, v in (headers or {}).items()])

        # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
        if "Host" not in headers:
            headers["Host"] = "{0:s}{1:s}".format(
                host, "" if port in (80, 443) else ":{0:d}".format(port)
            )

        if body is not None:
            headers["Content-Length"] = len(body)

        command = "%s %s HTTP/1.1" % (method, path)
        message = "%s\r\n%s" % (command, headers)
        self.fire(write(message.encode('utf-8')), self._transport)
        if body is not None:
            self.fire(write(body), self._transport)

        yield (yield self.wait("response"))
Beispiel #5
0
 def _on_connected(self, host, port):
     headers = Headers([(k, v) for k, v in self._headers.items()])
     # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
     if "Host" not in headers:
         headers["Host"] = self._host \
             + (":" + str(self._port)) if self._port else ""
     headers["Upgrade"] = "websocket"
     headers["Connection"] = "Upgrade"
     try:
         sec_key = os.urandom(16)
     except NotImplementedError:
         sec_key = "".join([chr(random.randint(0, 255)) for i in range(16)])
     headers["Sec-WebSocket-Key"] = base64.b64encode(sec_key).decode(
         "latin1")
     headers["Sec-WebSocket-Version"] = "13"
     UNSAFE_CHARS = re.compile(
         '[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~]'
     )
     escaped_resource = UNSAFE_CHARS.sub(
         '',
         self._resource.encode('ASCII', 'replace').decode('ASCII'))
     command = "GET %s HTTP/1.1" % (escaped_resource, )
     message = "%s\r\n%s" % (command, headers)
     self._pending += 1
     self.fire(write(message.encode('utf-8')), self._transport)
     return True
Beispiel #6
0
 def _on_connected(self, host, port):
     headers = Headers([(k, v) for k, v in self._headers.items()])
     # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
     if not "Host" in headers:
         headers["Host"] = self._host \
             + (":" + str(self._port)) if self._port else ""
     headers["Upgrade"] = "websocket"
     headers["Connection"] = "Upgrade"
     try:
         sec_key = os.urandom(16)
     except NotImplementedError:
         sec_key = "".join([chr(random.randint(0, 255)) for i in range(16)])
     headers["Sec-WebSocket-Key"] = base64.b64encode(sec_key).decode(
         "latin1")
     headers["Sec-WebSocket-Version"] = "13"
     command = "GET %s HTTP/1.1" % self._resource
     message = "%s\r\n%s" % (command, headers)
     self._pending += 1
     self.fire(write(message.encode('utf-8')), self._transport)
     return True