/
FastHttp.java
397 lines (306 loc) · 10.8 KB
/
FastHttp.java
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package org.rapidoid.http;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.buffer.Buf;
import org.rapidoid.bytes.BytesUtil;
import org.rapidoid.cache.Cache;
import org.rapidoid.collection.Coll;
import org.rapidoid.config.Config;
import org.rapidoid.config.ConfigImpl;
import org.rapidoid.data.BufRange;
import org.rapidoid.data.KeyValueRanges;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.customize.JsonResponseRenderer;
import org.rapidoid.http.handler.HttpHandler;
import org.rapidoid.http.impl.*;
import org.rapidoid.http.impl.lowlevel.HttpIO;
import org.rapidoid.http.processor.AbstractHttpProcessor;
import org.rapidoid.io.Upload;
import org.rapidoid.log.Log;
import org.rapidoid.log.LogLevel;
import org.rapidoid.net.abstracts.Channel;
import org.rapidoid.net.impl.RapidoidHelper;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
@Authors("Nikolche Mihajlovski")
@Since("4.3.0")
public class FastHttp extends AbstractHttpProcessor {
private static final HttpParser HTTP_PARSER = new HttpParser();
private static final String INTERNAL_SERVER_ERROR = "Internal Server Error!";
private final HttpRoutesGroup routesGroup;
private final Map<String, Object> attributes = Coll.synchronizedMap();
public FastHttp(HttpRoutesImpl... routeGroups) {
this(new HttpRoutesGroup(routeGroups), new ConfigImpl());
}
public FastHttp(HttpRoutesGroup routesGroup, Config serverConfig) {
super(null);
this.routesGroup = routesGroup;
}
@Override
public void onRequest(Channel channel, RapidoidHelper data) {
Buf buf = channel.input();
boolean isGet = data.isGet.value;
boolean isKeepAlive = data.isKeepAlive.value;
BufRange verb = data.verb;
BufRange uri = data.uri;
BufRange path = data.path;
HttpIO.INSTANCE.removeTrailingSlash(buf, path);
HttpIO.INSTANCE.removeTrailingSlash(buf, uri);
String err = validateRequest(buf, verb, uri);
if (err != null) {
HttpIO.INSTANCE.writeBadRequest(channel);
return;
}
HttpStatus status = HttpStatus.NOT_FOUND;
HttpRoutesImpl matchingRoutes = null;
Route matchingRoute = null;
HandlerMatch match = null;
for (HttpRoutesImpl r : routesGroup.all()) {
match = r.findHandler(buf, isGet, verb, path);
if (match != null) {
matchingRoutes = r;
matchingRoute = match.getRoute();
break;
}
}
if (match == null && isGet) {
for (HttpRoutesImpl r : routesGroup.all()) {
match = r.staticResourcesHandler();
if (match != null) {
matchingRoutes = r;
matchingRoute = match.getRoute();
break;
}
}
}
HttpHandler handler = match != null ? match.getHandler() : null;
boolean noReq = (handler != null && !handler.needsParams());
ReqImpl req = null;
if (!noReq) {
req = createReq(channel, isGet, isKeepAlive, data, buf, matchingRoutes, matchingRoute, match, handler);
if (serveFromCache(req)) return;
}
try {
if (handler != null) {
status = handleIfFound(channel, isKeepAlive, handler, req);
}
if (status == HttpStatus.NOT_FOUND) {
status = tryGenericHandlers(channel, isKeepAlive, req);
}
} catch (Throwable e) {
if (handleError(channel, isKeepAlive, req, e)) return;
}
if (status == HttpStatus.NOT_FOUND) {
handleNotFound(channel, isKeepAlive, req);
return;
}
if (status != HttpStatus.ASYNC) {
channel.closeIf(!isKeepAlive);
}
}
@Override
public void waitToInitialize() {
routesGroup.waitToInitialize();
}
private boolean serveFromCache(ReqImpl req) {
// if the HTTP request is not cacheable, the cache key will be null
HTTPCacheKey cacheKey = req.cacheKey();
Route route = req.route();
if (route != null) {
Cache<HTTPCacheKey, CachedResp> cache = route.cache();
if (cache != null) {
if (cacheKey != null) {
CachedResp resp = cache.getIfExists(cacheKey);
if (resp != null) {
serveCached(req, resp);
return true;
}
} else {
cache.bypass(); // notify it's not cacheable
}
}
}
return false;
}
private void serveCached(ReqImpl req, CachedResp resp) {
Channel channel = req.channel();
req.cached(true);
HttpIO.INSTANCE.respond(HttpUtils.req(req), channel, -1, resp.statusCode,
req.isKeepAlive(), resp.contentType, resp.body.duplicate(), resp.headers, null);
channel.send().closeIf(!req.isKeepAlive());
}
@SuppressWarnings("unchecked")
public ReqImpl createReq(Channel channel, boolean isGet, boolean isKeepAlive,
RapidoidHelper helper, Buf buf, HttpRoutesImpl matchingRoutes,
Route matchingRoute, HandlerMatch match, HttpHandler handler) {
ReqImpl req;
KeyValueRanges paramsKV = helper.params.reset();
KeyValueRanges headersKV = helper.headersKV.reset();
KeyValueRanges cookiesKV = helper.cookies.reset();
HTTP_PARSER.parseParams(buf, paramsKV, helper.query);
Map<String, String> params = U.cast(paramsKV.toMap(buf, true, true, false));
if (match != null && match.getParams() != null) {
params.putAll(match.getParams());
}
HTTP_PARSER.parseHeadersIntoKV(buf, helper.headers, headersKV, cookiesKV, helper);
Map<String, String> headers = U.cast(headersKV.toMap(buf, false, false, true));
Map<String, String> cookies = U.cast(cookiesKV.toMap(buf, false, false, false));
byte[] body;
Map<String, Object> posted;
Map<String, List<Upload>> files;
boolean pendingBodyParsing = false;
if (!isGet && !helper.body.isEmpty()) {
KeyValueRanges postedKV = helper.pairs3.reset();
body = helper.body.bytes(buf);
// parse posted body as data
posted = U.map();
files = U.map();
pendingBodyParsing = !HTTP_PARSER.parsePosted(buf, headersKV, helper.body, postedKV, files, helper, posted);
posted = Collections.synchronizedMap(posted);
files = Collections.synchronizedMap(files);
} else {
posted = Collections.EMPTY_MAP;
files = Collections.EMPTY_MAP;
body = null;
}
String verb = helper.verb.str(buf);
String uri = helper.uri.str(buf);
String path = Msc.urlDecode(helper.path.str(buf));
String query = Msc.urlDecodeOrKeepOriginal(helper.query.str(buf));
String zone = null;
MediaType contentType = HttpUtils.getDefaultContentType();
if (handler != null) {
contentType = handler.contentType();
zone = handler.options().zone();
}
zone = U.or(zone, "main");
params = Collections.synchronizedMap(params);
headers = Collections.synchronizedMap(headers);
cookies = Collections.synchronizedMap(cookies);
req = new ReqImpl(this, channel, isKeepAlive, verb, uri, path, query, body, params, headers, cookies,
posted, files, pendingBodyParsing, contentType, zone, matchingRoutes, matchingRoute);
if (!attributes.isEmpty()) {
req.attrs().putAll(attributes);
}
channel.setRequest(req);
return req;
}
private HttpStatus handleIfFound(Channel channel, boolean isKeepAlive, HttpHandler handler, ReqImpl req) {
try {
return handler.handle(channel, isKeepAlive, req, null);
} catch (NotFound nf) {
return HttpStatus.NOT_FOUND;
}
}
private void internalServerError(final Channel channel, final boolean isKeepAlive, final Req req) {
MediaType contentType = req != null ? req.contentType() : HttpUtils.getDefaultContentType();
JsonResponseRenderer jsonRenderer = Customization.of(req).jsonResponseRenderer();
byte[] body = HttpUtils.responseToBytes(req, INTERNAL_SERVER_ERROR, contentType, jsonRenderer);
HttpIO.INSTANCE.respond(HttpUtils.maybe(req), channel, -1, 500, isKeepAlive, contentType, body, null, null);
}
private boolean handleError(Channel channel, boolean isKeepAlive, Req req, Throwable e) {
if (req != null) {
if (!((ReqImpl) req).isStopped()) {
try {
HttpIO.INSTANCE.errorAndDone(req, e, LogLevel.ERROR);
} catch (Exception e1) {
Log.error("HTTP error handler error!", e1);
internalServerError(channel, isKeepAlive, req);
}
}
return true;
} else {
Log.error("Low-level HTTP handler error!", e);
internalServerError(channel, isKeepAlive, null);
}
return false;
}
private void handleNotFound(Channel channel, boolean isKeepAlive, Req req) {
handleError(channel, isKeepAlive, req, new NotFound());
}
public Customization custom() {
return routesGroup.customization();
}
private String validateRequest(Buf input, BufRange verb, BufRange uri) {
if (verb.isEmpty()) {
return "HTTP verb cannot be empty!";
}
if (!BytesUtil.isValidURI(input.bytes(), uri)) {
return "Invalid HTTP URI!";
}
return null; // OK, no error
}
private HttpStatus tryGenericHandlers(Channel channel, boolean isKeepAlive, ReqImpl req) {
for (HttpRoutesImpl routes : routesGroup.all()) {
// trying with different routes
req.routes(routes);
for (HttpHandler handler : routes.genericHandlers()) {
HttpStatus status = handleIfFound(channel, isKeepAlive, handler, req);
if (status != HttpStatus.NOT_FOUND) {
return status;
}
}
}
req.routes(null);
return HttpStatus.NOT_FOUND;
}
public synchronized void resetConfig() {
routesGroup.reset();
}
public void notFound(Channel ctx, boolean isKeepAlive, MediaType contentType, HttpHandler fromHandler, Req req) {
HttpStatus status = HttpStatus.NOT_FOUND;
tryRoutes:
for (HttpRoutesImpl route : routesGroup.all()) {
List<HttpHandler> genericHandlers = route.genericHandlers();
int count = genericHandlers.size();
for (int i = 0; i < count; i++) {
HttpHandler handler = genericHandlers.get(i);
if (handler == fromHandler) {
// a generic handler returned "not found" -> go to the next one
if (i < count - 1) {
// trying with different routes
ReqImpl reqi = (ReqImpl) req;
reqi.routes(route);
HttpHandler nextHandler = genericHandlers.get(i + 1);
status = handleIfFound(ctx, isKeepAlive, nextHandler, reqi);
break tryRoutes;
}
}
}
}
if (status == HttpStatus.NOT_FOUND) {
handleNotFound(ctx, isKeepAlive, req);
}
}
public Map<String, Object> attributes() {
return attributes;
}
public HttpRoutesGroup routes() {
return routesGroup;
}
public boolean hasRouteOrResource(HttpVerb verb, String uri) {
return routesGroup.hasRouteOrResource(verb, uri);
}
}