forked from stevenh/HttpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpContext.cs
367 lines (319 loc) · 10.5 KB
/
HttpContext.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
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
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using HttpServer.Headers;
using HttpServer.Logging;
using HttpServer.Messages;
using HttpServer.Messages.Parser;
using HttpServer.Tools;
using HttpServer.Transports;
namespace HttpServer
{
/// <summary>
/// A HTTP context
/// </summary>
/// <remarks>
///
/// </remarks>
[Component]
public class HttpContext : IHttpContext, IDisposable
{
private static readonly ObjectPool<byte[]> Buffers = new ObjectPool<byte[]>(() => new byte[65535]);
[ThreadStatic] private static IHttpContext _context;
private readonly byte[] _buffer;
private readonly ILogger _logger = LogFactory.CreateLogger(typeof (HttpContext));
private Timer _keepAlive;
private int _keepAliveTimeout = 100000; // 100 seconds.
/// <summary>
/// Initializes a new instance of the <see cref="HttpContext"/> class.
/// </summary>
/// <param name="socket">Socket received from HTTP listener.</param>
/// <param name="context">Context used to parse incoming messages.</param>
public HttpContext(Socket socket, MessageFactoryContext context)
{
Socket = socket;
MessageFactoryContext = context;
MessageFactoryContext.RequestCompleted += OnRequest;
MessageFactoryContext.ContinueResponseRequested += On100Continue;
_buffer = Buffers.Dequeue();
}
/// <summary>
/// Gets currently executing HTTP context.
/// </summary>
public static IHttpContext Current
{
get { return _context; }
internal set { _context = value; }
}
/// <summary>
/// Gets or sets description
/// </summary>
internal HttpFactory HttpFactory { get; set; }
/// <summary>
/// gets factory used to build request objects
/// </summary>
internal MessageFactoryContext MessageFactoryContext { get; private set; }
/// <summary>
/// Gets socket
/// </summary>
internal Socket Socket { get; private set; }
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
Buffers.Enqueue(_buffer);
Close();
}
#endregion
#region IHttpContext Members
/// <summary>
/// Gets remove end point
/// </summary>
public IPEndPoint RemoteEndPoint
{
get { return (IPEndPoint) Socket.RemoteEndPoint; }
}
/// <summary>
/// Gets network stream.
/// </summary>
public Stream Stream { get; private set; }
/// <summary>
/// Gets the currently handled request
/// </summary>
/// <value>The request.</value>
public IRequest Request { get; internal set; }
/// <summary>
/// Gets the response that is going to be sent back
/// </summary>
/// <value>The response.</value>
public IResponse Response { get; internal set; }
/// <summary>
/// Gets logger.
/// </summary>
public ILogger Logger
{
get { return _logger; }
}
/// <summary>
/// Gets if current context is using a secure connection.
/// </summary>
public virtual bool IsSecure
{
get { return false; }
}
/// <summary>
/// Disconnect context.
/// </summary>
public void Disconnect()
{
Close();
}
#endregion
/// <summary>
/// Triggered for all requests in the server (after the response have been sent)
/// </summary>
public static event EventHandler<RequestEventArgs> CurrentRequestCompleted = delegate { };
/// <summary>
/// Triggered for current request (after the response have been sent)
/// </summary>
public event EventHandler<RequestEventArgs> RequestCompleted = delegate { };
private void On100Continue(object sender, ContinueEventArgs e)
{
ContinueResponseRequested(this, e);
}
/// <summary>
/// Close and release socket.
/// </summary>
private void Close()
{
lock (this)
{
if (Socket == null)
return;
try
{
if (_keepAlive != null)
{
_keepAlive.Dispose();
_keepAlive = null;
}
Socket.Disconnect(true);
Socket.Close();
Socket = null;
Stream.Dispose();
Stream = null;
MessageFactoryContext.RequestCompleted -= OnRequest;
MessageFactoryContext.ContinueResponseRequested -= On100Continue;
MessageFactoryContext.Reset();
}
catch(Exception err)
{
_logger.Warning("Failed to close context properly.", err);
}
}
Disconnected(this, EventArgs.Empty);
}
/// <summary>
/// Create stream used to send and receive bytes from the socket.
/// </summary>
/// <param name="socket">Socket to wrap</param>
/// <returns>Stream</returns>
/// <exception cref="InvalidOperationException">Stream could not be created.</exception>
protected virtual Stream CreateStream(Socket socket)
{
return new ReusableSocketNetworkStream(socket, true);
}
/// <summary>
/// Interpret incoming data.
/// </summary>
/// <param name="ar"></param>
private void OnReceive(IAsyncResult ar)
{
// been closed by our side.
if (Stream == null)
return;
_context = this;
HttpFactory.Current = HttpFactory;
try
{
int bytesLeft = Stream.EndRead(ar);
if (bytesLeft == 0)
{
_logger.Trace("Client disconnected.");
Close();
return;
}
_logger.Debug(Socket.RemoteEndPoint + " received " + bytesLeft + " bytes.");
if (bytesLeft < 5000)
{
string temp = Encoding.Default.GetString(_buffer, 0, bytesLeft);
_logger.Trace(temp);
}
int offset = ParseBuffer(bytesLeft);
bytesLeft -= offset;
if (bytesLeft > 0)
{
_logger.Warning("Moving " + bytesLeft + " from " + offset + " to beginning of array.");
Buffer.BlockCopy(_buffer, offset, _buffer, 0, bytesLeft);
}
Stream.BeginRead(_buffer, 0, _buffer.Length - offset, OnReceive, null);
}
catch (ParserException err)
{
_logger.Warning(err.ToString());
var response = new Response("HTTP/1.0", HttpStatusCode.BadRequest, err.Message);
var generator = HttpFactory.Current.Get<ResponseWriter>();
generator.SendErrorPage(this, response, err);
Close();
}
catch (Exception err)
{
if (!(err is IOException))
{
_logger.Error("Failed to read from stream: " + err);
var responseWriter = HttpFactory.Current.Get<ResponseWriter>();
var response = new Response("HTTP/1.0", HttpStatusCode.InternalServerError, err.Message);
responseWriter.SendErrorPage(this, response, err);
}
Close();
}
}
/// <summary>
/// A request was received from the parser.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnRequest(object sender, FactoryRequestEventArgs e)
{
_context = this;
Response = HttpFactory.Current.Get<IResponse>(this, e.Request);
_logger.Debug("Received '" + e.Request.Method + " " + e.Request.Uri.PathAndQuery + "' from " +
Socket.RemoteEndPoint);
// keep alive.
if (e.Request.Connection != null && e.Request.Connection.Type == ConnectionType.KeepAlive)
{
Response.Add(new StringHeader("Keep-Alive", "timeout=5, max=100"));
// refresh timer
if (_keepAlive != null)
_keepAlive.Change(_keepAliveTimeout, _keepAliveTimeout);
}
Request = e.Request;
CurrentRequestReceived(this, new RequestEventArgs(this, e.Request, Response));
RequestReceived(this, new RequestEventArgs(this, e.Request, Response));
//
if (Response.Connection.Type == ConnectionType.KeepAlive)
{
if (_keepAlive == null)
_keepAlive = new Timer(OnConnectionTimeout, null, _keepAliveTimeout, _keepAliveTimeout);
}
RequestCompleted(this, new RequestEventArgs(this, e.Request, Response));
CurrentRequestCompleted(this, new RequestEventArgs(this, e.Request, Response));
}
private void OnConnectionTimeout(object state)
{
if (_keepAlive != null)
_keepAlive.Dispose();
_logger.Info("Keep-Alive timeout");
Disconnect();
}
/// <summary>
/// Parse all complete requests in buffer.
/// </summary>
/// <param name="bytesLeft"></param>
/// <returns>offset in buffer where parsing stopped.</returns>
/// <exception cref="InvalidOperationException">Parsing failed.</exception>
private int ParseBuffer(int bytesLeft)
{
int offset = MessageFactoryContext.Parse(_buffer, 0, bytesLeft);
bytesLeft -= offset;
// try another pass if we got bytes left.
if (bytesLeft <= 0)
return offset;
// Continue until offset is not changed.
int oldOffset = 0;
while (offset != oldOffset)
{
oldOffset = offset;
_logger.Trace("Parsing from index " + offset + ", " + bytesLeft + " bytes.");
offset = MessageFactoryContext.Parse(_buffer, offset, bytesLeft);
bytesLeft -= offset;
}
return offset;
}
/// <summary>
/// Start content.
/// </summary>
/// <exception cref="SocketException">A socket operation failed.</exception>
/// <exception cref="IOException">Reading from stream failed.</exception>
internal void Start()
{
Stream = CreateStream(Socket);
Stream.BeginRead(_buffer, 0, _buffer.Length, OnReceive, null);
}
/// <summary>
/// A new request have been received.
/// </summary>
public event EventHandler<RequestEventArgs> RequestReceived = delegate { };
/// <summary>
/// A new request have been received (invoked for ALL requests)
/// </summary>
public static event EventHandler<RequestEventArgs> CurrentRequestReceived = delegate { };
/// <summary>
/// Client have been disconnected.
/// </summary>
public event EventHandler Disconnected = delegate { };
/// <summary>
/// Client asks if he may continue.
/// </summary>
/// <remarks>
/// If the body is too large or anything like that you should respond <see cref="HttpStatusCode.ExpectationFailed"/>.
/// </remarks>
public event EventHandler<ContinueEventArgs> ContinueResponseRequested = delegate { };
}
}