Example #1
0
def request(url,
            method=None,
            target=None,
            cachebrowse=None,
            headers=None,
            port=None,
            scheme='http',
            raw_request=None):
    if raw_request is not None and (headers is not None or method is not None):
        raise ValueError(
            "Can't specify raw request and request method/headers together")
    if target and cachebrowse:
        raise ValueError("Can't specify target and use cachebrowse together")

    if cachebrowse is None:
        cachebrowse = True

    headers = headers or {}
    method = method or 'GET'

    if '://' not in url:
        url = scheme + '://' + url

    parsed_url = urlparse.urlparse(url, scheme=scheme)
    path = parsed_url.path or '/'

    if target:
        target, cachebrowsed = dns.resolve_host(
            target, use_cachebrowser_db=cachebrowse)
    else:
        target, cachebrowsed = dns.resolve_host(
            parsed_url.hostname, use_cachebrowser_db=cachebrowse)

    # if not cachebrowsed:
    # target = parsed_url.hostname

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if parsed_url.scheme == 'https':
        sock = ssl.wrap_socket(sock)
    sock.connect((target, port
                  or (443 if parsed_url.scheme == 'https' else 80)))

    if raw_request is None:
        headers = headers or {}
        for header in headers.keys():
            if ':' in header:
                del headers[header]
        headers["Host"] = parsed_url.hostname

        http_request = httplib.HTTPConnection(target, 80)
        http_request.sock = sock
        http_request.request(method, path, headers=headers)
        http_request.response_class = HttpResponse
        response = http_request.getresponse()
    else:
        sock.send(raw_request)
        response = HttpResponse(sock, method=method)
        response.begin()

    return response
Example #2
0
def request(url, method=None, target=None, cachebrowse=None, headers=None, port=None, scheme='http', raw_request=None):
    if raw_request is not None and (headers is not None or method is not None):
        raise ValueError("Can't specify raw request and request method/headers together")
    if target and cachebrowse:
        raise ValueError("Can't specify target and use cachebrowse together")

    if cachebrowse is None:
        cachebrowse = True

    headers = headers or {}
    method = method or 'GET'

    if '://' not in url:
        url = scheme + '://' + url

    parsed_url = urlparse.urlparse(url, scheme=scheme)
    path = parsed_url.path or '/'

    if target:
        target, cachebrowsed = dns.resolve_host(target, use_cachebrowser_db=cachebrowse)
    else:
        target, cachebrowsed = dns.resolve_host(parsed_url.hostname, use_cachebrowser_db=cachebrowse)

    if port is None and parsed_url.port:
        port = parsed_url.port

    # if not cachebrowsed:
    # target = parsed_url.hostname

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if parsed_url.scheme == 'https':
        sock = ssl.wrap_socket(sock)

    sock.connect((target, port or (443 if parsed_url.scheme == 'https' else 80)))

    if raw_request is None:
        headers = headers or {}
        for header in headers.keys():
            if ':' in header:
                del headers[header]
        headers["Host"] = parsed_url.hostname

        http_request = httplib.HTTPConnection(target, 80)
        http_request.sock = sock
        http_request.request(method, path, headers=headers)
        http_request.response_class = HttpResponse
        response = http_request.getresponse()
    else:
        sock.send(raw_request)
        response = HttpResponse(sock, method=method)
        response.begin()

    return response
Example #3
0
    def _connect_upstream(self, host, port):
        ip, cachebrowsed = dns.resolve_host(host)
        if not ip:
            return

        # Return response to client
        self.connection.send_local("HTTP/1.1 200 OK\r\n\r\n")

        # Create remote socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Shouldn't use SSL, ssl is forwarded directly from the client
        # sock = ssl.wrap_socket(sock)
        sock.connect((ip, port))

        self._upstream_started = True
        self._host = host

        # ! Ref to connection._buffer not updated
        self._buffer = StringIO()

        # !! Why does this line not work here?
        # self.connection.send_local("HTTP/1.1 200 OK\r\n\r\n")

        self.connection.start_remote(sock)

        return True
Example #4
0
    def _connect_upstream(self, host, port):
        ip, cachebrowsed = dns.resolve_host(host)
        if not ip:
            return

        # Return response to client
        self.connection.send_local("HTTP/1.1 200 OK\r\n\r\n")

        # Create remote socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Shouldn't use SSL, ssl is forwarded directly from the client
        # sock = ssl.wrap_socket(sock)
        sock.connect((ip, port))

        self._upstream_started = True
        self._host = host

        # ! Ref to connection._buffer not updated
        self._buffer = StringIO()

        # !! Why does this line not work here?
        # self.connection.send_local("HTTP/1.1 200 OK\r\n\r\n")

        self.connection.start_remote(sock)

        return True