Commit 6e2acc49 authored by Honfika's avatar Honfika

Handle exception in user events, small fix

parent 236e7563
...@@ -34,6 +34,8 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -34,6 +34,8 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
private Func<SessionEventArgs, Task> httpResponseHandler; private Func<SessionEventArgs, Task> httpResponseHandler;
private readonly Action<Exception> exceptionFunc;
/// <summary> /// <summary>
/// Backing field for corresponding public property /// Backing field for corresponding public property
/// </summary> /// </summary>
...@@ -104,12 +106,14 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -104,12 +106,14 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary> /// <summary>
/// Constructor to initialize the proxy /// Constructor to initialize the proxy
/// </summary> /// </summary>
internal SessionEventArgs(int bufferSize, internal SessionEventArgs(int bufferSize,
ProxyEndPoint endPoint, ProxyEndPoint endPoint,
Func<SessionEventArgs, Task> httpResponseHandler) Func<SessionEventArgs, Task> httpResponseHandler,
Action<Exception> exceptionFunc)
{ {
this.bufferSize = bufferSize; this.bufferSize = bufferSize;
this.httpResponseHandler = httpResponseHandler; this.httpResponseHandler = httpResponseHandler;
this.exceptionFunc = exceptionFunc;
ProxyClient = new ProxyClient(); ProxyClient = new ProxyClient();
WebSession = new HttpWebClient(bufferSize); WebSession = new HttpWebClient(bufferSize);
...@@ -169,17 +173,41 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -169,17 +173,41 @@ namespace Titanium.Web.Proxy.EventArguments
internal void OnDataSent(byte[] buffer, int offset, int count) internal void OnDataSent(byte[] buffer, int offset, int count)
{ {
DataSent?.Invoke(this, new DataEventArgs(buffer, offset, count)); try
{
DataSent?.Invoke(this, new DataEventArgs(buffer, offset, count));
}
catch (Exception ex)
{
var ex2 = new Exception("Exception thrown in user event", ex);
exceptionFunc(ex2);
}
} }
internal void OnDataReceived(byte[] buffer, int offset, int count) internal void OnDataReceived(byte[] buffer, int offset, int count)
{ {
DataReceived?.Invoke(this, new DataEventArgs(buffer, offset, count)); try
{
DataReceived?.Invoke(this, new DataEventArgs(buffer, offset, count));
}
catch (Exception ex)
{
var ex2 = new Exception("Exception thrown in user event", ex);
exceptionFunc(ex2);
}
} }
internal void OnMultipartRequestPartSent(string boundary, HeaderCollection headers) internal void OnMultipartRequestPartSent(string boundary, HeaderCollection headers)
{ {
MultipartRequestPartSent?.Invoke(this, new MultipartRequestPartSentEventArgs(boundary, headers)); try
{
MultipartRequestPartSent?.Invoke(this, new MultipartRequestPartSentEventArgs(boundary, headers));
}
catch (Exception ex)
{
var ex2 = new Exception("Exception thrown in user event", ex);
exceptionFunc(ex2);
}
} }
/// <summary> /// <summary>
......
using Titanium.Web.Proxy.Http; using System;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.EventArguments namespace Titanium.Web.Proxy.EventArguments
...@@ -7,8 +8,8 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -7,8 +8,8 @@ namespace Titanium.Web.Proxy.EventArguments
{ {
public bool IsHttpsConnect { get; set; } public bool IsHttpsConnect { get; set; }
internal TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ConnectRequest connectRequest) internal TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ConnectRequest connectRequest, Action<Exception> exceptionFunc)
: base(bufferSize, endPoint, null) : base(bufferSize, endPoint, null, exceptionFunc)
{ {
WebSession.Request = connectRequest; WebSession.Request = connectRequest;
} }
......
...@@ -86,7 +86,7 @@ namespace Titanium.Web.Proxy ...@@ -86,7 +86,7 @@ namespace Titanium.Web.Proxy
await HeaderParser.ReadHeaders(clientStreamReader, connectRequest.Headers); await HeaderParser.ReadHeaders(clientStreamReader, connectRequest.Headers);
var connectArgs = new TunnelConnectSessionEventArgs(BufferSize, endPoint, connectRequest); var connectArgs = new TunnelConnectSessionEventArgs(BufferSize, endPoint, connectRequest, ExceptionFunc);
connectArgs.ProxyClient.TcpClient = tcpClient; connectArgs.ProxyClient.TcpClient = tcpClient;
connectArgs.ProxyClient.ClientStream = clientStream; connectArgs.ProxyClient.ClientStream = clientStream;
...@@ -164,7 +164,8 @@ namespace Titanium.Web.Proxy ...@@ -164,7 +164,8 @@ namespace Titanium.Web.Proxy
{ {
if (isClientHello) if (isClientHello)
{ {
if (clientStream.Available > 0) int available = clientStream.Available;
if (available > 0)
{ {
//send the buffered data //send the buffered data
var data = BufferPool.GetBuffer(BufferSize); var data = BufferPool.GetBuffer(BufferSize);
...@@ -172,8 +173,8 @@ namespace Titanium.Web.Proxy ...@@ -172,8 +173,8 @@ namespace Titanium.Web.Proxy
try try
{ {
// clientStream.Available sbould be at most BufferSize because it is using the same buffer size // clientStream.Available sbould be at most BufferSize because it is using the same buffer size
await clientStream.ReadAsync(data, 0, clientStream.Available); await clientStream.ReadAsync(data, 0, available);
await connection.StreamWriter.WriteAsync(data, true); await connection.StreamWriter.WriteAsync(data, 0, available, true);
} }
finally finally
{ {
...@@ -309,7 +310,7 @@ namespace Titanium.Web.Proxy ...@@ -309,7 +310,7 @@ namespace Titanium.Web.Proxy
break; break;
} }
var args = new SessionEventArgs(BufferSize, endPoint, HandleHttpSessionResponse) var args = new SessionEventArgs(BufferSize, endPoint, HandleHttpSessionResponse, ExceptionFunc)
{ {
ProxyClient = { TcpClient = client }, ProxyClient = { TcpClient = client },
WebSession = { ConnectRequest = connectRequest } WebSession = { ConnectRequest = connectRequest }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment