-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.cs
More file actions
547 lines (435 loc) · 20.1 KB
/
Copy pathWebServer.cs
File metadata and controls
547 lines (435 loc) · 20.1 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public static class Extensions {
// Расширения, которые симулируют работу аналогов из Java - чтобы не переписывать тонну кода
public static string[] Split(string source, string separator) {
return source.Split(new string[] { separator }, StringSplitOptions.None);
}
public static string[] Split(string source, string separator, int count) {
return source.Split(new string[] { separator }, count, StringSplitOptions.None);
}
}
public class WebServer {
static class HttpCommonVersion {
public const string HTTP10 = "HTTP/1.0";
public const string HTTP11 = "HTTP/1.1";
}
class HttpCommonHeaders {
public Dictionary<string, string> headers = new Dictionary<string,string>();
public HttpCommonHeaders(string request) {
// Сюда передаются заголовки (и это может быть пустая строка вообще)
if(request.Trim().Length > 0) {
string[] splitRequest = Extensions.Split(request, "\r\n");
if(splitRequest.Length > 0) {
for (int i = 0; i < splitRequest.Length; i++) {
string[] pair = Extensions.Split(splitRequest[i], ":", 2);
this.headers.Add(pair[0].Trim(), pair.Length > 1 ? pair[1].Trim() : string.Empty);
}
}
}
}
public new string ToString() {
string returnValue = string.Empty;
foreach (string key in this.headers.Keys)
{
returnValue += string.Format("{0}: {1}\r\n", key, this.headers[key]);
}
return returnValue;
}
}
static class HttpCommonContentTypes {
public static Dictionary<string, string> contentTypes = new Dictionary<string,string>()
{
{"htm", "text/html" },
{"html", "text/html" },
{"txt", "text/plain" },
{"gif", "image/gif" },
{"png", "image/png" },
{"jpg", "image/jpeg" },
{"jpeg", "image/jpeg" }
};
}
enum HttpRequestMethod {
Get,
Unsupported
// Может быть в будущем и будем обрабатывать что-то кроме GET
}
class HttpRequestURI {
public string path = "/";
public Dictionary<string, string> query = new Dictionary<string,string>();
public HttpRequestURI(string uri) {
// Сюда передается что-то вроде /folder/test.html?param=value¶m2=value2
// И пустая строка тут тоже может быть
if(uri.Length > 0) {
string rawQuery = string.Empty;
string[] split1 = Extensions.Split(uri, "?", 2);
this.path = split1[0];
if(split1.Length > 1) { rawQuery = split1[1]; }
// Теперь нужно преобразовать RawQuery в набор значений
string[] pairs = Extensions.Split(rawQuery, "&");
if(pairs.Length > 0) {
for (int i = 0; i < pairs.Length; i++) {
string[] pair = Extensions.Split(pairs[i], "=", 2);
this.query.Add(pair[0], pair.Length > 1 ? pair[1] : string.Empty);
}
}
}
}
}
class HttpRequest {
// В этих объектах храним формализованные значения
public HttpRequestMethod method = HttpRequestMethod.Unsupported;
public HttpRequestURI requestURI = new HttpRequestURI("/");
public string httpVersion;
// В этих объектах мы храним переданные значения (для toString)
private string _method;
private string _URI;
private string _httpVersion;
public HttpRequest(string requestText) {
// Сюда передается "GET / HTTP/1.1" (пустой строки тут быть не может)
string[] splitRequest = Extensions.Split(requestText, " ", 3);
_method = splitRequest[0];
switch(_method.ToLower())
{
case "get": this.method = HttpRequestMethod.Get; break;
default: this.method = HttpRequestMethod.Unsupported; break;
}
if(splitRequest.Length > 1)
{
_URI = splitRequest[1];
this.requestURI = new HttpRequestURI(_URI);
}
if(splitRequest.Length > 2)
{
_httpVersion = splitRequest[2];
switch(_httpVersion.ToUpper())
{
case HttpCommonVersion.HTTP10: this.httpVersion = HttpCommonVersion.HTTP10; break;
case HttpCommonVersion.HTTP11: this.httpVersion = HttpCommonVersion.HTTP11; break;
}
}
}
public new string ToString()
{
return string.Format("{0} {1} {2}", _method, _URI, _httpVersion);
}
}
static class HttpResponseStatusCodes {
public const string HTTP200OK = "200 OK";
public const string HTTP302Redirect = "302 Found";
public const string HTTP400BadRequest = "400 Bad Request";
public const string HTTP401Unauthorized = "401 Unauthorized";
public const string HTTP404NotFound = "404 Not Found";
public const string HTTP500InternalServerError = "500 Internal Server Error";
public static string CreateStatusLine(string httpVersion, string statusCode) {
return httpVersion + " " + statusCode;
}
}
class PerseusHttpRequest {
public HttpRequest request = null;
public HttpCommonHeaders headers = null;
public PerseusHttpRequest(string requestText) {
string[] splitRequest = Extensions.Split(requestText, "\r\n", 2);
// Тут может быть как 2 штуки (если есть запрос и заголовки)
// Либо вообще 1 штука (только запрос)
this.request = new HttpRequest(splitRequest[0]);
this.headers = splitRequest.Length > 1 ? new HttpCommonHeaders(splitRequest[1]) : new HttpCommonHeaders(string.Empty);
}
}
class PerseusHttpResponse {
public string statusLine;
public HttpCommonHeaders headers;
public byte[] data;
public PerseusHttpResponse(string statusLine, HttpCommonHeaders headers, byte[] data) {
this.statusLine = statusLine;
this.headers = headers;
this.data = new byte[data.Length];
data.CopyTo(this.data, 0);
}
/* public new string ToString() {
return string.format("%s\r\n%s\r\n%s", this.statusLine, this.headers.ToString(), this.data.Length.ToString() + " bytes");
}*/
public byte[] ToBytes() {
byte[] byteStatusLine = Encoding.ASCII.GetBytes(this.statusLine + "\r\n");
byte[] byteHeaders = Encoding.ASCII.GetBytes(this.headers.ToString() + "\r\n");
byte[] byteToSend = ConcatenateBytes(byteStatusLine, byteHeaders, this.data);
return byteToSend;
}
private byte[] ConcatenateBytes(byte[] first, byte[] second, byte[] third) {
List<byte> returnValue = new List<byte>(first.Length + second.Length + third.Length);
int offset = 0;
returnValue.InsertRange(offset, first);
offset += first.Length;
returnValue.InsertRange(offset, second);
offset += second.Length;
returnValue.InsertRange(offset, third);
return returnValue.ToArray();
}
}
class PerseusWebThread {
private Socket clientSocket = null;
private string name = string.Empty;
public PerseusWebThread(Socket sock) {
this.clientSocket = sock;
}
public void ProcessRequest() {
this.name = Thread.CurrentThread.Name;
try
{
byte[] readBuffer = new byte[8192];
int readCount = clientSocket.Receive(readBuffer);
// -1 - Конец потока, ничего не делаем
// 0 - Ничего не прочиталось, ничего не делаем
if(readCount > 0)
{
// Теперь надо собрать строчку и проанализировать, что получилось
// TODO: Тут строчка делается с подрезанием массива по readCount
string requestString = Encoding.ASCII.GetString(readBuffer);
PerseusHttpRequest request = new PerseusHttpRequest(requestString);
PerseusHttpResponse response = null;
// request.request.method = HttpRequestMethod.Unsupported;
// В зависимости от метода подготавливаем ответ
switch(request.request.method)
{
case HttpRequestMethod.Get: response = CreateGetResponse(request); break;
case HttpRequestMethod.Unsupported: response = CreateUnsupportedResponse(request); break;
}
// Ставим подпись :)
response.headers.headers.Add("Server", "Perseus");
// Теперь высылаем его браузеру
SendToClient(request, response);
}
}
catch (Exception e)
{
// Ошибка чтения или обработки запроса
Console.WriteLine("{0}: Error on processing: {1}\r\n", this.name, e.Message);
}
try
{
clientSocket.Close();
}
catch // (Exception e)
{
//
}
}
private PerseusHttpResponse CreateGetResponse(PerseusHttpRequest request) {
string statusLine = string.Empty;
HttpCommonHeaders headers = new HttpCommonHeaders(string.Empty);
byte[] content = new byte[0];
// Запрос всегда приходит с разделителем / и всегда начинается с него
// Надо заменить / в запросе на разделитель (платформа)
string preparedPath = request.request.requestURI.path.Replace("/", Path.DirectorySeparatorChar.ToString());
// Теперь этот путь нужно приделать к нашему пути . (у него на конце нет /)
string completePath = string.Empty;
bool fileError = false;
try {
completePath = Path.GetFullPath(".") + preparedPath;
}
catch { // (Exception e) {
fileError = true;
}
if (fileError) {
// Ошибка получения имени файла
statusLine = HttpResponseStatusCodes.CreateStatusLine(request.request.httpVersion, HttpResponseStatusCodes.HTTP500InternalServerError);
} else {
// Нет, ошибки получения файла не было
// File requestedFile = new File(completePath);
bool directoryRequested = request.request.requestURI.path.EndsWith("/");
bool itemExists = File.Exists(completePath) || Directory.Exists(completePath);
if(itemExists) {
// Да, что-то есть, только надо понять - это файл или папка
if(Directory.Exists(completePath)) {
// То, что существует, папка
if(directoryRequested) {
// Пользователь запрашивал папку, выдаем ее
statusLine = HttpResponseStatusCodes.CreateStatusLine(request.request.httpVersion, HttpResponseStatusCodes.HTTP200OK);
content = CreateGetResponseFolder(completePath, headers);
} else {
// Пользователь запрашивал файл, редирект на папку
statusLine = HttpResponseStatusCodes.CreateStatusLine(request.request.httpVersion, HttpResponseStatusCodes.HTTP302Redirect);
headers.headers.Add("Location", request.request.requestURI.path + "/");
// HACK: Мы не поддерживаем параметры, т.е. их получаем, но не передаем далее
}
} else {
// То, что существует, файл
if(!directoryRequested) {
// Пользователь запрашивал файл, выдаем файл
statusLine = HttpResponseStatusCodes.CreateStatusLine(request.request.httpVersion, HttpResponseStatusCodes.HTTP200OK);
content = CreateGetResponseFile(completePath, headers);
} else {
// Пользователь запрашивал папку - 404
statusLine = HttpResponseStatusCodes.CreateStatusLine(request.request.httpVersion, HttpResponseStatusCodes.HTTP404NotFound);
}
}
} else {
// Нет ни файла, ни папки - это 404
statusLine = HttpResponseStatusCodes.CreateStatusLine(request.request.httpVersion, HttpResponseStatusCodes.HTTP404NotFound);
}
}
// Правила обработки запросов
// Если на конце нет /, то пользователь запрашивает файл
// Если есть файл, то читаем файл
// Если такого файла нет, надо проверить, а есть ли такая папка?
// Если есть папка, то редирект на адрес + /
// Если папки нет, то 404
// Если на конце есть /, то пользователь запрашивает папку
// Если в папке есть index.html или index.htm, то вернем их
// Если нет этих файлов, то вернем листинг папки
return new PerseusHttpResponse(statusLine, headers, content);
// HACK: Возможно, это крайне порочная архитектура - высылать отсюда объект целиком в памяти - это приведет к адовому ее расходу
}
private byte[] CreateGetResponseFile(string file, HttpCommonHeaders headers) {
byte[] returnValue = new byte[0];
FileStream fs = null;
try {
fs = new FileStream(file, FileMode.Open, FileAccess.Read);
returnValue = new byte[(int)fs.Length];
fs.Read(returnValue, 0, (int)fs.Length);
}
catch (Exception e) {
Console.WriteLine("{0}: File read error: {1}\r\n", this.name, file);
}
// Content-Type
string fileName = Path.GetFileName(file);
int indexOfDot = fileName.IndexOf(".");
string extension = indexOfDot > -1 ? fileName.Substring(indexOfDot + 1) : "html";
extension = HttpCommonContentTypes.contentTypes.ContainsKey(extension) ? extension : "html";
headers.headers.Add("Content-Type", HttpCommonContentTypes.contentTypes[extension]);
headers.headers.Add("Content-Lenght", returnValue.Length.ToString());
return returnValue;
}
private byte[] CreateGetResponseFolder(string folder, HttpCommonHeaders headers) {
// HACK: Нам насрать на index.html или index.htm
string returnValue;
string returnHTMLTemplate = "<html><head><title>{0}</title></head><body><h1>{0}</h1>{1}</body></html>";
string returnFileListing = string.Empty;
string returnFileListingTemplate = "<a href=\"{0}\">{0}</a><br />\r\n";
// Возвращаем листинг папки
string[] directories = Directory.GetDirectories(folder);
string[] files = Directory.GetFiles(folder);
if((directories.Length + files.Length) > 0) {
// Да, в папке есть какие-то файлы или папки
foreach (string dir in directories) {
string name = Path.GetFileName(dir) + "/";
returnFileListing += string.Format(returnFileListingTemplate, name);
}
foreach (string file in files) {
string name = Path.GetFileName(file);
returnFileListing += string.Format(returnFileListingTemplate, name);
}
} else {
// Нет, папка пустая
returnFileListing = "Empty folder";
}
// Вот тут надо добавить HTML-обрамление
returnValue = string.Format(returnHTMLTemplate, Path.GetFileName(Path.GetDirectoryName(folder)), returnFileListing);
// Добавляем content-type
headers.headers.Add("Content-type", HttpCommonContentTypes.contentTypes["html"]);
return Encoding.ASCII.GetBytes(returnValue);
}
private PerseusHttpResponse CreateUnsupportedResponse(PerseusHttpRequest request) {
string statusLine = HttpResponseStatusCodes.CreateStatusLine(request.request.httpVersion, HttpResponseStatusCodes.HTTP400BadRequest);
HttpCommonHeaders headers = new HttpCommonHeaders(string.Empty);
return new PerseusHttpResponse(statusLine, headers, new byte[0]);
}
private void SendToClient(PerseusHttpRequest request, PerseusHttpResponse response) {
byte[] byteData = response.ToBytes();
// HACK: Возможно для больших объемов тут нужно будет делать буферизацию чтения/отправки
// И объект тогда будет другой, вместо DataOutputStream
try {
if (clientSocket.Connected) {
if (byteData.Length != clientSocket.Send(byteData, SocketFlags.None)) {
// Отправилось неправильное количество байт
// Протоколируем неправильную отправку
Console.WriteLine("{0}: Error on .send (wrong count)", this.name);
} else {
// Все нормально отправилось
// Протоколируем
Console.WriteLine("{0}: Sent {1} bytes, {2} [{3}]", this.name, byteData.Length, response.statusLine, request.request.requestURI.path);
}
} else {
// Клиент отвалился
Console.WriteLine("{0}: Error on .send (client disconnected)", this.name);
}
}
catch (Exception e)
{
// Что-то не так с сокетом
// Протоколируем ошибку сокета
Console.WriteLine("{0}: Socket exception on .send: {1}\r\n", this.name, e.Message);
}
}
}
static class PerseusServer {
private const int WEB_PORT = 8090;
private const int QUEUE_LENGTH = 100;
private const int TIMEOUT_RESTART = 60;
private static DateTime lastRestart = new DateTime(1970, 1, 1);
public static void Start() {
// accept может выдавать 4 исключения
IPEndPoint webEndPoint = new IPEndPoint(IPAddress.Any, WEB_PORT);
Socket webSocket = null;
while(true) {
try {
Socket acceptedSocket = webSocket.Accept();
IPEndPoint endPoint = (IPEndPoint)acceptedSocket.RemoteEndPoint;
string userID = endPoint.Address + ":" + endPoint.Port;
// Инициализируем поток
PerseusWebThread pwt = new PerseusWebThread(acceptedSocket);
Thread webThread = new Thread(new ThreadStart(pwt.ProcessRequest));
webThread.Name = "WWW / " + userID;
webThread.IsBackground = true;
webThread.Start();
}
catch (SocketException se) {
Console.WriteLine("Socket exception on .accept: " + se.Message);
}
catch (Exception e) {
Console.WriteLine("General exception on .accept: " + e.Message);
// Это не ошибка, на самом деле, но ладно
// Определяем, прошло ли уже время для рестарта?
TimeSpan tmpSpan = DateTime.Now - lastRestart;
if (tmpSpan.TotalSeconds > TIMEOUT_RESTART) {
// Выполняем рестарт
lastRestart = DateTime.Now;
// Если сейчас в webSocket что-то есть, то это надо зачистить
if(webSocket != null) {
try {
webSocket.Shutdown(SocketShutdown.Both);
} catch (Exception closeException) {
Console.WriteLine("General exception on .Shutdown: " + closeException.Message);
}
// .Close может генерировать исключение!
try {
webSocket.Close();
} catch (Exception e3) {
// Ошибка закрытия...
Console.WriteLine("General exception on .Close: " + e3.Message);
}
}
// Выполняем создание сокета
try {
webSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
webSocket.Bind(webEndPoint);
webSocket.Listen(QUEUE_LENGTH);
}
catch (Exception createException) {
Console.WriteLine("General exception on creating: " + createException.Message);
}
}
}
}
}
}
public static void main(String[] args) {
PerseusServer.Start();
}
}