mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/DefaultResponseParser.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl.conn;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31
michael@0 32 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0 33
michael@0 34 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
michael@0 35 /* LogFactory removed by HttpClient for Android script. */
michael@0 36 import ch.boye.httpclientandroidlib.HttpException;
michael@0 37 import ch.boye.httpclientandroidlib.HttpMessage;
michael@0 38 import ch.boye.httpclientandroidlib.HttpResponseFactory;
michael@0 39 import ch.boye.httpclientandroidlib.NoHttpResponseException;
michael@0 40 import ch.boye.httpclientandroidlib.ProtocolException;
michael@0 41 import ch.boye.httpclientandroidlib.StatusLine;
michael@0 42 import ch.boye.httpclientandroidlib.conn.params.ConnConnectionPNames;
michael@0 43 import ch.boye.httpclientandroidlib.impl.io.AbstractMessageParser;
michael@0 44 import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
michael@0 45 import ch.boye.httpclientandroidlib.message.LineParser;
michael@0 46 import ch.boye.httpclientandroidlib.message.ParserCursor;
michael@0 47 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 48 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 49
michael@0 50 /**
michael@0 51 * Default HTTP response parser implementation.
michael@0 52 * <p>
michael@0 53 * The following parameters can be used to customize the behavior of this
michael@0 54 * class:
michael@0 55 * <ul>
michael@0 56 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
michael@0 57 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
michael@0 58 * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnConnectionPNames#MAX_STATUS_LINE_GARBAGE}</li>
michael@0 59 * </ul>
michael@0 60 *
michael@0 61 * @since 4.0
michael@0 62 */
michael@0 63 @ThreadSafe // no public methods
michael@0 64 public class DefaultResponseParser extends AbstractMessageParser {
michael@0 65
michael@0 66 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
michael@0 67
michael@0 68 private final HttpResponseFactory responseFactory;
michael@0 69 private final CharArrayBuffer lineBuf;
michael@0 70 private final int maxGarbageLines;
michael@0 71
michael@0 72 public DefaultResponseParser(
michael@0 73 final SessionInputBuffer buffer,
michael@0 74 final LineParser parser,
michael@0 75 final HttpResponseFactory responseFactory,
michael@0 76 final HttpParams params) {
michael@0 77 super(buffer, parser, params);
michael@0 78 if (responseFactory == null) {
michael@0 79 throw new IllegalArgumentException
michael@0 80 ("Response factory may not be null");
michael@0 81 }
michael@0 82 this.responseFactory = responseFactory;
michael@0 83 this.lineBuf = new CharArrayBuffer(128);
michael@0 84 this.maxGarbageLines = params.getIntParameter(
michael@0 85 ConnConnectionPNames.MAX_STATUS_LINE_GARBAGE, Integer.MAX_VALUE);
michael@0 86 }
michael@0 87
michael@0 88
michael@0 89 @Override
michael@0 90 protected HttpMessage parseHead(
michael@0 91 final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
michael@0 92 //read out the HTTP status string
michael@0 93 int count = 0;
michael@0 94 ParserCursor cursor = null;
michael@0 95 do {
michael@0 96 // clear the buffer
michael@0 97 this.lineBuf.clear();
michael@0 98 int i = sessionBuffer.readLine(this.lineBuf);
michael@0 99 if (i == -1 && count == 0) {
michael@0 100 // The server just dropped connection on us
michael@0 101 throw new NoHttpResponseException("The target server failed to respond");
michael@0 102 }
michael@0 103 cursor = new ParserCursor(0, this.lineBuf.length());
michael@0 104 if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
michael@0 105 // Got one
michael@0 106 break;
michael@0 107 } else if (i == -1 || count >= this.maxGarbageLines) {
michael@0 108 // Giving up
michael@0 109 throw new ProtocolException("The server failed to respond with a " +
michael@0 110 "valid HTTP response");
michael@0 111 }
michael@0 112 if (this.log.isDebugEnabled()) {
michael@0 113 this.log.debug("Garbage in response: " + this.lineBuf.toString());
michael@0 114 }
michael@0 115 count++;
michael@0 116 } while(true);
michael@0 117 //create the status line from the status string
michael@0 118 StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
michael@0 119 return this.responseFactory.newHttpResponse(statusline, null);
michael@0 120 }
michael@0 121
michael@0 122 }

mercurial