forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpsSession.cs
More file actions
210 lines (187 loc) · 8.29 KB
/
Copy pathHttpsSession.cs
File metadata and controls
210 lines (187 loc) · 8.29 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
namespace NetCoreServer
{
/// <summary>
/// HTTPS session is used to receive/send HTTP requests/responses from the connected HTTPS client.
/// </summary>
/// <remarks>Thread-safe.</remarks>
public class HttpsSession : SslSession
{
public HttpsSession(HttpsServer server) : base(server)
{
Cache = server.Cache;
Request = new HttpRequest();
Response = new HttpResponse();
}
/// <summary>
/// Get the static content cache
/// </summary>
public FileCache Cache { get; }
/// <summary>
/// Get the HTTP request
/// </summary>
protected HttpRequest Request { get; }
/// <summary>
/// Get the HTTP response
/// </summary>
public HttpResponse Response { get; }
#region Send response / Send response body
/// <summary>
/// Send the current HTTP response (synchronous)
/// </summary>
/// <returns>Size of sent data</returns>
public long SendResponse() { return SendResponse(Response); }
/// <summary>
/// Send the HTTP response (synchronous)
/// </summary>
/// <param name="response">HTTP response</param>
/// <returns>Size of sent data</returns>
public long SendResponse(HttpResponse response) { return Send(response.Cache.Data, response.Cache.Offset, response.Cache.Size); }
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="body">HTTP response body</param>
/// <returns>Size of sent data</returns>
public long SendResponseBody(string body) { return Send(body); }
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <returns>Size of sent data</returns>
public long SendResponseBody(byte[] buffer) { return Send(buffer); }
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <param name="offset">HTTP response body buffer offset</param>
/// <param name="size">HTTP response body size</param>
/// <returns>Size of sent data</returns>
public long SendResponseBody(byte[] buffer, long offset, long size) { return Send(buffer, offset, size); }
/// <summary>
/// Send the current HTTP response (asynchronous)
/// </summary>
/// <returns>'true' if the current HTTP response was successfully sent, 'false' if the session is not connected</returns>
public bool SendResponseAsync() { return SendResponseAsync(Response); }
/// <summary>
/// Send the HTTP response (asynchronous)
/// </summary>
/// <param name="response">HTTP response</param>
/// <returns>'true' if the current HTTP response was successfully sent, 'false' if the session is not connected</returns>
public bool SendResponseAsync(HttpResponse response) { return SendAsync(response.Cache.Data, response.Cache.Offset, response.Cache.Size); }
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="body">HTTP response body</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public bool SendResponseBodyAsync(string body) { return SendAsync(body); }
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public bool SendResponseBodyAsync(byte[] buffer) { return SendAsync(buffer); }
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <param name="offset">HTTP response body buffer offset</param>
/// <param name="size">HTTP response body size</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public bool SendResponseBodyAsync(byte[] buffer, long offset, long size) { return SendAsync(buffer, offset, size); }
#endregion
#region Session handlers
protected override void OnReceived(byte[] buffer, long offset, long size)
{
// Receive HTTP request header
if (Request.IsPendingHeader())
{
if (Request.ReceiveHeader(buffer, (int)offset, (int)size))
OnReceivedRequestHeader(Request);
size = 0;
}
// Check for HTTP request error
if (Request.IsErrorSet)
{
OnReceivedRequestError(Request, "Invalid HTTP request!");
Request.Clear();
Disconnect();
return;
}
// Receive HTTP request body
if (Request.ReceiveBody(buffer, (int)offset, (int)size))
{
OnReceivedRequestInternal(Request);
Request.Clear();
return;
}
// Check for HTTP request error
if (Request.IsErrorSet)
{
OnReceivedRequestError(Request, "Invalid HTTP request!");
Request.Clear();
Disconnect();
return;
}
}
protected override void OnDisconnected()
{
// Receive HTTP request body
if (Request.IsPendingBody())
{
OnReceivedRequestInternal(Request);
Request.Clear();
return;
}
}
/// <summary>
/// Handle HTTP request header received notification
/// </summary>
/// <remarks>Notification is called when HTTP request header was received from the client.</remarks>
/// <param name="request">HTTP request</param>
protected virtual void OnReceivedRequestHeader(HttpRequest request) {}
/// <summary>
/// Handle HTTP request received notification
/// </summary>
/// <remarks>Notification is called when HTTP request was received from the client.</remarks>
/// <param name="request">HTTP request</param>
protected virtual void OnReceivedRequest(HttpRequest request) {}
/// <summary>
/// Handle HTTP cached request received notification
/// </summary>
/// <remarks>
/// Notification is called when HTTP request was received
/// from the client and the corresponding cached content
/// was found.
///
/// Default behavior is just send cached response content
/// to the client.
/// </remarks>
/// <param name="request">HTTP request</param>
/// <param name="content">Cached response content</param>
protected virtual void OnReceivedCachedRequest(HttpRequest request, byte[] content) { SendAsync(content); }
/// <summary>
/// Handle HTTP request error notification
/// </summary>
/// <remarks>Notification is called when HTTP request error was received from the client.</remarks>
/// <param name="request">HTTP request</param>
/// <param name="error">HTTP request error</param>
protected virtual void OnReceivedRequestError(HttpRequest request, string error) {}
#endregion
private void OnReceivedRequestInternal(HttpRequest request)
{
// Try to get the cached response
if (request.Method == "GET")
{
var index = request.Url.IndexOf('?');
var response = Cache.Find((index < 0) ? request.Url : request.Url.Substring(0, index));
if (response.Item1)
{
// Process the request with the cached response
OnReceivedCachedRequest(request, response.Item2);
return;
}
}
// Process the request
OnReceivedRequest(request);
}
}
}