Пример #1
0
    def test_read_from_stringio(self):
        s = (b"HTTP/1.1 200 OK\r\n"
             b"Content-Length: 7\r\n"
             b"\r\n"
             b"content\r\n"
             b"HTTP/1.1 204 OK\r\n"
             b"\r\n")
        rfile = BytesIO(s)
        r = http1.read_response(rfile, treq())
        assert r.status_code == 200
        assert r.content == b"content"
        assert http1.read_response(rfile, treq()).status_code == 204

        rfile = BytesIO(s)
        # HEAD must not have content by spec. We should leave it on the pipe.
        r = http1.read_response(rfile, treq(method=b"HEAD"))
        assert r.status_code == 200
        assert r.content == b""

        with raises(HttpSyntaxException):
            http1.read_response(rfile, treq())
Пример #2
0
    def test_read_from_stringio(self):
        s = (
            b"HTTP/1.1 200 OK\r\n"
            b"Content-Length: 7\r\n"
            b"\r\n"
            b"content\r\n"
            b"HTTP/1.1 204 OK\r\n"
            b"\r\n"
        )
        rfile = BytesIO(s)
        r = http1.read_response(rfile, treq())
        assert r.status_code == 200
        assert r.content == b"content"
        assert http1.read_response(rfile, treq()).status_code == 204

        rfile = BytesIO(s)
        # HEAD must not have content by spec. We should leave it on the pipe.
        r = http1.read_response(rfile, treq(method=b"HEAD"))
        assert r.status_code == 200
        assert r.content == b""

        with raises(HttpSyntaxException):
            http1.read_response(rfile, treq())
Пример #3
0
    def connect(self):
        super(WebSocketsClient, self).connect()

        preamble = b'GET / HTTP/1.1'
        self.wfile.write(preamble + b"\r\n")
        headers = self.protocol.client_handshake_headers()
        self.client_nonce = headers["sec-websocket-key"].encode("ascii")
        self.wfile.write(bytes(headers) + b"\r\n")
        self.wfile.flush()

        resp = read_response(self.rfile, treq(method=b"GET"))
        server_nonce = self.protocol.check_server_handshake(resp.headers)

        if not server_nonce == self.protocol.create_server_nonce(self.client_nonce):
            self.close()
Пример #4
0
    def connect(self):
        super(WebSocketsClient, self).connect()

        preamble = b'GET / HTTP/1.1'
        self.wfile.write(preamble + b"\r\n")
        headers = self.protocol.client_handshake_headers()
        self.client_nonce = headers["sec-websocket-key"].encode("ascii")
        self.wfile.write(bytes(headers) + b"\r\n")
        self.wfile.flush()

        resp = read_response(self.rfile, treq(method=b"GET"))
        server_nonce = self.protocol.check_server_handshake(resp.headers)

        if not server_nonce == self.protocol.create_server_nonce(self.client_nonce):
            self.close()
Пример #5
0
    def test_simple(self):
        sc = ServerConnection((self.d.IFACE, self.d.port))
        sc.connect()
        f = tutils.tflow()
        f.server_conn = sc
        f.request.path = "/p/200:da"

        # use this protocol just to assemble - not for actual sending
        sc.wfile.write(http1.assemble_request(f.request))
        sc.wfile.flush()

        assert http1.read_response(sc.rfile, f.request, 1000)
        assert self.d.last_log()

        sc.finish()
Пример #6
0
    def test_simple(self):
        self.d = test.Daemon()
        sc = ServerConnection((self.d.IFACE, self.d.port))
        sc.connect()
        f = tutils.tflow()
        f.server_conn = sc
        f.request.path = "/p/200:da"

        # use this protocol just to assemble - not for actual sending
        sc.wfile.write(http1.assemble_request(f.request))
        sc.wfile.flush()

        assert http1.read_response(sc.rfile, f.request, 1000)
        assert self.d.last_log()

        sc.finish()
        self.d.shutdown()
