Пример #1
0
    def _read_request_origin_form(self, client_conn, scheme, host, port):
        """
        Read a HTTP request with regular (origin-form) request line.
        An example origin-form request line would be:
            GET /foo.html HTTP/1.1

        The request destination is already known from one of the following sources:
        1) transparent proxy: destination provided by platform resolver
        2) reverse proxy: fixed destination
        3) regular proxy: known from CONNECT command.
        """
        if scheme.lower() == "https" and not self.ssl_established:
            self.establish_ssl(client_conn, host, port)

        line = self.get_line(self.rfile)
        if line == "":
            return None

        r = http.parse_init_http(line)
        if not r:
            raise ProxyError(400, "Bad HTTP request line: %s" % repr(line))
        method, path, httpversion = r
        headers = self.read_headers(authenticate=False)
        content = http.read_http_body_request(self.rfile, self.wfile, headers,
                                              httpversion,
                                              self.config.body_size_limit)
        return flow.Request(client_conn, httpversion, host, port, scheme,
                            method, path, headers, content,
                            self.rfile.first_byte_timestamp, utils.timestamp())
Пример #2
0
 def read_request(self, client_conn):
     if self.config.transparent_proxy:
         host, port = self.config.transparent_proxy["resolver"].original_addr(self.connection)
         if not self.ssl_established and (port in self.config.transparent_proxy["sslports"]):
             scheme = "https"
             certfile = self.find_cert(host, port, None)
             try:
                 self.convert_to_ssl(certfile, self.config.certfile or self.config.cacert)
             except tcp.NetLibError, v:
                 raise ProxyError(400, str(v))
         else:
             scheme = "http"
         host = self.sni or host
         line = self.get_line(self.rfile)
         if line == "":
             return None
         r = http.parse_init_http(line)
         if not r:
             raise ProxyError(400, "Bad HTTP request line: %s"%repr(line))
         method, path, httpversion = r
         headers = http.read_headers(self.rfile)
         if headers is None:
             raise ProxyError(400, "Invalid headers")
         content = http.read_http_body_request(
                     self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit
                 )
         return flow.Request(client_conn, httpversion, host, port, scheme, method, path, headers, content)
Пример #3
0
 def read_request_reverse(self, client_conn):
     line = self.get_line(self.rfile)
     if line == "":
         return None
     scheme, host, port = self.config.reverse_proxy
     r = http.parse_init_http(line)
     if not r:
         raise ProxyError(400, "Bad HTTP request line: %s" % repr(line))
     method, path, httpversion = r
     headers = self.read_headers(authenticate=False)
     content = http.read_http_body_request(self.rfile, self.wfile, headers,
                                           httpversion,
                                           self.config.body_size_limit)
     return flow.Request(client_conn, httpversion, host, port, "http",
                         method, path, headers, content,
                         self.rfile.first_byte_timestamp, utils.timestamp())
Пример #4
0
 def _read_request_absolute_form(self, client_conn, line):
     """
     When making a request to a proxy (other than CONNECT or OPTIONS),
     a client must send the target uri in absolute-form.
     An example absolute-form request line would be:
         GET http://www.example.com/foo.html HTTP/1.1
     """
     r = http.parse_init_proxy(line)
     if not r:
         raise ProxyError(400, "Bad HTTP request line: %s" % repr(line))
     method, scheme, host, port, path, httpversion = r
     headers = self.read_headers(authenticate=True)
     content = http.read_http_body_request(self.rfile, self.wfile, headers,
                                           httpversion,
                                           self.config.body_size_limit)
     return flow.Request(client_conn, httpversion, host, port, scheme,
                         method, path, headers, content,
                         self.rfile.first_byte_timestamp, utils.timestamp())
Пример #5
0
                    raise ProxyError(400, str(v))
        else:
            scheme = "http"
        line = self.get_line(self.rfile)
        if line == "":
            return None
        r = http.parse_init_http(line)
        if not r:
            raise ProxyError(400, "Bad HTTP request line: %s" % repr(line))
        method, path, httpversion = r
        headers = self.read_headers(authenticate=False)
        content = http.read_http_body_request(self.rfile, self.wfile, headers,
                                              httpversion,
                                              self.config.body_size_limit)
        return flow.Request(client_conn, httpversion, host, port, scheme,
                            method, path, headers, content,
                            self.rfile.first_byte_timestamp, utils.timestamp())

    def read_request_proxy(self, client_conn):
        line = self.get_line(self.rfile)
        if line == "":
            return None

        if not self.proxy_connect_state:
            connparts = http.parse_init_connect(line)
            if connparts:
                host, port, httpversion = connparts
                headers = self.read_headers(authenticate=True)
                self.wfile.write('HTTP/1.1 200 Connection established\r\n' +
                                 ('Proxy-agent: %s\r\n' %
                                  self.server_version) + '\r\n')
Пример #6
0
 elif self.config.reverse_proxy:
     line = self.get_line(self.rfile)
     if line == "":
         return None
     scheme, host, port = self.config.reverse_proxy
     r = http.parse_init_http(line)
     if not r:
         raise ProxyError(400, "Bad HTTP request line: %s"%repr(line))
     method, path, httpversion = r
     headers = http.read_headers(self.rfile)
     if headers is None:
         raise ProxyError(400, "Invalid headers")
     content = http.read_http_body_request(
                 self.rfile, self.wfile, headers, httpversion, self.config.body_size_limit
             )
     return flow.Request(client_conn, httpversion, host, port, "http", method, path, headers, content)
 else:
     line = self.get_line(self.rfile)
     if line == "":
         return None
     if line.startswith("CONNECT"):
         r = http.parse_init_connect(line)
         if not r:
             raise ProxyError(400, "Bad HTTP request line: %s"%repr(line))
         host, port, httpversion = r
         # FIXME: Discard additional headers sent to the proxy. Should I expose
         # these to users?
         while 1:
             d = self.rfile.readline()
             if d == '\r\n' or d == '\n':
                 break