View Javadoc

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   */
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   * Default HTTP response parser implementation.
51   * <p>
52   * The following parameters can be used to customize the behavior of this
53   * class:
54   * <ul>
55   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
56   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
57   *  <li>{@link org.apache.http.conn.params.ConnConnectionPNames#MAX_STATUS_LINE_GARBAGE}</li>
58   * </ul>
59   *
60   * @since 4.0
61   *
62   * @deprecated (4.2) use {@link DefaultHttpResponseParser}
63   */
64  @Deprecated
65  @ThreadSafe // no public methods
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          //read out the HTTP status string
96          int count = 0;
97          ParserCursor cursor = null;
98          do {
99              // clear the buffer
100             this.lineBuf.clear();
101             final int i = sessionBuffer.readLine(this.lineBuf);
102             if (i == -1 && count == 0) {
103                 // The server just dropped connection on us
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                 // Got one
109                 break;
110             } else if (i == -1 || count >= this.maxGarbageLines) {
111                 // Giving up
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         //create the status line from the status string
121         final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
122         return this.responseFactory.newHttpResponse(statusline, null);
123     }
124 
125 }