Esempio n. 1
0
    def proxy_connection(self, request, req_addr, is_ssl):
        """ do the proxy connection """
        proxy_settings = os.environ.get('%s_proxy' % request.parsed_url.scheme)

        if proxy_settings and proxy_settings is not None:
            request.is_proxied = True

            proxy_settings, proxy_auth = _get_proxy_auth(proxy_settings)
            addr = parse_netloc(urlparse.urlparse(proxy_settings))

            if is_ssl:
                if proxy_auth:
                    proxy_auth = 'Proxy-authorization: %s' % proxy_auth
                proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % req_addr

                user_agent = request.headers.iget('user_agent')
                if not user_agent:
                    user_agent = "User-Agent: restkit/%s\r\n" % __version__

                proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth,
                                               user_agent)

                conn = self._pool.get(host=addr[0],
                                      port=addr[1],
                                      pool=self._pool,
                                      is_ssl=is_ssl,
                                      extra_headers=[],
                                      **self.ssl_args)

                conn.send(proxy_pieces)
                p = HttpStream(SocketReader(conn.socket()),
                               kind=1,
                               decompress=True)

                if p.status_code != 200:
                    raise ProxyError("Tunnel connection failed: %d %s" %
                                     (p.status_code(), p.body_string()))
                # read body
                p.body_string()

            else:
                headers = []
                if proxy_auth:
                    headers = [('Proxy-authorization', proxy_auth)]

                conn = self._pool.get(host=addr[0],
                                      port=addr[1],
                                      pool=self._pool,
                                      is_ssl=False,
                                      extra_headers=[],
                                      **self.ssl_args)
            return conn

        return
Esempio n. 2
0
    def proxy_connection(self, request, req_addr, is_ssl):
        """ do the proxy connection """
        proxy_settings = os.environ.get('%s_proxy' %
                request.parsed_url.scheme)

        if proxy_settings and proxy_settings is not None:
            request.is_proxied = True

            proxy_settings, proxy_auth =  _get_proxy_auth(proxy_settings)
            addr = parse_netloc(urlparse.urlparse(proxy_settings))

            if is_ssl:
                if proxy_auth:
                    proxy_auth = 'Proxy-authorization: %s' % proxy_auth
                proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % req_addr

                user_agent = request.headers.iget('user_agent')
                if not user_agent:
                    user_agent = "User-Agent: restkit/%s\r\n" % __version__

                proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth,
                        user_agent)


                conn = self._pool.get(host=addr[0], port=addr[1],
                    pool=self._pool, is_ssl=is_ssl,
                    extra_headers=[], **self.ssl_args)


                conn.send(proxy_pieces)
                p = HttpStream(SocketReader(conn.socket()), kind=1,
                    decompress=True)

                if p.status_code != 200:
                    raise ProxyError("Tunnel connection failed: %d %s" %
                            (p.status_code(), p.body_string()))
                # read body
                p.body_string()

            else:
                headers = []
                if proxy_auth:
                    headers = [('Proxy-authorization', proxy_auth)]

                conn = self._pool.get(host=addr[0], port=addr[1],
                        pool=self._pool, is_ssl=False,
                        extra_headers=[], **self.ssl_args)
            return conn

        return
Esempio n. 3
0
 def read_get(self):
     reader = SocketReader(self.sock)
     resp = HttpStream(reader)
     status_code = resp.status_code()
     headers = dict(resp.headers())
     body = resp.body_string()
     return {"status_code": status_code, "headers": headers, "body": body}
Esempio n. 4
0
    def _handle_client(self, client, address):
        try:

            r = SocketReader(client)
            p = HttpStream(r)
            headers = p.headers()
            body = ""
            if "CONTENT-LENGTH" in headers and int(
                    headers["CONTENT-LENGTH"]) > 0:
                body = p.body_string(binary=False)

            # System requests
            if p.path() == "/alive":
                self._respond(client, "ok")
                return

            # User requests
            req = IpcServer.Request(self._respond, client, p.path(),
                                    p.method(), headers, body)
            if p.path() == "/message":
                req.respond()

            self.requests.put(req)

        except Exception as e:
            client.close()
            raise e
Esempio n. 5
0
 def send_downstream(self, p, rhost, rport, method, url, upsock):
     with self.connect_downstream(rhost, rport) as downsock:
         request = '{method} {url} HTTP/{version[0]}.{version[1]}\r\n{headers}\r\n\r\n'.format(method=method.decode(), url=url, version=p.version(), headers='\r\n'.join(['{0}: {1}'.format(name, value) for name, value in p.headers().items()]))
         self.logger.debug('sending request {0!r}'.format(request))
         downsock.send(request.encode())
         downstream = HttpStream(SocketReader(downsock))
         self.logger.debug('response: header={0}'.format(downstream.headers()))
         upsock.send('HTTP/{version[0]}.{version[1]} {code} {status}\r\n{headers}\r\n\r\n'.format(version=downstream.version(), code=downstream.status_code(), status=downstream.status(), headers='\r\n'.join(['{0}: {1}'.format(name, value) for name, value in downstream.headers().items()])).encode())
         upsock.send(downstream.body_string())
Esempio n. 6
0
    def handle(self):
        h = SocketReader(self.request)
        p = HttpStream(h)
        #delagate=url[p.url()]
        delagate = Server.urls[p.url()]
        data = p.body_string()
        if data:
            data = data.decode('utf-8')
        else:
            data = ""

        js = None
        data = self.createData(data)
        print(p.url(), data)
        try:
            res = delagate(self.request, data)
            #todo change to:
            # self.request.send(res)
        except Exception as ex:
            print(ex)
Esempio n. 7
0
def join_data(request: HttpStream, type_: str):
    assert type_ in ('Request', 'Response')
    # try:
    major, minor = request.version()
    # except ParserError:
    #     raise
    if type_ == 'Request':
        url = request.url()
        start_line = f'{request.method()} {url} HTTP/{major}.{minor}'
    else:
        start_line = f'HTTP/{major}.{minor} {request.status()}'

    body = request.body_string(binary=True)
    headers = request.headers()
    setcookies = headers['Set-Cookie'] if 'Set-Cookie' in headers else None
    if "Content-Encoding" in headers:
        assert headers["Content-Encoding"] == "br"
        body = brotli.decompress(body)

    for key in ['Transfer-Encoding', 'Content-Encoding', 'Content-Length', 'Set-Cookie']:
        if key in headers:
            del headers[key]

    headers['Content-Length'] = len(body)
    headers = [f'{key}: {value}' for key, value in headers.items()]
    if setcookies is not None:
        setcookies = setcookies.split(",")
        ind = 0
        while ind < len(setcookies):
            if setcookies[ind].lower()[:-4].endswith('expires'):
                headers.append(f"Set-Cookie: {setcookies[ind]}, {setcookies[ind + 1]}")
                ind += 2
            else:
                headers.append(f"Set-Cookie: {setcookies[ind]}")
                ind += 1
    headers = END_ONE_HEADER.join(headers)

    return (start_line + END_ONE_HEADER + headers + END_HEADERS).encode('ISO-8859-1') + body
Esempio n. 8
0
def test_204_no_content():
    stream = io.BytesIO(b'HTTP/1.1 204 No Content\r\n\r\n')
    response = HttpStream(stream)
    assert response.status_code() == 204
    assert response.body_string() == b''