Пример #7
0
    def test_simple(self):
        client = TCPClient(("127.0.0.1", self.proxy.port))
        client.connect()

        # call pathod server, wait a second to complete the request
        client.wfile.write(b"POST http://localhost:%d/p/200 HTTP/1.1\r\n"
                           b"Expect: 100-continue\r\n"
                           b"Content-Length: 16\r\n"
                           b"\r\n" % self.server.port)
        client.wfile.flush()

        assert client.rfile.readline() == b"HTTP/1.1 100 Continue\r\n"
        assert client.rfile.readline() == b"\r\n"

        client.wfile.write(b"0123456789abcdef\r\n")
        client.wfile.flush()

        resp = http1.read_response(client.rfile, treq())
        assert resp.status_code == 200

        client.finish()
Пример #8
0
    def test_simple(self):
        client = TCPClient(("127.0.0.1", self.proxy.port))
        client.connect()

        # call pathod server, wait a second to complete the request
        client.wfile.write(
            b"POST http://localhost:%d/p/200 HTTP/1.1\r\n"
            b"Expect: 100-continue\r\n"
            b"Content-Length: 16\r\n"
            b"\r\n" % self.server.port
        )
        client.wfile.flush()

        assert client.rfile.readline() == "HTTP/1.1 100 Continue\r\n"
        assert client.rfile.readline() == "\r\n"

        client.wfile.write(b"0123456789abcdef\r\n")
        client.wfile.flush()

        resp = http1.read_response(client.rfile, treq())
        assert resp.status_code == 200

        client.finish()
Пример #9
0
    def run(self):
        r = self.flow.request
        first_line_format_backup = r.first_line_format
        try:
            self.flow.response = None

            # If we have a channel, run script hooks.
            if self.channel:
                request_reply = self.channel.ask("request", self.flow)
                if isinstance(request_reply, HTTPResponse):
                    self.flow.response = request_reply

            if not self.flow.response:
                # In all modes, we directly connect to the server displayed
                if self.config.mode == "upstream":
                    server_address = self.config.upstream_server.address
                    server = ServerConnection(server_address, (self.config.host, 0))
                    server.connect()
                    if r.scheme == "https":
                        connect_request = make_connect_request((r.host, r.port))
                        server.wfile.write(http1.assemble_request(connect_request))
                        server.wfile.flush()
                        resp = http1.read_response(
                            server.rfile,
                            connect_request,
                            body_size_limit=self.config.body_size_limit
                        )
                        if resp.status_code != 200:
                            raise ReplayException("Upstream server refuses CONNECT request")
                        server.establish_ssl(
                            self.config.clientcerts,
                            sni=self.flow.server_conn.sni
                        )
                        r.first_line_format = "relative"
                    else:
                        r.first_line_format= "absolute"
                else:
                    server_address = (r.host, r.port)
                    server = ServerConnection(server_address, (self.config.host, 0))
                    server.connect()
                    if r.scheme == "https":
                        server.establish_ssl(
                            self.config.clientcerts,
                            sni=self.flow.server_conn.sni
                        )
                    r.first_line_format = "relative"

                server.wfile.write(http1.assemble_request(r))
                server.wfile.flush()
                self.flow.server_conn = server
                self.flow.response = HTTPResponse.wrap(http1.read_response(
                    server.rfile,
                    r,
                    body_size_limit=self.config.body_size_limit
                ))
            if self.channel:
                response_reply = self.channel.ask("response", self.flow)
                if response_reply == Kill:
                    raise Kill()
        except (ReplayException, HttpException, TcpException) as e:
            self.flow.error = Error(str(e))
            if self.channel:
                self.channel.ask("error", self.flow)
        except Kill:
            # Kill should only be raised if there's a channel in the
            # first place.
            from ..proxy.root_context import Log
            self.channel.tell("log", Log("Connection killed", "info"))
        except Exception:
            from ..proxy.root_context import Log
            self.channel.tell("log", Log(traceback.format_exc(), "error"))
        finally:
            r.first_line_format = first_line_format_backup
