forked from robaho/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHeaderParser.java
More file actions
390 lines (355 loc) · 14.5 KB
/
HttpHeaderParser.java
File metadata and controls
390 lines (355 loc) · 14.5 KB
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
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.test.lib.net;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static java.util.Objects.requireNonNull;
public final class HttpHeaderParser {
private static final char CR = '\r';
private static final char LF = '\n';
private static final char HT = '\t';
private static final char SP = ' ';
// ABNF primitives defined in RFC 7230
private static boolean[] tchar = new boolean[256];
private static boolean[] fieldvchar = new boolean[256];
static {
char[] allowedTokenChars =
("!#$%&'*+-.^_`|~0123456789" +
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
for (char c : allowedTokenChars) {
tchar[c] = true;
}
for (char c = 0x21; c <= 0xFF; c++) {
fieldvchar[c] = true;
}
fieldvchar[0x7F] = false; // a little hole (DEL) in the range
}
private StringBuilder sb = new StringBuilder();
private Map <String, List<String>> headerMap = new LinkedHashMap<>();
private List <String> keyList = new ArrayList<>();
private String requestOrStatusLine;
private int responseCode;
private boolean eof;
enum State { INITIAL,
STATUS_OR_REQUEST_LINE,
STATUS_OR_REQUEST_LINE_FOUND_CR,
STATUS_OR_REQUEST_LINE_FOUND_LF,
STATUS_OR_REQUEST_LINE_END,
STATUS_OR_REQUEST_LINE_END_CR,
STATUS_OR_REQUEST_LINE_END_LF,
HEADER,
HEADER_FOUND_CR,
HEADER_FOUND_LF,
HEADER_FOUND_CR_LF,
HEADER_FOUND_CR_LF_CR,
FINISHED }
private HttpHeaderParser.State state = HttpHeaderParser.State.INITIAL;
public HttpHeaderParser() {
}
public HttpHeaderParser(InputStream is) throws IOException, ProtocolException {
parse(is);
}
public Map<String, List<String>> getHeaderMap() {
return headerMap;
}
public List<String> getHeaderValue(String key) {
if(headerMap.containsKey(key.toLowerCase(Locale.ROOT))) {
return headerMap.get(key.toLowerCase(Locale.ROOT));
}
return null;
}
public List<String> getValue(int id) {
String key = keyList.get(id);
return headerMap.get(key);
}
public String getRequestDetails() {
return requestOrStatusLine;
}
/**
* Parses HTTP/1.X status-line or request-line and headers from the given input stream.
* @param input Containing the input stream of bytes representing request or response header data
* @return true if the end of the headers block has been reached
*/
public boolean parse(InputStream input) throws IOException {
requireNonNull(input, "null input");
while (canContinueParsing()) {
switch (state) {
case INITIAL -> state = HttpHeaderParser.State.STATUS_OR_REQUEST_LINE;
case STATUS_OR_REQUEST_LINE -> readResumeStatusLine(input);
case STATUS_OR_REQUEST_LINE_FOUND_CR, STATUS_OR_REQUEST_LINE_FOUND_LF -> readStatusLineFeed(input);
case STATUS_OR_REQUEST_LINE_END -> maybeStartHeaders(input);
case STATUS_OR_REQUEST_LINE_END_CR, STATUS_OR_REQUEST_LINE_END_LF -> maybeEndHeaders(input);
case HEADER -> readResumeHeader(input);
case HEADER_FOUND_CR, HEADER_FOUND_LF -> resumeOrLF(input);
case HEADER_FOUND_CR_LF -> resumeOrSecondCR(input);
case HEADER_FOUND_CR_LF_CR -> resumeOrEndHeaders(input);
default -> throw new InternalError("Unexpected state: " + state);
}
}
return state == HttpHeaderParser.State.FINISHED;
}
private boolean canContinueParsing() {
// some states don't require any input to transition
// to the next state.
return switch (state) {
case FINISHED -> false;
case STATUS_OR_REQUEST_LINE_FOUND_LF, STATUS_OR_REQUEST_LINE_END_LF, HEADER_FOUND_LF -> true;
default -> !eof;
};
}
/**
* Returns a character (char) corresponding to the next byte in the
* input, interpreted as an ISO-8859-1 encoded character.
* <p>
* The ISO-8859-1 encoding is a 8-bit character coding that
* corresponds to the first 256 Unicode characters - from U+0000 to
* U+00FF. UTF-16 is backward compatible with ISO-8859-1 - which
* means each byte in the input should be interpreted as an unsigned
* value from [0, 255] representing the character code.
*
* @param input a {@code InputStream} containing input stream of Bytes.
* @return the next byte in the input, interpreted as an ISO-8859-1
* encoded char
* @throws IOException
* if an I/O error occurs.
*/
private char get(InputStream input) throws IOException {
int c = input.read();
if(c < 0)
eof = true;
return (char)(c & 0xFF);
}
private void readResumeStatusLine(InputStream input) throws IOException {
char c;
while ((c = get(input)) != CR && !eof) {
if (c == LF) break;
sb.append(c);
}
if (c == CR) {
state = HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_FOUND_CR;
} else if (c == LF) {
state = HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_FOUND_LF;
}
}
private void readStatusLineFeed(InputStream input) throws IOException {
char c = state == HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_FOUND_LF ? LF : get(input);
if (c != LF) {
throw protocolException("Bad trailing char, \"%s\", when parsing status line, \"%s\"",
c, sb.toString());
}
requestOrStatusLine = sb.toString();
sb = new StringBuilder();
if (!requestOrStatusLine.startsWith("HTTP/1.")) {
if(!requestOrStatusLine.startsWith("GET") && !requestOrStatusLine.startsWith("POST") &&
!requestOrStatusLine.startsWith("PUT") && !requestOrStatusLine.startsWith("DELETE") &&
!requestOrStatusLine.startsWith("OPTIONS") && !requestOrStatusLine.startsWith("HEAD") &&
!requestOrStatusLine.startsWith("PATCH") && !requestOrStatusLine.startsWith("CONNECT")) {
throw protocolException("Invalid request Or Status line: \"%s\"", requestOrStatusLine);
} else { //This is request
System.out.println("Request is :"+requestOrStatusLine);
}
} else { //This is response
if (requestOrStatusLine.length() < 12) {
throw protocolException("Invalid status line: \"%s\"", requestOrStatusLine);
}
try {
responseCode = Integer.parseInt(requestOrStatusLine.substring(9, 12));
} catch (NumberFormatException nfe) {
throw protocolException("Invalid status line: \"%s\"", requestOrStatusLine);
}
// response code expected to be a 3-digit integer (RFC-2616, section 6.1.1)
if (responseCode < 100) {
throw protocolException("Invalid status line: \"%s\"", requestOrStatusLine);
}
}
state = HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_END;
}
private void maybeStartHeaders(InputStream input) throws IOException {
assert state == HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_END;
assert sb.length() == 0;
char c = get(input);
if(!eof) {
if (c == CR) {
state = HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_END_CR;
} else if (c == LF) {
state = HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_END_LF;
} else {
sb.append(c);
state = HttpHeaderParser.State.HEADER;
}
}
}
private void maybeEndHeaders(InputStream input) throws IOException {
assert state == HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_END_CR || state == HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_END_LF;
assert sb.length() == 0;
char c = state == HttpHeaderParser.State.STATUS_OR_REQUEST_LINE_END_LF ? LF : get(input);
if (c == LF) {
state = HttpHeaderParser.State.FINISHED; // no headers
} else {
throw protocolException("Unexpected \"%s\", after status line CR", c);
}
}
private void readResumeHeader(InputStream input) throws IOException {
assert state == HttpHeaderParser.State.HEADER;
assert !eof;
char c = get(input);
while (!eof) {
if (c == CR) {
state = HttpHeaderParser.State.HEADER_FOUND_CR;
break;
} else if (c == LF) {
state = HttpHeaderParser.State.HEADER_FOUND_LF;
break;
}
if (c == HT)
c = SP;
sb.append(c);
c = get(input);
}
}
private void addHeaderFromString(String headerString) throws ProtocolException {
assert sb.length() == 0;
int idx = headerString.indexOf(':');
if (idx == -1)
return;
String name = headerString.substring(0, idx);
// compatibility with HttpURLConnection;
if (name.isEmpty()) return;
if (!isValidName(name)) {
throw protocolException("Invalid header name \"%s\"", name);
}
String value = headerString.substring(idx + 1).trim();
if (!isValidValue(value)) {
throw protocolException("Invalid header value \"%s: %s\"", name, value);
}
keyList.add(name);
headerMap.computeIfAbsent(name.toLowerCase(Locale.US),
k -> new ArrayList<>()).add(value);
}
private void resumeOrLF(InputStream input) throws IOException {
assert state == HttpHeaderParser.State.HEADER_FOUND_CR || state == HttpHeaderParser.State.HEADER_FOUND_LF;
char c = state == HttpHeaderParser.State.HEADER_FOUND_LF ? LF : get(input);
if (!eof) {
if (c == LF) {
state = HttpHeaderParser.State.HEADER_FOUND_CR_LF;
} else if (c == SP || c == HT) {
sb.append(SP); // parity with MessageHeaders
state = HttpHeaderParser.State.HEADER;
} else {
sb = new StringBuilder();
sb.append(c);
state = HttpHeaderParser.State.HEADER;
}
}
}
private void resumeOrSecondCR(InputStream input) throws IOException {
assert state == HttpHeaderParser.State.HEADER_FOUND_CR_LF;
char c = get(input);
if (!eof) {
if (c == CR || c == LF) {
if (sb.length() > 0) {
// no continuation line - flush
// previous header value.
String headerString = sb.toString();
sb = new StringBuilder();
addHeaderFromString(headerString);
}
if (c == CR) {
state = HttpHeaderParser.State.HEADER_FOUND_CR_LF_CR;
} else {
state = HttpHeaderParser.State.FINISHED;
}
} else if (c == SP || c == HT) {
assert sb.length() != 0;
sb.append(SP); // continuation line
state = HttpHeaderParser.State.HEADER;
} else {
if (sb.length() > 0) {
// no continuation line - flush
// previous header value.
String headerString = sb.toString();
sb = new StringBuilder();
addHeaderFromString(headerString);
}
sb.append(c);
state = HttpHeaderParser.State.HEADER;
}
}
}
private void resumeOrEndHeaders(InputStream input) throws IOException {
assert state == HttpHeaderParser.State.HEADER_FOUND_CR_LF_CR;
char c = get(input);
if (!eof) {
if (c == LF) {
state = HttpHeaderParser.State.FINISHED;
} else {
throw protocolException("Unexpected \"%s\", after CR LF CR", c);
}
}
}
private ProtocolException protocolException(String format, Object ... args) {
return new ProtocolException(String.format(format, args));
}
/*
* Validates a RFC 7230 field-name.
*/
public boolean isValidName(String token) {
for (int i = 0; i < token.length(); i++) {
char c = token.charAt(i);
if (c > 255 || !tchar[c]) {
return false;
}
}
return !token.isEmpty();
}
/*
* Validates a RFC 7230 field-value.
*
* "Obsolete line folding" rule
*
* obs-fold = CRLF 1*( SP / HTAB )
*
* is not permitted!
*/
public boolean isValidValue(String token) {
for (int i = 0; i < token.length(); i++) {
char c = token.charAt(i);
if (c > 255) {
return false;
}
if (c == ' ' || c == '\t') {
continue;
} else if (!fieldvchar[c]) {
return false; // forbidden byte
}
}
return true;
}
}