-
Notifications
You must be signed in to change notification settings - Fork 35
/
HttpService.cs
115 lines (98 loc) · 4.4 KB
/
HttpService.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
using System;
using System.Net;
using Griffin.Networking.Buffers;
using Griffin.Networking.Protocol.Http.Implementation;
using Griffin.Networking.Protocol.Http.Protocol;
using Griffin.Networking.Servers;
namespace Griffin.Networking.Protocol.Http
{
/// <summary>
/// Base class for handling HTTP requests in the server.
/// </summary>
public abstract class HttpService : INetworkService
{
private readonly IBufferSliceStack _stack;
/// <summary>
/// Initializes a new instance of the <see cref="HttpService" /> class.
/// </summary>
/// <param name="sliceStack">Used to retreive the buffers which are used during message serialization. Make sure that each buffer is large enough to serialize all headers.</param>
/// <exception cref="System.ArgumentNullException">sliceStack</exception>
/// <remarks>You typically want to pass a <c>static</c> stack to this constructor if you want performance.</remarks>
protected HttpService(IBufferSliceStack sliceStack)
{
if (sliceStack == null) throw new ArgumentNullException("sliceStack");
_stack = sliceStack;
}
/// <summary>
/// Context used to communicate with the client
/// </summary>
public IServerClientContext Context { get; private set; }
#region INetworkService Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public abstract void Dispose();
/// <summary>
/// Assign the context which can be used to communicate with the client
/// </summary>
/// <param name="context">Context</param>
public void Assign(IServerClientContext context)
{
Context = context;
}
/// <summary>
/// A new message have been received from the remote end.
/// </summary>
/// <param name="message">You'll receive <see cref="IRequest"/> or <see cref="IResponse"/> depending on the type of application.</param>
void INetworkService.HandleReceive(object message)
{
// Violates LSP, but the best solution that I could come up with.
var ourRequest = message as HttpRequest;
if (ourRequest != null)
ourRequest.RemoteEndPoint = Context.RemoteEndPoint;
var request = (IRequest)message;
request.AddHeader("RemoteAddress", Context.RemoteEndPoint.ToString());
OnRequest(request);
}
/// <summary>
/// We've received a HTTP request.
/// </summary>
/// <param name="request">HTTP request</param>
public abstract void OnRequest(IRequest request);
/// <summary>
/// An unhandled exception was caught when handling incoming bytes.
/// </summary>
/// <param name="context">Information about the exception that was caught</param>
public virtual void OnUnhandledException(ServiceExceptionContext context)
{
}
#endregion
/// <summary>
/// Send a HTTP message
/// </summary>
/// <param name="message">Message to send</param>
public void Send(IMessage message)
{
if (message == null) throw new ArgumentNullException("message");
var slice = _stack.Pop();
var stream = new SliceStream(slice);
var serializer = new HttpHeaderSerializer();
serializer.SerializeResponse((IResponse)message, stream);
Context.Send(slice, (int)stream.Length);
if (message.ContentLength > 0 && message.Body == null)
throw new InvalidOperationException("A content length is specified, but the Body stream is null.");
if (message.Body != null)
Context.Send(message.Body);
if (message.ProtocolVersion == "HTTP/1.0")
Context.Close();
else
{
//Close connection according to headers
var connectionHeader = message.Headers["Connection"];
if (connectionHeader != null && connectionHeader.Value == "close")
Context.Close();
}
}
}
}