HttpServerBoxed 0.0.11 alpha
A simple http server for C# and .NET
Loading...
Searching...
No Matches
HSB.Servlet Class Reference
Inheritance diagram for HSB.Servlet:
HSB.Error Runner.AssociatedFileServletCustomMethods Runner.AssociatedFileServletMultipleCustomMethods Runner.AssociatedFileServletMultipleMethods Runner.AssociatedFileServletOneMethod Runner.Form Runner.JsonResponse Runner.MultiBinding Runner.Parameters Runner.SendHTMLFile Runner.SharedObjects Runner.SingleBinding Runner.TestRunnerClasses.PostWithAuth Runner.TestRunnerClasses.RegexBinding Template.Servlet

Public Member Functions

 Servlet (Request req, Response res)
 
 Servlet (Request req, Response res, Configuration conf)
 
void AddCustomMethodHandler (string name, Delegate handler)
 
void RemoveCustomMethodHandler (string name)
 
string GetRoute ()
 
void Process ()
 
virtual void ProcessPost ()
 
virtual void ProcessGet ()
 
virtual void ProcessDelete ()
 
virtual void ProcessPut ()
 
virtual void ProcessHead ()
 
virtual void ProcessPatch ()
 
virtual void ProcessOptions ()
 
virtual void ProcessTrace ()
 
virtual void ProcessConnect ()
 

Static Protected Member Functions

static string ReadFromResources (string resourceName)
 Extract a string from an embedded resource.
 

Protected Attributes

Request req
 
Response res
 
Configuration configuration
 
Delegate? handlerFallback
 

Detailed Description

Definition at line 5 of file Servlet.cs.

Constructor & Destructor Documentation

◆ Servlet() [1/2]

HSB.Servlet.Servlet ( Request  req,
Response  res 
)

Definition at line 14 of file Servlet.cs.

15 {
16 if (req == null || res == null)
17 throw new Exception("Request or Response cannot be null!");
18 this.req = req;
19 this.res = res;
20 configuration = new();
21 CustomMethodsMap = new();
22 }

◆ Servlet() [2/2]

HSB.Servlet.Servlet ( Request  req,
Response  res,
Configuration  conf 
)

Definition at line 24 of file Servlet.cs.

25 {
26 if (req == null || res == null)
27 throw new Exception("Request or Response cannot be null!");
28 this.req = req;
29 this.res = res;
30 configuration = conf;
31 CustomMethodsMap = new();
32 }

Member Function Documentation

◆ AddCustomMethodHandler()

void HSB.Servlet.AddCustomMethodHandler ( string  name,
Delegate  handler 
)

Definition at line 34 of file Servlet.cs.

35 {
36 CustomMethodsMap.Add(name.ToUpper(), handler);
37 }

◆ GetRoute()

string HSB.Servlet.GetRoute ( )

Definition at line 44 of file Servlet.cs.

45 {
46 //if class has Binding (Attribute), we return is value, else ""
47 var binding = this.GetType().GetCustomAttribute<Binding>();
48 return binding == null ? "" : binding.Path;
49 }

◆ Process()

void HSB.Servlet.Process ( )

Definition at line 76 of file Servlet.cs.

