1 module hunt.http.server.ServerHttpHandler;
2 
3 import hunt.http.server.HttpServerConnection;
4 
5 import hunt.http.codec.http.model.MetaData;
6 import hunt.http.codec.http.stream.HttpConnection;
7 import hunt.http.codec.http.stream.HttpHandler;
8 import hunt.http.codec.http.stream.HttpOutputStream;
9 import hunt.http.codec.http.stream.HttpTunnelConnection;
10 
11 import hunt.lang.common;
12 
13 import hunt.container.ByteBuffer;
14 
15 /**
16 */
17 interface ServerHttpHandler : HttpHandler {
18 
19     void acceptConnection(HttpConnection connection);
20 
21     bool accept100Continue(HttpRequest request, HttpResponse response,
22                               HttpOutputStream output,
23                               HttpConnection connection);
24 
25     bool acceptHttpTunnelConnection(HttpRequest request, HttpResponse response,
26                                        HttpOutputStream output,
27                                        HttpServerConnection connection);
28 
29 }
30 
31 
32 class ServerHttpHandlerAdapter : HttpHandlerAdapter, ServerHttpHandler {
33 
34     protected Action1!HttpConnection _acceptConnection;
35     protected Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) _accept100Continue;
36     protected Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpServerConnection, bool) _acceptHttpTunnelConnection;
37 
38     ServerHttpHandlerAdapter headerComplete(
39             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) h) {
40         this._headerComplete = h;
41         return this;
42     }
43 
44     ServerHttpHandlerAdapter messageComplete(
45             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) m) {
46         this._messageComplete = m;
47         return this;
48     }
49 
50     ServerHttpHandlerAdapter content(
51             Func5!(ByteBuffer, HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) c) {
52         this._content = c;
53         return this;
54     }
55 
56     ServerHttpHandlerAdapter contentComplete(
57             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) c) {
58         this._contentComplete = c;
59         return this;
60     }
61 
62     ServerHttpHandlerAdapter badMessage(
63             Action6!(int, string, HttpRequest, HttpResponse, HttpOutputStream, HttpConnection) b) {
64         this._badMessage = b;
65         return this;
66     }
67 
68     ServerHttpHandlerAdapter earlyEOF(
69             Action4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection) e) {
70         this._earlyEOF = e;
71         return this;
72     }
73 
74     ServerHttpHandlerAdapter acceptConnection(Action1!HttpConnection c) {
75         this._acceptConnection = c;
76         return this;
77     }
78 
79     ServerHttpHandlerAdapter accept100Continue(
80             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) a) {
81         this._accept100Continue = a;
82         return this;
83     }
84 
85     ServerHttpHandlerAdapter acceptHttpTunnelConnection(
86             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpServerConnection, bool) a) {
87         this._acceptHttpTunnelConnection = a;
88         return this;
89     }
90 
91     override
92     void acceptConnection(HttpConnection connection) {
93         if (_acceptConnection != null) {
94             _acceptConnection(connection);
95         }
96     }
97 
98     override
99     bool accept100Continue(HttpRequest request, HttpResponse response, HttpOutputStream output,
100                                         HttpConnection connection) {
101         if (_accept100Continue !is null) {
102             return _accept100Continue(request, response, output, connection);
103         } else {
104             return false;
105         }
106     }
107 
108     override
109     bool acceptHttpTunnelConnection(HttpRequest request, HttpResponse response,
110                                                 HttpOutputStream output,
111                                                 HttpServerConnection connection) {
112         if (_acceptHttpTunnelConnection != null) {
113             return _acceptHttpTunnelConnection(request, response, output, connection);
114         } else {
115             return true;
116         }
117     }
118 
119 }