Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
28 package ch.boye.httpclientandroidlib.impl.conn;
30 import java.io.IOException;
32 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
34 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
35 /* LogFactory removed by HttpClient for Android script. */
36 import ch.boye.httpclientandroidlib.HttpException;
37 import ch.boye.httpclientandroidlib.HttpMessage;
38 import ch.boye.httpclientandroidlib.HttpResponseFactory;
39 import ch.boye.httpclientandroidlib.NoHttpResponseException;
40 import ch.boye.httpclientandroidlib.ProtocolException;
41 import ch.boye.httpclientandroidlib.StatusLine;
42 import ch.boye.httpclientandroidlib.conn.params.ConnConnectionPNames;
43 import ch.boye.httpclientandroidlib.impl.io.AbstractMessageParser;
44 import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
45 import ch.boye.httpclientandroidlib.message.LineParser;
46 import ch.boye.httpclientandroidlib.message.ParserCursor;
47 import ch.boye.httpclientandroidlib.params.HttpParams;
48 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
50 /**
51 * Default HTTP response parser implementation.
52 * <p>
53 * The following parameters can be used to customize the behavior of this
54 * class:
55 * <ul>
56 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
57 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
58 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnConnectionPNames#MAX_STATUS_LINE_GARBAGE}</li>
59 * </ul>
60 *
61 * @since 4.0
62 */
63 @ThreadSafe // no public methods
64 public class DefaultResponseParser extends AbstractMessageParser {
66 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
68 private final HttpResponseFactory responseFactory;
69 private final CharArrayBuffer lineBuf;
70 private final int maxGarbageLines;
72 public DefaultResponseParser(
73 final SessionInputBuffer buffer,
74 final LineParser parser,
75 final HttpResponseFactory responseFactory,
76 final HttpParams params) {
77 super(buffer, parser, params);
78 if (responseFactory == null) {
79 throw new IllegalArgumentException
80 ("Response factory may not be null");
81 }
82 this.responseFactory = responseFactory;
83 this.lineBuf = new CharArrayBuffer(128);
84 this.maxGarbageLines = params.getIntParameter(
85 ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE, Integer.MAX_VALUE);
86 }
89 @Override
90 protected HttpMessage parseHead(
91 final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
92 //read out the HTTP status string
93 int count = 0;
94 ParserCursor cursor = null;
95 do {
96 // clear the buffer
97 this.lineBuf.clear();
98 int i = sessionBuffer.readLine(this.lineBuf);
99 if (i == -1 && count == 0) {
100 // The server just dropped connection on us
101 throw new NoHttpResponseException("The target server failed to respond");
102 }
103 cursor = new ParserCursor(0, this.lineBuf.length());
104 if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
105 // Got one
106 break;
107 } else if (i == -1 || count >= this.maxGarbageLines) {
108 // Giving up
109 throw new ProtocolException("The server failed to respond with a " +
110 "valid HTTP response");
111 }
112 if (this.log.isDebugEnabled()) {
113 this.log.debug("Garbage in response: " + this.lineBuf.toString());
114 }
115 count++;
116 } while(true);
117 //create the status line from the status string
118 StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
119 return this.responseFactory.newHttpResponse(statusline, null);
120 }
122 }