77 {
78 //if the servlet has a method with a file associated and it exists it will be send
79 var associatedFiles = GetType().GetCustomAttributes<AssociatedFile>();
80 if (associatedFiles.Any())
81 {
82 var file = associatedFiles.Where(a => a.MethodMatches(req.METHOD) || a.CustomMethodMatches(req.RawMethod.ToUpper()));
83 if (file.Any())
84 {
85 var path = file.First().FilePath;
86 if(!Path.IsPathRooted(path)){
87 //if the path is not rooted, we assume it is relative to the current directory
88 path = Path.Combine(Directory.GetCurrentDirectory(), path);
89 }
90 if(File.Exists(path)){
91 res.SendFile(file.First().FilePath);
92 // configuration.debug.INFO($"Serving associated file {}", true);
93 }
94 return;
95 }
96 }
97
98 switch (req.METHOD)
99 {
100 case HTTP_METHOD.GET:
101 ProcessGet();
102 break;
103 case HTTP_METHOD.POST:
104 ProcessPost();
105 break;
106 case HTTP_METHOD.PUT:
107 ProcessPut();
108 break;
109 case HTTP_METHOD.DELETE:
110 ProcessDelete();
111 break;
112 case HTTP_METHOD.HEAD:
113 ProcessHead();
114 break;
115 case HTTP_METHOD.PATCH:
116 ProcessPatch();
117 break;
118 case HTTP_METHOD.OPTIONS:
119 ProcessOptions();
120 break;
121 case HTTP_METHOD.TRACE:
122 ProcessTrace();
123 break;
124 case HTTP_METHOD.CONNECT:
125 ProcessConnect();
126 break;
127 default:
128 if (!CustomMethodsMap.Any()) { res.Send(HTTP_CODES.METHOD_NOT_ALLOWED); return; };
129 if (CustomMethodsMap.ContainsKey(req.RawMethod.ToUpper()))
130 {
131 Terminal.INFO($"Custom method requested for route '{req.URL}'", true);
132 CustomMethodsMap[req.RawMethod].DynamicInvoke(req, res);
133 return;
134 }
135 if (handlerFallback != null)
136 {
137 handlerFallback.DynamicInvoke();
138 return;
139 }
140 Terminal.ERROR($"Can't process request, unknown HTTP method or malformed request : {req.GetRawRequest}", true);
141 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
142 break;
143
144 }
145 }
HTTP_METHOD METHOD
Return the method of the request.
Definition Request.cs:255
void SendCode(int statusCode)
Send an HTTP Response with no body but with given status code.
Definition Response.cs:198
void Send(byte[] data, bool disconnect=true)
Send an un modified byte array to to the socket.
Definition Response.cs:40
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

◆ ProcessConnect()

virtual void HSB.Servlet.ProcessConnect ( )
virtual

Definition at line 187 of file Servlet.cs.

188 {
189 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
190 }

◆ ProcessDelete()

virtual void HSB.Servlet.ProcessDelete ( )
virtual

Definition at line 157 of file Servlet.cs.

158 {
159 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
160 }

◆ ProcessGet()

virtual void HSB.Servlet.ProcessGet ( )
virtual

Definition at line 152 of file Servlet.cs.

153 {
154 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
155 }

◆ ProcessHead()

virtual void HSB.Servlet.ProcessHead ( )
virtual

Definition at line 167 of file Servlet.cs.

168 {
169 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
170 }

◆ ProcessOptions()

virtual void HSB.Servlet.ProcessOptions ( )
virtual

Definition at line 177 of file Servlet.cs.

178 {
179 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
180 }

◆ ProcessPatch()

virtual void HSB.Servlet.ProcessPatch ( )
virtual

Definition at line 172 of file Servlet.cs.

173 {
174 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
175 }

◆ ProcessPost()

virtual void HSB.Servlet.ProcessPost ( )
virtual

Definition at line 147 of file Servlet.cs.

148 {
149 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
150 }

◆ ProcessPut()

virtual void HSB.Servlet.ProcessPut ( )
virtual

Definition at line 162 of file Servlet.cs.

163 {
164 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
165 }

◆ ProcessTrace()

virtual void HSB.Servlet.ProcessTrace ( )
virtual

Definition at line 182 of file Servlet.cs.

183 {
184 res.SendCode(HTTP_CODES.METHOD_NOT_ALLOWED);
185 }

◆ ReadFromResources()

static string HSB.Servlet.ReadFromResources ( string  resourceName)
staticprotected

Extract a string from an embedded resource.

Parameters
resourceName
Returns
The resource as string or an empty string if not found

Definition at line 55 of file Servlet.cs.

56 {
57 try
58 {
59
60 Assembly assembly = Assembly.GetExecutingAssembly();
61 string _resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourceName));
62 string result;
63 using (Stream stream = assembly.GetManifestResourceStream(_resourceName)!)
64 using (StreamReader reader = new(stream))
65 {
66 result = reader.ReadToEnd();
67 }
68 return result;
69 }
70 catch (Exception)
71 {
72 return "";
73 }
74 }

◆ RemoveCustomMethodHandler()

void HSB.Servlet.RemoveCustomMethodHandler ( string  name)

Definition at line 39 of file Servlet.cs.

40 {
41 CustomMethodsMap.Remove(name);
42 }

Member Data Documentation

◆ configuration

Configuration HSB.Servlet.configuration
protected

Definition at line 9 of file Servlet.cs.

◆ handlerFallback

Delegate? HSB.Servlet.handlerFallback
protected

Definition at line 11 of file Servlet.cs.

◆ req

Request HSB.Servlet.req
protected

Definition at line 7 of file Servlet.cs.

◆ res

Response HSB.Servlet.res
protected

Definition at line 8 of file Servlet.cs.


The documentation for this class was generated from the following file: