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