1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/DefaultResponseParser.java $
3 * $Revision: 617638 $
4 * $Date: 2008-02-01 12:49:26 -0800 (Fri, 01 Feb 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.impl.conn;
33
34import java.io.IOException;
35
36import org.apache.http.HttpException;
37import org.apache.http.HttpMessage;
38import org.apache.http.HttpResponseFactory;
39import org.apache.http.NoHttpResponseException;
40import org.apache.http.ProtocolException;
41import org.apache.http.StatusLine;
42import org.apache.http.conn.params.ConnConnectionPNames;
43import org.apache.http.impl.io.AbstractMessageParser;
44import org.apache.http.io.SessionInputBuffer;
45import org.apache.http.message.LineParser;
46import org.apache.http.message.ParserCursor;
47import org.apache.http.params.HttpParams;
48import org.apache.http.util.CharArrayBuffer;
49/**
50 * @deprecated Please use {@link java.net.URL#openConnection} instead.
51 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
52 *     for further details.
53*/
54
55@Deprecated
56public class DefaultResponseParser extends AbstractMessageParser {
57
58    private final HttpResponseFactory responseFactory;
59    private final CharArrayBuffer lineBuf;
60    private final int maxGarbageLines;
61
62    public DefaultResponseParser(
63            final SessionInputBuffer buffer,
64            final LineParser parser,
65            final HttpResponseFactory responseFactory,
66            final HttpParams params) {
67        super(buffer, parser, params);
68        if (responseFactory == null) {
69            throw new IllegalArgumentException
70                ("Response factory may not be null");
71        }
72        this.responseFactory = responseFactory;
73        this.lineBuf = new CharArrayBuffer(128);
74        this.maxGarbageLines = params.getIntParameter(
75            ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE, Integer.MAX_VALUE);
76    }
77
78
79    @Override
80    protected HttpMessage parseHead(
81            final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
82        // clear the buffer
83        this.lineBuf.clear();
84        //read out the HTTP status string
85        int count = 0;
86        ParserCursor cursor = null;
87        do {
88            int i = sessionBuffer.readLine(this.lineBuf);
89            if (i == -1 && count == 0) {
90                // The server just dropped connection on us
91                throw new NoHttpResponseException("The target server failed to respond");
92            }
93            cursor = new ParserCursor(0, this.lineBuf.length());
94            if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
95                // Got one
96                break;
97            } else if (i == -1 || count >= this.maxGarbageLines) {
98                // Giving up
99                throw new ProtocolException("The server failed to respond with a " +
100                        "valid HTTP response");
101            }
102            count++;
103        } while(true);
104        //create the status line from the status string
105        StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
106        return this.responseFactory.newHttpResponse(statusline, null);
107    }
108
109}
110