public static async Task <IActionResult> DfmServeStaticsFunction( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = StaticsRoute)] HttpRequest req, string p1, string p2, string p3, ExecutionContext context, ILogger log ) { return(await req.HandleErrors(log, async() => { // Checking nonce, if it was set as an env variable. // Don't care about return value of this method here. Auth.IsNonceSetAndValid(req.Headers); // Two bugs away. Making sure none of these segments ever contain any path separators and/or relative paths string path = Path.Join(Path.GetFileName(p1), Path.GetFileName(p2), Path.GetFileName(p3)); string root = Path.Join(context.FunctionAppDirectory, "DfmStatics"); var contentType = FileMap.FirstOrDefault((kv => path.StartsWith(kv[0]))); if (contentType != null) { string fullPath = Path.Join(root, path); if (!File.Exists(fullPath)) { return new NotFoundResult(); } return new FileStreamResult(File.OpenRead(fullPath), contentType[1]) { LastModified = File.GetLastWriteTimeUtc(fullPath) }; } // Adding anti-forgery token using (var generator = RandomNumberGenerator.Create()) { var bytes = new byte[64]; generator.GetBytes(bytes); string token = Convert.ToBase64String(bytes); req.HttpContext.Response.Cookies .Append(Globals.XsrfTokenCookieAndHeaderName, token, new CookieOptions { HttpOnly = false }); } // Returning index.html by default, to support client routing return await ReturnIndexHtml(context, log, root, p1); })); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "ui/{p1?}/{p2?}/{p3?}")] HttpRequest req, ExecutionContext context ) { string root = context.FunctionAppDirectory; string path = req.Path.Value; var contentType = FileMap.FirstOrDefault((kv => path.StartsWith(kv[0]))); if (contentType != null) { return(File.Exists(root + path) ? (IActionResult) new FileStreamResult(File.OpenRead(root + path), contentType[1]) : new NotFoundResult()); } // Returning index.html by default, to support client routing return(new FileStreamResult(File.OpenRead($"{root}/api/ui/index.html"), "text/html; charset=UTF-8")); }
public static async Task <IActionResult> DfmServeStaticsFunction( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = StaticsRoute)] HttpRequest req, ExecutionContext context, ILogger log ) { string root = context.FunctionAppDirectory + "/DfmStatics"; string path = req.Path.Value; string routePrefix = GetRoutePrefixFromHostJson(context, log); string dfmRoutePrefix = GetDfmRoutePrefixFromFunctionJson(context, log); if (!string.IsNullOrEmpty(dfmRoutePrefix)) { routePrefix = string.IsNullOrEmpty(routePrefix) ? dfmRoutePrefix : routePrefix + "/" + dfmRoutePrefix; } // Applying routePrefix, if it is set to something other than empty string if (!string.IsNullOrEmpty(routePrefix) && path.StartsWith("/" + routePrefix)) { path = path.Substring(routePrefix.Length + 1); } var contentType = FileMap.FirstOrDefault((kv => path.StartsWith(kv[0]))); if (contentType != null) { return(File.Exists(root + path) ? (IActionResult) new FileStreamResult(File.OpenRead(root + path), contentType[1]) : new NotFoundResult()); } // Returning index.html by default, to support client routing string html = await File.ReadAllTextAsync($"{root}/index.html"); // Replacing our custom meta tag with customized code from Storage or with default Content Security Policy string customMetaTagCode = (await CustomTemplates.GetCustomMetaTagCodeAsync()) ?? DefaultContentSecurityPolicyMeta; html = html.Replace(Globals.CustomMetaTag, customMetaTagCode); // Applying routePrefix, if it is set to something other than empty string if (!string.IsNullOrEmpty(routePrefix)) { html = html.Replace("<script>var DfmRoutePrefix=\"\"</script>", $"<script>var DfmRoutePrefix=\"{routePrefix}\"</script>"); html = html.Replace("href=\"/", $"href=\"/{routePrefix}/"); html = html.Replace("src=\"/", $"src=\"/{routePrefix}/"); } // Applying client config, if any string clientConfigString = Environment.GetEnvironmentVariable(EnvVariableNames.DFM_CLIENT_CONFIG); if (!string.IsNullOrEmpty(clientConfigString)) { dynamic clientConfig = JObject.Parse(clientConfigString); html = html.Replace("<script>var DfmClientConfig={}</script>", "<script>var DfmClientConfig=" + clientConfig.ToString() + "</script>"); } return(new ContentResult() { Content = html, ContentType = "text/html; charset=UTF-8" }); }
protected string GetFilePath(string fileName) { return(FileMap.FirstOrDefault(f => f.Value == fileName).Key); }