Return response in an inline script on a URL that does not exist

I’m trying to return a response for a URL that only exists on a VPN, while I’m not connected to the VPN. When I’m not on the VPN the request times out, but I have a mock response to return for the request, but the response(flow): is not called.

In the docs under HTTP Events -> http_connect(flow), it says:
“Called when we receive an HTTP CONNECT request. Setting a non 2xx response on the flow will return the response to the client abort the connection. CONNECT requests and responses do not generate the usual HTTP handler events. CONNECT requests are only valid in regular and upstream proxy modes.”

I’ve tried setting a mitmproxy.http.HTTPResponse object on the flow with an http status of 200, but it doesn’t seem to do anything.

Am I going down the right path? And is there a way to accomplish this?


Hi Matt,

We do not actually connect upstream when the client sends a CONNECT request, as there are many cases (e.g. server replay) where this should be done later on. Assuming you have a HTTPS flow, one issue might be that the TLS layer then triggers an actual connection establishment to determine some connection parameters (e.g. if HTTP/2 should be used). You can disable that by passing --no-upstream-cert to mitmproxy, in which case you should see the request hook being called.