1 module hunt.http.server.ServerHttpHandler;
2 
3 import hunt.http.server.HttpServerConnection;
4 import hunt.http.server.HttpServerOptions;
5 
6 import hunt.http.codec.http.stream.HttpHandler;
7 import hunt.http.HttpOutputStream;
8 import hunt.http.codec.http.stream.HttpTunnelConnection;
9 
10 import hunt.http.HttpConnection;
11 import hunt.http.HttpRequest;
12 import hunt.http.HttpResponse;
13 
14 import hunt.io.ByteBuffer;
15 import hunt.Functions;
16 
17 
18 /**
19  * 
20  */
21 interface ServerHttpHandler : HttpHandler {
22 
23     void acceptConnection(HttpConnection connection);
24 
25     bool accept100Continue(HttpRequest request, HttpResponse response,
26                               HttpOutputStream output,
27                               HttpConnection connection);
28 
29     bool acceptHttpTunnelConnection(HttpRequest request, HttpResponse response,
30                                        HttpOutputStream output,
31                                        HttpServerConnection connection);
32 
33 }
34 
35 // deprecated("Using AbstractServerHttpHandler instead.")
36 // alias ServerHttpHandlerAdapter = AbstractServerHttpHandler;
37 
38 /**
39  * 
40  */
41 class ServerHttpHandlerAdapter : AbstractHttpHandler, ServerHttpHandler {
42 
43     private HttpServerOptions _httpOptions;
44 
45     protected Action1!HttpConnection _acceptConnection;
46     protected Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) _accept100Continue;
47     protected Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpServerConnection, bool) _acceptHttpTunnelConnection;
48 
49     this(HttpServerOptions options) {
50         _httpOptions = options;
51     }
52 
53     HttpServerOptions serverOptions() {
54         return _httpOptions;
55     }
56 
57     // FIXME: Needing refactor or cleanup -@zhangxueping at 2019-10-16T11:46:06+08:00
58     // refactor to use HttpServerContext 
59     ServerHttpHandlerAdapter headerComplete(
60             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) h) {
61         this._headerComplete = h;
62         return this;
63     }
64 
65     ServerHttpHandlerAdapter messageComplete(
66             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) m) {
67         this._messageComplete = m;
68         return this;
69     }
70 
71     ServerHttpHandlerAdapter content(
72             Func5!(ByteBuffer, HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) c) {
73         this._content = c;
74         return this;
75     }
76 
77     ServerHttpHandlerAdapter contentComplete(
78             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) c) {
79         this._contentComplete = c;
80         return this;
81     }
82 
83     ServerHttpHandlerAdapter badMessage(
84             Action6!(int, string, HttpRequest, HttpResponse, HttpOutputStream, HttpConnection) b) {
85         this._badMessage = b;
86         return this;
87     }
88 
89     ServerHttpHandlerAdapter earlyEOF(
90             Action4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection) e) {
91         this._earlyEOF = e;
92         return this;
93     }
94 
95     ServerHttpHandlerAdapter acceptConnection(Action1!HttpConnection c) {
96         this._acceptConnection = c;
97         return this;
98     }
99 
100     ServerHttpHandlerAdapter accept100Continue(
101             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpConnection, bool) a) {
102         this._accept100Continue = a;
103         return this;
104     }
105 
106     ServerHttpHandlerAdapter acceptHttpTunnelConnection(
107             Func4!(HttpRequest, HttpResponse, HttpOutputStream, HttpServerConnection, bool) a) {
108         this._acceptHttpTunnelConnection = a;
109         return this;
110     }
111 
112     override
113     void acceptConnection(HttpConnection connection) {
114         if (_acceptConnection != null) {
115             _acceptConnection(connection);
116         }
117     }
118 
119     override
120     bool accept100Continue(HttpRequest request, HttpResponse response, HttpOutputStream output,
121                                         HttpConnection connection) {
122         if (_accept100Continue !is null) {
123             return _accept100Continue(request, response, output, connection);
124         } else {
125             return false;
126         }
127     }
128 
129     override
130     bool acceptHttpTunnelConnection(HttpRequest request, HttpResponse response,
131                                                 HttpOutputStream output,
132                                                 HttpServerConnection connection) {
133         if (_acceptHttpTunnelConnection != null) {
134             return _acceptHttpTunnelConnection(request, response, output, connection);
135         } else {
136             return true;
137         }
138     }
139 
140 }