Task NewServerCallback(IDictionary <string, object> env) { var path = (string)env[OwinConstants.RequestPath]; Tracing.Info(path); if (!content.IsAvailable(path)) { var response = new Response(env) { ContentType = path.MimeType() }; using (var writer = new StreamWriter(response.OutputStream)) { writer.Write("Page not found: " + path); } return(TaskHelpers.Completed()); } if (path.MimeType().IsBinaryMime()) { var fileContents = content.GetBinaryContent(path); var response = new Response(env) { ContentType = path.MimeType() }; response.Headers["Content-Range"] = new[] { string.Format("bytes 0-{0}", (fileContents.Length - 1)) }; response.Headers["Content-Length"] = new[] { fileContents.Length.ToString(CultureInfo.InvariantCulture) }; response.Write(new ArraySegment <byte>(fileContents)); } else { var response = new Response(env) { ContentType = path.MimeType() }; using (var writer = new StreamWriter(response.OutputStream)) { writer.Write(content.GetContent(path)); } } return(TaskHelpers.Completed()); }
private void ServerCallback(IDictionary <string, object> env, ResultDelegate result, Action <Exception> fault) { var requestString = (string)env[OwinConstants.RequestPath]; Tracing.Info(requestString); if (!content.IsAvailable(requestString)) { SendText(result, env, "Page not found: " + requestString); return; } if (requestString.MimeType().IsBinaryMime()) { var fileContents = content.GetBinaryContent(requestString); SendData(result, env, fileContents); } else { var fileContents = content.GetContent(requestString); SendText(result, env, fileContents); } }
Task NewServerCallback(IDictionary <string, object> env) { var path = (string)env[OwinConstants.RequestPath]; Tracing.Info(path); if (!content.IsAvailable(path)) { var path404 = "/404.html"; var use404 = content.IsAvailable(path404); var response = new Response(env) { ContentType = use404 ? path404.MimeType() : path.MimeType() }; using (var writer = new StreamWriter(response.OutputStream)) { if (use404) { writer.Write(content.GetContent(path404)); } else { writer.Write("Page not found: " + path); } } return(TaskHelpers.Completed()); } if (path.MimeType().IsBinaryMime()) { var fileContents = content.GetBinaryContent(path); var response = new Response(env) { ContentType = path.MimeType() }; response.Headers["Content-Range"] = new[] { string.Format("bytes 0-{0}", (fileContents.Length - 1)) }; response.Headers["Content-Length"] = new[] { fileContents.Length.ToString(CultureInfo.InvariantCulture) }; response.Write(new ArraySegment <byte>(fileContents)); } else if (content.IsDirectory(path) && !path.EndsWith("/")) { //if path is a directory without trailing slash, redirects to the same url with a trailing slash var response = new Response(env) { Status = "301 Moved Permanently" }; response.Headers["Location"] = new[] { String.Format("http://localhost:{0}{1}/", Port, path) }; } else { var response = new Response(env) { ContentType = path.MimeType() }; using (var writer = new StreamWriter(response.OutputStream)) { writer.Write(content.GetContent(path)); } } return(TaskHelpers.Completed()); }