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

Classes

class  AssociatedFile
 
class  Binding
 Defines the path of the servlet, optionally it can catch ALL the requests that search for path starting with the given value. More...
 
class  Configuration
 This class contains all the settings of the server. More...
 
class  Cookie
 
class  Custom
 
class  DataWriter
 A class to simplify the construction of byte arrays. More...
 
class  Debugger
 
class  Error
 
class  HttpUtils
 
class  Request
 
class  Response
 
class  Server
 
class  Servlet
 
class  Session
 
class  SessionManager
 
class  SslConfiguration
 
class  SystemTLS
 
class  Terminal
 
class  TLSImpl
 
class  Utils
 

Enumerations

enum  BG_COLOR {
  DEFAULT , BLACK , RED , GREEN ,
  YELLOW , BLUE , MAGENTA , CYAN ,
  WHITE
}
 
enum  FG_COLOR {
  DEFAULT , BLACK , RED , GREEN ,
  YELLOW , BLUE , MAGENTA , CYAN ,
  WHITE
}
 

Functions

class DataReader (byte[] data)
 A class to simplify reading data from a byte array.
 
readonly struct OAuth1_0Information (Dictionary< string, string > parameters)
 
class Index (Request req, Response res, Configuration config)
 

Enumeration Type Documentation

◆ BG_COLOR

enum HSB.BG_COLOR

Definition at line 3 of file Terminal.cs.

4 {
5 DEFAULT,
6 BLACK,
7 RED,
8 GREEN,
9 YELLOW,
10 BLUE,
11 MAGENTA,
12 CYAN,
13 WHITE
14 }

◆ FG_COLOR

enum HSB.FG_COLOR

Definition at line 15 of file Terminal.cs.

16 {
17 DEFAULT,
18 BLACK,
19 RED,
20 GREEN,
21 YELLOW,
22 BLUE,
23 MAGENTA,
24 CYAN,
25 WHITE
26 }

Function Documentation

◆ DataReader()

class HSB.DataReader ( byte[]  data)

A class to simplify reading data from a byte array.

Reads 1 byte from the data and returns it as a UInt16

Returns

Reads 2 bytes from the data and returns them as a ushort

Returns

Reads 3 bytes from the data and returns them as a uint24

Returns

Reads 4 bytes from the data and returns them as a uint32

Returns

Reads 4 bytes from the data and returns them as a signed int (int32)

Returns

Definition at line 5 of file DataReader.cs.

6{
7 private readonly byte[] data = data;
8 private uint offset = 0;
9 private uint endOffset = (uint)data.Length;
10
11 public void Rewind()
12 {
13 offset = 0;
14 }
15 public void SetReaderPosition(uint position)
16 {
17 this.offset = position;
18 }
19
20 public void SetEndPosition(uint position)
21 {
22 endOffset = position;
23 }
24
25 public uint GetCurrentPosition()
26 {
27 return offset;
28 }
29 public bool DataAvailable()
30 {
31 return offset < endOffset;
32 }
33
34 public int RemainingData => (int)(endOffset - offset);
35
36
37 public byte ReadByte()
38 {
39 offset++;
40 return data[offset - 1];
41 }
42
43 public byte[] ReadBytes(uint size)
44 {
45 if (size > endOffset - offset)
46 {
47 size = endOffset - offset - 1;
48 Console.WriteLine("Not enough data to read, returning what is available");
49 }
50
51 byte[] bytes = new byte[size];
52 Array.Copy(data, offset, bytes, 0, size);
53 offset += size;
54 return bytes;
55 }
56
57 public byte[] ReadBytes(uint size, uint offset)
58 {
59 byte[] bytes = new byte[size];
60 Array.Copy(data, offset, bytes, 0, size);
61 return bytes;
62 }
63
64 public byte[] ReadBytes((uint size, uint offset) values)
65 {
66 return ReadBytes(values.size, values.offset);
67 }
72 public UInt16 ReadSmallUint(){
73 byte[] ushrt = ReadBytes(1);
74 return Utils.BytesToUShort([0, ushrt[0]]);
75 }
76
77 //aka uint16
82 public ushort ReadUShort()
83 {
84 byte[] ushrt = ReadBytes(2);
85 return Utils.BytesToUShort(ushrt);
86 }
91 public uint ReadUInt24()
92 {
93 byte[] uint24 = ReadBytes(3);
94 return Utils.UInt24ToUInt32(uint24);
95 }
96
101 public uint ReadUInt()
102 {
103 byte[] ushrt = ReadBytes(4);
104 return BitConverter.ToUInt32(ushrt);
105 }
110 public int ReadInt()
111 {
112 byte[] uin = ReadBytes(4);
113 return BitConverter.ToInt32(uin);
114 }
115
116
117
118}

