justcoding121 / titanium-web-proxy

A cross-platform asynchronous HTTP(S) proxy server in C#.
MIT License
1.92k stars 598 forks source link

HTTP/2 BodyNotFoundException #856

Closed jgilbert2017 closed 2 years ago

jgilbert2017 commented 3 years ago

Hi. I have some working code that I'm trying to enable with HTTP/2.

I've set the EnableHttp2=true flag but my code dies when I call await e.GetResponseBodyAsString(); from a BeforeResponse application callback with a BodyNotFoundException.

I've run my code using an upstream debugging proxy and have verified that the server is sending a body over http2.

Am I doing something wrong (does my code need to change to support http2?).

Here's a stack:

    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.Http.Response.EnsureBodyAvailable(bool throwWhenNotReadYet) Line 124  C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.Http.RequestResponseBase.Body.get() Line 175  C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.Http.RequestResponseBase.BodyString.get() Line 199    C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.EventArguments.SessionEventArgs.GetResponseBodyAsString(System.Threading.CancellationToken cancellationToken) Line 436    C#
    MyCode.dll!MyCode.OnBeforeResponse(object sender, Titanium.Web.Proxy.EventArguments.SessionEventArgs e) Line 354    C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.Extensions.FuncExtensions.internalInvokeAsync<Titanium.Web.Proxy.EventArguments.SessionEventArgs>(Titanium.Web.Proxy.EventArguments.AsyncEventHandler<Titanium.Web.Proxy.EventArguments.SessionEventArgs> callback, object sender, Titanium.Web.Proxy.EventArguments.SessionEventArgs args, Titanium.Web.Proxy.ExceptionHandler exceptionFunc) Line 25    C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.Extensions.FuncExtensions.InvokeAsync<Titanium.Web.Proxy.EventArguments.SessionEventArgs>(Titanium.Web.Proxy.EventArguments.AsyncEventHandler<Titanium.Web.Proxy.EventArguments.SessionEventArgs> callback, object sender, Titanium.Web.Proxy.EventArguments.SessionEventArgs args, Titanium.Web.Proxy.ExceptionHandler exceptionFunc) Line 16    C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.ProxyServer.onBeforeResponse(Titanium.Web.Proxy.EventArguments.SessionEventArgs args) Line 137    C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.ProxyServer.handleClient.AnonymousMethod__6(Titanium.Web.Proxy.EventArguments.SessionEventArgs args) Line 365 C#
    Titanium.Web.Proxy.NetCore.dll!Titanium.Web.Proxy.Http2.Http2Helper.copyHttp2FrameAsync(System.IO.Stream input, System.IO.Stream output, Titanium.Web.Proxy.Http2.Http2Helper.Http2Settings localSettings, Titanium.Web.Proxy.Http2.Http2Helper.Http2Settings remoteSettings, System.Func<Titanium.Web.Proxy.EventArguments.SessionEventArgs> sessionFactory, System.Collections.Concurrent.ConcurrentDictionary<int, Titanium.Web.Proxy.EventArguments.SessionEventArgs> sessions, System.Func<Titanium.Web.Proxy.EventArguments.SessionEventArgs, System.Threading.Tasks.Task> onBeforeRequestResponse, System.Guid connectionId, bool isClient, System.Threading.CancellationToken cancellationToken, Titanium.Web.Proxy.ExceptionHandler exceptionFunc) Line 297    C#
    [Resuming Async Method] 
honfika commented 2 years ago

this exception is thrown when HasBody is false: https://github.com/justcoding121/titanium-web-proxy/blob/7265452f71df63d6dbbc4b7cbf665c09e99dab3f/src/Titanium.Web.Proxy/Http/Response.cs#L45-L77

Could you please check the values of the parameters which are used in this property? RequestMethod, ContentLength, IsChunhed, KeepAlive, HttpVersion (it should be 2.0)

jgilbert2017 commented 2 years ago

RequestMethod=null ContentLength=-1 IsChunked=false KeepAlive=true HttpVersion={2.0}

checking the response headers, it looks like there is NO Content-length header present.

honfika commented 2 years ago

Do you know any public site where i can reproduce the problem? I tried it, but fo me it seeems that the HasBody is false when it should be false.

What is the status code of your response?

Edit: Ok, maybe I found it.

jgilbert2017 commented 2 years ago

status is 200 OK. i get the error with https://www.cloudflare.com/diagnostic-center/ the page seems to return no content-length header but has a body.

honfika commented 2 years ago

Please try the latest beta in some minutes (when build completes)

jgilbert2017 commented 2 years ago

will do. you da man.

jgilbert2017 commented 2 years ago

the fix works for me! thank you!!!