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

Public Member Functions

 Response (Socket socket, Request request, Configuration c, SslStream? sslStream)
 
void Send (byte[] data, bool disconnect=true)
 Send an un modified byte array to to the socket.
 
void Send (string data, string mimeType="text/plain", int statusCode=HTTP_CODES.OK, Dictionary< string, string >? customHeaders=null)
 Sends an HTTP Response with the body passed as parameter.
 
void SendHTMLFile (string path, bool process=false, Dictionary< string, string >? customHeaders=null)
 Loads and HTML file from path and sends it as HTTP Response with mimeType = text/html Optionally it can provides a basic processor function.
 
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.
 
void SendFile (byte[] data, string mimeType, int statusCode=HTTP_CODES.OK, Dictionary< string, string >? customHeaders=null)
 Sends data to the client.
 
void SendObject (object obj, string fileName="")
 Sends a generic object to the client, with possible optimization (string, byte[], FilePart, generic object)
 
void SendFile (FilePart filePart, int statusCode=HTTP_CODES.OK, Dictionary< string, string >? customHeaders=null)
 Send a FilePart to the client.
 
void SendCode (int statusCode)
 Send an HTTP Response with no body but with given status code.
 
void Send (int statusCode)
 Shorthand for SendCode.
 
void Redirect (string route, int statusCode=HTTP_CODES.FOUND)
 Sends a redirect to the client.
 
void Redirect (Servlet s, int statusCode=HTTP_CODES.FOUND)
 Redirects to a given servlet.
 
void E400 ()
 Bad Request.
 
void E401 ()
 Unauthorized.
 
void E404 ()
 Not Found.
 
void E500 ()
 Internal Server Error.
 
void JSON (string content)
 Sends a HTTP Response with a JSON body passed as parameter.
 
void JSON< T > (T o, JsonSerializerOptions options)
 Serializes and sends an Object in JSON format.
 
void JSON< T > (T o, bool includeFields=true, bool WriteIndented=true)
 Serialize and sends an Object in JSON Format.
 
void SendJSON< T > (T o, JsonSerializerOptions options)
 Alternate name for function JSON.
 
void SendJSON< T > (T o, bool includeFields=true)
 Alternate name for function JSON.
 
void SendJSON (string content)
 
void AddAttribute (string name, string value)
 Adds an attribute to the HTML file that will be processed, if it already exists it will be overwritten.
 
void RemoveAttribute (string name)
 Removes an attribute to the HTML file that will be processed.
 
string GetAttribute (string name)
 Retrieves the value of an attribute to the HTML file that will be processed, if doesn't exists it will return an empty string.
 

Detailed Description

Definition at line 14 of file Response.cs.

Constructor & Destructor Documentation

◆ Response()

HSB.Response.Response ( Socket  socket,
Request  request,
Configuration  c,
SslStream sslStream 
)

Definition at line 25 of file Response.cs.

26 {
27 this.socket = socket;
28 this.request = request;
29 this.sslStream = sslStream;
30 config = c;
31 }
void Send(byte[] data, bool disconnect=true)
Send an un modified byte array to to the socket.
Definition Response.cs:40

Member Function Documentation

◆ AddAttribute()

void HSB.Response.AddAttribute ( string  name,
string  value 
)

Adds an attribute to the HTML file that will be processed, if it already exists it will be overwritten.

Parameters
nameName of the attribute
valueValue of the attribute

Definition at line 413 of file Response.cs.

414 {
415 if (attributes.ContainsKey(name))
416 attributes[name] = value;
417 else
418 attributes.Add(name, value);
419 }

◆ E400()

void HSB.Response.E400 ( )

Bad Request.

Definition at line 243 of file Response.cs.

244 {
245 SendCode(HTTP_CODES.BAD_REQUEST);
246 }
void SendCode(int statusCode)
Send an HTTP Response with no body but with given status code.
Definition Response.cs:198

◆ E401()

void HSB.Response.E401 ( )

Unauthorized.

Definition at line 250 of file Response.cs.

251 {
252 SendCode(HTTP_CODES.UNAUTHORIZED);
253 }

◆ E404()

void HSB.Response.E404 ( )

Not Found.

Definition at line 257 of file Response.cs.

258 {
259 SendCode(HTTP_CODES.NOT_FOUND);
260 }

◆ E500()

void HSB.Response.E500 ( )

Internal Server Error.

Definition at line 264 of file Response.cs.

265 {
266 SendCode(HTTP_CODES.INTERNAL_SERVER_ERROR);
267 }

◆ GetAttribute()

string HSB.Response.GetAttribute ( string  name)

Retrieves the value of an attribute to the HTML file that will be processed, if doesn't exists it will return an empty string.

Parameters
nameName of the attribute

Definition at line 432 of file Response.cs.

