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 package org.apache.http.impl.conn;
29
30 import java.io.IOException;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.http.HttpException;
35 import org.apache.http.HttpMessage;
36 import org.apache.http.HttpResponseFactory;
37 import org.apache.http.NoHttpResponseException;
38 import org.apache.http.ProtocolException;
39 import org.apache.http.StatusLine;
40 import org.apache.http.annotation.ThreadSafe;
41 import org.apache.http.impl.io.AbstractMessageParser;
42 import org.apache.http.io.SessionInputBuffer;
43 import org.apache.http.message.LineParser;
44 import org.apache.http.message.ParserCursor;
45 import org.apache.http.params.HttpParams;
46 import org.apache.http.util.Args;
47 import org.apache.http.util.CharArrayBuffer;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @Deprecated
65 @ThreadSafe
66 public class DefaultResponseParser extends AbstractMessageParser<HttpMessage> {
67
68 private final Log log = LogFactory.getLog(getClass());
69
70 private final HttpResponseFactory responseFactory;
71 private final CharArrayBuffer lineBuf;
72 private final int maxGarbageLines;
73
74 public DefaultResponseParser(
75 final SessionInputBuffer buffer,
76 final LineParser parser,
77 final HttpResponseFactory responseFactory,
78 final HttpParams params) {
79 super(buffer, parser, params);
80 Args.notNull(responseFactory, "Response factory");
81 this.responseFactory = responseFactory;
82 this.lineBuf = new CharArrayBuffer(128);
83 this.maxGarbageLines = getMaxGarbageLines(params);
84 }
85
86 protected int getMaxGarbageLines(final HttpParams params) {
87 return params.getIntParameter(
88 org.apache.http.conn.params.ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE,
89 Integer.MAX_VALUE);
90 }
91
92 @Override
93 protected HttpMessage parseHead(
94 final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
95
96 int count = 0;
97 ParserCursor cursor = null;
98 do {
99
100 this.lineBuf.clear();
101 final int i = sessionBuffer.readLine(this.lineBuf);
102 if (i == -1 && count == 0) {
103
104 throw new NoHttpResponseException("The target server failed to respond");
105 }
106 cursor = new ParserCursor(0, this.lineBuf.length());
107 if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
108
109 break;
110 } else if (i == -1 || count >= this.maxGarbageLines) {
111
112 throw new ProtocolException("The server failed to respond with a " +
113 "valid HTTP response");
114 }
115 if (this.log.isDebugEnabled()) {
116 this.log.debug("Garbage in response: " + this.lineBuf.toString());
117 }
118 count++;
119 } while(true);
120
121 final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
122 return this.responseFactory.newHttpResponse(statusline, null);
123 }
124
125 }