Example #1
0
 def send_response_headers(self, response):
     headers = response.headers.copy()
     headers.insert(0, ":status", str(response.status_code))
     with self.connections[self.client_conn].lock:
         self.connections[self.client_conn].safe_send_headers(
             self.raise_zombie,
             self.client_stream_id,
             headers
         )
Example #2
0
 def send_response_headers(self, response):
     headers = response.headers.copy()
     headers.insert(0, ":status", str(response.status_code))
     for forbidden_header in h2.utilities.CONNECTION_HEADERS:
         if forbidden_header in headers:
             del headers[forbidden_header]
     with self.connections[self.client_conn].lock:
         self.connections[self.client_conn].safe_send_headers(
             self.raise_zombie, self.client_stream_id, headers)
Example #3
0
 def send_response_headers(self, response):
     headers = response.headers.copy()
     headers.insert(0, ":status", str(response.status_code))
     for forbidden_header in h2.utilities.CONNECTION_HEADERS:
         if forbidden_header in headers:
             del headers[forbidden_header]
     with self.connections[self.client_conn].lock:
         self.connections[self.client_conn].safe_send_headers(
             self.raise_zombie,
             self.client_stream_id,
             headers
         )
Example #4
0
    def assemble_request(self, request):
        assert isinstance(request, mitmproxy.net.http.request.Request)

        authority = self.tcp_handler.sni if self.tcp_handler.sni else self.tcp_handler.address[
            0]
        if self.tcp_handler.address[1] != 443:
            authority += ":%d" % self.tcp_handler.address[1]

        headers = request.headers.copy()

        if ':authority' not in headers:
            headers.insert(0, ':authority', authority)
        headers.insert(0, ':scheme', request.scheme)
        headers.insert(0, ':path', request.path)
        headers.insert(0, ':method', request.method)

        if hasattr(request, 'stream_id'):
            stream_id = request.stream_id
        else:
            stream_id = self._next_stream_id()

        return list(
            itertools.chain(
                self._create_headers(
                    headers,
                    stream_id,
                    end_stream=(request.content is None
                                or len(request.content) == 0)),
                self._create_body(request.content, stream_id)))
Example #5
0
    def send_request_headers(self, request):
        if self.pushed:
            # nothing to do here
            return

        while True:
            self.raise_zombie()
            self.connections[self.server_conn].lock.acquire()

            max_streams = self.connections[self.server_conn].remote_settings.max_concurrent_streams
            if self.connections[self.server_conn].open_outbound_streams + 1 >= max_streams:
                # wait until we get a free slot for a new outgoing stream
                self.connections[self.server_conn].lock.release()
                time.sleep(0.1)
                continue

            # keep the lock
            break

        # We must not assign a stream id if we are already a zombie.
        self.raise_zombie()

        self.server_stream_id = self.connections[self.server_conn].get_next_available_stream_id()
        self.server_to_client_stream_ids[self.server_stream_id] = self.client_stream_id

        headers = request.headers.copy()
        if request.authority:
            headers.insert(0, ":authority", request.authority)
        headers.insert(0, ":path", request.path)
        headers.insert(0, ":method", request.method)
        headers.insert(0, ":scheme", request.scheme)

        priority_exclusive = None
        priority_depends_on = None
        priority_weight = None
        if self.handled_priority_event:
            # only send priority information if they actually came with the original HeadersFrame
            # and not if they got updated before/after with a PriorityFrame
            if not self.config.options.http2_priority:
                self.log("HTTP/2 PRIORITY information in HEADERS frame suppressed. Use --http2-priority to enable forwarding.", "debug")
            else:
                priority_exclusive = self.priority_exclusive
                priority_depends_on = self._map_depends_on_stream_id(self.server_stream_id, self.priority_depends_on)
                priority_weight = self.priority_weight

        try:
            self.connections[self.server_conn].safe_send_headers(
                self.raise_zombie,
                self.server_stream_id,
                headers,
                end_stream=(False if request.content or request.trailers or request.stream else True),
                priority_exclusive=priority_exclusive,
                priority_depends_on=priority_depends_on,
                priority_weight=priority_weight,
            )
        except Exception as e:  # pragma: no cover
            raise e
        finally:
            self.raise_zombie()
            self.connections[self.server_conn].lock.release()
    def assemble_response(self, response):
        assert isinstance(response, mitmproxy.net.http.response.Response)

        headers = response.headers.copy()

        if ':status' not in headers:
            headers.insert(0, b':status', str(response.status_code).encode())

        if hasattr(response, 'stream_id'):
            stream_id = response.stream_id
        else:
            stream_id = self._next_stream_id()

        return list(itertools.chain(
            self._create_headers(headers, stream_id, end_stream=(response.content is None or len(response.content) == 0)),
            self._create_body(response.content, stream_id),
        ))
Example #7
0
    def assemble_response(self, response):
        assert isinstance(response, mitmproxy.net.http.response.Response)

        headers = response.headers.copy()

        if ':status' not in headers:
            headers.insert(0, b':status', str(response.status_code).encode())

        if hasattr(response, 'stream_id'):
            stream_id = response.stream_id
        else:
            stream_id = self._next_stream_id()

        return list(itertools.chain(
            self._create_headers(headers, stream_id, end_stream=(response.content is None or len(response.content) == 0)),
            self._create_body(response.content, stream_id),
        ))
