author | dfuchs |
Thu, 24 May 2018 11:31:50 +0100 | |
branch | http-client-branch |
changeset 56601 | e38ce7e04995 |
parent 56451 | 9585061fdb04 |
child 56795 | 03ece2518428 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
49765 | 24 |
package jdk.internal.net.http; |
48083 | 25 |
|
26 |
import java.io.ByteArrayInputStream; |
|
27 |
import java.net.ProtocolException; |
|
28 |
import java.nio.ByteBuffer; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.Arrays; |
|
31 |
import java.util.Collections; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.List; |
|
34 |
import java.util.Map; |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
35 |
import java.util.concurrent.atomic.AtomicBoolean; |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
36 |
import java.util.concurrent.atomic.AtomicInteger; |
48083 | 37 |
import java.util.stream.IntStream; |
38 |
import sun.net.www.MessageHeader; |
|
39 |
import org.testng.annotations.Test; |
|
40 |
import org.testng.annotations.DataProvider; |
|
41 |
import static java.lang.System.out; |
|
42 |
import static java.lang.String.format; |
|
43 |
import static java.nio.charset.StandardCharsets.US_ASCII; |
|
44 |
import static java.util.stream.Collectors.toList; |
|
45 |
import static org.testng.Assert.*; |
|
46 |
||
47 |
// Mostly verifies the "new" Http1HeaderParser returns the same results as the |
|
48 |
// tried and tested sun.net.www.MessageHeader. |
|
49 |
||
50 |
public class Http1HeaderParserTest { |
|
51 |
||
52 |
@DataProvider(name = "responses") |
|
53 |
public Object[][] responses() { |
|
54 |
List<String> responses = new ArrayList<>(); |
|
55 |
||
56 |
String[] basic = |
|
57 |
{ "HTTP/1.1 200 OK\r\n\r\n", |
|
58 |
||
59 |
"HTTP/1.1 200 OK\r\n" + |
|
60 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
|
61 |
"Server: Apache/1.3.14 (Unix)\r\n" + |
|
62 |
"Connection: close\r\n" + |
|
63 |
"Content-Type: text/html; charset=iso-8859-1\r\n" + |
|
64 |
"Content-Length: 10\r\n\r\n" + |
|
65 |
"123456789", |
|
66 |
||
67 |
"HTTP/1.1 200 OK\r\n" + |
|
68 |
"Content-Length: 9\r\n" + |
|
69 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" + |
|
70 |
"XXXXX", |
|
71 |
||
72 |
"HTTP/1.1 200 OK\r\n" + |
|
73 |
"Content-Length: 9\r\n" + |
|
74 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" + // more than one SP after ':' |
|
75 |
"XXXXX", |
|
76 |
||
77 |
"HTTP/1.1 200 OK\r\n" + |
|
78 |
"Content-Length:\t10\r\n" + |
|
79 |
"Content-Type:\ttext/html; charset=UTF-8\r\n\r\n" + // HT separator |
|
80 |
"XXXXX", |
|
81 |
||
82 |
"HTTP/1.1 200 OK\r\n" + |
|
83 |
"Content-Length:\t\t10\r\n" + |
|
84 |
"Content-Type:\t\ttext/html; charset=UTF-8\r\n\r\n" + // more than one HT after ':' |
|
85 |
"XXXXX", |
|
86 |
||
87 |
"HTTP/1.1 407 Proxy Authorization Required\r\n" + |
|
88 |
"Proxy-Authenticate: Basic realm=\"a fake realm\"\r\n\r\n", |
|
89 |
||
90 |
"HTTP/1.1 401 Unauthorized\r\n" + |
|
91 |
"WWW-Authenticate: Digest realm=\"wally land\" domain=/ " + |
|
92 |
"nonce=\"2B7F3A2B\" qop=\"auth\"\r\n\r\n", |
|
93 |
||
94 |
"HTTP/1.1 200 OK\r\n" + |
|
95 |
"X-Foo:\r\n\r\n", // no value |
|
96 |
||
97 |
"HTTP/1.1 200 OK\r\n" + |
|
98 |
"X-Foo:\r\n\r\n" + // no value, with response body |
|
99 |
"Some Response Body", |
|
100 |
||
101 |
"HTTP/1.1 200 OK\r\n" + |
|
102 |
"X-Foo:\r\n" + // no value, followed by another header |
|
103 |
"Content-Length: 10\r\n\r\n" + |
|
104 |
"Some Response Body", |
|
105 |
||
106 |
"HTTP/1.1 200 OK\r\n" + |
|
107 |
"X-Foo:\r\n" + // no value, followed by another header, with response body |
|
108 |
"Content-Length: 10\r\n\r\n", |
|
109 |
||
110 |
"HTTP/1.1 200 OK\r\n" + |
|
111 |
"X-Foo: chegar\r\n" + |
|
112 |
"X-Foo: dfuchs\r\n" + // same header appears multiple times |
|
113 |
"Content-Length: 0\r\n" + |
|
114 |
"X-Foo: michaelm\r\n" + |
|
115 |
"X-Foo: prappo\r\n\r\n", |
|
116 |
||
117 |
"HTTP/1.1 200 OK\r\n" + |
|
118 |
"X-Foo:\r\n" + // no value, same header appears multiple times |
|
119 |
"X-Foo: dfuchs\r\n" + |
|
120 |
"Content-Length: 0\r\n" + |
|
121 |
"X-Foo: michaelm\r\n" + |
|
122 |
"X-Foo: prappo\r\n\r\n", |
|
123 |
||
124 |
"HTTP/1.1 200 OK\r\n" + |
|
125 |
"Accept-Ranges: bytes\r\n" + |
|
126 |
"Cache-control: max-age=0, no-cache=\"set-cookie\"\r\n" + |
|
127 |
"Content-Length: 132868\r\n" + |
|
128 |
"Content-Type: text/html; charset=UTF-8\r\n" + |
|
129 |
"Date: Sun, 05 Nov 2017 22:24:03 GMT\r\n" + |
|
130 |
"Server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.1e-fips Communique/4.2.2\r\n" + |
|
131 |
"Set-Cookie: AWSELB=AF7927F5100F4202119876ED2436B5005EE;PATH=/;MAX-AGE=900\r\n" + |
|
132 |
"Vary: Host,Accept-Encoding,User-Agent\r\n" + |
|
133 |
"X-Mod-Pagespeed: 1.12.34.2-0\r\n" + |
|
134 |
"Connection: keep-alive\r\n\r\n" |
|
135 |
}; |
|
136 |
Arrays.stream(basic).forEach(responses::add); |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
137 |
// add some tests where some of the CRLF are replaced |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
138 |
// by a single LF |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
139 |
Arrays.stream(basic) |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
140 |
.map(Http1HeaderParserTest::mixedCRLF) |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
141 |
.forEach(responses::add); |
48083 | 142 |
|
143 |
String[] foldingTemplate = |
|
144 |
{ "HTTP/1.1 200 OK\r\n" + |
|
145 |
"Content-Length: 9\r\n" + |
|
146 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r' |
|
147 |
" charset=UTF-8\r\n" + // one preceding SP |
|
148 |
"Connection: close\r\n\r\n" + |
|
149 |
"XXYYZZAABBCCDDEE", |
|
150 |
||
151 |
"HTTP/1.1 200 OK\r\n" + |
|
152 |
"Content-Length: 19\r\n" + |
|
153 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r |
|
154 |
" charset=UTF-8\r\n" + // more than one preceding SP |
|
155 |
"Connection: keep-alive\r\n\r\n" + |
|
156 |
"XXYYZZAABBCCDDEEFFGG", |
|
157 |
||
158 |
"HTTP/1.1 200 OK\r\n" + |
|
159 |
"Content-Length: 999\r\n" + |
|
160 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r |
|
161 |
"\tcharset=UTF-8\r\n" + // one preceding HT |
|
162 |
"Connection: close\r\n\r\n" + |
|
163 |
"XXYYZZAABBCCDDEE", |
|
164 |
||
165 |
"HTTP/1.1 200 OK\r\n" + |
|
166 |
"Content-Length: 54\r\n" + |
|
167 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r |
|
168 |
"\t\t\tcharset=UTF-8\r\n" + // more than one preceding HT |
|
169 |
"Connection: keep-alive\r\n\r\n" + |
|
170 |
"XXYYZZAABBCCDDEEFFGG", |
|
171 |
||
172 |
"HTTP/1.1 200 OK\r\n" + |
|
173 |
"Content-Length: -1\r\n" + |
|
174 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r |
|
175 |
"\t \t \tcharset=UTF-8\r\n" + // mix of preceding HT and SP |
|
176 |
"Connection: keep-alive\r\n\r\n" + |
|
177 |
"XXYYZZAABBCCDDEEFFGGHH", |
|
178 |
||
179 |
"HTTP/1.1 200 OK\r\n" + |
|
180 |
"Content-Length: 65\r\n" + |
|
181 |
"Content-Type: text/html;$NEWLINE" + // folding field-value with '\n'|'\r |
|
182 |
" \t \t charset=UTF-8\r\n" + // mix of preceding SP and HT |
|
183 |
"Connection: keep-alive\r\n\r\n" + |
|
184 |
"XXYYZZAABBCCDDEEFFGGHHII", |
|
48535
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
185 |
|
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
186 |
"HTTP/1.1 401 Unauthorized\r\n" + |
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
187 |
"WWW-Authenticate: Digest realm=\"wally land\"," |
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
188 |
+"$NEWLINE domain=/," |
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
189 |
+"$NEWLINE nonce=\"2B7F3A2B\"," |
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
190 |
+"$NEWLINE\tqop=\"auth\"\r\n\r\n", |
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
191 |
|
48083 | 192 |
}; |
48535
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
193 |
for (String newLineChar : new String[] { "\n", "\r", "\r\n" }) { |
48083 | 194 |
for (String template : foldingTemplate) |
195 |
responses.add(template.replace("$NEWLINE", newLineChar)); |
|
196 |
} |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
197 |
// add some tests where some of the CRLF are replaced |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
198 |
// by a single LF |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
199 |
for (String newLineChar : new String[] { "\n", "\r", "\r\n" }) { |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
200 |
for (String template : foldingTemplate) |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
201 |
responses.add(mixedCRLF(template).replace("$NEWLINE", newLineChar)); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
202 |
} |
48083 | 203 |
|
204 |
String[] bad = // much of this is to retain parity with legacy MessageHeaders |
|
205 |
{ "HTTP/1.1 200 OK\r\n" + |
|
206 |
"Connection:\r\n\r\n", // empty value, no body |
|
207 |
||
208 |
"HTTP/1.1 200 OK\r\n" + |
|
209 |
"Connection:\r\n\r\n" + // empty value, with body |
|
210 |
"XXXXX", |
|
211 |
||
212 |
"HTTP/1.1 200 OK\r\n" + |
|
213 |
": no header\r\n\r\n", // no/empty header-name, no body, no following header |
|
214 |
||
215 |
"HTTP/1.1 200 OK\r\n" + |
|
216 |
": no; header\r\n" + // no/empty header-name, no body, following header |
|
217 |
"Content-Length: 65\r\n\r\n", |
|
218 |
||
219 |
"HTTP/1.1 200 OK\r\n" + |
|
220 |
": no header\r\n" + // no/empty header-name |
|
221 |
"Content-Length: 65\r\n\r\n" + |
|
222 |
"XXXXX", |
|
223 |
||
224 |
"HTTP/1.1 200 OK\r\n" + |
|
225 |
": no header\r\n\r\n" + // no/empty header-name, followed by header |
|
226 |
"XXXXX", |
|
227 |
||
228 |
"HTTP/1.1 200 OK\r\n" + |
|
229 |
"Conte\r" + |
|
230 |
" nt-Length: 9\r\n" + // fold/bad header name ??? |
|
231 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" + |
|
232 |
"XXXXX", |
|
233 |
||
234 |
"HTTP/1.1 200 OK\r\n" + |
|
235 |
"Conte\r" + |
|
236 |
"nt-Length: 9\r\n" + // fold/bad header name ??? without preceding space |
|
237 |
"Content-Type: text/html; charset=UTF-8\r\n\r\n" + |
|
238 |
"XXXXXYYZZ", |
|
239 |
||
240 |
"HTTP/1.0 404 Not Found\r\n" + |
|
241 |
"header-without-colon\r\n\r\n", |
|
242 |
||
243 |
"HTTP/1.0 404 Not Found\r\n" + |
|
244 |
"header-without-colon\r\n\r\n" + |
|
245 |
"SOMEBODY", |
|
246 |
||
247 |
}; |
|
248 |
Arrays.stream(bad).forEach(responses::add); |
|
249 |
||
250 |
return responses.stream().map(p -> new Object[] { p }).toArray(Object[][]::new); |
|
251 |
} |
|
252 |
||
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
253 |
static final AtomicInteger index = new AtomicInteger(); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
254 |
static final AtomicInteger limit = new AtomicInteger(1); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
255 |
static final AtomicBoolean useCRLF = new AtomicBoolean(); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
256 |
// A small method to replace part of the CRLF present in a string |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
257 |
// with simple LF. The method uses a deterministic algorithm based |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
258 |
// on current values of static index/limit/useCRLF counters. |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
259 |
// These counters are used to produce a stream of substitutes that |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
260 |
// looks like this: |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
261 |
// LF CRLF LF LF CRLF CRLF LF LF LF CRLF CRLF CRLF (then repeat from start) |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
262 |
static final String mixedCRLF(String headers) { |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
263 |
int next; |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
264 |
int start = 0; |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
265 |
int last = headers.lastIndexOf("\r\n"); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
266 |
String prev = ""; |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
267 |
StringBuilder res = new StringBuilder(); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
268 |
while ((next = headers.indexOf("\r\n", start)) > 0) { |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
269 |
res.append(headers.substring(start, next)); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
270 |
if ("\n".equals(prev) && next == last) { |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
271 |
// for some reason the legacy MessageHeader parser will |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
272 |
// not consume the final LF if the headers are terminated |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
273 |
// by <LF><CRLF> instead of <CRLF><CRLF>. It consume |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
274 |
// <LF><CR> but leaves the last <LF> in the stream. |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
275 |
// Here we just make sure to avoid using <LF><CRLF> |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
276 |
// as that would cause the legacy parser to consume |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
277 |
// 1 byte less than the Http1HeadersParser - which |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
278 |
// does consume the last <LF>, as it should. |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
279 |
// if this is the last CRLF and the previous one |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
280 |
// was replaced by LF then use LF. |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
281 |
res.append(prev); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
282 |
} else { |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
283 |
prev = useCRLF.get() ? "\r\n" : "\n"; |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
284 |
res.append(prev); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
285 |
} |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
286 |
// skip CRLF |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
287 |
start = next + 2; |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
288 |
|
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
289 |
// The idea is to substitute some of the CRLF with LF. |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
290 |
// Rather than doing this randomly, always use the following |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
291 |
// sequence: |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
292 |
// LF CRLF LF LF CRLF CRLF LF LF LF CRLF CRLF CRLF |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
293 |
index.incrementAndGet(); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
294 |
if (index.get() == limit.get()) { |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
295 |
index.set(0); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
296 |
if (useCRLF.get()) limit.incrementAndGet(); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
297 |
if (limit.get() > 3) limit.set(1); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
298 |
useCRLF.set(!useCRLF.get()); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
299 |
} |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
300 |
} |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
301 |
res.append(headers.substring(start)); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
302 |
return res.toString(); |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
303 |
} |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
304 |
|
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
305 |
|
48083 | 306 |
@Test(dataProvider = "responses") |
307 |
public void verifyHeaders(String respString) throws Exception { |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
308 |
System.out.println("\ntesting:\n\t" + respString |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
309 |
.replace("\r\n", "<CRLF>") |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
310 |
.replace("\r", "<CR>") |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
311 |
.replace("\n","<LF>") |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
312 |
.replace("LF>", "LF>\n\t")); |
48083 | 313 |
byte[] bytes = respString.getBytes(US_ASCII); |
314 |
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
|
315 |
MessageHeader m = new MessageHeader(bais); |
|
316 |
Map<String,List<String>> messageHeaderMap = m.getHeaders(); |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
317 |
int availableBytes = bais.available(); |
48083 | 318 |
|
319 |
Http1HeaderParser decoder = new Http1HeaderParser(); |
|
320 |
ByteBuffer b = ByteBuffer.wrap(bytes); |
|
321 |
decoder.parse(b); |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
322 |
System.out.printf("Http1HeaderParser parsed %d bytes out of %d%n", b.position(), bytes.length); |
48083 | 323 |
Map<String,List<String>> decoderMap1 = decoder.headers().map(); |
324 |
||
325 |
// assert status-line |
|
326 |
String statusLine1 = messageHeaderMap.get(null).get(0); |
|
327 |
String statusLine2 = decoder.statusLine(); |
|
328 |
if (statusLine1.startsWith("HTTP")) {// skip the case where MH's messes up the status-line |
|
329 |
assertEquals(statusLine1, statusLine2, "Status-line not equal"); |
|
330 |
} else { |
|
331 |
assertTrue(statusLine2.startsWith("HTTP/1."), "Status-line not HTTP/1."); |
|
332 |
} |
|
333 |
||
334 |
// remove the null'th entry with is the status-line |
|
335 |
Map<String,List<String>> map = new HashMap<>(); |
|
336 |
for (Map.Entry<String,List<String>> e : messageHeaderMap.entrySet()) { |
|
337 |
if (e.getKey() != null) { |
|
338 |
map.put(e.getKey(), e.getValue()); |
|
339 |
} |
|
340 |
} |
|
341 |
messageHeaderMap = map; |
|
342 |
||
343 |
assertHeadersEqual(messageHeaderMap, decoderMap1, |
|
344 |
"messageHeaderMap not equal to decoderMap1"); |
|
345 |
||
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
346 |
assertEquals(availableBytes, b.remaining(), |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
347 |
String.format("stream available (%d) not equal to remaining (%d)", |
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
348 |
availableBytes, b.remaining())); |
48083 | 349 |
// byte at a time |
350 |
decoder = new Http1HeaderParser(); |
|
351 |
List<ByteBuffer> buffers = IntStream.range(0, bytes.length) |
|
352 |
.mapToObj(i -> ByteBuffer.wrap(bytes, i, 1)) |
|
353 |
.collect(toList()); |
|
354 |
while (decoder.parse(buffers.remove(0)) != true); |
|
355 |
Map<String,List<String>> decoderMap2 = decoder.headers().map(); |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
356 |
assertEquals(availableBytes, buffers.size(), |
48083 | 357 |
"stream available not equals to remaining buffers"); |
358 |
assertEquals(decoderMap1, decoderMap2, "decoder maps not equal"); |
|
56601
e38ce7e04995
http-client-branch: HttpClient HTTP/1.1 could be more lenient and accept LF as well as CRLF as header separator.
dfuchs
parents:
56451
diff
changeset
|
359 |
|
48083 | 360 |
} |
361 |
||
362 |
@DataProvider(name = "errors") |
|
363 |
public Object[][] errors() { |
|
364 |
List<String> responses = new ArrayList<>(); |
|
365 |
||
366 |
// These responses are parsed, somewhat, by MessageHeaders but give |
|
367 |
// nonsensible results. They, correctly, fail with the Http1HeaderParser. |
|
368 |
String[] bad = |
|
369 |
{// "HTTP/1.1 402 Payment Required\r\n" + |
|
370 |
// "Content-Length: 65\r\n\r", // missing trailing LF //TODO: incomplete |
|
371 |
||
372 |
"HTTP/1.1 402 Payment Required\r\n" + |
|
373 |
"Content-Length: 65\r\n\rT\r\n\r\nGGGGGG", |
|
374 |
||
375 |
"HTTP/1.1 200OK\r\n\rT", |
|
376 |
||
377 |
"HTTP/1.1 200OK\rT", |
|
378 |
}; |
|
379 |
Arrays.stream(bad).forEach(responses::add); |
|
380 |
||
381 |
return responses.stream().map(p -> new Object[] { p }).toArray(Object[][]::new); |
|
382 |
} |
|
383 |
||
384 |
@Test(dataProvider = "errors", expectedExceptions = ProtocolException.class) |
|
385 |
public void errors(String respString) throws ProtocolException { |
|
386 |
byte[] bytes = respString.getBytes(US_ASCII); |
|
387 |
Http1HeaderParser decoder = new Http1HeaderParser(); |
|
388 |
ByteBuffer b = ByteBuffer.wrap(bytes); |
|
389 |
decoder.parse(b); |
|
390 |
} |
|
391 |
||
392 |
void assertHeadersEqual(Map<String,List<String>> expected, |
|
393 |
Map<String,List<String>> actual, |
|
394 |
String msg) { |
|
395 |
||
396 |
if (expected.equals(actual)) |
|
397 |
return; |
|
398 |
||
399 |
assertEquals(expected.size(), actual.size(), |
|
400 |
format("%s. Expected size %d, actual size %s. %nexpected= %s,%n actual=%s.", |
|
401 |
msg, expected.size(), actual.size(), mapToString(expected), mapToString(actual))); |
|
402 |
||
403 |
for (Map.Entry<String,List<String>> e : expected.entrySet()) { |
|
404 |
String key = e.getKey(); |
|
405 |
List<String> values = e.getValue(); |
|
406 |
||
407 |
boolean found = false; |
|
408 |
for (Map.Entry<String,List<String>> other: actual.entrySet()) { |
|
409 |
if (key.equalsIgnoreCase(other.getKey())) { |
|
410 |
found = true; |
|
411 |
List<String> otherValues = other.getValue(); |
|
412 |
assertEquals(values.size(), otherValues.size(), |
|
413 |
format("%s. Expected list size %d, actual size %s", |
|
414 |
msg, values.size(), otherValues.size())); |
|
48535
5f9977540ac9
8195138: The asynchronous Http1HeaderParser doesn't handle all line folds correctly
dfuchs
parents:
48083
diff
changeset
|
415 |
if (!(values.containsAll(otherValues) && otherValues.containsAll(values))) |
48083 | 416 |
assertTrue(false, format("Lists are unequal [%s] [%s]", values, otherValues)); |
417 |
break; |
|
418 |
} |
|
419 |
} |
|
420 |
assertTrue(found, format("header name, %s, not found in %s", key, actual)); |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
static String mapToString(Map<String,List<String>> map) { |
|
425 |
StringBuilder sb = new StringBuilder(); |
|
426 |
List<String> sortedKeys = new ArrayList(map.keySet()); |
|
427 |
Collections.sort(sortedKeys); |
|
428 |
for (String key : sortedKeys) { |
|
429 |
List<String> values = map.get(key); |
|
430 |
sb.append("\n\t" + key + " | " + values); |
|
431 |
} |
|
432 |
return sb.toString(); |
|
433 |
} |
|
434 |
||
435 |
// --- |
|
436 |
||
437 |
/* Main entry point for standalone testing of the main functional test. */ |
|
438 |
public static void main(String... args) throws Exception { |
|
439 |
Http1HeaderParserTest test = new Http1HeaderParserTest(); |
|
440 |
int count = 0; |
|
441 |
for (Object[] objs : test.responses()) { |
|
442 |
out.println("Testing " + count++ + ", " + objs[0]); |
|
443 |
test.verifyHeaders((String) objs[0]); |
|
444 |
} |
|
445 |
for (Object[] objs : test.errors()) { |
|
446 |
out.println("Testing " + count++ + ", " + objs[0]); |
|
447 |
try { |
|
448 |
test.errors((String) objs[0]); |
|
449 |
throw new RuntimeException("Expected ProtocolException for " + objs[0]); |
|
450 |
} catch (ProtocolException expected) { /* Ok */ } |
|
451 |
} |
|
452 |
} |
|
453 |
} |