This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeaders.cs
316 lines (298 loc) · 8.17 KB
/
Headers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using Amazon.CDK.AWS.IAM;
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
namespace WebsiteProxy
{
public class Headers
{
public static Dictionary<int, string> messages = new Dictionary<int, string>()
{
{ 200, "OK" }, { 204, "No Content" },
{ 400, "Bad Request" }, { 401, "Unauthorized"}, { 404, "Not Found" }, { 405, "Method Not Allowed" }, { 408, "Request Timeout"}, { 415, "Unsupported Media Type" }, { 422, "Unprocessable Entity" },
{ 300, "Multiple Choices" }, { 303, "See Other"}, { 308, "Permanent Redirect" },
{ 500, "Internal Server Error" }, { 501, "Not Implemented" }, { 504, "Gateway Timeout"}
};
const int bufferSize = 1;
public string? protocol;
// https://stackoverflow.com/a/13230450
public Dictionary<string, object> headers = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
public void Add(string name, object value)
{
if (!headers.ContainsKey(name))
{
headers.Add(name, value);
}
}
public static byte[]? ReadSocketToNewline(Socket socket, Log? log = null)
{
byte[] bytesBuffer = new byte[bufferSize];
List<byte> bytesList = new List<byte>();
while (true)
{
if (!socket.IsConnected())
{
if (log != null)
{
log.Add("Connection lost", LogColor.Error);
log.Write();
}
return null;
}
int bufferLength = socket.Receive(bytesBuffer, 0, bufferSize, SocketFlags.None, out SocketError error);
if (error != SocketError.Success)
{
if (log != null)
{
log.AddRange(LogColor.Info, "Header error:", error);
}
socket.SendError(408, "Connection timed out.", log: log);
return null;
}
bytesList.AddRange(bytesBuffer);
string buffer = Encoding.ASCII.GetString(bytesBuffer);
if (bufferLength <= 0 || buffer[0] == '\n')
{
return bytesList.ToArray();
}
}
}
public static byte[]? ReadStreamToNewline(Stream stream, Log? log = null)
{
byte[] bytesBuffer = new byte[bufferSize];
List<byte> bytesList = new List<byte>();
while (true)
{
int bufferLength;
try
{
bufferLength = stream.Read(bytesBuffer, 0, bufferSize);
}
catch
{
if (log != null)
{
log.Add("Connection lost", LogColor.Error);
log.Write();
}
return null;
}
bytesList.AddRange(bytesBuffer);
string buffer = Encoding.ASCII.GetString(bytesBuffer);
//Log.Write(buffer, LogColor.Hidden);
if (bufferLength <= 0 || buffer[0] == '\n')
{
return bytesList.ToArray();
}
}
}
}
public class ResponseHeaders : Headers
{
public int code;
public string message
{
get
{
if (messages.ContainsKey(code))
{
return messages[code];
}
else if (code >= 100 && code < 200)
{
return "Unknown Information";
}
else if (code >= 200 && code < 300)
{
return "Unknown Success";
}
else if (code >= 300 && code < 400)
{
return "Unknown Redirect";
}
else if (code >= 400 && code < 500)
{
return "Unknown Client Error";
}
else if (code >= 500 && code < 600)
{
return "Unknown Server Error";
}
return "Unknown Error";
}
}
public ResponseHeaders(int code = 200, Dictionary<string, object>? headers = null)
{
protocol = "HTTP/1.0";
this.code = code;
this.headers["Content-Language"] = "en";
this.headers["Server"] = "Dave's Fantastic Server (" + RuntimeInformation.RuntimeIdentifier + ")";
if (headers != null)
{
foreach (KeyValuePair<string, object> headerField in headers)
{
this.headers[headerField.Key] = headerField.Value;
}
}
}
// Used for setting the MD5 checksum header field (may be unnecessary).
// https://stackoverflow.com/a/24031467
public void SetHash(string body)
{
SetHash(Encoding.ASCII.GetBytes(body));
}
public void SetHash(byte[] bytes)
{
using (MD5 md5 = MD5.Create())
{
headers["Content-MD5"] = Convert.ToHexString(md5.ComputeHash(bytes));
}
}
// https://stackoverflow.com/a/10520086
public void SetHashFile(string path)
{
using (MD5 md5 = MD5.Create())
using (FileStream stream = File.OpenRead(path))
{
headers["Content-MD5"] = Convert.ToHexString(md5.ComputeHash(stream));
}
}
public void SetPreferredRedirect(string currentUrl, string preferredUrl)
{
if (currentUrl != preferredUrl)
{
code = 300;
headers["Location"] = preferredUrl;
}
}
public string GetString()
{
string str = protocol + " " + code + " " + message;
Dictionary<string, object> allHeaders = new Dictionary<string, object>(headers)
{
{ "Status", code + " " + message },
{ "Date", DateTime.UtcNow.ToString("r") }
};
foreach (KeyValuePair<string, object> header in allHeaders)
{
str += Environment.NewLine + header.Key + ": " + header.Value.ToString();
}
str += Environment.NewLine + Environment.NewLine;
//Writes the raw response header.
/*MyConsole.color = ConsoleColor.DarkGray;
MyConsole.WriteLine(str);/**/
return str;
}
public byte[] GetBytes()
{
return Encoding.ASCII.GetBytes(GetString());
}
}
public class RequestHeaders : Headers
{
public string? method;
public string? url;
public byte[]? raw;
public Dictionary<string, string> cookie = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public static RequestHeaders? ReadFromSocket(Socket socket, Log? log = null)
{
/*SslStream sslStream;
try
{
sslStream = Authenticator.GetSslStream(socket, log);
}
catch (Exception exception)
{
if (log != null)
{
log.secondRow = new LogPart(exception, LogColor.Error);
log.Write();
}
}
return null;
if (!sslStream.CanRead)
{
if (log != null)
{
socket.SendError(400, "Authentication failed.");
}
return null;
}/**/
RequestHeaders requestHeaders = new RequestHeaders();
List<byte> bytesList = new List<byte>();
while (true)
{
//byte[]? bytes = ReadStreamToNewline(new NetworkStream(socket));
byte[]? bytes = ReadSocketToNewline(socket, log);
if (bytes == null)
{
string headers = Encoding.ASCII.GetString(bytesList.ToArray());
if (log != null && !string.IsNullOrWhiteSpace(headers))
{
log.Add(headers, LogColor.Hidden);
}
return null;
}
bytesList.AddRange(bytes);
string header = Encoding.ASCII.GetString(bytes);
if (string.IsNullOrWhiteSpace(header))
{
break;
}
//Console.Write(header, LogColor.Hidden);
string[] headerParts = header.Split(':', 2);
if (headerParts.Length >= 1 && !string.IsNullOrWhiteSpace(headerParts[0]))
{
if (headerParts.Length >= 2 && !string.IsNullOrWhiteSpace(headerParts[1])) // Sets normal header row.
{
requestHeaders.Add(headerParts[0].Trim().ToLower(), headerParts[1].Trim());
}
else
{
headerParts = header.Split(" ", 3);
if (headerParts.Length >= 1 && !string.IsNullOrWhiteSpace(headerParts[0])) // Sets first header row (method, route and protocol).
{
requestHeaders.method = headerParts[0];
if (headerParts.Length >= 2 && !string.IsNullOrWhiteSpace(headerParts[1]))
{
requestHeaders.url = headerParts[1];
if (headerParts.Length >= 3 && !string.IsNullOrWhiteSpace(headerParts[2]))
{
requestHeaders.protocol = headerParts[2];
}
}
}
}
}
}
if (requestHeaders.method == null || requestHeaders.url == null || requestHeaders.protocol == null)
{
if (log != null)
{
log.Add("Header error: MissingFields", LogColor.Info);
}
socket.SendError(400, "Missing vital header fields.", log: log);
return null;
}
if (requestHeaders.headers.ContainsKey("Cookie"))
{
string? rawCookie = requestHeaders.headers["Cookie"].ToString();
if (rawCookie != null)
{
foreach (string cookie in rawCookie.Split(';'))
{
string[] cookieParts = cookie.Split('=', 2);
if (cookieParts.Length >= 2)
{
requestHeaders.cookie.Add(cookieParts[0].Trim(), cookieParts[1].Trim());
}
}
}
}
requestHeaders.raw = bytesList.ToArray();
return requestHeaders;
}
}
}