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