Ejemplo n.º 1
0
    def recv(self, wsgi=False):
        try:
            stream = HttpStream(self.reader, kind=HTTP_REQUEST, parser_class=HttpParser, decompress=True)

            if bool(wsgi):
                environ = stream.wsgi_environ()
                environ['wsgi.url_scheme'] = guess_scheme(environ)
                environ['wsgi.input'] = stream.body_file()
                environ['wsgi.socket'] = self.socket
                return environ

            # BUG:
            # http-parser has an issue here, if we call 'method' before 'headers'
            # and invalid method name is returned...
            fields  = stream.headers()
            method  = stream.method()
            url     = stream.url()
            version = stream.version()
            content = stream.body_file()

            url      = urlparse(url)
            path     = url.path
            query    = parse_qs(url.query, keep_blank_values=True)
            fragment = url.fragment

            for k, v in six.iteritems(dict(query)):
                query[k] = parse_query_value(v)

            self.version = 'HTTP/%s.%s' % version
            return method, path, query, fragment, fields, content
        except NoMoreData:
            pass
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
 def handle(self, upsock, peer):
     with closing(upsock) as upsock:
         origaddr = upsock.getsockname()
         self.logger.debug('intercepted connection from {0} to {1}'.format(peer, origaddr))
         p = HttpStream(SocketReader(upsock))
         self.logger.debug('request: method={0} url={1} version={2} headers={3}'.format(p.method(), p.url(), p.version(), p.headers()))
         url = None
         if p.method() == b'CONNECT':
             self.handle_connect(p, upsock)
         elif p.url()[0] == '/':
             self.handle_transparent(p, upsock)
         else:
             self.handle_proxy(p, upsock)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def rewrite_request(self, req, extra):

        try:
            while True:
                if extra.get('rewrite_location', True):
                    parser = HttpStream(req)

                    prefix = extra.get('prefix', '')
                    location = parser.url()
                    if prefix:
                        try:
                            location = location.split(prefix, 1)[1] or '/'
                        except IndexError:
                            pass

                    headers = rewrite_headers(parser, location,
                                              [('host', extra.get('host'))])

                    if headers is None:
                        break

                    extra['path'] = parser.path()

                    req.writeall(headers)
                    body = parser.body_file()
                    while True:
                        data = body.read(8192)
                        if not data:
                            break
                        req.writeall(data)
                else:
                    while True:
                        data = req.read(io.DEFAULT_BUFFER_SIZE)
                        if not data:
                            break
                        req.writeall(data)
        except (socket.error, NoMoreData):
            pass
Ejemplo n.º 6
0
    def rewrite_request(self, req, extra):
    
        try:
            while True:
                if extra.get('rewrite_location', True):
                    parser = HttpStream(req)
                    
                    prefix = extra.get('prefix', '')
                    location = parser.url()
                    if prefix:
                        try:
                            location = location.split(prefix, 1)[1] or '/'
                        except IndexError:
                            pass
                    
                    headers = rewrite_headers(parser, location,
                            [('host', extra.get('host'))])

                    if headers is None:
                        break

                    extra['path'] = parser.path()

                    req.writeall(headers)
                    body = parser.body_file()
                    while True:
                        data = body.read(8192)
                        if not data:
                            break
                        req.writeall(data)
                else:
                    while True:
                        data = req.read(io.DEFAULT_BUFFER_SIZE)
                        if not data:
                            break
                        req.writeall(data) 
        except (socket.error, NoMoreData):
            pass
Ejemplo n.º 7
0
def rewrite_request(req):
    try:
        while True:
            parser = HttpStream(req)
            headers = parser.headers()

            parsed_url = urlparse.urlparse(parser.url())

            is_ssl = parsed_url.scheme == "https"

            host = get_host(parse_address(parsed_url.netloc, 80),
                            is_ssl=is_ssl)
            headers['Host'] = host
            headers['Connection'] = 'close'

            if 'Proxy-Connection' in headers:
                del headers['Proxy-Connection']

            location = urlparse.urlunparse(
                ('', '', parsed_url.path, parsed_url.params, parsed_url.query,
                 parsed_url.fragment))

            httpver = "HTTP/%s" % ".".join(map(str, parser.version()))

            new_headers = [
                "%s %s %s\r\n" % (parser.method(), location, httpver)
            ]

            new_headers.extend(["%s: %s\r\n" % (hname, hvalue) \
                    for hname, hvalue in headers.items()])

            req.writeall(bytes("".join(new_headers) + "\r\n"))
            body = parser.body_file()
            send_body(req, body, parser.is_chunked())

    except (socket.error, NoMoreData, ParserError):
        pass
Ejemplo n.º 8
0
def rewrite_request(req):
    try:
        while True:
            parser = HttpStream(req)
            headers = parser.headers()

            parsed_url = urlparse.urlparse(parser.url())

            is_ssl = parsed_url.scheme == "https"

            host = get_host(parse_address(parsed_url.netloc, 80),
                is_ssl=is_ssl)
            headers['Host'] = host
            headers['Connection'] = 'close'

            if 'Proxy-Connection' in headers:
                del headers['Proxy-Connection']


            location = urlparse.urlunparse(('', '', parsed_url.path,
                parsed_url.params, parsed_url.query, parsed_url.fragment))

            httpver = "HTTP/%s" % ".".join(map(str, 
                        parser.version()))

            new_headers = ["%s %s %s\r\n" % (parser.method(), location, 
                httpver)]

            new_headers.extend(["%s: %s\r\n" % (hname, hvalue) \
                    for hname, hvalue in headers.items()])

            req.writeall(bytes("".join(new_headers) + "\r\n"))
            body = parser.body_file()
            send_body(req, body, parser.is_chunked())

    except (socket.error, NoMoreData, ParserError):
            pass
Ejemplo n.º 9
0
    def http_proxy(self, connectreq: HttpStream):
        reqheaders = connectreq.headers()
        # if ":" in connectreq.path():
        parsed_url = connectreq.url().split('//')[1].split(':')
        if len(parsed_url) > 1:
            host = parsed_url[0]
            port = parsed_url[1].split('/')[0]
        else:
            host, port = reqheaders['host'], 80
        if 'Proxy-Connection' in reqheaders:
            del reqheaders['Proxy-Connection']
        elif 'proxy-connection' in reqheaders:
            del reqheaders['proxy-connection']

        # request to mail.ru as client
        sock_server = socket.create_connection((host, port))
        try:
            clientreq = join_data(connectreq, 'Request')
        except (http_parser.http.ParserError, http_parser.http.BadStatusLine, ConnectionResetError):
            return

        sock_server.sendall(clientreq)

        # request from mail.ru to localhost as client
        if connectreq.method() == 'HEAD':
            return
        server_reader = SocketReader(sock_server)
        server_resp = HttpStream(server_reader, decompress=True)
        try:
            server_resp_tosend = join_data(server_resp, 'Response')
        except (http_parser.http.ParserError, http_parser.http.BadStatusLine, ConnectionResetError):
            return
        self.request.sendall(server_resp_tosend)

        req = Request(connectreq.method(), server_resp.status_code(), clientreq, host)
        self.add_date_base(connectreq, req)