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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package ch.boye.httpclientandroidlib.message;
import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpVersion;
import ch.boye.httpclientandroidlib.ParseException;
import ch.boye.httpclientandroidlib.ProtocolVersion;
import ch.boye.httpclientandroidlib.RequestLine;
import ch.boye.httpclientandroidlib.StatusLine;
import ch.boye.httpclientandroidlib.annotation.Immutable;
import ch.boye.httpclientandroidlib.protocol.HTTP;
import ch.boye.httpclientandroidlib.util.Args;
import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
/**
* Basic parser for lines in the head section of an HTTP message.
* There are individual methods for parsing a request line, a
* status line, or a header line.
* The lines to parse are passed in memory, the parser does not depend
* on any specific IO mechanism.
* Instances of this class are stateless and thread-safe.
* Derived classes MUST maintain these properties.
*
* <p>
* Note: This class was created by refactoring parsing code located in
* various other classes. The author tags from those other classes have
* been replicated here, although the association with the parsing code
* taken from there has not been traced.
* </p>
*
* @since 4.0
*/
@Immutable
public class BasicLineParser implements LineParser {
/**
* A default instance of this class, for use as default or fallback.
* Note that {@link BasicLineParser} is not a singleton, there can
* be many instances of the class itself and of derived classes.
* The instance here provides non-customized, default behavior.
*
* @deprecated (4.3) use {@link #INSTANCE}
*/
@Deprecated
public final static BasicLineParser DEFAULT = new BasicLineParser();
public final static BasicLineParser INSTANCE = new BasicLineParser();
/**
* A version of the protocol to parse.
* The version is typically not relevant, but the protocol name.
*/
protected final ProtocolVersion protocol;
/**
* Creates a new line parser for the given HTTP-like protocol.
*
* @param proto a version of the protocol to parse, or
* <code>null</code> for HTTP. The actual version
* is not relevant, only the protocol name.
*/
public BasicLineParser(final ProtocolVersion proto) {
this.protocol = proto != null? proto : HttpVersion.HTTP_1_1;
}
/**
* Creates a new line parser for HTTP.
*/
public BasicLineParser() {
this(null);
}
public static
ProtocolVersion parseProtocolVersion(final String value,
final LineParser parser) throws ParseException {
Args.notNull(value, "Value");
final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
buffer.append(value);
final ParserCursor cursor = new ParserCursor(0, value.length());
return (parser != null ? parser : BasicLineParser.INSTANCE)
.parseProtocolVersion(buffer, cursor);
}
// non-javadoc, see interface LineParser
public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer,
final ParserCursor cursor) throws ParseException {
Args.notNull(buffer, "Char array buffer");
Args.notNull(cursor, "Parser cursor");
final String protoname = this.protocol.getProtocol();
final int protolength = protoname.length();
final int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
skipWhitespace(buffer, cursor);
int i = cursor.getPos();
// long enough for "HTTP/1.1"?
if (i + protolength + 4 > indexTo) {
throw new ParseException
("Not a valid protocol version: " +
buffer.substring(indexFrom, indexTo));
}
// check the protocol name and slash
boolean ok = true;
for (int j=0; ok && (j<protolength); j++) {
ok = (buffer.charAt(i+j) == protoname.charAt(j));
}
if (ok) {
ok = (buffer.charAt(i+protolength) == '/');
}
if (!ok) {
throw new ParseException
("Not a valid protocol version: " +
buffer.substring(indexFrom, indexTo));
}
i += protolength+1;
final int period = buffer.indexOf('.', i, indexTo);
if (period == -1) {
throw new ParseException
("Invalid protocol version number: " +
buffer.substring(indexFrom, indexTo));
}
final int major;
try {
major = Integer.parseInt(buffer.substringTrimmed(i, period));
} catch (final NumberFormatException e) {
throw new ParseException
("Invalid protocol major version number: " +
buffer.substring(indexFrom, indexTo));
}
i = period + 1;
int blank = buffer.indexOf(' ', i, indexTo);
if (blank == -1) {
blank = indexTo;
}
final int minor;
try {
minor = Integer.parseInt(buffer.substringTrimmed(i, blank));
} catch (final NumberFormatException e) {
throw new ParseException(
"Invalid protocol minor version number: " +
buffer.substring(indexFrom, indexTo));
}
cursor.updatePos(blank);
return createProtocolVersion(major, minor);
} // parseProtocolVersion
/**
* Creates a protocol version.
* Called from {@link #parseProtocolVersion}.
*
* @param major the major version number, for example 1 in HTTP/1.0
* @param minor the minor version number, for example 0 in HTTP/1.0
*
* @return the protocol version
*/
protected ProtocolVersion createProtocolVersion(final int major, final int minor) {
return protocol.forVersion(major, minor);
}
// non-javadoc, see interface LineParser
public boolean hasProtocolVersion(final CharArrayBuffer buffer,
final ParserCursor cursor) {
Args.notNull(buffer, "Char array buffer");
Args.notNull(cursor, "Parser cursor");
int index = cursor.getPos();
final String protoname = this.protocol.getProtocol();
final int protolength = protoname.length();
if (buffer.length() < protolength+4)
{
return false; // not long enough for "HTTP/1.1"
}
if (index < 0) {
// end of line, no tolerance for trailing whitespace
// this works only for single-digit major and minor version
index = buffer.length() -4 -protolength;
} else if (index == 0) {
// beginning of line, tolerate leading whitespace
while ((index < buffer.length()) &&
HTTP.isWhitespace(buffer.charAt(index))) {
index++;
}
} // else within line, don't tolerate whitespace
if (index + protolength + 4 > buffer.length()) {
return false;
}
// just check protocol name and slash, no need to analyse the version
boolean ok = true;
for (int j=0; ok && (j<protolength); j++) {
ok = (buffer.charAt(index+j) == protoname.charAt(j));
}
if (ok) {
ok = (buffer.charAt(index+protolength) == '/');
}
return ok;
}
public static
RequestLine parseRequestLine(final String value,
final LineParser parser) throws ParseException {
Args.notNull(value, "Value");
final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
buffer.append(value);
final ParserCursor cursor = new ParserCursor(0, value.length());
return (parser != null ? parser : BasicLineParser.INSTANCE)
.parseRequestLine(buffer, cursor);
}
/**
* Parses a request line.
*
* @param buffer a buffer holding the line to parse
*
* @return the parsed request line
*
* @throws ParseException in case of a parse error
*/
public RequestLine parseRequestLine(final CharArrayBuffer buffer,
final ParserCursor cursor) throws ParseException {
Args.notNull(buffer, "Char array buffer");
Args.notNull(cursor, "Parser cursor");
final int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
try {
skipWhitespace(buffer, cursor);
int i = cursor.getPos();
int blank = buffer.indexOf(' ', i, indexTo);
if (blank < 0) {
throw new ParseException("Invalid request line: " +
buffer.substring(indexFrom, indexTo));
}
final String method = buffer.substringTrimmed(i, blank);
cursor.updatePos(blank);
skipWhitespace(buffer, cursor);
i = cursor.getPos();
blank = buffer.indexOf(' ', i, indexTo);
if (blank < 0) {
throw new ParseException("Invalid request line: " +
buffer.substring(indexFrom, indexTo));
}
final String uri = buffer.substringTrimmed(i, blank);
cursor.updatePos(blank);
final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
skipWhitespace(buffer, cursor);
if (!cursor.atEnd()) {
throw new ParseException("Invalid request line: " +
buffer.substring(indexFrom, indexTo));
}
return createRequestLine(method, uri, ver);
} catch (final IndexOutOfBoundsException e) {
throw new ParseException("Invalid request line: " +
buffer.substring(indexFrom, indexTo));
}
} // parseRequestLine
/**
* Instantiates a new request line.
* Called from {@link #parseRequestLine}.
*
* @param method the request method
* @param uri the requested URI
* @param ver the protocol version
*
* @return a new status line with the given data
*/
protected RequestLine createRequestLine(final String method,
final String uri,
final ProtocolVersion ver) {
return new BasicRequestLine(method, uri, ver);
}
public static
StatusLine parseStatusLine(final String value,
final LineParser parser) throws ParseException {
Args.notNull(value, "Value");
final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
buffer.append(value);
final ParserCursor cursor = new ParserCursor(0, value.length());
return (parser != null ? parser : BasicLineParser.INSTANCE)
.parseStatusLine(buffer, cursor);
}
// non-javadoc, see interface LineParser
public StatusLine parseStatusLine(final CharArrayBuffer buffer,
final ParserCursor cursor) throws ParseException {
Args.notNull(buffer, "Char array buffer");
Args.notNull(cursor, "Parser cursor");
final int indexFrom = cursor.getPos();
final int indexTo = cursor.getUpperBound();
try {
// handle the HTTP-Version
final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
// handle the Status-Code
skipWhitespace(buffer, cursor);
int i = cursor.getPos();
int blank = buffer.indexOf(' ', i, indexTo);
if (blank < 0) {
blank = indexTo;
}
final int statusCode;
final String s = buffer.substringTrimmed(i, blank);
for (int j = 0; j < s.length(); j++) {
if (!Character.isDigit(s.charAt(j))) {
throw new ParseException(
"Status line contains invalid status code: "
+ buffer.substring(indexFrom, indexTo));
}
}
try {
statusCode = Integer.parseInt(s);
} catch (final NumberFormatException e) {
throw new ParseException(
"Status line contains invalid status code: "
+ buffer.substring(indexFrom, indexTo));
}
//handle the Reason-Phrase
i = blank;
final String reasonPhrase;
if (i < indexTo) {
reasonPhrase = buffer.substringTrimmed(i, indexTo);
} else {
reasonPhrase = "";
}
return createStatusLine(ver, statusCode, reasonPhrase);
} catch (final IndexOutOfBoundsException e) {
throw new ParseException("Invalid status line: " +
buffer.substring(indexFrom, indexTo));
}
} // parseStatusLine
/**
* Instantiates a new status line.
* Called from {@link #parseStatusLine}.
*
* @param ver the protocol version
* @param status the status code
* @param reason the reason phrase
*
* @return a new status line with the given data
*/
protected StatusLine createStatusLine(final ProtocolVersion ver,
final int status,
final String reason) {
return new BasicStatusLine(ver, status, reason);
}
public static
Header parseHeader(final String value,
final LineParser parser) throws ParseException {
Args.notNull(value, "Value");
final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
buffer.append(value);
return (parser != null ? parser : BasicLineParser.INSTANCE)
.parseHeader(buffer);
}
// non-javadoc, see interface LineParser
public Header parseHeader(final CharArrayBuffer buffer)
throws ParseException {
// the actual parser code is in the constructor of BufferedHeader
return new BufferedHeader(buffer);
}
/**
* Helper to skip whitespace.
*/
protected void skipWhitespace(final CharArrayBuffer buffer, final ParserCursor cursor) {
int pos = cursor.getPos();
final int indexTo = cursor.getUpperBound();
while ((pos < indexTo) &&
HTTP.isWhitespace(buffer.charAt(pos))) {
pos++;
}
cursor.updatePos(pos);
}
} // class BasicLineParser
|