-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathWebContext.cs
More file actions
156 lines (131 loc) · 4.84 KB
/
WebContext.cs
File metadata and controls
156 lines (131 loc) · 4.84 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Microsoft.ClearScript.Util.Web
{
internal sealed class WebContext
{
public WebRequest Request { get; }
public WebResponse Response { get; }
private WebContext(Socket socket, Uri uri, NameValueCollection headers)
{
Request = new WebRequest(uri, headers);
Response = new WebResponse(socket, 200);
}
public static async Task<WebContext> CreateAsync(Socket socket)
{
try
{
var lines = new List<string>();
while (true)
{
var line = await socket.ReceiveLineAsync().ConfigureAwait(false);
if (line.Length < 1)
{
break;
}
lines.Add(line);
}
if (lines.Count < 1)
{
throw new InvalidDataException("HTTP request line not found");
}
var parts = lines[0].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
throw new InvalidDataException("Malformed HTTP request line");
}
var method = parts[0].Trim().ToUpperInvariant();
if (!WebRequest.Methods.Contains(method))
{
throw new InvalidDataException("Unrecognized HTTP method");
}
var requestUrl = parts[1].Trim();
if (string.IsNullOrEmpty(requestUrl))
{
throw new InvalidDataException("Invalid HTTP request URI");
}
var headers = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
for (var index = 1; index < lines.Count; index++)
{
var line = lines[index];
var pos = line.IndexOf(':');
if (pos < 0)
{
throw new InvalidDataException("Malformed HTTP header line");
}
var name = line.Substring(0, pos).Trim();
if (string.IsNullOrEmpty(name))
{
throw new InvalidDataException("Malformed HTTP header line");
}
var value = line.Substring(pos + 1).Trim();
if (string.IsNullOrEmpty(value))
{
throw new InvalidDataException("Malformed HTTP header line");
}
headers[name] = value;
}
string hostName = null;
var port = -1;
var hostHeader = headers.Get("Host");
if (!string.IsNullOrEmpty(hostHeader))
{
var pos = hostHeader.IndexOf(':');
if (pos < 0)
{
hostName = hostHeader.Trim();
}
else
{
hostName = hostHeader.Substring(0, pos).Trim();
if (int.TryParse(hostHeader.Substring(pos + 1), out var tempPort))
{
port = tempPort;
}
}
}
if (string.IsNullOrEmpty(hostName))
{
hostName = Dns.GetHostName();
}
if (port < 1)
{
port = ((IPEndPoint)socket.LocalEndPoint).Port;
}
var uri = new Uri("http://" + hostName + ":" + port + "/");
if (requestUrl != "*")
{
uri = new Uri(uri, requestUrl);
}
return new WebContext(socket, uri, headers);
}
catch
{
Abort(socket, 400);
throw;
}
}
public async Task<WebSocket> AcceptWebSocketAsync()
{
if (!Request.IsWebSocketRequest)
{
throw new InvalidOperationException("The request is not a WebSocket handshake");
}
return await Response.AcceptWebSocketAsync(Request.Headers["Sec-WebSocket-Key"].Trim()).ConfigureAwait(false);
}
private static void Abort(Socket socket, int statusCode)
{
using (new WebResponse(socket, statusCode))
{
}
}
}
}