public void Should_not_find_other_files_on_disk() { var middleware = new StaticFileMiddleware(new DummyNext()); var context = new OwinContext { Request = { Path = new PathString("/filename.js"), Method = "GET" } }; middleware.Invoke(context); Assert.AreEqual(null, context.Response.ContentLength); Assert.AreEqual(null, context.Response.ContentType); context = new OwinContext { Request = { Path = new PathString("/../ServicePulse.Host.exe.config"), Method = "GET" } }; middleware.Invoke(context); Assert.AreEqual(null, context.Response.ContentLength); Assert.AreEqual(null, context.Response.ContentType); }
public async Task Invoke(IDictionary <string, object> arg) { await innerMiddleware.Invoke(arg); if ((int)arg["owin.ResponseStatusCode"] == 404) { arg["owin.RequestPath"] = entryPath.Value; await innerMiddleware.Invoke(arg); } }
public async Task Invoke(IDictionary <string, object> environment) { var context = new OwinContext(environment); await _innerMiddleware.Invoke(environment); if (context.Response.StatusCode == 404 && _options.Html5Mode) { context.Request.Path = _options.EntryPath; await _innerMiddleware.Invoke(environment); } }
/// <summary> /// 以非同步方式趨動 AngularServerMiddleware 的 threading task. /// </summary> /// <param name="arg">傳入 System.Collections.Generic.IDictionary 型態的參數</param> /// <returns>回傳非同步執行結果。若找不到網頁,server 產生 404 錯誤時,將 web routing 指回 EntryPath。</returns> public async Task Invoke(IDictionary <string, object> arg) { await _innerMiddleware.Invoke(arg); // route to root path if the status code is 404 // and need support angular html5mode if ((int)arg["owin.ResponseStatusCode"] == 404 && _options.Html5Mode) { arg["owin.RequestPath"] = _options.EntryPath.Value; await _innerMiddleware.Invoke(arg); } }
public async Task Invoke(IDictionary <string, object> arg) { await _innerMiddleware.Invoke(arg); var responseStatusCode = (int)arg["owin.ResponseStatusCode"]; if (responseStatusCode == 404 && _options.Html5Mode) { arg["owin.RequestPath"] = _options.EntryPath.Value; await _innerMiddleware.Invoke(arg); } }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var path = httpContext.Request.Path.Value; // If the RoutePrefix is requested (with or without trailing slash), redirect to index URL if (httpMethod == "GET" && _options.RoutePrefix.Equals(path.Trim('/'), StringComparison.OrdinalIgnoreCase)) { // Use relative redirect to support proxy environments var relativeIndexUrl = string.IsNullOrEmpty(path) || path.EndsWith("/") ? "index.html" : $"{path.Split('/').Last()}/index.html"; RespondWithRedirect(httpContext.Response, relativeIndexUrl); return; } if (httpMethod == "GET" && $"/{_options.RoutePrefix}/index.html".Equals(path, StringComparison.OrdinalIgnoreCase)) { await RespondWithIndexHtml(httpContext.Response); return; } await _staticFileMiddleware.Invoke(httpContext); }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var path = httpContext.Request.Path.Value; // If the RoutePrefix is requested (with or without trailing slash), redirect to index URL if (httpMethod == "GET" && Regex.IsMatch(path, $"^/?{Regex.Escape(_options.RoutePrefix)}/?$", RegexOptions.IgnoreCase)) { // Use relative redirect to support proxy environments var relativeRedirectPath = string.IsNullOrEmpty(path) || path.EndsWith("/") ? "index.html" : $"{path.Split('/').Last()}/index.html"; relativeRedirectPath = $"{httpContext.Request.PathBase.Value}/{relativeRedirectPath.TrimStart('/')}".TrimStart('/'); RespondWithRedirect(httpContext.Response, relativeRedirectPath); return; } if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{Regex.Escape(_options.RoutePrefix)}/?index.html$", RegexOptions.IgnoreCase)) { await RespondWithIndexHtml(httpContext.Response); return; } await _staticFileMiddleware.Invoke(httpContext); }
private static void RegisterFileInfo(this IWebBuilder webBuilder, IFileProvider fileProvider, PathString prefix, string basePath, IFileInfo fileInfo, RegisterOptions options) { if (fileInfo.IsDirectory) { var content = fileProvider.GetDirectoryContents(Path.Combine(basePath, fileInfo.Name)); if (content == null || !content.Exists) { return; } foreach (var child in content) { webBuilder.RegisterFileInfo(fileProvider, prefix, Path.Combine(basePath, fileInfo.Name), child, options); } } else { var filePath = Path.Combine(basePath, fileInfo.Name); var requestPath = new PathString().Add(prefix) .Add(filePath); if (options != null && options.Matcher != null) { if (!options.Matcher.Match(filePath.Substring(1)).HasMatches) { // We are ignoring this file return; } } var builtState = options?.State?.Invoke(prefix, requestPath, filePath, fileInfo, fileProvider); webBuilder.Register(requestPath, async context => { var env = context.RequestServices.GetRequiredService <IHostingEnvironment>(); var statileFileOptions = Options.Create(new StaticFileOptions()); statileFileOptions.Value.FileProvider = fileProvider; statileFileOptions.Value.ServeUnknownFileTypes = true; var loggerFactory = context.RequestServices.GetRequiredService <ILoggerFactory>(); var middleware = new StaticFileMiddleware(_ => Task.CompletedTask, env, statileFileOptions, loggerFactory); var oldPath = context.Request.Path; try { context.Request.Path = filePath; await middleware.Invoke(context); } finally { context.Request.Path = oldPath; } }, builtState, /*don't convert "/file" to "/file/index.html"*/ true); } }
public async Task Invoke(HttpContext context) { if (await _bundleManager.TryEnsureUrlAsync(context)) await _staticFileMiddleware.Invoke(context); else await _next(context); }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var path = httpContext.Request.Path.Value ?? ""; var route = GetRoute(path); if (httpMethod == "GET") { if (route == Routes.Redirect) { var relativeIndexUrl = string.IsNullOrEmpty(path) || path.EndsWith("/") ? "index.html" : $"{path.Split('/').Last()}/index.html"; httpContext.Response.StatusCode = 301; httpContext.Response.Headers["Location"] = relativeIndexUrl; return; } if (route == Routes.Index) { await RespondWithIndexHtml(httpContext.Response); return; } if (route == Routes.JSON) { await RespondWithJSON(httpContext.Response); return; } } await _staticFileMiddleware.Invoke(httpContext); }
public async Task Invoke(HttpContext httpContext) { if (string.Equals(httpContext.Request.Method, "GET") && httpContext.Request.Path.StartsWithSegments(_options.RoutePrefix)) { var path = httpContext.Request.Path.Value; if (path.Equals(_options.RoutePrefix, StringComparison.OrdinalIgnoreCase) || (path[path.Length - 1] == '/' && path.Length == _options.RoutePrefix.Length + 1)) { httpContext.Response.StatusCode = 302; httpContext.Response.Headers["Location"] = $"{_options.RoutePrefix}/index.html"; return; } if (path.EndsWith("config.json", StringComparison.OrdinalIgnoreCase)) { //获取配置 var config = JsonConvert.SerializeObject(new UIConfigDto() { ApiAddress = _options.APIAddress, }, _serializerSettings); #if DEBUG httpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); #endif httpContext.Response.ContentType = "text/json;charset=utf-8"; await httpContext.Response.WriteAsync(config, Encoding.UTF8); return; } } await _staticFileMiddleware.Invoke(httpContext); }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var path = httpContext.Request.Path.Value; // If the RoutePrefix is requested (with or without trailing slash), redirect to index URL if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{_options.RoutePrefix}/?$")) { // Use relative redirect to support proxy environments var relativeRedirectPath = path.EndsWith("/") ? "index.html" : $"{path.Split('/').Last()}/index.html"; RespondWithRedirect(httpContext.Response, relativeRedirectPath); return; } if (httpMethod == "GET" && Regex.IsMatch(path, $"/{_options.RoutePrefix}/?index.html")) { await RespondWithIndexHtml(httpContext.Response); return; } await _staticFileMiddleware.Invoke(httpContext); }
/// <summary> /// StaticFileMiddleware Invoke /// </summary> /// <param name="httpContext">HttpRequest</param> /// <returns></returns> public async Task Invoke(HttpContext httpContext) { string httpMethod = httpContext.Request.Method; string path = httpContext.Request.Path.Value.ToLower(); if (httpMethod == "GET" && Regex.IsMatch(path, $"/apihelper/")) { if (Regex.IsMatch(path, $"/apihelper/index.html")) { //index.html特殊处理 await RespondWithIndexHtml(httpContext.Response); return; } else { string resourceName = path.Replace($"/apihelper/", "") .Replace("/", "."); if (!apiResources.Any(a => a.ToLower() == $"{embeddedFileNamespace}.{resourceName}".ToLower())) { // 处理刷新界面 await RespondWithIndexHtml(httpContext.Response); return; } } } await staticFileMiddleware.Invoke(httpContext); }
public async Task InvokeAsync(HttpContext context) { if (RequestingSwaggerExport(context.Request)) { await ExportApiDocument(context); } await _staticFileMiddleware.Invoke(context); }
public Task Invoke(HttpContext context) { if (context.Request.Path.HasValue) { ProcessRequest(context); } return(_base.Invoke(context)); }
public Task Invoke(HttpContext context) { if (context.Request.Path.HasValue) { ProcessRequest(context); } return(_base.Invoke(context)); // first, we preprocess the request (change context.Request.Path), and then we let StaticFileMiddleware load it from disk }
public async Task Invoke(HttpContext context) { if (IsRequestingUiBase(context.Request)) { context.Response.StatusCode = (int)HttpStatusCode.MovedPermanently; if (context.TryGetDocument(_options, out var document)) { context.Response.Headers["Location"] = GetUiIndexFullRoute(context.Request).Replace("{document}", document); } else { context.Response.Headers["Location"] = GetUiIndexFullRoute(context.Request); } return; } if (IsRequestingAsyncApiUi(context.Request)) { if (context.TryGetDocument(_options, out var document)) { await RespondWithAsyncApiHtml(context.Response, GetDocumentFullRoute(context.Request).Replace("{document}", document)); } else { await RespondWithAsyncApiHtml(context.Response, GetDocumentFullRoute(context.Request)); } return; } if (!context.TryGetDocument(_options, out var documentName)) { await _staticFiles.Invoke(context); } else { if (_namedStaticFiles.TryGetValue(documentName, out var files)) { await files.Invoke(context); } else { await _staticFiles.Invoke(context); } } }
public Task Invoke(HttpContext context) { if (context.Request.Path.HasValue) { string acceptEncoding = context.Request.Headers["Accept-Encoding"]; FileInfo matchedFile = null; string[] browserSupportedCompressionTypes = context.Request.Headers["Accept-Encoding"].ToString().Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); var orginalFilePath = System.IO.Path.Combine( _hostingEnv.WebRootPath, context.Request.Path.Value.StartsWith("/") ? context.Request.Path.Value.Remove(0, 1) : context.Request.Path.Value ); var orginalFile = new FileInfo(orginalFilePath); if (orginalFile.Exists) { foreach (var compressionType in compressionTypes.Keys) { if (browserSupportedCompressionTypes.Contains(compressionType, StringComparer.OrdinalIgnoreCase)) { var fileExtension = compressionTypes[compressionType]; var filePath = orginalFilePath + fileExtension; var file = new FileInfo(filePath); if (file.Exists && file.Length < orginalFile.Length) { if (matchedFile == null) { matchedFile = file; } else if (matchedFile.Length > file.Length) { matchedFile = file; } } } } if (matchedFile != null) { var matchedPath = context.Request.Path.Value + matchedFile.Extension; _logger.LogInformation($"Request: {context.Request.Path.Value}, matched: {matchedPath}, ({matchedFile.Length} instead of {orginalFile.Length} bytes)"); context.Request.Path = new PathString(matchedPath); return(_base.Invoke(context)); } } } return(_base.Invoke(context)); }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var path = httpContext.Request.Path.Value; // If the RoutePrefix is requested (with or without trailing slash), redirect to index URL //if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{_options.RoutePrefix}/?$")) //{ // // Use relative redirect to support proxy environments // var relativeRedirectPath = path.EndsWith("/") // ? "doc.html" // : $"{path.Split('/').Last()}/doc.html"; // RespondWithRedirect(httpContext.Response, relativeRedirectPath); // return; //} if (httpMethod == "GET" && Regex.IsMatch(path, $"/doc.html")) { await RespondWithIndexHtml(httpContext.Response); return; } if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{_options.StaticFilePrefix}/")) { var _reg = new Regex("[.][0-9]"); var _srcPath = path.Substring(0, path.LastIndexOf("/")) ?? ""; if (!string.IsNullOrEmpty(_srcPath)) { var _destPath = _reg.Replace(_srcPath, r => r.Value.Replace(".", "._")); httpContext.Request.Path = path.Replace(_srcPath, _destPath.Replace("-", "_")); } } if (httpMethod == "GET" && Regex.IsMatch(path, $"^/swagger-resources$")) { var _data = new List <Dictionary <string, object> > { new Dictionary <string, object> { { "name", "默认分组" }, { "swaggerVersion", "2.0" }, { "location", "/swagger/v1/swagger.json" }, { "url", "/swagger/v1/swagger.json" } } }; if (_options.ConfigObject.Urls.Count() > 0) { await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(_options.ConfigObject.Urls)); } else { await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(_data)); } return; } await _staticFileMiddleware.Invoke(httpContext); }
/// <summary> /// Try to execute the logic of the middleware /// </summary> /// <param name="httpContext">The HttpContext</param> public Task Invoke(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } return(IsAltairRequest(httpContext.Request) ? InvokeAltair(httpContext.Response) : _staticFileMiddleware.Invoke(httpContext)); }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var path = httpContext.Request.Path.Value; // If the RoutePrefix is requested (with or without trailing slash), redirect to index URL if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{Regex.Escape(_options.RoutePrefix)}/api/logs/?$", RegexOptions.IgnoreCase)) { try { httpContext.Response.ContentType = "application/json;charset=utf-8"; if (!CanAccess(httpContext)) { httpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; return; } var result = await FetchLogsAsync(httpContext); httpContext.Response.StatusCode = (int)HttpStatusCode.OK; await httpContext.Response.WriteAsync(result); } catch (Exception ex) { _logger.LogError(ex, ex.Message); httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; var errorMessage = httpContext.Request.IsLocal() ? JsonConvert.SerializeObject(new { errorMessage = ex.Message }) : JsonConvert.SerializeObject(new { errorMessage = "Internal server error" }); await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(new { errorMessage })); } return; } if (httpMethod == "GET" && Regex.IsMatch(path, $"^/?{Regex.Escape(_options.RoutePrefix)}/?$", RegexOptions.IgnoreCase)) { var indexUrl = httpContext.Request.GetEncodedUrl().TrimEnd('/') + "/index.html"; RespondWithRedirect(httpContext.Response, indexUrl); return; } if (httpMethod == "GET" && Regex.IsMatch(path, $"^/{Regex.Escape(_options.RoutePrefix)}/?index.html$", RegexOptions.IgnoreCase)) { await RespondWithIndexHtml(httpContext.Response); return; } await _staticFileMiddleware.Invoke(httpContext); }
/// <summary> /// StaticFileMiddleware Invoke /// </summary> /// <param name="httpContext">HttpRequest</param> /// <returns></returns> public async Task Invoke(HttpContext httpContext) { string httpMethod = httpContext.Request.Method; string path = httpContext.Request.Path.Value; if (httpMethod == "GET" && Regex.IsMatch(path, $"ApiHelper/index.html")) { //index.html特殊处理 await RespondWithIndexHtml(httpContext.Response); return; } await staticFileMiddleware.Invoke(httpContext); }
public void Should_return_correct_mimetype() { var middleware = new StaticFileMiddleware(new DummyNext()); var context = new OwinContext { Request = { Path = new PathString("/js/app.constants.js"), Method = "GET" } }; middleware.Invoke(context); Assert.AreEqual(("application/javascript"), context.Response.ContentType); }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var page = httpContext.Request.RouteValues.FirstOrDefault().Value as string; if (httpMethod == "GET" && (string.IsNullOrEmpty(page) || page == "//")) { await RespondWithIndexHtml(httpContext.Response); return; } // Inserted for future use, to include also JS library in assembly await _staticFileMiddleware.Invoke(httpContext); }
public async Task InvokeAsync(HttpContext context, RequestDelegate next) { var staticFileOptions = new StaticFileOptions { RequestPath = "/resources", FileProvider = new EmbeddedFileProvider(Assembly, EmbeddedFileNamespace), }; var staticMiddleware = new StaticFileMiddleware( next, webHostEnvironment, Options.Create(staticFileOptions), loggerFactory); await staticMiddleware.Invoke(context); }
private MiddlewareContinuation forMethodAndFile(string method, string path, string etag = null) { theRequest.HttpMethod(method); if (theRequest.Environment.ContainsKey(OwinConstants.RequestPathKey)) { theRequest.Environment[OwinConstants.RequestPathKey] = path; } else { theRequest.Environment.Add(OwinConstants.RequestPathKey, path); } return(theMiddleware.Invoke(theRequest, theResponse)); }
public void Should_handle_get_and_head(string method) { var middleware = new StaticFileMiddleware(new DummyNext()); var context = new OwinContext { Request = { Path = new PathString("/js/app.js"), Method = method } }; middleware.Invoke(context); Assert.IsNotNull(context.Response.ContentLength); Assert.IsNotEmpty(context.Response.ContentType); }
public void Should_only_handle_get_and_head() { var middleware = new StaticFileMiddleware(new DummyNext()); var context = new OwinContext { Request = { Path = new PathString("/whatever"), Method = "POST" } }; middleware.Invoke(context); Assert.AreEqual(null, context.Response.ContentLength); Assert.AreEqual(null, context.Response.ContentType); }
public async Task Invoke(HttpContext httpContext) { var httpMethod = httpContext.Request.Method; var path = httpContext.Request.Path.Value; if (httpMethod == "GET" && Regex.IsMatch(path, $"^/?{Regex.Escape(_options.PathMatch)}/?$", RegexOptions.IgnoreCase)) { var redirectUrl = string.IsNullOrEmpty(path) || path.EndsWith("/") ? "index.html" : $"{path.Split('/').Last()}/index.html"; httpContext.Response.StatusCode = 301; httpContext.Response.Headers["Location"] = redirectUrl; return; } if (httpMethod == "GET" && Regex.IsMatch(path, $"^/?{Regex.Escape(_options.PathMatch)}/?index.html$", RegexOptions.IgnoreCase)) { var isAuthenticated = httpContext.User?.Identity?.IsAuthenticated; if (isAuthenticated == false && _options.UseChallengeOnAuth) { await httpContext.ChallengeAsync(_options.DefaultChallengeScheme); return; } httpContext.Response.StatusCode = 200; httpContext.Response.ContentType = "text/html;charset=utf-8"; await using var stream = GetType().Assembly.GetManifestResourceStream(EmbeddedFileNamespace + ".index.html"); if (stream == null) { throw new InvalidOperationException(); } using var sr = new StreamReader(stream); var htmlBuilder = new StringBuilder(await sr.ReadToEndAsync()); htmlBuilder.Replace("%(servicePrefix)", _options.PathMatch + "/api"); htmlBuilder.Replace("%(pollingInterval)", _options.StatsPollingInterval.ToString()); await httpContext.Response.WriteAsync(htmlBuilder.ToString(), Encoding.UTF8); return; } await _staticFileMiddleware.Invoke(httpContext); }
/// <summary> /// Invokes the <see cref="AdminUIMiddleware"/>. /// </summary> /// <param name="httpContext">Encapsulates all HTTP-specific information about an individual HTTP request.</param> public async Task Invoke(HttpContext httpContext) { var requestMethod = httpContext.Request.Method; var requestPath = httpContext.Request.Path.Value; if (_next != null && !requestPath.StartsWith(_options.Path, StringComparison.OrdinalIgnoreCase)) { await _next.Invoke(httpContext); return; } if (!_staticFileOptions.ContentTypeProvider.TryGetContentType(requestPath, out var _)) { httpContext.Request.Path = new PathString($"{_options.Path}/index.html"); } await _staticFileMiddleware.Invoke(httpContext); }