Example #8
0
    def send_request_headers(self, request):
        if self.pushed:
            # nothing to do here
            return

        while True:
            self.raise_zombie()
            self.connections[self.server_conn].lock.acquire()

            max_streams = self.connections[self.server_conn].remote_settings.max_concurrent_streams
            if self.connections[self.server_conn].open_outbound_streams + 1 >= max_streams:
                # wait until we get a free slot for a new outgoing stream
                self.connections[self.server_conn].lock.release()
                time.sleep(0.1)
                continue

            # keep the lock
            break

        # We must not assign a stream id if we are already a zombie.
        self.raise_zombie()

        self.server_stream_id = self.connections[self.server_conn].get_next_available_stream_id()
        self.server_to_client_stream_ids[self.server_stream_id] = self.client_stream_id

        headers = request.headers.copy()
        headers.insert(0, ":path", request.path)
        headers.insert(0, ":method", request.method)
        headers.insert(0, ":scheme", request.scheme)

        priority_exclusive = None
        priority_depends_on = None
        priority_weight = None
        if self.handled_priority_event:
            # only send priority information if they actually came with the original HeadersFrame
            # and not if they got updated before/after with a PriorityFrame
            if not self.config.options.http2_priority:
                self.log("HTTP/2 PRIORITY information in HEADERS frame suppressed. Use --http2-priority to enable forwarding.", "debug")
            else:
                priority_exclusive = self.priority_exclusive
                priority_depends_on = self._map_depends_on_stream_id(self.server_stream_id, self.priority_depends_on)
                priority_weight = self.priority_weight

        try:
            self.connections[self.server_conn].safe_send_headers(
                self.raise_zombie,
                self.server_stream_id,
                headers,
                end_stream=self.no_body,
                priority_exclusive=priority_exclusive,
                priority_depends_on=priority_depends_on,
                priority_weight=priority_weight,
            )
        except Exception as e:  # pragma: no cover
            raise e
        finally:
            self.raise_zombie()
            self.connections[self.server_conn].lock.release()
Example #9
0
    def assemble_request(self, request):
        assert isinstance(request, mitmproxy.net.http.request.Request)

        authority = self.tcp_handler.sni if self.tcp_handler.sni else self.tcp_handler.address[0]
        if self.tcp_handler.address[1] != 443:
            authority += ":%d" % self.tcp_handler.address[1]

        headers = request.headers.copy()

        if ':authority' not in headers:
            headers.insert(0, ':authority', authority)
        headers.insert(0, ':scheme', request.scheme)
        headers.insert(0, ':path', request.path)
        headers.insert(0, ':method', request.method)

        if hasattr(request, 'stream_id'):
            stream_id = request.stream_id
        else:
            stream_id = self._next_stream_id()

        return list(itertools.chain(
            self._create_headers(headers, stream_id, end_stream=(request.content is None or len(request.content) == 0)),
            self._create_body(request.content, stream_id)))
Example #10
0
    def send_request(self, message):
        if self.pushed:
            # nothing to do here
            return

        while True:
            self.raise_zombie()
            self.server_conn.h2.lock.acquire()

            max_streams = self.server_conn.h2.remote_settings.max_concurrent_streams
            if self.server_conn.h2.open_outbound_streams + 1 >= max_streams:
                # wait until we get a free slot for a new outgoing stream
                self.server_conn.h2.lock.release()
                time.sleep(0.1)
                continue

            # keep the lock
            break

        # We must not assign a stream id if we are already a zombie.
        self.raise_zombie()

        self.server_stream_id = self.server_conn.h2.get_next_available_stream_id()
        self.server_to_client_stream_ids[self.server_stream_id] = self.client_stream_id

        headers = message.headers.copy()
        headers.insert(0, ":path", message.path)
        headers.insert(0, ":method", message.method)
        headers.insert(0, ":scheme", message.scheme)

        priority_exclusive = None
        priority_depends_on = None
        priority_weight = None
        if self.handled_priority_event:
            # only send priority information if they actually came with the original HeadersFrame
            # and not if they got updated before/after with a PriorityFrame
            priority_exclusive = self.priority_exclusive
            priority_depends_on = self._map_depends_on_stream_id(self.server_stream_id, self.priority_depends_on)
            priority_weight = self.priority_weight

        try:
            self.server_conn.h2.safe_send_headers(
                self.raise_zombie,
                self.server_stream_id,
                headers,
                end_stream=self.no_body,
                priority_exclusive=priority_exclusive,
                priority_depends_on=priority_depends_on,
                priority_weight=priority_weight,
            )
        except Exception as e:  # pragma: no cover
            raise e
        finally:
            self.raise_zombie()
            self.server_conn.h2.lock.release()

        if not self.no_body:
            self.server_conn.h2.safe_send_body(
                self.raise_zombie,
                self.server_stream_id,
                [message.body]
            )