If path == / -> list files and folders of the cd (Environment.CurrentDirectory If path == / + subdirectory -> list file of subdirectory if path == / + file -> download
13{
14 public override void ProcessGet()
15 {
16
17 try
18 {
19 var cd = Environment.CurrentDirectory;
20 var url = NormalizeIfWindows(req.
URL.Replace(
"%20",
" "));
21 var cwd = cd;
22 var rootRequested = req.URL == "/";
23
24
35
36 if (File.Exists("." + url))
37 {
38 configuration.Debug.INFO($"{req.METHOD} '{url}' 200 (Static file)");
40 return;
41 }
42
43 var filelist = "";
45 {
46 filelist = "<div class='row'><a href='/'>.</a><br/></div>";
47 filelist += $"<div class='row'><a href='{Path.GetRelativePath(cd, cwd)}'>..</a></br/></div>";
48 cwd = Path.Combine(cd, url[1..]);
49 }
50
51
52
53
54 if (Directory.Exists(cwd))
55 {
56
57 configuration.Debug.INFO($"{req.METHOD} '{url}' 200");
58 List<string> items = [.. Directory.GetDirectories(cwd)];
59 items.AddRange(Directory.GetFiles(cwd).ToList());
60 foreach (var i in items)
61 {
62 var filename = Path.GetFileName(i);
63 var relativePath = i.Replace(cd, "");
64 filelist += $"<div class='row'><a href='{relativePath}'>{filename}</a></div>";
65 }
66
67
68
69 string version = "";
70 if (Assembly.GetExecutingAssembly().GetName().Version != null)
71 {
72 version = "v"+Assembly.GetExecutingAssembly().GetName().Version!.ToString();
73 }
74
75
76 string footer_div;
77 string server_name;
78 if (configuration.CustomServerName != "")
79 {
80 server_name = configuration.CustomServerName;
81 footer_div = "";
82 }
83 else
84 {
85
86 footer_div = "<div class=\"footer\">Copyright © 2021-2023 Lorenzo L. Concas</div>";
87 server_name = "HSB<sup>#</sup>";
88 }
89 res.
AddAttribute(
"folder", rootRequested ?
" / " : url[1..]);
96 string page = ReadFromResources("filelisting.html");
98 return;
99 }
100
101 configuration.Debug.INFO($"{req.METHOD} '{url}' 404 (Resource not found)");
102 new Error(req, res, configuration, "Page not found", HTTP_CODES.NOT_FOUND).Process();
103 }
104 catch(Exception e)
105 {
106 configuration.Debug.ERROR($"{req.METHOD} '{req.URL}' 500 (Internal Server Error)\n{e}");
107 new Error(req, res, configuration, e.ToString(), HTTP_CODES.INTERNAL_SERVER_ERROR).Process();
108 }
109 }
110
111 private static string NormalizeIfWindows(string url)
112 {
113 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
114 {
115 url = url.Replace("/", "\\");
116 }
117
118 return url;
119 }
120}
string URL
Return the url of the request.
void AddAttribute(string name, string value)
Adds an attribute to the HTML file that will be processed, if it already exists it will be overwritte...
void SendHTMLContent(string content, bool process=false, int statusCode=HTTP_CODES.OK, string encoding="UTF-8", Dictionary< string, string >? customHeaders=null)
Sends an html page passed as string.
void SendFile(string absPath, string? mimeType=null, int statusCode=HTTP_CODES.OK, Dictionary< string, string >? customHeaders=null)
Loads a file from a given path and sends an HTTP Response.