mitmproxy

Return custom response with mitmproxy


I'm trying to return custom response when using mitmproxy with the following code

from libmproxy.models import HTTPResponse
from netlib.http import Headers

def request(context, flow):
    if flow.request.pretty_url.endswith("example.com/index.php"):
        resp = HTTPResponse(
            [1, 1], 200, "OK",
            Headers(Content_Type="text/html"),
            "<html><body>hello world</body></html>")
        flow.reply(resp)

But the result is not as expected, in browser it shows up with http status codes, headers och body as clear text

[1,1] 200 OK
Content-Type: text/html

<html><body>hello world</body></html>

how can i return it as actual "http" response so that chrome render it as html?


Solution

  • don't set http version as the example. Replace [1, 1] with "HTTP/1.1"

    from libmproxy.models import HTTPResponse
    from netlib.http import Headers
    
    def request(context, flow):
        if flow.request.pretty_url.endswith("example.com/index.php"):
            resp = HTTPResponse(
                "HTTP/1.1", 200, "OK",
                Headers(Content_Type="text/html"),
                "<html><body>hello world</body></html>")
            flow.reply(resp)