Closed AxisRay closed 2 years ago
Please specify how to reproduce the problem. I checked it with some request, but I was not able to reproduce it.
like #547
when I switched from AfterResponse
event to BeforeResponse
event,
the problem is gone.
I will try to find a way to reproduce it this weekend.
public class MoaProxy
{
private readonly ProxyServer proxyServer;
private ExplicitProxyEndPoint explicitEndPoint;
public MoaProxy(IPAddress address, int port)
{
explicitEndPoint = new ExplicitProxyEndPoint(address, port);
proxyServer = new ProxyServer(userTrustRootCertificate: false);
proxyServer.ExceptionFunc = exception =>
{
Console.WriteLine(exception.Message + ": " + exception.InnerException?.Message);
};
proxyServer.BeforeResponse += onBeforeResponse;
proxyServer.AfterResponse += onAfterResponse;
proxyServer.ReuseSocket = false;
}
public void StartProxy()
{
proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.Start();
}
private async Task onBeforeResponse(object sender, SessionEventArgs e)
{
//everything will be fine , if you uncomment this
//var bodyString = await e.GetResponseBodyAsString();
//Console.WriteLine("onBeforeRequest" + bodyString);
}
private async Task onAfterResponse(object sender, SessionEventArgs e)
{
var bodyString = await e.GetResponseBodyAsString();
Console.WriteLine("onAfterResponse" + bodyString);
}
}
Seems that we cannot use GetResponseBodyAsString
or GetResponseBody
in AfterResponse
.
Beacause the connection has already been released.
// Otherwise it will keep authenticating per session.
if (EnableConnectionPool && connection != null
&& !connection.IsWinAuthenticated)
{
await tcpConnectionFactory.Release(connection); //released!!!!
connection = null;
}
}
catch (Exception e) when (!(e is ProxyHttpException))
{
throw new ProxyHttpException("Error occured whilst handling session request", e, args);
}
}
catch (Exception e)
{
args.Exception = e;
closeServerConnection = true;
throw;
}
finally
{
Console.WriteLine(args.HttpClient.Response);
await onAfterResponse(args);
args.Dispose();
Yes, that is true. GetResponseBody* should be used in BeforeResponse event.
By default the response body is not preserved. Imagine, that there is a 2GB video or something like that. It would need a lot of memory. This is by design.
If you need the resposne body in after respose, set the KeepBody property to true in the before response event.
Anyway I'll try to reproduce the original problem. The header in the body string.... it sould not be there even in the AfterRsponse event.
I still can't reproduce the header problem. I get an exception when I call GetResponseBodyAsString in AfterResponse... which is expected.
Edit: I reproduced it. Sometimes happens... probably reads the next response header.
It should be fixed.. now you should receive an exception when you call the GetResponseBodyAsString in the AfterResponse event. (when there was a body, and it was not read in the beforerequest eventhandler)
Thanks! call GetResponseBody in AfterResponse is not safe. it will cause various exception.
Edit: I reproduced it. Sometimes happens... probably reads the next response header.
yeah, like the issue mentioned
Various exceptions? Could you please specify what type of exceptions?
yeah, various exceptions. But I think the reason is same.
For example: Exception thrown in user event: Invalid chunk length: 'Date: Sat, 22 Jan 2022 09:32:22 GMT' Error occured whilst handling session request: Invalid http status code. Titanium.Web.Proxy.Http.HeaderCollectionException thrown in user event: Data received after stream end Titanium.Web.Proxy.Http.HeaderCollectionError occured whilst handling session request: The ReadAsync method cannot be called when another read operation is pending.
System.ObjectDisposedException: Cannot access a disposed object.
But this is an internal exception. Do you receive any non expected exception as a user of the library? Can you still reproduce the wrong respinse body?
Also please include the various exception messages.
the app didn't work properly through the proxy, when I call GetResponseBody
in AfterResponse
.
something was broken.
probably reads the next response header.
i think it's the reason. it breaked the http stream.
please put the GetResponseBodyAsString to a try-catch.
It is normal, that it throws an exception, since it is not allowed to call GetResponseBodyAsString in AfterResponse when you are not reading the body in BeforeResponse.
I see. it works well when i change to BeforeResponse. so it's time to close. thanks for your help