Exemple #1
0
    def read_request_headers(self, flow):
        self.request_message.arrived.wait()
        self.raise_zombie()

        if self.pushed:
            flow.metadata['h2-pushed-stream'] = True

        # pseudo header must be present, see https://http2.github.io/http2-spec/#rfc.section.8.1.2.3
        authority = self.request_message.headers.pop(':authority', "")
        method = self.request_message.headers.pop(':method')
        scheme = self.request_message.headers.pop(':scheme')
        path = self.request_message.headers.pop(':path')

        host, port = url.parse_authority(authority, check=True)
        port = port or url.default_port(scheme) or 0

        return http.HTTPRequest(
            host,
            port,
            method.encode(),
            scheme.encode(),
            authority.encode(),
            path.encode(),
            b"HTTP/2.0",
            self.request_message.headers,
            None,
            None,
            self.timestamp_start,
            self.timestamp_end,
        )
Exemple #2
0
 def handle_h2_event(self,
                     event: h2.events.Event) -> CommandGenerator[bool]:
     if isinstance(event, h2.events.RequestReceived):
         try:
             host, port, method, scheme, authority, path, headers = parse_h2_request_headers(
                 event.headers)
         except ValueError as e:
             yield from self.protocol_error(
                 f"Invalid HTTP/2 request headers: {e}")
             return True
         request = http.HTTPRequest(
             host=host,
             port=port,
             method=method,
             scheme=scheme,
             authority=authority,
             path=path,
             http_version=b"HTTP/2.0",
             headers=headers,
             content=None,
             trailers=None,
             timestamp_start=time.time(),
             timestamp_end=None,
         )
         self.streams[event.stream_id] = StreamState.HEADERS_RECEIVED
         yield ReceiveHttp(
             RequestHeaders(event.stream_id,
                            request,
                            end_stream=bool(event.stream_ended)))
         return False
     else:
         return (yield from super().handle_h2_event(event))
Exemple #3
0
def twebsocketflow(client_conn=True,
                   server_conn=True,
                   messages=True,
                   err=None,
                   handshake_flow=True):

    if client_conn is True:
        client_conn = tclient_conn()
    if server_conn is True:
        server_conn = tserver_conn()
    if handshake_flow is True:
        req = http.HTTPRequest("relative",
                               "GET",
                               "http",
                               "example.com",
                               "80",
                               "/ws",
                               "HTTP/1.1",
                               headers=net_http.Headers(
                                   connection="upgrade",
                                   upgrade="websocket",
                                   sec_websocket_version="13",
                                   sec_websocket_key="1234",
                               ),
                               content=b'')
        resp = http.HTTPResponse(
            "HTTP/1.1",
            101,
            reason=net_http.status_codes.RESPONSES.get(101),
            headers=net_http.Headers(
                connection='upgrade',
                upgrade='websocket',
                sec_websocket_accept=b'',
            ),
            content=b'',
        )
        handshake_flow = http.HTTPFlow(client_conn, server_conn)
        handshake_flow.request = req
        handshake_flow.response = resp

    f = websocket.WebSocketFlow(client_conn, server_conn, handshake_flow)
    handshake_flow.metadata['websocket_flow'] = f

    if messages is True:
        messages = [
            websocket.WebSocketMessage(websockets.OPCODE.BINARY, True,
                                       b"hello binary"),
            websocket.WebSocketMessage(websockets.OPCODE.TEXT, True,
                                       "hello text".encode()),
            websocket.WebSocketMessage(websockets.OPCODE.TEXT, False,
                                       "it's me".encode()),
        ]
    if err is True:
        err = terr()

    f.messages = messages
    f.error = err
    f.reply = controller.DummyReply()
    return f
Exemple #4
0
    def create_request(self, method, scheme, host, port, path):
        """
            this method creates a new artificial and minimalist request also adds it to flowlist
        """
        c = connections.ClientConnection.make_dummy(("", 0))
        s = connections.ServerConnection.make_dummy((host, port))

        f = http.HTTPFlow(c, s)
        headers = mitmproxy.net.http.Headers()

        req = http.HTTPRequest("absolute", method, scheme, host, port, path,
                               b"HTTP/1.1", headers, b"")
        f.request = req
        self.load_flow(f)
        return f
Exemple #5
0
 def read_request_headers(self):
     self.request_arrived.wait()
     self.raise_zombie()
     first_line_format, method, scheme, host, port, path = http2.parse_headers(self.request_headers)
     return http.HTTPRequest(
         first_line_format,
         method,
         scheme,
         host,
         port,
         path,
         b"HTTP/2.0",
         self.request_headers,
         None,
         timestamp_start=self.timestamp_start,
         timestamp_end=self.timestamp_end,
     )
Exemple #6
0
    def read_request_headers(self, flow):
        self.request_arrived.wait()
        self.raise_zombie()

        if self.pushed:
            flow.metadata['h2-pushed-stream'] = True

        first_line_format, method, scheme, host, port, path = http2.parse_headers(self.request_headers)
        return http.HTTPRequest(
            first_line_format,
            method,
            scheme,
            host,
            port,
            path,
            b"HTTP/2.0",
            self.request_headers,
            None,
            timestamp_start=self.timestamp_start,
            timestamp_end=self.timestamp_end,
        )
Exemple #7
0
def twebsocketflow(client_conn=True,
                   server_conn=True,
                   messages=True,
                   err=None,
                   handshake_flow=True):

    if client_conn is True:
        client_conn = tclient_conn()
    if server_conn is True:
        server_conn = tserver_conn()
    if handshake_flow is True:
        req = http.HTTPRequest(
            "example.com",
            80,
            b"GET",
            b"http",
            b"example.com",
            b"/ws",
            b"HTTP/1.1",
            headers=net_http.Headers(
                connection="upgrade",
                upgrade="websocket",
                sec_websocket_version="13",
                sec_websocket_key="1234",
            ),
            content=b'',
            trailers=None,
            timestamp_start=946681200,
            timestamp_end=946681201,
        )
        resp = http.HTTPResponse(
            b"HTTP/1.1",
            101,
            reason=net_http.status_codes.RESPONSES.get(101),
            headers=net_http.Headers(
                connection='upgrade',
                upgrade='websocket',
                sec_websocket_accept=b'',
            ),
            content=b'',
            trailers=None,
            timestamp_start=946681202,
            timestamp_end=946681203,
        )
        handshake_flow = http.HTTPFlow(client_conn, server_conn)
        handshake_flow.request = req
        handshake_flow.response = resp

    f = websocket.WebSocketFlow(client_conn, server_conn, handshake_flow)
    f.metadata['websocket_handshake'] = handshake_flow.id
    handshake_flow.metadata['websocket_flow'] = f.id
    handshake_flow.metadata['websocket'] = True

    if messages is True:
        messages = [
            websocket.WebSocketMessage(Opcode.BINARY, True, b"hello binary"),
            websocket.WebSocketMessage(Opcode.TEXT, True, b"hello text"),
            websocket.WebSocketMessage(Opcode.TEXT, False, b"it's me"),
        ]
    if err is True:
        err = terr()

    f.messages = messages
    f.error = err
    f.reply = controller.DummyReply()
    return f