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

Public Member Functions

 Request (byte[] data, Socket socket, Configuration config, bool isTLS=false)
 
Session GetSession ()
 Return the session associated with the request.
 
Tuple< string, string >? GetBasicAuthInformation ()
 
OAuth1_0Information? GetOAuth1_0Information ()
 
bool IsJSON ()
 Test if a request contains a JSON document in the body.
 
bool IsWebSocket ()
 Returns true if the request is a websocket request.
 
bool IsFileUpload ()
 Returns true if the request is a file upload.
 
bool IsFormUpload ()
 Returns true if the request is a form upload.
 
MultiPartFormData? GetMultiPartFormData ()
 Returns the form data if the request is a multipart formdata upload, else null.
 
FormGetFormData ()
 Returns the form data if the request is a form upload, else null.
 
Socket GetSocket ()
 
void DumpRequest (string path="./request.txt")
 
void DumpBody (string path="./body.txt")
 
void FullPrint ()
 
override string ToString ()
 

Public Attributes

bool validRequest = false
 
readonly bool IsTLS = false
 
bool IsValidRequest = true
 

Properties

HTTP_METHOD METHOD [get]
 Return the method of the request.
 
HTTP_PROTOCOL PROTOCOL [get]
 Return the protocol of the request.
 
string URL [get]
 Return the url of the request.
 
string ClientIP [get]
 Return the ip of the client (request source ip)
 
int ClientPort [get]
 Return the port of the client (request source port)
 
AddressFamily ClientIPVersion [get]
 Return the ip version of the client (request source ip version (v4 or v6))
 
byte[] RawBody [get]
 Return the raw body of the request.
 
string Body [get]
 Return the body of the request parsed as string.
 
Dictionary< string, string > Headers [get]
 Return the headers.
 
List< string > RawHeaders [get]
 Return the unparsed headers.
 
Dictionary< string, string > Parameters [get]
 Return the parameters.
 
bool IsAjaxRequest [get]
 Returns if the request is an ajax request.
 
string GetRawRequestText [get]
 

Detailed Description

Definition at line 10 of file Request.cs.

Constructor & Destructor Documentation

◆ Request()

HSB.Request.Request ( byte[]  data,
Socket  socket,
Configuration  config,
bool  isTLS = false 
)

Definition at line 45 of file Request.cs.

46 {
47 connectionSocket = socket;
48 rawData = data;
49 rawBody = [];
50 this.config = config;
51 requestContent = [];
52 IsTLS = isTLS;
53
54 if (data == null || data.Length == 0)
55 {
56 return;
57 }
58 var rEP = socket.RemoteEndPoint;
59 //extract ipv4 or ipv6 from the remote endpoint
60 if (rEP != null)
61 {
62 var rIEP = (IPEndPoint)rEP;
63 clientIP = rIEP.Address.ToString();
64 clientPort = rIEP.Port;
65 clientIPVersion = rIEP.AddressFamily;
66 }
67
68 switch (Utils.GetEncoding(data))
69 {
70 case UTF8Encoding:
71 reqText = Encoding.UTF8.GetString(data);
72 break;
73 case UTF32Encoding:
74 reqText = Encoding.UTF32.GetString(data);
75 break;
76 case ASCIIEncoding:
77 reqText = Encoding.ASCII.GetString(data);
78 break;
79 }
80
81 if (reqText.Replace("\0", "") == "")
82 {
83 //note:
84 //it can happen in programs like postman that a request to localhost produces two requests
85 //one for IPv6 and one for IPv4
86 //i don't know why but the second request is invalid
87 validRequest = false;
88 config.Debug.INFO("Got an invalid request, ignoring...");
89 requestContent.Add(" ");
90 return;
91 }
92 // reqText = Encoding.UTF8.GetString(data);
93 requestContent = [.. reqText.Split("\r\n")];
94 ParseRequest();
95
96
97 }
Debugger Debug
Holds all debug information and routines.

Member Function Documentation

◆ DumpBody()

void HSB.Request.DumpBody ( string  path = "./body.txt")

Definition at line 354 of file Request.cs.

355 {
356 File.WriteAllText(path, body);
357 }

◆ DumpRequest()

void HSB.Request.DumpRequest ( string  path = "./request.txt")

Definition at line 350 of file Request.cs.

351 {
352 File.WriteAllBytes(path, rawData);
353 }

◆ FullPrint()

void HSB.Request.FullPrint ( )

Definition at line 362 of file Request.cs.