433 {
434 return attributes[name] ?? "";
435 }

◆ JSON()

void HSB.Response.JSON ( string  content)

Sends a HTTP Response with a JSON body passed as parameter.

Parameters
contentString of the body in JSON format

Definition at line 272 of file Response.cs.

273 {
274 Send(content, "application/json");
275 }

◆ JSON< T >() [1/2]

void HSB.Response.JSON< T > ( T  o,
bool  includeFields = true,
bool  WriteIndented = true 
)

Serialize and sends an Object in JSON Format.

Template Parameters
T
Parameters
o
includeFieldsWhether or not or not include fields of the object

Definition at line 291 of file Response.cs.

292 {
293 JsonSerializerOptions jo = new()
294 {
296 MaxDepth = 0,
298 };
299
300 JSON(JsonSerializer.Serialize(o, jo));
301 }
void JSON(string content)
Sends a HTTP Response with a JSON body passed as parameter.
Definition Response.cs:272

◆ JSON< T >() [2/2]

void HSB.Response.JSON< T > ( T  o,
JsonSerializerOptions  options 
)

Serializes and sends an Object in JSON format.

Parameters
oObject to be serialized and sended as response
optionsOptions for the serializer (System.Text.Json.JsonSerializer)

Definition at line 281 of file Response.cs.

282 {
283 JSON(JsonSerializer.Serialize(o, options));
284 }

◆ Redirect() [1/2]

void HSB.Response.Redirect ( Servlet  s,
int  statusCode = HTTP_CODES::FOUND 
)

Redirects to a given servlet.

Parameters
s
statusCode

Definition at line 235 of file Response.cs.

236 {
237 Redirect(s.GetRoute(), statusCode);
238 }
void Redirect(string route, int statusCode=HTTP_CODES.FOUND)
Sends a redirect to the client.
Definition Response.cs:216

◆ Redirect() [2/2]

void HSB.Response.Redirect ( string  route,
int  statusCode = HTTP_CODES::FOUND 
)

Sends a redirect to the client.

Parameters
route

Definition at line 216 of file Response.cs.

217 {
220
221 string response = GetHeaders(
223 0,
224 MimeTypeUtils.TEXT_PLAIN,
225 new Dictionary<string, string>() { { "Location", route } }
226 ) + NEW_LINE;
227
228 Send(Encoding.UTF8.GetBytes(response));
229 }

◆ RemoveAttribute()

void HSB.Response.RemoveAttribute ( string  name)

Removes an attribute to the HTML file that will be processed.

Parameters
nameName of the attribute

Definition at line 424 of file Response.cs.

425 {
426 attributes.Remove(name);
427 }

◆ Send() [1/3]

void HSB.Response.Send ( byte[]  data,
bool  disconnect = true 
)

Send an un modified byte array to to the socket.

Parameters
data

Definition at line 40 of file Response.cs.

41 {
42 try
43 {
44 if (sslStream != null)
45 {
46 sslStream.Write(data);
47 if (disconnect)
48 sslStream.Close();
49 }
50 else
51 {
52 int totalBytes = socket.Send(data);
53 socket.Disconnect(disconnect);
54 }
55 data = [];
56 }
57 catch (Exception e)
58 {
59 Terminal.ERROR($"Error sending data ->\n {e}");
60 }
61 }

◆ Send() [2/3]

void HSB.Response.Send ( int  statusCode)

Shorthand for SendCode.

Parameters
statusCode

Definition at line 208 of file Response.cs.

209 {
211 }

◆ Send() [3/3]

void HSB.Response.Send ( string  data,
string  mimeType = "text/plain",
int  statusCode = HTTP_CODES::OK,
Dictionary< string, string >?  customHeaders = null 
)

Sends an HTTP Response with the body passed as parameter.

Parameters
dataBody of the response
mimeTypeMimeType of the body
statusCodeResponse status code

Definition at line 68 of file Response.cs.

69 {
70 string _mime = mimeType;
71 string resp = GetHeaders(statusCode, Encoding.UTF8.GetBytes(data).Length, _mime, customHeaders) + data;
72
73 Send(Encoding.UTF8.GetBytes(resp));
74
75 }

◆ SendCode()

void HSB.Response.SendCode ( int  statusCode)

Send an HTTP Response with no body but with given status code.

Parameters
statusCode

Definition at line 198 of file Response.cs.

199 {
200 string resp = GetHeaders(statusCode, 0, MimeTypeUtils.TEXT_PLAIN) + NEW_LINE;
201
202 Send(Encoding.UTF8.GetBytes(resp));
203 }

◆ SendFile() [1/3]

void HSB.Response.SendFile ( byte[]  data,
string  mimeType,
int  statusCode = HTTP_CODES::OK,
Dictionary< string, string >?  customHeaders = null 
)

Sends data to the client.

