Exception handler
Firstly thank you for this masterpiece !
And to the problem. Lately I have tried to properly handle Titanium exceptions by using
ProxyServer.ExceptionFunc
and if this exception occured during request/response handling I want to get all the response/request data. In this particluar case on exception:
"Error occured whilst handling session request"
with inner exception
"Authentication failed because the remote party has closed the transport stream."
I am not able to access RequestBody because it is null which seems weird to me. My code:
ProxyServer.ExceptionFunc = async exception =>
{
if (exception is ProxyHttpException phex)
{
if (phex.Message == "Error occured whilst handling session request")
{
string reqUrl = phex.Session.HttpClient.Request.Url;
string reqMethod = phex.Session.HttpClient.Request.Method;
HeaderCollection reqHeaders = phex.Session.HttpClient.Request.Headers;//string ReqHeaders = phex.Session.HttpClient.Request.HeaderText;//hlavicky+cookies
string reqParameters = phex.Session.HttpClient.Request.HasBody ? await phex.Session.GetRequestBodyAsString(CancellationToken.None) : ""; //-------------Here is the problem...
//if (OnErrorAction != null) OnErrorAction(HeadersToDictionary(reqHeaders), reqUrl, reqParameters, String.Empty);//my exception handling
return;
}
else if (phex.Message.ToLower().Contains("response"))//only response error handling
{
Debug.WriteLine("Response err: "+exception.Message);
}
}
else
{
Debug.WriteLine(exception.Message);
}
};
It seems weird because it contains everything else(url, method, headers), but not request parameters even though HasBody is True. I have also tried:
var param = phex.Session.HttpClient.Request.BodyString;
but also without success, with NULL returned.
Are my hypothesis right that it should contain also request parameters (request body), especially in case HasBody == TRUE ?
Yes, it seems that the sessioneventargs is disposed before the exception handler is called: https://github.com/justcoding121/Titanium-Web-Proxy/blob/0052e4ace263079914d8ca8a07776df9ae157ee6/src/Titanium.Web.Proxy/RequestHandler.cs#L239-L254
I'll change this.
Anyway the body will be allways unavailable unless you explicitly get it in the onbeforerequest handler... otherwise it is just streamed to the server/client to void huge memory usage. (especially for resposne bodies)
Alright, I dunno where request Authentication check is processed but I thought it was before "OnBeforeRequest" or am I wrong ? ...based on that exception:
"Authentication failed because the remote party has closed the transport stream."
I thought some TCP handshake/magic happens. And also on every "OnBeforeRequest" I insert data to each request with e.UserData. And during debugging that exception phex.Session.HttpClient.UserData was Empty. So that request havent come to my OnBeforeRequest cos it would have e.UserData in it.
Since it contains all request header (url, method, header values) this is the server connection authentication, which should connect after the onbeforerequest, since you can modify the url in that event.
When you put it this way it makes sense. And from what you said saving the body of request probably wont be added right ?
It won't be saved by default, but you can save it.