diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index 83de8555c..1b58cbe92 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -73,6 +73,7 @@ public class HttpServer private WebSocketServiceManager _services; private volatile ServerState _state; private object _sync; + private bool _autoCloseResponse; #endregion @@ -736,6 +737,57 @@ public WebSocketServiceManager WebSocketServices { } } + /// + /// Gets or sets a value indicating whether the responses + /// are synchronously closed after the request event handler + /// has returned. If set to false, it is the responsibility to + /// the event handler delegate to call the + /// method on the response object, otherwise resources may not be properly cleaned. + /// + /// + /// + /// You should set this property to true if you would + /// like to send response after executing an asynchronous task. + /// + /// + /// The set operation does nothing if the server has already + /// started or it is shutting down. + /// + /// + /// + /// + /// true if the server synchronously close the response + /// after the handler delegate has returned; otherwise, false. + /// + /// + /// The default value is true. + /// + /// + 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 @@ -884,6 +936,8 @@ private void init ( _log = _listener.Log; _services = new WebSocketServiceManager (_log); _sync = new object (); + + AutoCloseResponse = true; } private void processRequest (HttpListenerContext context) @@ -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)