-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentService.cs
More file actions
67 lines (59 loc) · 3 KB
/
Copy pathContentService.cs
File metadata and controls
67 lines (59 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Text;
public static class ContentService
{
public static async Task<(string StatusLine, byte[] BodyBytes, string ContentType, IReadOnlyList<string> SetCookieHeaders)> ServePathAsync(
HttpRequestContext request,
string contentPath)
{
var response = new HttpResponseContext();
var requestPath = request.Path;
var relativePath = requestPath == "/" ? "index.html" : requestPath.TrimStart('/');
relativePath = relativePath.Replace('/', Path.DirectorySeparatorChar);
var pathSegments = relativePath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
if (pathSegments.Any(segment => segment.Equals("hidden", StringComparison.OrdinalIgnoreCase)))
{
return ("HTTP/1.1 404 Not Found", Encoding.UTF8.GetBytes("Not Found"), "text/plain", response.SetCookieHeaders);
}
var fullPath = Path.GetFullPath(Path.Combine(contentPath, relativePath));
var contentRoot = Path.GetFullPath(contentPath);
if (!fullPath.StartsWith(contentRoot, StringComparison.Ordinal))
{
return ("HTTP/1.1 403 Forbidden", Encoding.UTF8.GetBytes("Forbidden"), "text/plain", response.SetCookieHeaders);
}
if (!File.Exists(fullPath))
{
return ("HTTP/1.1 404 Not Found", Encoding.UTF8.GetBytes("Not Found"), "text/plain", response.SetCookieHeaders);
}
var extension = Path.GetExtension(fullPath);
if (extension.Equals(".md", StringComparison.OrdinalIgnoreCase))
{
var markdown = File.ReadAllText(fullPath);
markdown = await CsTagsParser.RenderInTextAsync(markdown, fullPath, contentPath, request, response);
var html = MarkdownParser.BuildHtml(markdown, Path.GetFileName(fullPath));
return ("HTTP/1.1 200 OK", Encoding.UTF8.GetBytes(html), "text/html", response.SetCookieHeaders);
}
if (extension.Equals(".html", StringComparison.OrdinalIgnoreCase) || extension.Equals(".htm", StringComparison.OrdinalIgnoreCase))
{
var html = File.ReadAllText(fullPath);
html = await CsTagsParser.RenderInHtmlAsync(html, fullPath, contentPath, request, response);
return ("HTTP/1.1 200 OK", Encoding.UTF8.GetBytes(html), GetContentType(fullPath), response.SetCookieHeaders);
}
return ("HTTP/1.1 200 OK", File.ReadAllBytes(fullPath), GetContentType(fullPath), response.SetCookieHeaders);
}
public static string GetContentType(string filePath)
{
return Path.GetExtension(filePath).ToLowerInvariant() switch
{
".html" => "text/html",
".md" => "text/markdown",
".css" => "text/css",
".js" => "application/javascript",
".json" => "application/json",
".txt" => "text/plain",
".png" => "image/png",
".jpg" or ".jpeg" => "image/jpeg",
".gif" => "image/gif",
_ => "application/octet-stream"
};
}
}