Пример #10
0
    def run(self):
        r = self.flow.request
        form_out_backup = r.form_out
        try:
            self.flow.response = None

            # If we have a channel, run script hooks.
            #if self.channel:
            #    request_reply = self.channel.ask("request", self.flow)
            #    if request_reply == Kill:
            #        raise Kill()
            #    elif isinstance(request_reply, HTTPResponse):
            #        self.flow.response = request_reply

            if not self.flow.response:
                # In all modes, we directly connect to the server displayed
                if self.config.mode == "upstream":
                    print("In upstream replay")
                    print(r.scheme)
                    print(r.method)
                    print("host [%s] port [%d]" % (r.host, r.port))
                    print(self.flow.server_conn.address)
                    server = ServerConnection(self.flow.server_conn.address,
                                              (self.config.host, 0))
                    server.connect()
                    print("connected")
                    #print(r)
                    if (r.method == "CONNECT"):
                        print('HTTPS replay')
                        connect_request = make_connect_request(
                            (r.host, r.port))
                        print(r)
                        server.wfile.write(http1.assemble_request(r))
                        server.wfile.flush()
                        resp = http1.read_response(
                            server.rfile,
                            connect_request,
                            body_size_limit=self.config.body_size_limit)
                        self.flow.response = HTTPResponse.wrap(resp)
                        print('https resp: %s' % self.flow.response)
                        if resp.status_code != 200:
                            raise ReplayException(
                                "Upstream server refuses CONNECT request")
                        server.establish_ssl(self.config.clientcerts,
                                             sni=self.flow.server_conn.sni)
                        r.form_out = "relative"
                        return
                    else:
                        if (r.scheme == 'https'):
                            #need to resend CONNECT request and establish before GET
                            print('GET in HTTPS')
                            r.form_out = "authority"
                            connect_request = make_connect_request(
                                (r.host, r.port))
                            oldmethod = r.method
                            r.method = 'CONNECT'
                            oldpath = r.path
                            r.path = None
                            server.wfile.write(http1.assemble_request(r))
                            server.wfile.flush()
                            resp = http1.read_response(
                                server.rfile,
                                connect_request,
                                body_size_limit=self.config.body_size_limit)
                            self.flow.response = HTTPResponse.wrap(resp)
                            print('https resp: %s' % self.flow.response)
                            if resp.status_code != 200:
                                raise ReplayException(
                                    "Upstream server refuses CONNECT request")
                            #set the old request and change to relative to keep
                            #it under the same host
                            r.method = oldmethod
                            r.path = oldpath
                            r.form_out = 'relative'
                            server.establish_ssl(self.config.clientcerts,
                                                 sni=self.flow.server_conn.sni)
                            print(r)
                        else:
                            r.form_out = 'absolute'
                else:
                    print("in NOT upstream replay")
                    server_address = (r.host, r.port)
                    server = ServerConnection(server_address,
                                              (self.config.host, 0))
                    server.connect()
                    if r.scheme == "https":
                        server.establish_ssl(self.config.clientcerts,
                                             sni=self.flow.server_conn.sni)
                    r.form_out = "relative"
                msg = http1.assemble_request(r)
                print('msg: %s' % msg)
                server.wfile.write(msg)
                #print("set current server to flush")
                server.wfile.flush()
                print("set current server to main server_conn")
                print(server)
                self.flow.server_conn = server
                myResponse = http1.read_response(
                    server.rfile,
                    r,
                    body_size_limit=self.config.body_size_limit)
                self.flow.response = HTTPResponse.wrap(myResponse)
                print('myresp: %s' % self.flow.response)
                #self.flow.client_conn.send(http1.assemble_response(myResponse))
            if self.channel:
                response_reply = self.channel.ask("response", self.flow)
                if response_reply == Kill:
                    print("raise kill")
                    raise Kill()
        except (ReplayException, HttpException, TcpException) as e:
            self.flow.error = Error(str(e))
            if self.channel:
                self.channel.ask("error", self.flow)
        except Kill:
            # Kill should only be raised if there's a channel in the
            # first place.
            from ..proxy.root_context import Log
            self.channel.tell("log", Log("Connection killed", "info"))
        except Exception as e:
            from ..proxy.root_context import Log
            print(e)
            #self.channel.tell("log", Log(traceback.format_exc(), "error"))
        finally:
            r.form_out = form_out_backup
            print("End Replay Thread")
