Commit beed62ca authored by justcoding121's avatar justcoding121

#411 close server connection option in api user response

parent 56fb7aa5
...@@ -463,7 +463,9 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -463,7 +463,9 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
/// <param name="html">HTML content to sent.</param> /// <param name="html">HTML content to sent.</param>
/// <param name="headers">HTTP response headers.</param> /// <param name="headers">HTTP response headers.</param>
public void Ok(string html, Dictionary<string, HttpHeader> headers = null) /// <param name="closeServerConnection">Close the server connection?</param>
public void Ok(string html, Dictionary<string, HttpHeader> headers = null,
bool closeServerConnection = false)
{ {
var response = new OkResponse(); var response = new OkResponse();
if (headers != null) if (headers != null)
...@@ -474,7 +476,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -474,7 +476,7 @@ namespace Titanium.Web.Proxy.EventArguments
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.Body = response.Encoding.GetBytes(html ?? string.Empty); response.Body = response.Encoding.GetBytes(html ?? string.Empty);
Respond(response); Respond(response, closeServerConnection);
} }
/// <summary> /// <summary>
...@@ -483,14 +485,16 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -483,14 +485,16 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
/// <param name="result">The html content bytes.</param> /// <param name="result">The html content bytes.</param>
/// <param name="headers">The HTTP headers.</param> /// <param name="headers">The HTTP headers.</param>
public void Ok(byte[] result, Dictionary<string, HttpHeader> headers = null) /// <param name="closeServerConnection">Close the server connection?</param>
public void Ok(byte[] result, Dictionary<string, HttpHeader> headers = null,
bool closeServerConnection = false)
{ {
var response = new OkResponse(); var response = new OkResponse();
response.Headers.AddHeaders(headers); response.Headers.AddHeaders(headers);
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.Body = result; response.Body = result;
Respond(response); Respond(response, closeServerConnection);
} }
/// <summary> /// <summary>
...@@ -501,15 +505,16 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -501,15 +505,16 @@ namespace Titanium.Web.Proxy.EventArguments
/// <param name="html">The html content.</param> /// <param name="html">The html content.</param>
/// <param name="status">The HTTP status code.</param> /// <param name="status">The HTTP status code.</param>
/// <param name="headers">The HTTP headers.</param> /// <param name="headers">The HTTP headers.</param>
/// <returns></returns> /// <param name="closeServerConnection">Close the server connection?</param>
public void GenericResponse(string html, HttpStatusCode status, Dictionary<string, HttpHeader> headers = null) public void GenericResponse(string html, HttpStatusCode status,
Dictionary<string, HttpHeader> headers = null, bool closeServerConnection = false)
{ {
var response = new GenericResponse(status); var response = new GenericResponse(status);
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.Headers.AddHeaders(headers); response.Headers.AddHeaders(headers);
response.Body = response.Encoding.GetBytes(html ?? string.Empty); response.Body = response.Encoding.GetBytes(html ?? string.Empty);
Respond(response); Respond(response, closeServerConnection);
} }
/// <summary> /// <summary>
...@@ -519,51 +524,57 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -519,51 +524,57 @@ namespace Titanium.Web.Proxy.EventArguments
/// <param name="result">The bytes to sent.</param> /// <param name="result">The bytes to sent.</param>
/// <param name="status">The HTTP status code.</param> /// <param name="status">The HTTP status code.</param>
/// <param name="headers">The HTTP headers.</param> /// <param name="headers">The HTTP headers.</param>
/// <returns></returns> /// <param name="closeServerConnection">Close the server connection?</param>
public void GenericResponse(byte[] result, HttpStatusCode status, Dictionary<string, HttpHeader> headers) public void GenericResponse(byte[] result, HttpStatusCode status,
Dictionary<string, HttpHeader> headers, bool closeServerConnection = false)
{ {
var response = new GenericResponse(status); var response = new GenericResponse(status);
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.Headers.AddHeaders(headers); response.Headers.AddHeaders(headers);
response.Body = result; response.Body = result;
Respond(response); Respond(response, closeServerConnection);
} }
/// <summary> /// <summary>
/// Redirect to provided URL. /// Redirect to provided URL.
/// </summary> /// </summary>
/// <param name="url">The URL to redirect.</param> /// <param name="url">The URL to redirect.</param>
/// <returns></returns> /// <param name="closeServerConnection">Close the server connection?</param>
public void Redirect(string url) public void Redirect(string url, bool closeServerConnection = false)
{ {
var response = new RedirectResponse(); var response = new RedirectResponse();
response.HttpVersion = WebSession.Request.HttpVersion; response.HttpVersion = WebSession.Request.HttpVersion;
response.Headers.AddHeader(KnownHeaders.Location, url); response.Headers.AddHeader(KnownHeaders.Location, url);
response.Body = emptyData; response.Body = emptyData;
Respond(response); Respond(response, closeServerConnection);
} }
/// <summary> /// <summary>
/// Respond with given response object to client. /// Respond with given response object to client.
/// </summary> /// </summary>
/// <param name="response">The response object.</param> /// <param name="response">The response object.</param>
public void Respond(Response response) /// <param name="closeServerConnection">Close the server connection?</param>
public void Respond(Response response, bool closeServerConnection = false)
{ {
//request already send.
if (WebSession.Request.Locked) if (WebSession.Request.Locked)
{ {
//response already received from server and ready to be sent to client.
if (WebSession.Response.Locked) if (WebSession.Response.Locked)
{ {
throw new Exception("You cannot call this function after response is sent to the client."); throw new Exception("You cannot call this function after response is sent to the client.");
} }
//response already received from server but not yet ready to sent to client.
response.Locked = true; response.Locked = true;
response.TerminateResponse = WebSession.Response.TerminateResponse; response.TerminateResponse = WebSession.Response.TerminateResponse;
WebSession.Response = response; WebSession.Response = response;
} }
else else
{ {
//request not yet sent.
WebSession.Request.Locked = true; WebSession.Request.Locked = true;
response.Locked = true; response.Locked = true;
...@@ -571,6 +582,11 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -571,6 +582,11 @@ namespace Titanium.Web.Proxy.EventArguments
WebSession.Request.CancelRequest = true; WebSession.Request.CancelRequest = true;
} }
if(closeServerConnection)
{
TerminateServerConnection();
}
} }
/// <summary> /// <summary>
......
...@@ -182,7 +182,6 @@ namespace Titanium.Web.Proxy ...@@ -182,7 +182,6 @@ namespace Titanium.Web.Proxy
} }
//If prefetch task is available. //If prefetch task is available.
//Delay awaiting prefect task as far as possible.
if (serverConnection == null && prefetchTask != null) if (serverConnection == null && prefetchTask != null)
{ {
serverConnection = await prefetchTask; serverConnection = await prefetchTask;
......
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