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); } }
public async void When_RequestIsSent_Should_ReceiveResponseAndStatusCode200() { using (var dispatcher = new WebDavDispatcher(ConfigureHttpClient())) { var response = await dispatcher.Send(new Uri("http://example.com"), HttpMethod.Get, new RequestParameters(), CancellationToken.None); Assert.IsType(typeof(HttpResponse), response); Assert.Equal(200, response.StatusCode); } }
public async void When_CancellationIsRequested_Should_CancelRequest() { using (var dispatcher = new WebDavDispatcher(ConfigureHttpClient())) { var cts = new CancellationTokenSource(); cts.Cancel(); await Assert.ThrowsAsync <OperationCanceledException>( () => dispatcher.Send(new Uri("http://example.com"), HttpMethod.Get, new RequestParameters(), cts.Token)); } }
public async void When_ResponseIsReceived_Should_ReadContent() { const string responseContent = "content"; using (var dispatcher = new WebDavDispatcher(ConfigureHttpClient(responseContent))) { var response = await dispatcher.Send(new Uri("http://example.com"), HttpMethod.Get, new RequestParameters(), CancellationToken.None); Assert.Equal(responseContent, await response.Content.ReadAsStringAsync()); } }
public async void When_DisposeIsCalled_Should_DisposeHttpClient() { var httpClient = ConfigureHttpClient(); using (var dispatcher = new WebDavDispatcher(httpClient)) { await dispatcher.Send(new Uri("http://example.com"), HttpMethod.Get, new RequestParameters(), CancellationToken.None); httpClient.DidNotReceive().Dispose(); } httpClient.Received().Dispose(); }
public async void When_ContentIsSent_Should_ReceiveResponseAndStatusCode200() { using (var dispatcher = new WebDavDispatcher(ConfigureHttpClient())) { var requestParams = new RequestParameters { Content = new StringContent("content", Encoding.UTF8, "application/xml") }; var response = await dispatcher.Send(new Uri("http://example.com"), HttpMethod.Put, requestParams, CancellationToken.None); Assert.IsType(typeof(HttpResponse), response); Assert.Equal(200, response.StatusCode); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { // Create the request handler factory var requestHandlerFactory = new RequestHandlerFactory(); // Create WebDAV dispatcher var homeFolder = Environment.GetEnvironmentVariable("HOME") ?? Environment.GetEnvironmentVariable("USERPROFILE"); var webDavDispatcher = new WebDavDispatcher(new DiskStore(homeFolder), requestHandlerFactory); app.Run(async context => { // Create the proper HTTP context var httpContext = new AspNetCoreContext(context); // Dispatch request await webDavDispatcher.DispatchRequestAsync(httpContext).ConfigureAwait(false); }); }
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); } } }
public void StartWebDavServer(int port, LogLevel logLevel, bool readOnly) { using (var manager = OpenQcdmManager()) { EfsFileManager.Instance.UpdateEntries(manager, "/"); using (var httpListener = new HttpListener()) { var uri = $"http://127.0.0.1:{port}/"; httpListener.Prefixes.Add(uri); httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; httpListener.Start(); if (_logger != null && logLevel >= LogLevel.Info) { _logger.LogInfo(Strings.WebDavServerStartedFormat, uri); } var cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; Console.CancelKeyPress += ((sender, args) => cancellationTokenSource.Cancel()); var requestHandlerFactory = new RequestHandlerFactory(); var webDavDispatcher = new WebDavDispatcher(new EfsStore(manager, readOnly, _logger, logLevel), requestHandlerFactory); //var homeFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); //var webDavDispatcher = new WebDavDispatcher(new DiskStore(homeFolder), requestHandlerFactory); while (!cancellationToken.IsCancellationRequested) { var contextTask = httpListener.GetContextAsync(); contextTask.Wait(cancellationToken); var httpListenerContext = contextTask.Result; if (httpListenerContext != null) { var httpContext = new HttpContext(httpListenerContext); var dispatchRequestTask = webDavDispatcher.DispatchRequestAsync(httpContext); dispatchRequestTask.Wait(cancellationToken); } } } manager.Close(); } }
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); } } }