363 {
364 Terminal.DEBUG("PRINTING RAW REQUEST\n====================");
365 Terminal.INFO(reqText);
366 Terminal.DEBUG("\n====================");
367 Terminal.INFO($"Has basic auth? {basicAuth != null}");
368 if (basicAuth != null)
369 Terminal.INFO(basicAuth);
370 Terminal.INFO($"Has oauth1.0? {oAuth1_0Information != null}");
371 if (oAuth1_0Information != null)
372 Terminal.INFO(oAuth1_0Information);
373
374 Terminal.INFO($"Has oAuth2.0? {oAuth2_0Token != ""} {oAuth2_0Token}");
375
376 }

◆ GetFormData()

Form? HSB.Request.GetFormData ( )

Returns the form data if the request is a form upload, else null.

Returns

◆ GetMultiPartFormData()

MultiPartFormData? HSB.Request.GetMultiPartFormData ( )

Returns the form data if the request is a multipart formdata upload, else null.

Returns

◆ GetSession()

Session HSB.Request.GetSession ( )

Return the session associated with the request.

Returns

◆ IsFileUpload()

bool HSB.Request.IsFileUpload ( )

Returns true if the request is a file upload.

Returns

◆ IsFormUpload()

bool HSB.Request.IsFormUpload ( )

Returns true if the request is a form upload.

Returns

◆ IsJSON()

bool HSB.Request.IsJSON ( )

Test if a request contains a JSON document in the body.

Returns

◆ IsWebSocket()

bool HSB.Request.IsWebSocket ( )

Returns true if the request is a websocket request.

Returns

Definition at line 318 of file Request.cs.

319 {
320 return
321 headers.ContainsKey("Connection") && headers["Connection"].Equals("upgrade", StringComparison.CurrentCultureIgnoreCase) &&
322 headers.ContainsKey("Upgrade") && headers["Upgrade"].Equals("websocket", StringComparison.CurrentCultureIgnoreCase);
323 }

◆ ToString()

override string HSB.Request.ToString ( )

Definition at line 385 of file Request.cs.

386 {
387 string str = _method.ToString() + " - " + _url + " - " + _protocol.ToString();
388 return str;
389 }

Member Data Documentation

◆ IsTLS

readonly bool HSB.Request.IsTLS = false

Definition at line 34 of file Request.cs.

◆ IsValidRequest

bool HSB.Request.IsValidRequest = true

Definition at line 44 of file Request.cs.

◆ validRequest

bool HSB.Request.validRequest = false

Definition at line 22 of file Request.cs.

Property Documentation

◆ Body

string HSB.Request.Body
get

Return the body of the request parsed as string.

Definition at line 283 of file Request.cs.

◆ ClientIP

string HSB.Request.ClientIP
get

Return the ip of the client (request source ip)

Definition at line 267 of file Request.cs.

◆ ClientIPVersion

AddressFamily HSB.Request.ClientIPVersion
get

Return the ip version of the client (request source ip version (v4 or v6))

Definition at line 275 of file Request.cs.

◆ ClientPort

int HSB.Request.ClientPort
get

Return the port of the client (request source port)

Definition at line 271 of file Request.cs.

◆ GetRawRequestText

string HSB.Request.GetRawRequestText
get

Definition at line 349 of file Request.cs.

◆ Headers

Dictionary<string, string> HSB.Request.Headers
get

Return the headers.

Definition at line 287 of file Request.cs.

◆ IsAjaxRequest

bool HSB.Request.IsAjaxRequest
get

Returns if the request is an ajax request.

Definition at line 313 of file Request.cs.

◆ METHOD

HTTP_METHOD HSB.Request.METHOD
get

Return the method of the request.

Definition at line 255 of file Request.cs.

◆ Parameters

Dictionary<string, string> HSB.Request.Parameters
get

Return the parameters.

Definition at line 295 of file Request.cs.

◆ PROTOCOL

HTTP_PROTOCOL HSB.Request.PROTOCOL
get

Return the protocol of the request.

Definition at line 259 of file Request.cs.

◆ RawBody

byte [] HSB.Request.RawBody
get

Return the raw body of the request.

Definition at line 279 of file Request.cs.

◆ RawHeaders

List<string> HSB.Request.RawHeaders
get

Return the unparsed headers.

Definition at line 291 of file Request.cs.

◆ URL

string HSB.Request.URL
get

Return the url of the request.

Definition at line 263 of file Request.cs.


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