HttpServerBoxed 0.0.11 alpha
A simple http server for C# and .NET
Loading...
Searching...
No Matches
HSB.DefaultPages Namespace Reference

Functions

class FileList (Request req, Response res, Configuration config)
 

Function Documentation

◆ FileList()

class HSB.DefaultPages.FileList ( Request  req,
Response  res,
Configuration  config 
)

If path == / -> list files and folders of the cd (Environment.CurrentDirectory If path == / + subdirectory -> list file of subdirectory if path == / + file -> download

Disk path == cd + requested path (url)

Definition at line 12 of file FileList.cs.

12 : Servlet(req, res, config)
13{
14 public override void ProcessGet()
15 {
16 //since this mode bypasses normal error handling we must handle them manually
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 //if is file -> download
36 if (File.Exists("." + url))
37 {
38 configuration.Debug.INFO($"{req.METHOD} '{url}' 200 (Static file)");
39 res.SendFile("." + url);
40 return;
41 }
42
43 var filelist = "";
44 if (req.URL != "/")
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 //if requested path is directory -> list files
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, ""); //the path is relative to the current directory (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 &copy; 2021-2023 Lorenzo L. Concas</div>";
87 server_name = "HSB<sup>#</sup>";
88 }
89 res.AddAttribute("folder", rootRequested ? " / " : url[1..]);
90 res.AddAttribute("items", filelist);
91 res.AddAttribute("footer_div", footer_div);
92 res.AddAttribute("serverName", server_name);
93 res.AddAttribute("footer_div", footer_div);
94 res.AddAttribute("hsbVersion", version);
95 res.AddAttribute("footerExtra", "- File Listing Mode");
96 string page = ReadFromResources("filelisting.html");
97 res.SendHTMLContent(page, true);
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.
Definition Request.cs:263
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...
Definition Response.cs:413
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.
Definition Response.cs:110
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.
Definition Response.cs:122