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.HttpResponse;
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.NotThreadSafe;
41  import org.apache.http.config.MessageConstraints;
42  import org.apache.http.impl.DefaultHttpResponseFactory;
43  import org.apache.http.impl.io.AbstractMessageParser;
44  import org.apache.http.io.SessionInputBuffer;
45  import org.apache.http.message.LineParser;
46  import org.apache.http.message.ParserCursor;
47  import org.apache.http.params.HttpParams;
48  import org.apache.http.util.Args;
49  import org.apache.http.util.CharArrayBuffer;
50  
51  /**
52   * Lenient HTTP response parser implementation that can skip malformed data until
53   * a valid HTTP response message head is encountered.
54   *
55   * @since 4.2
56   */
57  @SuppressWarnings("deprecation")
58  @NotThreadSafe
59  public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
60  
61      private final Log log = LogFactory.getLog(getClass());
62  
63      private final HttpResponseFactory responseFactory;
64      private final CharArrayBuffer lineBuf;
65  
66      /**
67       * @deprecated (4.3) use {@link DefaultHttpResponseParser#DefaultHttpResponseParser(
68       *   SessionInputBuffer, LineParser, HttpResponseFactory, MessageConstraints)}
69       */
70      @Deprecated
71      public DefaultHttpResponseParser(
72              final SessionInputBuffer buffer,
73              final LineParser parser,
74              final HttpResponseFactory responseFactory,
75              final HttpParams params) {
76          super(buffer, parser, params);
77          Args.notNull(responseFactory, "Response factory");
78          this.responseFactory = responseFactory;
79          this.lineBuf = new CharArrayBuffer(128);
80      }
81  
82      /**
83       * Creates new instance of DefaultHttpResponseParser.
84       *
85       * @param buffer the session input buffer.
86       * @param lineParser the line parser. If <code>null</code>
87       *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
88       * @param responseFactory HTTP response factory. If <code>null</code>
89       *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
90       * @param constraints the message constraints. If <code>null</code>
91       *   {@link MessageConstraints#DEFAULT} will be used.
92       *
93       * @since 4.3
94       */
95      public DefaultHttpResponseParser(
96              final SessionInputBuffer buffer,
97              final LineParser lineParser,
98              final HttpResponseFactory responseFactory,
99              final MessageConstraints constraints) {
100         super(buffer, lineParser, constraints);
101         this.responseFactory = responseFactory != null ? responseFactory :
102                 DefaultHttpResponseFactory.INSTANCE;
103         this.lineBuf = new CharArrayBuffer(128);
104     }
105 
106     /**
107      * Creates new instance of DefaultHttpResponseParser.
108      *
109      * @param buffer the session input buffer.
110      * @param constraints the message constraints. If <code>null</code>
111      *   {@link MessageConstraints#DEFAULT} will be used.
112      *
113      * @since 4.3
114      */
115     public DefaultHttpResponseParser(
116         final SessionInputBuffer buffer, final MessageConstraints constraints) {
117         this(buffer, null, null, constraints);
118     }
119 
120     /**
121      * Creates new instance of DefaultHttpResponseParser.
122      *
123      * @param buffer the session input buffer.
124      *
125      * @since 4.3
126      */
127     public DefaultHttpResponseParser(final SessionInputBuffer buffer) {
128         this(buffer, null, null, MessageConstraints.DEFAULT);
129     }
130 
131     @Override
132     protected HttpResponse parseHead(
133             final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
134         //read out the HTTP status string
135         int count = 0;
136         ParserCursor cursor = null;
137         do {
138             // clear the buffer
139             this.lineBuf.clear();
140             final int i = sessionBuffer.readLine(this.lineBuf);
141             if (i == -1 && count == 0) {
142                 // The server just dropped connection on us
143                 throw new NoHttpResponseException("The target server failed to respond");
144             }
145             cursor = new ParserCursor(0, this.lineBuf.length());
146             if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
147                 // Got one
148                 break;
149             } else if (i == -1 || reject(this.lineBuf, count)) {
150                 // Giving up
151                 throw new ProtocolException("The server failed to respond with a " +
152                         "valid HTTP response");
153             }
154             if (this.log.isDebugEnabled()) {
155                 this.log.debug("Garbage in response: " + this.lineBuf.toString());
156             }
157             count++;
158         } while(true);
159         //create the status line from the status string
160         final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
161         return this.responseFactory.newHttpResponse(statusline, null);
162     }
163 
164     protected boolean reject(final CharArrayBuffer line, final int count) {
165         return false;
166     }
167 
168 }