Commit 47ba3eca authored by Honfika's avatar Honfika

Fixes: Check the value of the RequestLocked property in the Response method to...

Fixes: Check the value of the RequestLocked property in the Response method to ensure that it is always checked. (Earlier it was checked in 2 + 1 methods)
Set the CancelRequest to true in the same method. (Earlier it was not set it Task Respond(Response response) method)
Use the correct encoding (from header)
parent c4790ef3
...@@ -409,17 +409,6 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -409,17 +409,6 @@ namespace Titanium.Web.Proxy.EventArguments
return await decompressor.Decompress(responseBodyStream, bufferSize); return await decompressor.Decompress(responseBodyStream, bufferSize);
} }
/// <summary>
/// Before request is made to server
/// Respond with the specified HTML string to client
/// and ignore the request
/// </summary>
/// <param name="html"></param>
public async Task Ok(string html)
{
await Ok(html, null);
}
/// <summary> /// <summary>
/// Before request is made to server /// Before request is made to server
/// Respond with the specified HTML string to client /// Respond with the specified HTML string to client
...@@ -429,30 +418,14 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -429,30 +418,14 @@ namespace Titanium.Web.Proxy.EventArguments
/// <param name="headers"></param> /// <param name="headers"></param>
public async Task Ok(string html, Dictionary<string, HttpHeader> headers) public async Task Ok(string html, Dictionary<string, HttpHeader> headers)
{ {
if (WebSession.Request.RequestLocked) var response = new OkResponse();
{ response.ResponseHeaders.AddHeaders(headers);
throw new Exception("You cannot call this function after request is made to server."); response.HttpVersion = WebSession.Request.HttpVersion;
} response.ResponseBody = response.Encoding.GetBytes(html ?? string.Empty);
if (html == null)
{
html = string.Empty;
}
var result = Encoding.Default.GetBytes(html);
await Ok(result, headers); await Respond(response);
}
/// <summary> WebSession.Request.CancelRequest = true;
/// Before request is made to server
/// Respond with the specified byte[] to client
/// and ignore the request
/// </summary>
/// <param name="result"></param>
public async Task Ok(byte[] result)
{
await Ok(result, null);
} }
/// <summary> /// <summary>
...@@ -462,37 +435,14 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -462,37 +435,14 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
/// <param name="result"></param> /// <param name="result"></param>
/// <param name="headers"></param> /// <param name="headers"></param>
public async Task Ok(byte[] result, Dictionary<string, HttpHeader> headers) public async Task Ok(byte[] result, Dictionary<string, HttpHeader> headers = null)
{ {
var response = new OkResponse(); var response = new OkResponse();
response.ResponseHeaders.AddHeaders(headers);
if (headers != null && headers.Count > 0)
{
foreach (var header in headers)
{
response.ResponseHeaders.AddHeader(header.Key, header.Value.Value);
}
}
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.ResponseBody = result; response.ResponseBody = result;
await Respond(response); await Respond(response);
WebSession.Request.CancelRequest = true;
}
/// <summary>
/// Before request is made to server 
/// Respond with the specified HTML string to client
/// and ignore the request 
/// </summary>
/// <param name="html"></param>
/// <param name="status"></param>
/// <returns></returns>
public async Task GenericResponse(string html, HttpStatusCode status)
{
await GenericResponse(html, null, status);
} }
/// <summary> /// <summary>
...@@ -502,24 +452,17 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -502,24 +452,17 @@ namespace Titanium.Web.Proxy.EventArguments
/// and ignore the request  /// and ignore the request 
/// </summary> /// </summary>
/// <param name="html"></param> /// <param name="html"></param>
/// <param name="headers"></param>
/// <param name="status"></param> /// <param name="status"></param>
/// <param name="headers"></param>
/// <returns></returns> /// <returns></returns>
public async Task GenericResponse(string html, Dictionary<string, HttpHeader> headers, HttpStatusCode status) public async Task GenericResponse(string html, HttpStatusCode status, Dictionary<string, HttpHeader> headers = null)
{ {
if (WebSession.Request.RequestLocked) var response = new GenericResponse(status);
{ response.HttpVersion = WebSession.Request.HttpVersion;
throw new Exception("You cannot call this function after request is made to server."); response.ResponseHeaders.AddHeaders(headers);
} response.ResponseBody = response.Encoding.GetBytes(html ?? string.Empty);
if (html == null)
{
html = string.Empty;
}
var result = Encoding.Default.GetBytes(html);
await GenericResponse(result, headers, status); await Respond(response);
} }
/// <summary> /// <summary>
...@@ -529,28 +472,17 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -529,28 +472,17 @@ namespace Titanium.Web.Proxy.EventArguments
/// and ignore the request /// and ignore the request
/// </summary> /// </summary>
/// <param name="result"></param> /// <param name="result"></param>
/// <param name="headers"></param>
/// <param name="status"></param> /// <param name="status"></param>
/// <param name="headers"></param>
/// <returns></returns> /// <returns></returns>
public async Task GenericResponse(byte[] result, Dictionary<string, HttpHeader> headers, HttpStatusCode status) public async Task GenericResponse(byte[] result, HttpStatusCode status, Dictionary<string, HttpHeader> headers)
{ {
var response = new GenericResponse(status); var response = new GenericResponse(status);
if (headers != null && headers.Count > 0)
{
foreach (var header in headers)
{
response.ResponseHeaders.AddHeader(header.Key, header.Value.Value);
}
}
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.ResponseHeaders.AddHeaders(headers);
response.ResponseBody = result; response.ResponseBody = result;
await Respond(response); await Respond(response);
WebSession.Request.CancelRequest = true;
} }
/// <summary> /// <summary>
...@@ -561,19 +493,21 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -561,19 +493,21 @@ namespace Titanium.Web.Proxy.EventArguments
public async Task Redirect(string url) public async Task Redirect(string url)
{ {
var response = new RedirectResponse(); var response = new RedirectResponse();
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.ResponseHeaders.AddHeader("Location", url); response.ResponseHeaders.AddHeader("Location", url);
response.ResponseBody = Encoding.ASCII.GetBytes(string.Empty); response.ResponseBody = new byte[0];
await Respond(response); await Respond(response);
WebSession.Request.CancelRequest = true;
} }
/// a generic responder method /// a generic responder method
public async Task Respond(Response response) public async Task Respond(Response response)
{ {
if (WebSession.Request.RequestLocked)
{
throw new Exception("You cannot call this function after request is made to server.");
}
WebSession.Request.RequestLocked = true; WebSession.Request.RequestLocked = true;
response.ResponseLocked = true; response.ResponseLocked = true;
...@@ -582,6 +516,8 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -582,6 +516,8 @@ namespace Titanium.Web.Proxy.EventArguments
WebSession.Response = response; WebSession.Response = response;
await httpResponseHandler(this); await httpResponseHandler(this);
WebSession.Request.CancelRequest = true;
} }
/// <summary> /// <summary>
......
...@@ -115,6 +115,62 @@ namespace Titanium.Web.Proxy.Http ...@@ -115,6 +115,62 @@ namespace Titanium.Web.Proxy.Http
} }
} }
/// <summary>
/// Adds the given header objects to Request
/// </summary>
/// <param name="newHeaders"></param>
public void AddHeaders(IEnumerable<HttpHeader> newHeaders)
{
if (newHeaders == null)
{
return;
}
foreach (var header in newHeaders)
{
AddHeader(header);
}
}
/// <summary>
/// Adds the given header objects to Request
/// </summary>
/// <param name="newHeaders"></param>
public void AddHeaders(IEnumerable<KeyValuePair<string, string>> newHeaders)
{
if (newHeaders == null)
{
return;
}
foreach (var header in newHeaders)
{
AddHeader(header.Key, header.Value);
}
}
/// <summary>
/// Adds the given header objects to Request
/// </summary>
/// <param name="newHeaders"></param>
public void AddHeaders(IEnumerable<KeyValuePair<string, HttpHeader>> newHeaders)
{
if (newHeaders == null)
{
return;
}
foreach (var header in newHeaders)
{
if (header.Key != header.Value.Name)
{
throw new Exception("Header name mismatch. Key and the name of the HttpHeader object should be the same.");
}
AddHeader(header.Value);
}
}
/// <summary> /// <summary>
/// removes all headers with given name /// removes all headers with given name
/// </summary> /// </summary>
......
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