forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientConnection.cs
More file actions
780 lines (671 loc) · 20.2 KB
/
HttpClientConnection.cs
File metadata and controls
780 lines (671 loc) · 20.2 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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Text;
using Waher.Content;
using Waher.Events;
using Waher.Networking.Sniffers;
using Waher.Networking.HTTP.HeaderFields;
using Waher.Networking.HTTP.TransferEncodings;
using Waher.Networking.HTTP.WebSockets;
using Waher.Runtime.Temporary;
using Waher.Security;
namespace Waher.Networking.HTTP
{
internal enum ConnectionMode
{
Http,
WebSocket
}
/// <summary>
/// Class managing a remote client connection to a local <see cref="HttpServer"/>.
/// </summary>
internal class HttpClientConnection : Sniffable, IDisposable
{
internal const byte CR = 13;
internal const byte LF = 10;
internal const int MaxHeaderSize = 65536;
internal const int MaxInmemoryMessageSize = 1024 * 1024; // 1 MB
internal const long MaxEntitySize = 1024 * 1024 * 1024; // 1 GB
private MemoryStream headerStream = null;
private Stream dataStream = null;
private TransferEncoding transferEncoding = null;
private readonly HttpServer server;
private BinaryTcpClient client;
private HttpRequestHeader header = null;
private ConnectionMode mode = ConnectionMode.Http;
private WebSocket webSocket = null;
private byte b1 = 0;
private byte b2 = 0;
private byte b3 = 0;
private readonly bool encrypted;
private bool disposed = false;
internal HttpClientConnection(HttpServer Server, BinaryTcpClient Client, bool Encrypted, params ISniffer[] Sniffers)
: base(Sniffers)
{
this.server = Server;
this.client = Client;
this.encrypted = Encrypted;
this.client.OnDisconnected += Client_OnDisconnected;
this.client.OnError += Client_OnError;
this.client.OnReceived += Client_OnReceived;
}
private Task<bool> Client_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
{
this.server.DataReceived(Count);
if (this.mode == ConnectionMode.Http)
{
if (this.header is null)
return this.BinaryHeaderReceived(Buffer, Offset, Count);
else
return this.BinaryDataReceived(Buffer, Offset, Count);
}
else
return this.webSocket?.WebSocketDataReceived(Buffer, Offset, Count) ?? Task.FromResult<bool>(false);
}
private Task Client_OnError(object Sender, Exception Exception)
{
this.Dispose();
return Task.CompletedTask;
}
private void Client_OnDisconnected(object sender, EventArgs e)
{
this.Dispose();
}
public void Dispose()
{
if (!this.disposed)
{
this.disposed = true;
this.webSocket?.Dispose();
this.webSocket = null;
this.headerStream?.Dispose();
this.headerStream = null;
this.dataStream?.Dispose();
this.dataStream = null;
this.client?.DisposeWhenDone();
this.client = null;
}
}
internal HttpServer Server
{
get { return this.server; }
}
internal bool Disposed
{
get { return this.disposed; }
}
internal BinaryTcpClient Client
{
get { return this.client; }
}
#if !WINDOWS_UWP
internal bool Encrypted
{
get { return this.encrypted; }
}
#endif
private async Task<bool> BinaryHeaderReceived(byte[] Data, int Offset, int NrRead)
{
string Header;
int i, c;
byte b;
c = Offset + NrRead;
for (i = Offset; i < c; i++)
{
b = Data[i];
if (this.b1 == CR && this.b2 == LF && this.b3 == CR && b == LF) // RFC 2616, §2.2
{
if (this.headerStream is null)
Header = InternetContent.ISO_8859_1.GetString(Data, Offset, i - Offset - 3);
else
{
this.headerStream.Write(Data, Offset, i - Offset - 3);
Header = InternetContent.ISO_8859_1.GetString(this.headerStream.ToArray(), 0, (int)this.headerStream.Position);
this.headerStream = null;
}
}
else if (this.b3 == LF && b == LF) // RFC 2616, §19.3
{
if (this.headerStream is null)
Header = InternetContent.ISO_8859_1.GetString(Data, Offset, i - Offset - 1);
else
{
this.headerStream.Write(Data, Offset, i - Offset - 1);
Header = InternetContent.ISO_8859_1.GetString(this.headerStream.ToArray(), 0, (int)this.headerStream.Position);
this.headerStream = null;
}
}
else
{
this.b1 = this.b2;
this.b2 = this.b3;
this.b3 = b;
continue;
}
this.ReceiveText(Header);
this.header = new HttpRequestHeader(Header, this.server.VanityResources, this.encrypted ? "https" : "http");
if (this.header.HttpVersion < 1)
{
await this.SendResponse(null, null, new HttpException(505, "HTTP Version Not Supported", "At least HTTP Version 1.0 is required."), true);
return false;
}
else if (this.header.ContentLength != null && (this.header.ContentLength.ContentLength > MaxEntitySize))
{
await this.SendResponse(null, null, new HttpException(413, "Request Entity Too Large", "Maximum Entity Size: " + MaxEntitySize.ToString()), true);
return false;
}
else if (i + 1 < NrRead)
return await this.BinaryDataReceived(Data, i + 1, NrRead - i - 1);
else if (!this.header.HasMessageBody)
return await this.RequestReceived();
else
return true;
}
if (this.headerStream is null)
this.headerStream = new MemoryStream();
this.headerStream.Write(Data, Offset, NrRead);
if (this.headerStream.Position < MaxHeaderSize)
return true;
else
{
if (this.HasSniffers)
{
int d = (int)this.headerStream.Position;
byte[] Data2 = new byte[d];
this.headerStream.Position = 0;
this.headerStream.Read(Data2, 0, d);
this.ReceiveBinary(Data2);
}
await this.SendResponse(null, null, new HttpException(431, "Request Header Fields Too Large", "Max Header Size: " + MaxHeaderSize.ToString()), true);
return false;
}
}
private async Task<bool> BinaryDataReceived(byte[] Data, int Offset, int NrRead)
{
if (this.dataStream is null)
{
HttpFieldTransferEncoding TransferEncoding = this.header.TransferEncoding;
if (!(TransferEncoding is null))
{
if (TransferEncoding.Value == "chunked")
{
this.dataStream = new TemporaryStream();
this.transferEncoding = new ChunkedTransferEncoding(new BinaryOutputStream(this.dataStream), null);
}
else
{
await this.SendResponse(null, null, new HttpException(501, "Not Implemented", "Transfer encoding not implemented."), false);
return true;
}
}
else
{
HttpFieldContentLength ContentLength = this.header.ContentLength;
if (!(ContentLength is null))
{
long l = ContentLength.ContentLength;
if (l < 0)
{
await this.SendResponse(null, null, new HttpException(400, "Bad Request", "Negative content lengths invalid."), false);
return true;
}
if (l <= MaxInmemoryMessageSize)
this.dataStream = new MemoryStream((int)l);
else
this.dataStream = new TemporaryStream();
this.transferEncoding = new ContentLengthEncoding(new BinaryOutputStream(this.dataStream), l, null);
}
else
{
await this.SendResponse(null, null, new HttpException(411, "Length Required", "Content Length required."), true);
return false;
}
}
}
ulong DecodingResponse = await this.transferEncoding.DecodeAsync(Data, Offset, NrRead);
int NrAccepted = (int)DecodingResponse;
bool Complete = (DecodingResponse & 0x100000000) != 0;
if (this.HasSniffers)
{
if (Offset == 0 && NrAccepted == Data.Length)
this.ReceiveBinary(Data);
else
{
byte[] Data2 = new byte[NrAccepted];
Array.Copy(Data, Offset, Data2, 0, NrAccepted);
this.ReceiveBinary(Data2);
}
}
if (Complete)
{
if (this.transferEncoding.InvalidEncoding)
{
await this.SendResponse(null, null, new HttpException(400, "Bad Request", "Invalid transfer encoding."), false);
return true;
}
else if (this.transferEncoding.TransferError)
{
await this.SendResponse(null, null, new HttpException(500, "Internal Server Error", "Unable to transfer content to resource."), false);
return true;
}
else
{
Offset += NrAccepted;
NrRead -= NrAccepted;
if (!await this.RequestReceived())
return false;
if (NrRead > 0)
return await this.BinaryHeaderReceived(Data, Offset, NrRead);
else
return true;
}
}
else if (this.dataStream.Position > MaxEntitySize)
{
this.dataStream.Dispose();
this.dataStream = null;
await this.SendResponse(null, null, new HttpException(413, "Request Entity Too Large", "Maximum Entity Size: " + MaxEntitySize.ToString()), true);
return false;
}
else
return true;
}
private async Task<bool> RequestReceived()
{
#if WINDOWS_UWP
HttpRequest Request = new HttpRequest(this.header, this.dataStream,
this.client.Client.Information.RemoteAddress.ToString() + ":" + this.client.Client.Information.RemotePort);
#else
HttpRequest Request = new HttpRequest(this.header, this.dataStream, this.client.Client.Client.RemoteEndPoint.ToString());
#endif
Request.clientConnection = this;
bool? Queued = await this.QueueRequest(Request);
if (Queued.HasValue)
{
if (!Queued.Value && this.dataStream != null)
this.dataStream.Dispose();
this.header = null;
this.dataStream = null;
this.transferEncoding = null;
return Queued.Value;
}
else
return true;
}
private async Task<bool?> QueueRequest(HttpRequest Request)
{
HttpAuthenticationScheme[] AuthenticationSchemes;
bool Result;
try
{
if (this.server.TryGetResource(Request.Header.Resource, out HttpResource Resource, out string SubPath))
{
Request.Resource = Resource;
Request.SubPath = SubPath;
#if WINDOWS_UWP
this.server.RequestReceived(Request, this.client.Client.Information.RemoteAddress.ToString() + ":" +
this.client.Client.Information.RemotePort, Resource, SubPath);
#else
this.server.RequestReceived(Request, this.client.Client.Client.RemoteEndPoint.ToString(), Resource, SubPath);
#endif
AuthenticationSchemes = Resource.GetAuthenticationSchemes(Request);
if (AuthenticationSchemes != null && AuthenticationSchemes.Length > 0)
{
ILoginAuditor Auditor = this.server.LoginAuditor;
if (!(Auditor is null))
{
DateTime? Next = await Auditor.GetEarliestLoginOpportunity(Request.RemoteEndPoint, "HTTP");
if (Next.HasValue)
{
StringBuilder sb = new StringBuilder();
DateTime TP = Next.Value;
DateTime Today = DateTime.Today;
HttpException Error;
if (Next.Value == DateTime.MaxValue)
{
sb.Append("This endpoint (");
sb.Append(Request.RemoteEndPoint);
sb.Append(") has been blocked from the system.");
Error = new ForbiddenException(sb.ToString());
}
else
{
sb.Append("Too many failed login attempts in a row registered. Try again in ");
TimeSpan Span = TP - DateTime.Now;
double d;
if ((d = Span.TotalDays) >= 1)
{
d = Math.Ceiling(d);
sb.Append(d.ToString());
sb.Append(" day");
}
else if ((d = Span.TotalHours) >= 1)
{
d = Math.Ceiling(d);
sb.Append(d.ToString());
sb.Append(" hour");
}
else
{
d = Math.Ceiling(Span.TotalMinutes);
sb.Append(d.ToString());
sb.Append(" minute");
}
if (d > 1)
sb.Append('s');
sb.Append('.');
Error = new TooManyRequestsException(sb.ToString());
}
await this.SendResponse(Request, null, Error, true);
Request.Dispose();
return true;
}
}
foreach (HttpAuthenticationScheme Scheme in AuthenticationSchemes)
{
if (Scheme.UserSessions && Request.Session is null)
{
HttpFieldCookie Cookie = Request.Header.Cookie;
if (!(Cookie is null))
{
string HttpSessionID = Cookie["HttpSessionID"];
if (!string.IsNullOrEmpty(HttpSessionID))
Request.Session = this.server.GetSession(HttpSessionID);
}
}
IUser User = await Scheme.IsAuthenticated(Request);
if (!(User is null))
{
Request.User = User;
break;
}
}
if (Request.User is null)
{
List<KeyValuePair<string, string>> Challenges = new List<KeyValuePair<string, string>>();
bool Encrypted = this.client.IsEncrypted;
#if !WINDOWS_UWP
int Strength = Encrypted ? Math.Min(
Math.Min(this.client.CipherStrength, this.client.HashStrength),
this.client.KeyExchangeStrength) : 0;
#endif
foreach (HttpAuthenticationScheme Scheme in AuthenticationSchemes)
{
if (Scheme.RequireEncryption)
{
if (!Encrypted)
continue;
#if !WINDOWS_UWP
if (Scheme.MinStrength > Strength)
continue;
#endif
}
string Challenge = Scheme.GetChallenge();
if (string.IsNullOrEmpty(Challenge))
continue;
Challenges.Add(new KeyValuePair<string, string>("WWW-Authenticate", Challenge));
}
await this.SendResponse(Request, null, new HttpException(401, "Unauthorized", "Unauthorized access prohibited."), false, Challenges.ToArray());
Request.Dispose();
return true;
}
}
Resource.Validate(Request);
if (Request.Header.Expect != null)
{
if (Request.Header.Expect.Continue100)
{
if (!Request.HasData)
{
await this.SendResponse(Request, null, new HttpException(100, "Continue", null), false);
return null;
}
}
else
{
await this.SendResponse(Request, null, new HttpException(417, "Expectation Failed", "Unable to parse Expect header."), true);
Request.Dispose();
return false;
}
}
Task _ = Task.Run(() => this.ProcessRequest(Request, Resource));
return true;
}
else
{
await this.SendResponse(Request, null, new NotFoundException("Resource not found: " + this.server.CheckResourceOverride(Request.Header.Resource)), false);
Result = true;
}
}
catch (HttpException ex)
{
Result = (Request.Header.Expect is null || !Request.Header.Expect.Continue100 || Request.HasData);
await this.SendResponse(Request, null, ex, !Result, ex.HeaderFields);
}
catch (System.NotImplementedException ex)
{
Result = (Request.Header.Expect is null || !Request.Header.Expect.Continue100 || Request.HasData);
Log.Critical(ex);
await this.SendResponse(Request, null, new NotImplementedException(ex.Message), !Result);
}
catch (IOException ex)
{
Log.Critical(ex);
int Win32ErrorCode = ex.HResult & 0xFFFF;
if (Win32ErrorCode == 0x27 || Win32ErrorCode == 0x70) // ERROR_HANDLE_DISK_FULL, ERROR_DISK_FULL
await this.SendResponse(Request, null, new HttpException(507, "Insufficient Storage", "Insufficient space."), true);
else
await this.SendResponse(Request, null, new InternalServerErrorException(ex.Message), true);
Result = false;
}
catch (Exception ex)
{
Result = (Request.Header.Expect is null || !Request.Header.Expect.Continue100 || Request.HasData);
Log.Critical(ex);
await this.SendResponse(Request, null, new InternalServerErrorException(ex.Message), !Result);
}
Request.Dispose();
return Result;
}
private KeyValuePair<string, string>[] Merge(KeyValuePair<string, string>[] Headers, LinkedList<Cookie> Cookies)
{
if (Cookies is null || Cookies.First is null)
return Headers;
List<KeyValuePair<string, string>> Result = new List<KeyValuePair<string, string>>();
Result.AddRange(Headers);
foreach (Cookie Cookie in Cookies)
Result.Add(new KeyValuePair<string, string>("Set-Cookie", Cookie.ToString()));
return Result.ToArray();
}
private async Task ProcessRequest(HttpRequest Request, HttpResource Resource)
{
HttpResponse Response = null;
try
{
#if !WINDOWS_UWP
HttpRequestHeader Header = Request.Header;
int? UpgradePort = null;
if (!this.encrypted &&
(Header.UpgradeInsecureRequests?.Upgrade ?? false) &&
Header.Host != null &&
string.Compare(Header.Host.Value, "localhost", true) != 0 &&
((UpgradePort = this.server.UpgradePort).HasValue))
{
StringBuilder Location = new StringBuilder();
string s;
int i;
Location.Append("https://");
s = Header.Host.Value;
i = s.IndexOf(':');
if (i > 0)
s = s.Substring(0, i);
Location.Append(s);
if (!(UpgradePort is null) && UpgradePort.Value != HttpServer.DefaultHttpsPort)
{
Location.Append(':');
Location.Append(UpgradePort.Value.ToString());
}
Location.Append(Header.Resource);
if (!string.IsNullOrEmpty(s = Header.QueryString))
{
Location.Append('?');
Location.Append(Header.QueryString);
}
if (!string.IsNullOrEmpty(s = Header.Fragment))
{
Location.Append('#');
Location.Append(Header.Fragment);
}
await this.SendResponse(Request, Response, new HttpException(307, "Moved Temporarily",
new KeyValuePair<string, string>("Location", Location.ToString()),
new KeyValuePair<string, string>("Vary", "Upgrade-Insecure-Requests")), false);
}
else
#endif
{
Response = new HttpResponse(this.client, this, this.server, Request);
await Resource.Execute(this.server, Request, Response);
}
}
catch (HttpException ex)
{
if (Response is null || !Response.HeaderSent)
{
try
{
await this.SendResponse(Request, Response, ex, false, this.Merge(ex.HeaderFields, Response.Cookies));
}
catch (Exception)
{
this.CloseStream();
}
}
else
this.CloseStream();
}
catch (Exception ex)
{
Log.Critical(ex);
if (Response is null || !Response.HeaderSent)
{
try
{
await this.SendResponse(Request, Response, new InternalServerErrorException(ex.Message), true);
}
catch (Exception)
{
this.CloseStream();
}
}
else
this.CloseStream();
}
finally
{
Request.Dispose();
}
}
private void CloseStream()
{
this.client?.DisposeWhenDone();
this.client = null;
}
private async Task SendResponse(HttpRequest Request, HttpResponse Response, HttpException ex, bool CloseAfterTransmission,
params KeyValuePair<string, string>[] HeaderFields)
{
bool DisposeResponse;
if (Response is null)
{
Response = new HttpResponse(this.client, this, this.server, Request)
{
StatusCode = ex.StatusCode,
StatusMessage = ex.Message,
ContentLength = null,
ContentType = null,
ContentLanguage = null
};
DisposeResponse = true;
}
else
{
Response.StatusCode = ex.StatusCode;
Response.StatusMessage = ex.Message;
Response.ContentLength = null;
Response.ContentType = null;
Response.ContentLanguage = null;
DisposeResponse = false;
}
try
{
foreach (KeyValuePair<string, string> P in HeaderFields)
Response.SetHeader(P.Key, P.Value);
if (CloseAfterTransmission)
{
Response.CloseAfterResponse = true;
Response.SetHeader("Connection", "close");
}
if (ex is null)
await Response.SendResponse();
else
await Response.SendResponse(ex);
}
finally
{
if (DisposeResponse)
Response.Dispose();
}
}
internal void Upgrade(WebSocket Socket)
{
this.mode = ConnectionMode.WebSocket;
this.webSocket = Socket;
}
/// <summary>
/// Checks if the connection is live.
/// </summary>
/// <returns>If the connection is still live.</returns>
internal bool CheckLive()
{
try
{
if (this.disposed)
return false;
if (!this.client.Connected)
return false;
#if WINDOWS_UWP
return true;
#else
// https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx
Socket Socket = this.client.Client.Client;
bool BlockingBak = Socket.Blocking;
try
{
byte[] Temp = new byte[1];
Socket.Blocking = false;
Socket.Send(Temp, 0, 0);
return true;
}
catch (SocketException ex)
{
int Win32ErrorCode = ex.HResult & 0xFFFF;
if (Win32ErrorCode == 10035) // WSAEWOULDBLOCK
return true;
else
return false;
}
finally
{
Socket.Blocking = BlockingBak;
}
#endif
}
catch (Exception)
{
return false;
}
}
}
}