Commit ae80fbe1 authored by Honfika's avatar Honfika

Terminate response property added

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