Пример #11
0
    def run(self):
        r = self.flow.request
        form_out_backup = r.form_out
        try:
            self.flow.response = None

            # If we have a channel, run script hooks.
            #if self.channel:
            #    request_reply = self.channel.ask("request", self.flow)
            #    if request_reply == Kill:
            #        raise Kill()
            #    elif isinstance(request_reply, HTTPResponse):
            #        self.flow.response = request_reply

            if not self.flow.response:
                # In all modes, we directly connect to the server displayed
                if self.config.mode == "upstream":
                    print("In upstream replay")
                    print(r.scheme)
                    print(r.method)
                    print("host [%s] port [%d]" % (r.host, r.port))
                    print(self.flow.server_conn.address)
                    server = ServerConnection(self.flow.server_conn.address, (self.config.host, 0))
                    server.connect()
                    print("connected")
                    #print(r)
                    if (r.method == "CONNECT"):
                        print('HTTPS replay')
                        connect_request = make_connect_request((r.host, r.port))
                        print(r)
                        server.wfile.write(http1.assemble_request(r))
                        server.wfile.flush()
                        resp = http1.read_response(
                            server.rfile,
                            connect_request,
                            body_size_limit=self.config.body_size_limit
                        )
                        self.flow.response = HTTPResponse.wrap(resp)
                        print ('https resp: %s' % self.flow.response)
                        if resp.status_code != 200:
                            raise ReplayException("Upstream server refuses CONNECT request")
                        server.establish_ssl(
                            self.config.clientcerts,
                            sni=self.flow.server_conn.sni
                        )
                        r.form_out = "relative"
                        return
                    else:
                        if (r.scheme == 'https'):
                        #need to resend CONNECT request and establish before GET
                            print('GET in HTTPS')
                            r.form_out = "authority"
                            connect_request = make_connect_request((r.host, r.port))
                            oldmethod = r.method
                            r.method = 'CONNECT'
                            oldpath = r.path
                            r.path = None
                            server.wfile.write(http1.assemble_request(r))
                            server.wfile.flush()
                            resp = http1.read_response(
                                server.rfile,
                                connect_request,
                                body_size_limit=self.config.body_size_limit
                            )
                            self.flow.response = HTTPResponse.wrap(resp)
                            print ('https resp: %s' % self.flow.response)
                            if resp.status_code != 200:
                                raise ReplayException("Upstream server refuses CONNECT request")
                            #set the old request and change to relative to keep
                            #it under the same host
                            r.method = oldmethod
                            r.path = oldpath
                            r.form_out = 'relative'
                            server.establish_ssl(
                                self.config.clientcerts,
                                sni=self.flow.server_conn.sni
                            )
                            print(r)
                        else:
                            r.form_out = 'absolute'
                else:
                    print("in NOT upstream replay")
                    server_address = (r.host, r.port)
                    server = ServerConnection(server_address, (self.config.host, 0))
                    server.connect()
                    if r.scheme == "https":
                        server.establish_ssl(
                            self.config.clientcerts,
                            sni=self.flow.server_conn.sni
                        )
                    r.form_out = "relative"
                msg = http1.assemble_request(r)
                print('msg: %s' % msg)
                server.wfile.write(msg)
                #print("set current server to flush")
                server.wfile.flush()
                print("set current server to main server_conn")
                print(server)
                self.flow.server_conn = server
                myResponse = http1.read_response(
                    server.rfile,
                    r,
                    body_size_limit=self.config.body_size_limit
                )
                self.flow.response = HTTPResponse.wrap(myResponse)
                print('myresp: %s' % self.flow.response)
                #self.flow.client_conn.send(http1.assemble_response(myResponse))
            if self.channel:
                response_reply = self.channel.ask("response", self.flow)
                if response_reply == Kill:
                    print("raise kill")
                    raise Kill()
        except (ReplayException, HttpException, TcpException) as e:
            self.flow.error = Error(str(e))
            if self.channel:
                self.channel.ask("error", self.flow)
        except Kill:
            # Kill should only be raised if there's a channel in the
            # first place.
            from ..proxy.root_context import Log
            self.channel.tell("log", Log("Connection killed", "info"))
        except Exception as e:
            from ..proxy.root_context import Log
            print(e)
            #self.channel.tell("log", Log(traceback.format_exc(), "error"))
        finally:
            r.form_out = form_out_backup
            print("End Replay Thread")