Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds an option to not close response synchronously #457

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion websocket-sharp/Server/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class HttpServer
private WebSocketServiceManager _services;
private volatile ServerState _state;
private object _sync;
private bool _autoCloseResponse;

#endregion

Expand Down Expand Up @@ -736,6 +737,57 @@ public WebSocketServiceManager WebSocketServices {
}
}

/// <summary>
/// Gets or sets a value indicating whether the responses
/// are synchronously closed after the request event handler
/// has returned. If set to <c>false</c>, it is the responsibility to
/// the event handler delegate to call the <see cref="HttpListenerResponse.Close()"/>
/// method on the response object, otherwise resources may not be properly cleaned.
/// </summary>
/// <remarks>
/// <para>
/// You should set this property to <c>true</c> if you would
/// like to send response after executing an asynchronous task.
/// </para>
/// <para>
/// The set operation does nothing if the server has already
/// started or it is shutting down.
/// </para>
/// </remarks>
/// <value>
/// <para>
/// <c>true</c> if the server synchronously close the response
/// after the handler delegate has returned; otherwise, <c>false</c>.
/// </para>
/// <para>
/// The default value is <c>true</c>.
/// </para>
/// </value>
public bool AutoCloseResponse
{
get
{
return _autoCloseResponse;
}
set
{
string msg;
if (!canSet (out msg)) {
_log.Warn (msg);
return;
}

lock (_sync) {
if (!canSet (out msg)) {
_log.Warn (msg);
return;
}

_autoCloseResponse = value;
}
}
}

#endregion

#region Public Events
Expand Down Expand Up @@ -884,6 +936,8 @@ private void init (
_log = _listener.Log;
_services = new WebSocketServiceManager (_log);
_sync = new object ();

AutoCloseResponse = true;
}

private void processRequest (HttpListenerContext context)
Expand All @@ -910,9 +964,14 @@ private void processRequest (HttpListenerContext context)
if (evt != null)
evt (this, new HttpRequestEventArgs (context, _docRootPath));
else
{
context.Response.StatusCode = 501; // Not Implemented
context.Response.Close ();
return;
}

context.Response.Close ();
if (AutoCloseResponse)
context.Response.Close ();
}

private void processRequest (HttpListenerWebSocketContext context)
Expand Down