public IHttpHandler CheckAndProcess(IHttpRequest request) { var abs_path = request.GetAbsolutePath(); if (abs_path.StartsWith("/resource")) { return(null); } if (abs_path.StartsWith("/oauth/") || abs_path.StartsWith("/api/")) { return(null); } else if (abs_path.StartsWith("/admin/") || abs_path.StartsWith("swagger-ui")) { return(this); } else if (abs_path.Count(c => c == '/') == 1) { return(this); } else { return(null); } }
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName) { response.EndHttpHandlerRequest(skipClose: true, afterBody: r => { // i.e. /static/folder/file.html => folder/file.html var requested_relative_path = request.GetAbsolutePath().Substring(RoutePath.Length); if (requested_relative_path.Count(c => c == '.') == 0) { // append index.html if (requested_relative_path.EndsWith("/")) { requested_relative_path += "index.html"; } else { requested_relative_path += "/index.html"; } } var file_content = ReadInEmbeddedResource(requested_relative_path); if (file_content == null) { throw new HttpException(404, "Not found"); } r.ContentType = MimeTypes.GetMimeType(requested_relative_path); TimeSpan maxAge; if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge)) { r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds); } if (request.HasNotModifiedSince(lastModified)) { r.StatusCode = 304; return; } try { r.AddHeaderLastModified(lastModified); r.ContentType = MimeTypes.GetMimeType(requested_relative_path); r.Write(file_content); } catch (Exception ex) { //log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message); throw new HttpException(400, "Server error."); } }); }
public IHttpHandler CheckAndProcess(IHttpRequest request) { if (!active) { return(null); } string abs_path = request.GetAbsolutePath(); if (this.HtdocsPath.Contains("swagger-ui")) { if (abs_path.StartsWith("/swagger-ui/")) { return(this); } } if (abs_path.StartsWith("/fonts/")) { return(this); } if (abs_path.StartsWith("/resource")) { return(null); } if (abs_path.StartsWith("/oauth/") || abs_path.StartsWith("/api/")) { return(null); } else if (abs_path.StartsWith("/admin/")) { return(this); } else if (abs_path.Count(c => c == '/') == 1) { return(this); } else { return(null); } /* * // TODO case insensitive on option * if (abs_path.StartsWith(RoutePath) && is_html_request) * // we want to handle this url path * return this; * else * return null; */ }
public IHttpHandler CheckAndProcess(IHttpRequest request) { var abs_path = request.GetAbsolutePath(); // TODO case insensitive on option if (abs_path.StartsWith(RoutePath)) { // we want to handle this url path return(this); } else { return(null); } }
/// <summary>An IHttpRequest extension method that gets the parent absolute path.</summary> /// /// <param name="httpReq">The httpReq to act on.</param> /// /// <returns>The parent absolute path.</returns> public static string GetParentAbsolutePath(this IHttpRequest httpReq) { return(httpReq.GetAbsolutePath().ToParentPath()); }
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName) { response.EndHttpHandlerRequest (skipClose: true, afterBody: r => { // i.e. /static/folder/file.html => folder/file.html var requested_relative_path = request.GetAbsolutePath().Substring(RoutePath.Length); if (requested_relative_path.Count (c => c == '.') == 0) { // append index.html if (requested_relative_path.EndsWith ("/")) { requested_relative_path += "index.html"; } else requested_relative_path += "/index.html"; } var file_content = ReadInEmbeddedResource (requested_relative_path); if (file_content == null) throw new HttpException(404, "Not found"); r.ContentType = MimeTypes.GetMimeType(requested_relative_path); TimeSpan maxAge; if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge)) { r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds); } if (request.HasNotModifiedSince(lastModified)) { r.StatusCode = 304; return; } try { r.AddHeaderLastModified(lastModified); r.ContentType = MimeTypes.GetMimeType(requested_relative_path); r.Write(file_content); } catch (Exception ex) { //log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message); throw new HttpException(400, "Server error."); } }); }
public IHttpHandler CheckAndProcess(IHttpRequest request) { var abs_path = request.GetAbsolutePath(); if (abs_path.StartsWith ("/resource")) { return null; } if (abs_path.StartsWith ("/oauth/") || abs_path.StartsWith ("/api/")) { return null; } else if (abs_path.StartsWith ("/admin/") || abs_path.StartsWith ("swagger-ui")) { return this; } else if (abs_path.Count (c => c == '/') == 1) { return this; } else { return null; } }
public IHttpHandler CheckAndProcess(IHttpRequest request) { var abs_path = request.GetAbsolutePath(); // TODO case insensitive on option if (abs_path.StartsWith(RoutePath)) // we want to handle this url path return this; else return null; }
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName) { response.EndHttpHandlerRequest(skipClose: true, afterBody: r => { // i.e. /static/folder/file.html => folder/file.html var abs_path = request.GetAbsolutePath(); string requested_relative_path = ""; if (abs_path == "/") requested_relative_path = "./"; else requested_relative_path = abs_path.Substring(RoutePath.Length); var fileName = Path.Combine (HtdocsPath, requested_relative_path); var fi = new FileInfo(fileName); if (!fi.Exists) { // append default filename if feasible (i.e. index.html) if ((fi.Attributes & FileAttributes.Directory) != 0) { foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments) { var defaultFileName = Path.Combine(fi.FullName, defaultDoc); if (!File.Exists(defaultFileName)) continue; fi = new FileInfo (defaultFileName); fileName = defaultFileName; } } if (!fi.Exists) { var msg = "Static File '" + request.PathInfo + "' not found."; throw new HttpException(404, msg); } } r.ContentType = MimeTypes.GetMimeType(fileName); TimeSpan maxAge; if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge)) { r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds); } if (request.HasNotModifiedSince(fi.LastWriteTime)) { r.ContentType = MimeTypes.GetMimeType(fileName); r.StatusCode = 304; return; } try { r.AddHeaderLastModified(fi.LastWriteTime); r.ContentType = MimeTypes.GetMimeType(fileName); if (!Env.IsMono) { r.TransmitFile(fileName); } else { r.WriteFile(fileName); } } catch (Exception ex) { //log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message); throw new HttpException(403, "Forbidden."); } }); }
public IHttpHandler CheckAndProcess(IHttpRequest request) { if (!active) return null; string abs_path = request.GetAbsolutePath(); if (this.HtdocsPath.Contains ("swagger-ui")) { if (abs_path.StartsWith ("/swagger-ui/")) { return this; } } if (abs_path.StartsWith ("/fonts/")) { return this; } if (abs_path.StartsWith ("/resource")) { return null; } if (abs_path.StartsWith ("/oauth/") || abs_path.StartsWith ("/api/")) { return null; } else if (abs_path.StartsWith ("/admin/")) { return this; } else if (abs_path.Count (c => c == '/') == 1) { return this; } else { return null; } /* // TODO case insensitive on option if (abs_path.StartsWith(RoutePath) && is_html_request) // we want to handle this url path return this; else return null; */ }
public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName) { response.EndHttpHandlerRequest(skipClose: true, afterBody: r => { // i.e. /static/folder/file.html => folder/file.html var abs_path = request.GetAbsolutePath(); string requested_relative_path = ""; if (abs_path == "/") { requested_relative_path = "./"; } else { requested_relative_path = abs_path.Substring(RoutePath.Length); } var fileName = Path.Combine(HtdocsPath, requested_relative_path); var fi = new FileInfo(fileName); if (!fi.Exists) { // append default filename if feasible (i.e. index.html) if ((fi.Attributes & FileAttributes.Directory) != 0) { foreach (var defaultDoc in EndpointHost.Config.DefaultDocuments) { var defaultFileName = Path.Combine(fi.FullName, defaultDoc); if (!File.Exists(defaultFileName)) { continue; } fi = new FileInfo(defaultFileName); fileName = defaultFileName; } } if (!fi.Exists) { var msg = "Static File '" + request.PathInfo + "' not found."; throw new HttpException(404, msg); } } r.ContentType = MimeTypes.GetMimeType(fileName); TimeSpan maxAge; if (r.ContentType != null && EndpointHost.Config.AddMaxAgeForStaticMimeTypes.TryGetValue(r.ContentType, out maxAge)) { r.AddHeader(HttpHeaders.CacheControl, "max-age=" + maxAge.TotalSeconds); } if (request.HasNotModifiedSince(fi.LastWriteTime)) { r.ContentType = MimeTypes.GetMimeType(fileName); r.StatusCode = 304; return; } try { r.AddHeaderLastModified(fi.LastWriteTime); r.ContentType = MimeTypes.GetMimeType(fileName); if (!Env.IsMono) { r.TransmitFile(fileName); } else { r.WriteFile(fileName); } } catch (Exception ex) { //log.ErrorFormat("Static file {0} forbidden: {1}", request.PathInfo, ex.Message); throw new HttpException(403, "Forbidden."); } }); }
public static void LogRequest(this IHttpResponse response, IHttpRequest request, int?statusCode = null) { try { if (!EndpointHost.Config.MetadataMap[request.ServicePath].LogCommonRequestInfo) { return; } Dictionary <string, string> additionalInfo = new Dictionary <string, string>() { { "ClientIP", request.RemoteIp }, { "AbsolutePath", request.GetAbsolutePath() }, { "HostAddress", request.GetUrlHostName() }, { "ResponseStatus", (statusCode ?? (response.StatusCode <= 0 ? 200 : response.StatusCode)).ToString() } }; string requestType = EndpointHost.Config.MetadataMap[request.ServicePath].FullServiceName; if (!string.IsNullOrWhiteSpace(request.OperationName)) { requestType += "." + request.OperationName; } additionalInfo["RequestType"] = requestType; string appId = request.Headers[ServiceUtils.AppIdHttpHeaderKey]; if (!string.IsNullOrWhiteSpace(appId)) { additionalInfo["ClientAppId"] = appId; } if (request.RequestObject != null && request.RequestObject is IHasMobileRequestHead) { IHasMobileRequestHead h5Request = request.RequestObject as IHasMobileRequestHead; if (h5Request.head != null) { Dictionary <string, string> extension = null; if (EndpointHost.Config.MetadataMap[request.ServicePath].LogH5HeadExtensionData) { extension = new Dictionary <string, string>(); foreach (ExtensionFieldType item in h5Request.head.extension) { if (!string.IsNullOrWhiteSpace(item.name) && item.name != ServiceUtils.MobileUserIdExtensionKey && item.name != ServiceUtils.MobileAuthTokenExtensionKey) { extension[item.name] = item.value; } } if (extension.Count == 0) { extension = null; } } additionalInfo["H5Head"] = TypeSerializer.SerializeToString( new { ClientID = h5Request.head.cid, ClientToken = h5Request.head.ctok, ClientVersion = h5Request.head.cver, SystemCode = h5Request.head.syscode, SourceID = h5Request.head.sid, Language = h5Request.head.lang, Extension = extension }); } } } catch { } }