private static async void DispatchHttpRequestsAsync(System.Net.HttpListener httpListener, CancellationToken cancellationToken) { // Create a request handler factory that uses basic authentication var requestHandlerFactory = new RequestHandlerFactory(); // Create WebDAV dispatcher var homeFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var webDavDispatcher = new WebDavDispatcher(new DiskStore(homeFolder), requestHandlerFactory); // Determine the WebDAV username/password for authorization // (only when basic authentication is enabled) var webdavUsername = ConfigurationManager.AppSettings["webdav.username"] ?? "test"; var webdavPassword = ConfigurationManager.AppSettings["webdav.password"] ?? "test"; HttpListenerContext httpListenerContext; while (!cancellationToken.IsCancellationRequested && (httpListenerContext = await httpListener.GetContextAsync().ConfigureAwait(false)) != null) { // Determine the proper HTTP context IHttpContext httpContext; if (httpListenerContext.Request.IsAuthenticated) { httpContext = new HttpBasicContext(httpListenerContext, checkIdentity: i => i.Name == webdavUsername && i.Password == webdavPassword); } else { httpContext = new HttpContext(httpListenerContext); } // Dispatch the request await webDavDispatcher.DispatchRequestAsync(httpContext).ConfigureAwait(false); } }
private static async Task DispatchHttpRequestsAsync(HttpListener httpListener, int maxThreadCount = Int32.MaxValue) { // Create a request handler factory that uses basic authentication var requestHandlerFactory = new RequestHandlerFactory(); // Create WebDAV dispatcher var homeFolder = new LocalStore( isEnabledPropFunc: Config.IsEnabledWebDAVProperty, lockingManager: CloudManager.Settings.UseLocks ? (ILockingManager) new InMemoryLockingManager() : new EmptyLockingManager()); 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.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 async void Loop() { token = new CancellationTokenSource(); var requestHandlerFactory = new RequestHandlerFactory(); var store = new DavSystem(sys); var webDavDispatcher = new WebDavDispatcher(store, requestHandlerFactory); HttpListenerContext context; while (!token.IsCancellationRequested && (context = await listener.GetContextAsync().ConfigureAwait(false)) != null) { IHttpContext httpCtx; if (context.Request.IsAuthenticated) { httpCtx = new HttpBasicContext(context, checkIdentity: CheckAuth); } else { httpCtx = new HttpContext(context); } await webDavDispatcher.DispatchRequestAsync(httpCtx).ConfigureAwait(false); } }
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); } } }