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.config.MessageConstraints;
41 import org.apache.http.impl.DefaultHttpResponseFactory;
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  * Lenient HTTP response parser implementation that can skip malformed data until
52  * a valid HTTP response message head is encountered.
53  *
54  * @since 4.2
55  */

56 @SuppressWarnings("deprecation")
57 public class DefaultHttpResponseParser extends AbstractMessageParser<HttpResponse> {
58
59     private final Log log = LogFactory.getLog(getClass());
60
61     private final HttpResponseFactory responseFactory;
62     private final CharArrayBuffer lineBuf;
63
64     /**
65      * @deprecated (4.3) use {@link DefaultHttpResponseParser#DefaultHttpResponseParser(
66      *   SessionInputBuffer, LineParser, HttpResponseFactory, MessageConstraints)}
67      */

68     @Deprecated
69     public DefaultHttpResponseParser(
70             final SessionInputBuffer buffer,
71             final LineParser parser,
72             final HttpResponseFactory responseFactory,
73             final HttpParams params) {
74         super(buffer, parser, params);
75         Args.notNull(responseFactory, "Response factory");
76         this.responseFactory = responseFactory;
77         this.lineBuf = new CharArrayBuffer(128);
78     }
79
80     /**
81      * Creates new instance of DefaultHttpResponseParser.
82      *
83      * @param buffer the session input buffer.
84      * @param lineParser the line parser. If {@code null}
85      *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
86      * @param responseFactory HTTP response factory. If {@code null}
87      *   {@link DefaultHttpResponseFactory#INSTANCE} will be used.
88      * @param constraints the message constraints. If {@code null}
89      *   {@link MessageConstraints#DEFAULT} will be used.
90      *
91      * @since 4.3
92      */

93     public DefaultHttpResponseParser(
94             final SessionInputBuffer buffer,
95             final LineParser lineParser,
96             final HttpResponseFactory responseFactory,
97             final MessageConstraints constraints) {
98         super(buffer, lineParser, constraints);
99         this.responseFactory = responseFactory != null ? responseFactory :
100                 DefaultHttpResponseFactory.INSTANCE;
101         this.lineBuf = new CharArrayBuffer(128);
102     }
103
104     /**
105      * Creates new instance of DefaultHttpResponseParser.
106      *
107      * @param buffer the session input buffer.
108      * @param constraints the message constraints. If {@code null}
109      *   {@link MessageConstraints#DEFAULT} will be used.
110      *
111      * @since 4.3
112      */

113     public DefaultHttpResponseParser(
114         final SessionInputBuffer buffer, final MessageConstraints constraints) {
115         this(buffer, nullnull, constraints);
116     }
117
118     /**
119      * Creates new instance of DefaultHttpResponseParser.
120      *
121      * @param buffer the session input buffer.
122      *
123      * @since 4.3
124      */

125     public DefaultHttpResponseParser(final SessionInputBuffer buffer) {
126         this(buffer, nullnull, MessageConstraints.DEFAULT);
127     }
128
129     @Override
130     protected HttpResponse parseHead(
131             final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
132         //read out the HTTP status string
133         int count = 0;
134         ParserCursor cursor = null;
135         do {
136             // clear the buffer
137             this.lineBuf.clear();
138             final int i = sessionBuffer.readLine(this.lineBuf);
139             if (i == -1 && count == 0) {
140                 // The server just dropped connection on us
141                 throw new NoHttpResponseException("The target server failed to respond");
142             }
143             cursor = new ParserCursor(0, this.lineBuf.length());
144             if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
145                 // Got one
146                 break;
147             } else if (i == -1 || reject(this.lineBuf, count)) {
148                 // Giving up
149                 throw new ProtocolException("The server failed to respond with a " +
150                         "valid HTTP response");
151             }
152             if (this.log.isDebugEnabled()) {
153                 this.log.debug("Garbage in response: " + this.lineBuf.toString());
154             }
155             count++;
156         } while(true);
157         //create the status line from the status string
158         final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
159         return this.responseFactory.newHttpResponse(statusline, null);
160     }
161
162     protected boolean reject(final CharArrayBuffer line, final int count) {
163         return false;
164     }
165
166 }
167