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.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   * 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 org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
57   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
58   *  <li>{@link org.apache.http.conn.params.ConnConnectionPNames#MAX_STATUS_LINE_GARBAGE}</li>
59   * </ul>
60   *
61   * @since 4.0
62   *
63   * @deprecated (4.2) use {@link DefaultHttpResponseParser}
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          //read out the HTTP status string
97          int count = 0;
98          ParserCursor cursor = null;
99          do {
100             // clear the buffer
101             this.lineBuf.clear();
102             final int i = sessionBuffer.readLine(this.lineBuf);
103             if (i == -1 && count == 0) {
104                 // The server just dropped connection on us
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                 // Got one
110                 break;
111             } else if (i == -1 || count >= this.maxGarbageLines) {
112                 // Giving up
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         //create the status line from the status string
122         final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
123         return this.responseFactory.newHttpResponse(statusline, null);
124     }
125 
126 }