private static async Task DispatchHttpRequestsAsync(HttpListener httpListener, int maxThreadCount = Int32.MaxValue) { // Create a request handler factory that uses basic authentication var requestHandlerFactory = new WebDavMailRu.CloudStore.Mailru.RequestHandlerFactory(); // Create WebDAV dispatcher var homeFolder = new MailruStore(); var webDavDispatcher = new WebDavDispatcher(homeFolder, requestHandlerFactory); try { using (var sem = new SemaphoreSlim(maxThreadCount)) { var semclo = sem; while (!CancelToken.IsCancellationRequested) { var httpListenerContext = await httpListener.GetContextAsync().ConfigureAwait(false); if (httpListenerContext == null) { break; } HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)httpListenerContext.User.Identity; IHttpContext httpContext = new HttpBasicContext(httpListenerContext, i => i.Name == identity.Name && i.Password == identity.Password); await semclo.WaitAsync(CancelToken.Token); var task = Task.Run(async() => { try { await webDavDispatcher.DispatchRequestAsync(httpContext) .ConfigureAwait(false); } finally { semclo.Release(); } }, CancelToken.Token); } } } catch (HttpListenerException) when(CancelToken.IsCancellationRequested) { Logger.Info("Server stopped"); } catch (OperationCanceledException) { Logger.Info("Server stopped"); } catch (Exception e) { Logger.Error("Global exception", e); } }
private static async void DispatchHttpRequestsAsync(HttpListener httpListener, CancellationToken cancellationToken, int maxThreadCount = Int32.MaxValue) { // Create a request handler factory that uses basic authentication var requestHandlerFactory = new RequestHandlerFactory(); // Create WebDAV dispatcher var homeFolder = new MailruStore(); var webDavDispatcher = new WebDavDispatcher(homeFolder, requestHandlerFactory); using (var sem = new SemaphoreSlim(maxThreadCount)) { var semclo = sem; while (!cancellationToken.IsCancellationRequested) { HttpListenerContext httpListenerContext; try { httpListenerContext = await httpListener.GetContextAsync().ConfigureAwait(false); } catch (Exception) { return; } IHttpContext httpContext = new HttpContext(httpListenerContext); await semclo.WaitAsync(cancellationToken); await Task .Run(async() => { try { await webDavDispatcher.DispatchRequestAsync(httpContext); } catch (Exception ex) { throw new Exception(ex.Message); } }, cancellationToken) .ContinueWith(t => semclo.Release(), cancellationToken); } } }
private static async Task DispatchHttpRequestsAsync(HttpListener httpListener, CancellationToken cancellationToken, int maxThreadCount = Int32.MaxValue) { // Create a request handler factory that uses basic authentication var requestHandlerFactory = new CloudStore.Mailru.RequestHandlerFactory(); // Create WebDAV dispatcher var homeFolder = new MailruStore(); var webDavDispatcher = new WebDavDispatcher(homeFolder, requestHandlerFactory); try { using (var sem = new SemaphoreSlim(maxThreadCount)) { var semclo = sem; while (!cancellationToken.IsCancellationRequested) { var httpListenerContext = await httpListener.GetContextAsync().ConfigureAwait(false); if (httpListenerContext == null) { break; } //if (httpListenerContext.Request.IsAuthenticated) // httpContext = new HttpBasicContext(httpListenerContext, i => i.Name == webdavUsername && i.Password == webdavPassword); //else httpContext = new HttpContext(httpListenerContext); HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)httpListenerContext.User.Identity; IHttpContext httpContext = new HttpBasicContext(httpListenerContext, i => i.Name == identity.Name && i.Password == identity.Password); await semclo.WaitAsync(cancellationToken); // ReSharper disable once UnusedVariable Task tsk = Task .Run(async() => { try { await webDavDispatcher.DispatchRequestAsync(httpContext); } catch (Exception ex) { Logger.Error("Exception", ex); } }, cancellationToken) .ContinueWith(t => semclo.Release(), cancellationToken); } } } catch (HttpListenerException excListener) { if (excListener.ErrorCode != ERROR_OPERATION_ABORTED) { throw; } } catch (Exception e) { Logger.Error("Global exception", e); } }
private static async void DispatchHttpRequestsAsync(HttpListener httpListener, CancellationToken cancellationToken, int maxThreadCount = Int32.MaxValue) { // Create a request handler factory that uses basic authentication var requestHandlerFactory = new RequestHandlerFactory(); // Create WebDAV dispatcher var homeFolder = new MailruStore(); var webDavDispatcher = new WebDavDispatcher(homeFolder, requestHandlerFactory); // Determine the WebDAV username/password for authorization // (only when basic authentication is enabled) var webdavUsername = "******"; var webdavPassword = "******"; using (var sem = new SemaphoreSlim(maxThreadCount)) { var semclo = sem; HttpListenerContext httpListenerContext; while ( !cancellationToken.IsCancellationRequested && (httpListenerContext = await httpListener.GetContextAsync().ConfigureAwait(false)) != null ) { IHttpContext httpContext; if (httpListenerContext.Request.IsAuthenticated) { httpContext = new HttpBasicContext(httpListenerContext, i => i.Name == webdavUsername && i.Password == webdavPassword); } else { httpContext = new HttpContext(httpListenerContext); } //var r = httpContext.Request; //var range = r.GetRange(); //Logger.Info($"HTTP {r.Url} {r.HttpMethod} "); //await webDavDispatcher.DispatchRequestAsync(httpContext); await semclo.WaitAsync(cancellationToken); Task tsk = Task .Run(async() => { try { //var r = httpContext.Request; //var range = r.GetRange(); //Logger.Info($"HTTP {r.Url} {r.HttpMethod} "); //if (null != range) Logger.Info($"Range {range.Start} / {range.End} {range.If}"); //Logger.Info($"-------awail {semclo.CurrentCount}"); await webDavDispatcher.DispatchRequestAsync(httpContext); //Logger.Info($"-------awail {semclo.CurrentCount}"); } catch (Exception ex) { Logger.Error("Exception", ex); } }, cancellationToken) .ContinueWith(t => semclo.Release(), cancellationToken); } } }