/// <summary> /// Handles GET and HEAD request. /// </summary> /// <param name="context">Instace of <see cref="DavContextBaseAsync"/>.</param> /// <param name="item">Instance of <see cref="IHierarchyItemAsync"/> which was returned by /// <see cref="DavContextBaseAsync.GetHierarchyItemAsync"/> for this request.</param> public async Task ProcessRequestAsync(DavContextBaseAsync context, IHierarchyItemAsync item) { string urlPath = context.Request.RawUrl.Substring(context.Request.ApplicationPath.TrimEnd('/').Length); if (item is IItemCollectionAsync) { // In case of GET requests to WebDAV folders we serve a web page to display // any information about this server and how to use it. // Remember to call EnsureBeforeResponseWasCalledAsync here if your context implementation // makes some useful things in BeforeResponseAsync. await context.EnsureBeforeResponseWasCalledAsync(); IHttpAsyncHandler page = (IHttpAsyncHandler)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath( "~/MyCustomHandlerPage.aspx", typeof(MyCustomHandlerPage)); if (Type.GetType("Mono.Runtime") != null) { page.ProcessRequest(HttpContext.Current); } else { // Here we call BeginProcessRequest instead of ProcessRequest to start an async page execution and be able to call RegisterAsyncTask if required. // To call APM method (Begin/End) from TAP method (Task/async/await) the Task.FromAsync must be used. await Task.Factory.FromAsync(page.BeginProcessRequest, page.EndProcessRequest, HttpContext.Current, null); } } else { await OriginalHandler.ProcessRequestAsync(context, item); } }
/// <summary> /// Handles GET and HEAD request. /// </summary> /// <param name="context">Instace of <see cref="DavContextBaseAsync"/>.</param> /// <param name="item">Instance of <see cref="IHierarchyItemAsync"/> which was returned by /// <see cref="DavContextBaseAsync.GetHierarchyItemAsync"/> for this request.</param> public async Task ProcessRequestAsync(DavContextBaseAsync context, IHierarchyItemAsync item) { string urlPath = context.Request.RawUrl.Substring(context.Request.ApplicationPath.TrimEnd('/').Length); if (item is IItemCollectionAsync) { // In case of GET requests to WebDAV folders we serve a web page to display // any information about this server and how to use it. // Remember to call EnsureBeforeResponseWasCalledAsync here if your context implementation // makes some useful things in BeforeResponseAsync. await context.EnsureBeforeResponseWasCalledAsync(); string htmlName = "MyCustomHandlerPage.html"; using (TextReader reader = File.OpenText(Path.Combine(htmlPath, htmlName))) { string html = await reader.ReadToEndAsync(); html = html.Replace("_webDavServerRoot_", context.Request.ApplicationPath.TrimEnd('/')); html = html.Replace("_webDavServerVersion_", typeof(DavEngineAsync).GetTypeInfo().Assembly.GetName().Version.ToString()); await WriteFileContentAsync(context, html, htmlName); } } else { await OriginalHandler.ProcessRequestAsync(context, item); } }
/// <summary> /// Handles GET and HEAD request. /// </summary> /// <param name="context">Instace of <see cref="DavContextBaseAsync"/>.</param> /// <param name="item">Instance of <see cref="IHierarchyItemAsync"/> which was returned by /// <see cref="DavContextBaseAsync.GetHierarchyItemAsync"/> for this request.</param> public async Task ProcessRequestAsync(DavContextBaseAsync context, IHierarchyItemAsync item) { if (item is IItemCollectionAsync) { // In case of GET requests to WebDAV folders we serve a web page to display // any information about this server and how to use it. // Remember to call EnsureBeforeResponseWasCalledAsync here if your context implementation // makes some useful things in BeforeResponseAsync. await context.EnsureBeforeResponseWasCalledAsync(); string htmlName = "MyCustomHandlerPage.html"; string html = await getFileContent(htmlName); html = html.Replace("_webDavServerRoot_", context.Request.ApplicationPath.TrimEnd('/')); html = html.Replace("_webDavServerVersion_", typeof(DavEngineAsync).GetTypeInfo().Assembly.GetName().Version.ToString()); await WriteFileContentAsync(context, html, htmlName); } else if (context.Request.RawUrl.StartsWith("/wwwroot/")) { // The "/wwwroot/" is not a WebDAV folder. It can be used to store client script files, // images, static HTML files or any other files that does not require access via WebDAV. // Any request to the files in this folder will just serve them to client. await context.EnsureBeforeResponseWasCalledAsync(); string relativeFilePath = context.Request.RawUrl.TrimStart('/').Replace('/', Path.DirectorySeparatorChar); // Remove query string. int queryIndex = relativeFilePath.LastIndexOf('?'); if (queryIndex > -1) { relativeFilePath = relativeFilePath.Remove(queryIndex); } await WriteFileContentAsync(context, await getFileContent(relativeFilePath), relativeFilePath); } else { await OriginalHandler.ProcessRequestAsync(context, item); } }
/// <summary> /// Handles GET and HEAD request. /// </summary> /// <param name="context">Instace of <see cref="DavContextBaseAsync"/>.</param> /// <param name="item">Instance of <see cref="IHierarchyItemAsync"/> which was returned by /// <see cref="DavContextBaseAsync.GetHierarchyItemAsync"/> for this request.</param> public async Task ProcessRequestAsync(DavContextBaseAsync context, IHierarchyItemAsync item) { string urlPath = context.Request.RawUrl.Substring(context.Request.ApplicationPath.TrimEnd('/').Length); if (item is IItemCollectionAsync) { // In case of GET requests to WebDAV folders we serve a web page to display // any information about this server and how to use it. // Remember to call EnsureBeforeResponseWasCalledAsync here if your context implementation // makes some useful things in BeforeResponseAsync. await context.EnsureBeforeResponseWasCalledAsync(); string htmlName = "MyCustomHandlerPage.html"; using (TextReader reader = File.OpenText(Path.Combine(htmlPath, htmlName))) { string html = await reader.ReadToEndAsync(); html = html.Replace("_webDavServerRoot_", context.Request.ApplicationPath.TrimEnd('/')); html = html.Replace("_webDavServerVersion_", typeof(DavEngineAsync).GetTypeInfo().Assembly.GetName().Version.ToString()); await WriteFileContentAsync(context, html, htmlName); } } else if (urlPath.StartsWith("/AjaxFileBrowser/") || urlPath.StartsWith("/wwwroot/")) { // The "/AjaxFileBrowser/" are not a WebDAV folders. They can be used to store client script files, // images, static HTML files or any other files that does not require access via WebDAV. // Any request to the files in this folder will just serve them to the client. await context.EnsureBeforeResponseWasCalledAsync(); string filePath = Path.Combine(htmlPath, urlPath.TrimStart('/').Replace('/', Path.DirectorySeparatorChar)); // Remove query string. int queryIndex = filePath.LastIndexOf('?'); if (queryIndex > -1) { filePath = filePath.Remove(queryIndex); } if (!File.Exists(filePath)) { throw new DavException("File not found: " + filePath, DavStatus.NOT_FOUND); } Encoding encoding = context.Engine.ContentEncoding; // UTF-8 by default context.Response.ContentType = string.Format("{0}; charset={1}", MimeType.GetMimeType(Path.GetExtension(filePath)) ?? "application/octet-stream", encoding.WebName); // Return file content in case of GET request, in case of HEAD just return headers. if (context.Request.HttpMethod == "GET") { using (FileStream fileStream = File.OpenRead(filePath)) { context.Response.ContentLength = fileStream.Length; await fileStream.CopyToAsync(context.Response.OutputStream); } } } else { await OriginalHandler.ProcessRequestAsync(context, item); } }