Parameters
data
mimeType
statusCode

Definition at line 147 of file Response.cs.

148 {
149 string _mime = mimeType;
150 string headers = GetHeaders(statusCode, data.Length, _mime, customHeaders);
151 byte[] headersBytes = Encoding.UTF8.GetBytes(headers);
152 byte[] responseBytes = new byte[data.Length + headersBytes.Length];
153
154 headersBytes.CopyTo(responseBytes, 0);
155 data.CopyTo(responseBytes, headersBytes.Length);
156
158 //clear memory after sending
159 data = [];
160 headersBytes = [];
161 responseBytes = [];
162 }

◆ SendFile() [2/3]

void HSB.Response.SendFile ( FilePart  filePart,
int  statusCode = HTTP_CODES::OK,
Dictionary< string, string >?  customHeaders = null 
)

Send a FilePart to the client.

Parameters
filePart
statusCode
customHeaders

Definition at line 190 of file Response.cs.

191 {
192 SendFile(filePart.GetBytes(), filePart.GetMimeType(), statusCode, customHeaders);
193 }
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

◆ SendFile() [3/3]

void HSB.Response.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.

Parameters
absPathPath (absolute) of the file
mimeTypeMimeType of the file
statusCodeResponse status code

Definition at line 122 of file Response.cs.

123 {
124 var data = File.ReadAllBytes(absPath);
125
126 string _mime = mimeType ?? MimeTypeUtils.GetMimeType(Path.GetExtension(absPath)) ?? MimeTypeUtils.APPLICATION_OCTET;
127 string headers = GetHeaders(statusCode, data.Length, _mime, customHeaders);
128 byte[] headersBytes = Encoding.UTF8.GetBytes(headers);
129 byte[] responseBytes = new byte[data.Length + headersBytes.Length];
130
131 headersBytes.CopyTo(responseBytes, 0);
132 data.CopyTo(responseBytes, headersBytes.Length);
133
135
136 //clear memory after sending
137 data = [];
138 headersBytes = [];
139 responseBytes = [];
140 }

◆ SendHTMLContent()

void HSB.Response.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.

Parameters
pathHTML content
processWhether or not or not process the document before sending
statusCodeResponse status code
encodingEncoding of the document
customHeadersOptional headers

Definition at line 110 of file Response.cs.

111 {
112 if (process)
113 content = ProcessContent(content);
114 Send(content, MimeTypeUtils.TEXT_HTML + $"; charset={encoding}", statusCode, customHeaders);
115 }

◆ SendHTMLFile()

void HSB.Response.SendHTMLFile ( string  path,
bool  process = false,
Dictionary< string, string >?  customHeaders = null 
)

Loads and HTML file from path and sends it as HTTP Response with mimeType = text/html Optionally it can provides a basic processor function.

Parameters
pathPath of the HTML file
processWhether or not or not process the document before sending

Definition at line 82 of file Response.cs.

83 {
84 try
85 {
86 string content = File.ReadAllText(path);
87 if (process)
88 content = ProcessContent(content);
89 Encoding encoding = Utils.GetEncoding(path);
90
91 Send(content, MimeTypeUtils.TEXT_HTML + $"; charset={encoding.BodyName}", customHeaders: customHeaders);
92
93 content = "";
94 }
95 catch (Exception)
96 {
97 //dato che l'invio dei dati รจ parte nostra, se non riusciamo diamo un errore 500
98 SendCode(HTTP_CODES.INTERNAL_SERVER_ERROR);
99 Terminal.ERROR("Error sending file : " + path);
100 }
101 }

◆ SendJSON< T >() [1/2]

void HSB.Response.SendJSON< T > ( T  o,
bool  includeFields = true 
)

Alternate name for function JSON.

Template Parameters
T
Parameters
o
options

summary> Alternate name for function JSON

Parameters
content


◆ SendJSON< T >() [2/2]

void HSB.Response.SendJSON< T > ( T  o,
JsonSerializerOptions  options 
)

Alternate name for function JSON.

Template Parameters
T
Parameters
o
options

◆ SendObject()

void HSB.Response.SendObject ( object  obj,
string  fileName = "" 
)

Sends a generic object to the client, with possible optimization (string, byte[], FilePart, generic object)

Parameters
obj
fileName

Definition at line 168 of file Response.cs.

169 {
170 if (obj == null) return;
171
172 if (obj is string str)
173 if (fileName != "")
174 Send(str, MimeTypeUtils.GetMimeType(Path.GetExtension(fileName)) ?? MimeTypeUtils.TEXT_PLAIN);
175 else
176 Send(str);
177 else if (obj is byte[] bytes)
178 Send(bytes);
179 else if (obj is FilePart filePart)
181 else
182 SendJSON(obj);
183 }
Represents a file of a multipart form.
Definition FilePart.cs:9

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