Commit ae80fbe1 authored by Honfika's avatar Honfika

Terminate response property added

parent 4306e64c
...@@ -183,8 +183,8 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -183,8 +183,8 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
internal async Task ClearResponse() internal async Task ClearResponse()
{ {
//siphon out the body //syphon out the response body from server
await ReadResponseBodyAsync(); await SyphonOutBodyAsync(false);
WebSession.Response = new Response(); WebSession.Response = new Response();
} }
...@@ -263,7 +263,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -263,7 +263,7 @@ namespace Titanium.Web.Proxy.EventArguments
} }
} }
internal async Task SiphonBodyAsync(bool isRequest) internal async Task SyphonOutBodyAsync(bool isRequest)
{ {
var requestResponse = isRequest ? (RequestResponseBase)WebSession.Request : WebSession.Response; var requestResponse = isRequest ? (RequestResponseBase)WebSession.Request : WebSession.Response;
if (requestResponse.IsBodyRead || !requestResponse.OriginalHasBody) if (requestResponse.IsBodyRead || !requestResponse.OriginalHasBody)
...@@ -476,7 +476,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -476,7 +476,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// Sets the request body /// Sets the request body
/// </summary> /// </summary>
/// <param name="body"></param> /// <param name="body"></param>
public async Task SetRequestBody(byte[] body) public void SetRequestBody(byte[] body)
{ {
var request = WebSession.Request; var request = WebSession.Request;
if (request.Locked) if (request.Locked)
...@@ -484,12 +484,6 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -484,12 +484,6 @@ namespace Titanium.Web.Proxy.EventArguments
throw new Exception("You cannot call this function after request is made to server."); throw new Exception("You cannot call this function after request is made to server.");
} }
//syphon out the request body from client before setting the new body
if (!request.IsBodyRead)
{
await ReadRequestBodyAsync();
}
request.Body = body; request.Body = body;
request.UpdateContentLength(); request.UpdateContentLength();
} }
...@@ -498,20 +492,14 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -498,20 +492,14 @@ namespace Titanium.Web.Proxy.EventArguments
/// Sets the body with the specified string /// Sets the body with the specified string
/// </summary> /// </summary>
/// <param name="body"></param> /// <param name="body"></param>
public async Task SetRequestBodyString(string body) public void SetRequestBodyString(string body)
{ {
if (WebSession.Request.Locked) if (WebSession.Request.Locked)
{ {
throw new Exception("You cannot call this function after request is made to server."); throw new Exception("You cannot call this function after request is made to server.");
} }
//syphon out the request body from client before setting the new body SetRequestBody(WebSession.Request.Encoding.GetBytes(body));
if (!WebSession.Request.IsBodyRead)
{
await ReadRequestBodyAsync();
}
await SetRequestBody(WebSession.Request.Encoding.GetBytes(body));
} }
/// <summary> /// <summary>
...@@ -546,7 +534,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -546,7 +534,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// Set the response body bytes /// Set the response body bytes
/// </summary> /// </summary>
/// <param name="body"></param> /// <param name="body"></param>
public async Task SetResponseBody(byte[] body) public void SetResponseBody(byte[] body)
{ {
if (!WebSession.Request.Locked) if (!WebSession.Request.Locked)
{ {
...@@ -554,17 +542,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -554,17 +542,7 @@ namespace Titanium.Web.Proxy.EventArguments
} }
var response = WebSession.Response; var response = WebSession.Response;
//syphon out the response body from server before setting the new body
if (response.Body == null)
{
await GetResponseBody();
}
response.Body = body; response.Body = body;
//If there is a content length header update it
response.UpdateContentLength();
} }
/// <summary> /// <summary>
...@@ -586,7 +564,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -586,7 +564,7 @@ namespace Titanium.Web.Proxy.EventArguments
var bodyBytes = WebSession.Response.Encoding.GetBytes(body); var bodyBytes = WebSession.Response.Encoding.GetBytes(body);
await SetResponseBody(bodyBytes); SetResponseBody(bodyBytes);
} }
/// <summary> /// <summary>
...@@ -687,17 +665,24 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -687,17 +665,24 @@ namespace Titanium.Web.Proxy.EventArguments
{ {
if (WebSession.Request.Locked) if (WebSession.Request.Locked)
{ {
throw new Exception("You cannot call this function after request is made to server."); if (WebSession.Response.Locked)
} {
throw new Exception("You cannot call this function after response is sent to the client.");
}
WebSession.Request.Locked = true; WebSession.Response = response;
}
else
{
WebSession.Request.Locked = true;
response.Locked = true; response.Locked = true;
response.IsBodyRead = true; response.IsBodyRead = true;
WebSession.Response = response; WebSession.Response = response;
WebSession.Request.CancelRequest = true; WebSession.Request.CancelRequest = true;
}
} }
/// <summary> /// <summary>
......
...@@ -132,6 +132,9 @@ namespace Titanium.Web.Proxy.Http ...@@ -132,6 +132,9 @@ namespace Titanium.Web.Proxy.Http
{ {
body = value; body = value;
bodyString = null; bodyString = null;
//If there is a content length header update it
UpdateContentLength();
} }
} }
...@@ -166,7 +169,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -166,7 +169,7 @@ namespace Titanium.Web.Proxy.Http
internal void UpdateContentLength() internal void UpdateContentLength()
{ {
ContentLength = IsChunked ? -1 : body.Length; ContentLength = IsChunked ? -1 : body?.Length ?? 0;
} }
/// <summary> /// <summary>
......
...@@ -77,6 +77,8 @@ namespace Titanium.Web.Proxy.Http ...@@ -77,6 +77,8 @@ namespace Titanium.Web.Proxy.Http
} }
} }
internal bool TerminateResponse { get; set; }
internal override void EnsureBodyAvailable(bool throwWhenNotReadYet = true) internal override void EnsureBodyAvailable(bool throwWhenNotReadYet = true)
{ {
if (!IsBodyRead && throwWhenNotReadYet) if (!IsBodyRead && throwWhenNotReadYet)
......
...@@ -390,7 +390,9 @@ namespace Titanium.Web.Proxy ...@@ -390,7 +390,9 @@ namespace Titanium.Web.Proxy
if (request.CancelRequest) if (request.CancelRequest)
{ {
await args.SiphonBodyAsync(true); //syphon out the request body from client before setting the new body
await args.SyphonOutBodyAsync(true);
await HandleHttpSessionResponse(args); await HandleHttpSessionResponse(args);
if (!response.KeepAlive) if (!response.KeepAlive)
...@@ -403,7 +405,7 @@ namespace Titanium.Web.Proxy ...@@ -403,7 +405,7 @@ namespace Titanium.Web.Proxy
//create a new connection if hostname/upstream end point changes //create a new connection if hostname/upstream end point changes
if (connection != null if (connection != null
&& (!connection.HostName.Equals(args.WebSession.Request.RequestUri.Host, StringComparison.OrdinalIgnoreCase) && (!connection.HostName.Equals(request.RequestUri.Host, StringComparison.OrdinalIgnoreCase)
|| (args.WebSession.UpStreamEndPoint != null || (args.WebSession.UpStreamEndPoint != null
&& !args.WebSession.UpStreamEndPoint.Equals(connection.UpStreamEndPoint)))) && !args.WebSession.UpStreamEndPoint.Equals(connection.UpStreamEndPoint))))
{ {
...@@ -420,7 +422,7 @@ namespace Titanium.Web.Proxy ...@@ -420,7 +422,7 @@ namespace Titanium.Web.Proxy
if (request.UpgradeToWebSocket) if (request.UpgradeToWebSocket)
{ {
//prepare the prefix content //prepare the prefix content
var requestHeaders = args.WebSession.Request.Headers; var requestHeaders = request.Headers;
await connection.StreamWriter.WriteLineAsync(httpCmd); await connection.StreamWriter.WriteLineAsync(httpCmd);
await connection.StreamWriter.WriteHeadersAsync(requestHeaders); await connection.StreamWriter.WriteHeadersAsync(requestHeaders);
string httpStatus = await connection.StreamReader.ReadLineAsync(); string httpStatus = await connection.StreamReader.ReadLineAsync();
......
...@@ -43,6 +43,12 @@ namespace Titanium.Web.Proxy ...@@ -43,6 +43,12 @@ namespace Titanium.Web.Proxy
await InvokeBeforeResponse(args); await InvokeBeforeResponse(args);
} }
if (response.TerminateResponse)
{
//syphon out the response body from server before setting the new body
await args.SyphonOutBodyAsync(false);
}
//if user requested to send request again //if user requested to send request again
//likely after making modifications from User Response Handler //likely after making modifications from User Response Handler
if (args.ReRequest) if (args.ReRequest)
......
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