/
HttpIO.java
326 lines (248 loc) · 8.92 KB
/
HttpIO.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
package org.rapidoid.http.impl;
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.buffer.Buf;
import org.rapidoid.commons.Dates;
import org.rapidoid.config.Conf;
import org.rapidoid.config.Config;
import org.rapidoid.ctx.Ctx;
import org.rapidoid.ctx.Ctxs;
import org.rapidoid.ctx.With;
import org.rapidoid.data.BufRange;
import org.rapidoid.data.JSON;
import org.rapidoid.http.*;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.job.Jobs;
import org.rapidoid.log.Log;
import org.rapidoid.log.LogLevel;
import org.rapidoid.net.abstracts.Channel;
import org.rapidoid.u.U;
import org.rapidoid.util.Constants;
import org.rapidoid.util.GlobalCfg;
import org.rapidoid.util.Msc;
import org.rapidoid.util.StreamUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2016 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("5.1.0")
public class HttpIO extends RapidoidThing implements Constants {
public static final byte[] HTTP_200_OK = "HTTP/1.1 200 OK\r\n".getBytes();
public static final byte[] HTTP_400_BAD_REQUEST = "HTTP/1.1 400 Bad Request\r\nContent-Length: 12\r\n\r\nBad Request!"
.getBytes();
private static final byte[] HEADER_SEP = ": ".getBytes();
private static final byte[] CONN_KEEP_ALIVE = "Connection: keep-alive\r\n".getBytes();
private static final byte[] CONN_CLOSE = "Connection: close\r\n".getBytes();
private static final byte[] SERVER_HEADER;
private static final byte[] CONTENT_LENGTH_IS = "Content-Length: ".getBytes();
static final byte[] CONTENT_LENGTH_UNKNOWN = "Content-Length: 0000000000".getBytes();
private static final int CONTENT_LENGTHS_SIZE = 5000;
private static final byte[] DATE_IS = "Date: ".getBytes();
private static final byte[][] CONTENT_LENGTHS = new byte[CONTENT_LENGTHS_SIZE][];
private static final boolean MANDATORY_HEADER_CONNECTION;
private static final boolean MANDATORY_HEADER_DATE;
private static final boolean MANDATORY_HEADER_SERVER;
private static final boolean MANDATORY_HEADER_CONTENT_TYPE;
private static final byte[] UNIFORM_DATE = "Sat, 10 Sep 2016 01:02:03 GMT".getBytes();
static {
for (int len = 0; len < CONTENT_LENGTHS.length; len++) {
CONTENT_LENGTHS[len] = (new String(CONTENT_LENGTH_IS) + len + new String(CR_LF)).getBytes();
}
HttpResponseCodes.init();
String serverName = Conf.HTTP.entry("serverName").or("Rapidoid");
SERVER_HEADER = ("Server: " + serverName + "\r\n").getBytes();
Config mandatoryHeaders = Conf.HTTP.sub("mandatoryHeaders");
MANDATORY_HEADER_CONNECTION = mandatoryHeaders.entry("connection").or(true);
MANDATORY_HEADER_DATE = mandatoryHeaders.entry("date").or(true);
MANDATORY_HEADER_SERVER = mandatoryHeaders.entry("server").or(true);
MANDATORY_HEADER_CONTENT_TYPE = mandatoryHeaders.entry("contentType").or(true);
}
private HttpIO() {
}
public static void removeTrailingSlash(Buf buf, BufRange range) {
if (range.length > 1 && buf.get(range.last()) == '/') {
range.length--;
}
}
public static void startResponse(Channel ctx, int code, boolean isKeepAlive, MediaType contentType) {
ctx.write(code == 200 ? HTTP_200_OK : HttpResponseCodes.get(code));
addDefaultHeaders(ctx, isKeepAlive, contentType);
}
public static void addDefaultHeaders(Channel ctx, boolean isKeepAlive, MediaType contentType) {
if (!isKeepAlive || MANDATORY_HEADER_CONNECTION) {
ctx.write(isKeepAlive ? CONN_KEEP_ALIVE : CONN_CLOSE);
}
if (MANDATORY_HEADER_SERVER) {
ctx.write(SERVER_HEADER);
}
if (MANDATORY_HEADER_DATE) {
ctx.write(DATE_IS);
if (!GlobalCfg.uniformOutput()) {
ctx.write(Dates.getDateTimeBytes());
} else {
ctx.write(UNIFORM_DATE);
}
ctx.write(CR_LF);
}
if (MANDATORY_HEADER_CONTENT_TYPE) {
ctx.write(contentType.asHttpHeader());
}
}
public static void addCustomHeader(Channel ctx, byte[] name, byte[] value) {
ctx.write(name);
ctx.write(HEADER_SEP);
ctx.write(value);
ctx.write(CR_LF);
}
public static void writeResponse(Channel ctx, boolean isKeepAlive, int code, MediaType contentTypeHeader, byte[] content) {
startResponse(ctx, code, isKeepAlive, contentTypeHeader);
writeContentLengthAndBody(ctx, content);
}
public static void write200(Channel ctx, boolean isKeepAlive, MediaType contentTypeHeader, byte[] content) {
writeResponse(ctx, isKeepAlive, 200, contentTypeHeader, content);
}
public static void error(final Req req, final Throwable error, LogLevel logLevel) {
try {
logError(req, error, logLevel);
Resp resp = req.response().code(500).result(null);
Object result = Customization.of(req).errorHandler().handleError(req, resp, error);
result = HttpUtils.postprocessResult(req, result);
HttpUtils.resultToResponse(req, result);
} catch (Exception e) {
Log.error("An error occurred inside the error handler!", e);
HttpUtils.resultToResponse(req, HttpUtils.getErrorInfo(req.response(), e));
}
}
private static void logError(Req req, Throwable error, LogLevel logLevel) {
if (error instanceof NotFound) return;
if (Msc.isValidationError(error)) {
if (Log.isDebugEnabled()) {
Log.debug("Validation error when handling request: " + req);
error.printStackTrace();
}
return;
}
if (error instanceof SecurityException) {
Log.warn("Access denied for request: " + req, "client", req.clientIpAddress());
return;
}
String msg = "Error occurred when handling request!";
switch (logLevel) {
// FIXME add proper support e.g. Log.msg(logLevel...)
case TRACE:
Log.trace(msg, "error", error);
break;
case DEBUG:
Log.debug(msg, "error", error);
break;
case INFO:
Log.info(msg, "error", error);
break;
case WARN:
Log.warn(msg, "error", error);
break;
case ERROR:
Log.error(msg, "error", error);
break;
}
}
public static HttpStatus errorAndDone(final Req req, final Throwable error, final LogLevel logLevel) {
req.revert();
req.async();
Runnable errorHandler = new Runnable() {
@Override
public void run() {
error(req, error, logLevel);
// the Req object will do the rendering
req.done();
}
};
Ctx ctx = Ctxs.get();
if (ctx == null) {
With.exchange(req).run(errorHandler);
} else {
Jobs.execute(errorHandler);
}
return HttpStatus.ASYNC;
}
public static void writeContentLengthAndBody(Channel ctx, byte[] content) {
writeContentLengthHeader(ctx, content.length);
ctx.write(CR_LF);
ctx.write(content);
}
public static void writeContentLengthAndBody(Channel ctx, ByteArrayOutputStream baos) {
writeContentLengthHeader(ctx, baos.size());
ctx.write(CR_LF);
ctx.output().append(baos);
}
public static void writeContentLengthHeader(Channel ctx, int len) {
if (len < CONTENT_LENGTHS_SIZE) {
ctx.write(CONTENT_LENGTHS[len]);
} else {
ctx.write(CONTENT_LENGTH_IS);
Buf out = ctx.output();
out.putNumAsText(out.size(), len, true);
ctx.write(CR_LF);
}
}
public static void writeAsJson(Channel ctx, int code, boolean isKeepAlive, Object value) {
startResponse(ctx, code, isKeepAlive, MediaType.JSON);
ByteArrayOutputStream os = Msc.locals().jsonRenderingStream();
JSON.stringify(value, os);
byte[] arr = os.toByteArray();
writeContentLengthAndBody(ctx, arr);
}
@SuppressWarnings("unused")
private static void writeOnBufferAsJson(Channel ctx, int code, boolean isKeepAlive, Object value) {
startResponse(ctx, code, isKeepAlive, MediaType.JSON);
Buf output = ctx.output();
synchronized (output) {
writeJsonBody(output.unwrap(), value);
}
}
public static void writeJsonBody(Buf out, Object value) {
out.append(CONTENT_LENGTH_UNKNOWN);
int posConLen = out.size() - 1;
out.append(CR_LF);
// finishing the headers
out.append(CR_LF);
int posBefore = out.size();
JSON.stringify(value, out.asOutputStream());
int posAfter = out.size();
int contentLength = posAfter - posBefore;
out.putNumAsText(posConLen, contentLength, false);
}
public static void writeContentLengthUnknown(Channel channel) {
channel.write(HttpIO.CONTENT_LENGTH_UNKNOWN);
}
public static void done(Channel ctx, boolean isKeepAlive) {
ctx.done();
ctx.closeIf(!isKeepAlive);
}
public static void writeNum(Channel ctx, int value) {
try {
StreamUtils.putNumAsText(ctx.output().asOutputStream(), value);
} catch (IOException e) {
throw U.rte(e);
}
}
}