◆ Index()

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

Definition at line 7 of file Index.cs.

7 : Servlet(req, res, config)
8{
9 public override void ProcessGet()
10 {
11 string page = ReadFromResources("index.html");
12 string version = "v";
13 if (Assembly.GetExecutingAssembly().GetName().Version != null)
14 {
15 version += Assembly.GetExecutingAssembly().GetName().Version!.ToString();
16 }
17
18
19
20 string footer_div = "";
21 string server_name;
22 string logo = "";
23 //string title = "";
24 if (configuration.CustomServerName != "")
25 {
26 server_name = configuration.CustomServerName;
27 }
28 else
29 {
30 server_name = "HSB<sup>#</sup>";
31 footer_div = "<div class=\"footer\">Copyright &copy; 2021-2023 Lorenzo L. Concas</div>";
32 string logo_b64 = ReadFromResources("logo_b64");
33 logo = $"<img width=\"32px\" src=\"{logo_b64}\" />";
34 // title = "Http Server Boxed <sup>#</sup>";
35 }
36
37 //set attributes
38 res.AddAttribute("logo", logo); //this break some configurations, logo must be replaced with a smaller image
39 // res.AddAttribute("title", title);
40 res.AddAttribute("serverName", server_name);
41 res.AddAttribute("footer_div", footer_div);
42 res.AddAttribute("hsbVersion", version);
43
44 res.SendHTMLContent(page, true);
45
46 }
47}
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

◆ OAuth1_0Information()

readonly struct HSB.OAuth1_0Information ( Dictionary< string, string >  parameters)

Definition at line 5 of file OAuth1_0Information.cs.

6{
7 public readonly string access_token = parameters.TryGetValueFromDict("access_token", "");
8 public readonly string nonce = parameters.TryGetValueFromDict("oauth_nonce", "");
9 public readonly string token = parameters.TryGetValueFromDict("oauth_token", "");
10 public readonly string version = parameters.TryGetValueFromDict("oauth_version", "");
11 public readonly string signature_method = parameters.TryGetValueFromDict("oauth_signature_method", "");
12 public readonly string timestamp = parameters.TryGetValueFromDict("oauth_timestamp", "");
13 public readonly string consumer_key = parameters.TryGetValueFromDict("oauth_consumer_key", "");
14 public readonly string signature = parameters.TryGetValueFromDict("oauth_signature", "");
15
16 public readonly bool IsValid()
17 {
18 return access_token != "" && nonce != "" && token != "" && version != "" && signature_method != "" && timestamp != "" && consumer_key != "" && signature != "";
19 }
20
21 public override readonly string ToString()
22 {
23 return IsValid() ? $"Valid oAuth1.0 with timestamp {timestamp}, version {version}" : "No valid oAuth1.0 found";
24 }
25
26 public override readonly bool Equals([NotNullWhen(true)] object? obj)
27 {
28 if(obj is OAuth1_0Information auth){
29 return access_token == auth.access_token && nonce == auth.nonce && token == auth.token && version == auth.version && signature_method == auth.signature_method && timestamp == auth.timestamp && consumer_key == auth.consumer_key && signature == auth.signature;
30 }
31 return false;
32 }
33
34 public override readonly int GetHashCode()
35 {
36 return HashCode.Combine(access_token, nonce, token, version, signature_method, timestamp, consumer_key, signature);
37 }
38
39 public static bool operator ==(OAuth1_0Information left, OAuth1_0Information right)
40 {
41 return left.Equals(right);
42 }
43
44 public static bool operator !=(OAuth1_0Information left, OAuth1_0Information right)
45 {
46 return !(left == right);
47 }
48}