mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/AbstractMessageParser.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/AbstractMessageParser.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,276 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.impl.io;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +import java.util.ArrayList;
    1.35 +import java.util.List;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.Header;
    1.38 +import ch.boye.httpclientandroidlib.HttpException;
    1.39 +import ch.boye.httpclientandroidlib.HttpMessage;
    1.40 +import ch.boye.httpclientandroidlib.ParseException;
    1.41 +import ch.boye.httpclientandroidlib.ProtocolException;
    1.42 +import ch.boye.httpclientandroidlib.io.HttpMessageParser;
    1.43 +import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
    1.44 +import ch.boye.httpclientandroidlib.message.LineParser;
    1.45 +import ch.boye.httpclientandroidlib.message.BasicLineParser;
    1.46 +import ch.boye.httpclientandroidlib.params.CoreConnectionPNames;
    1.47 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.48 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.49 +
    1.50 +/**
    1.51 + * Abstract base class for HTTP message parsers that obtain input from
    1.52 + * an instance of {@link SessionInputBuffer}.
    1.53 + * <p>
    1.54 + * The following parameters can be used to customize the behavior of this
    1.55 + * class:
    1.56 + * <ul>
    1.57 + *  <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
    1.58 + *  <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
    1.59 + * </ul>
    1.60 + *
    1.61 + * @since 4.0
    1.62 + */
    1.63 +public abstract class AbstractMessageParser implements HttpMessageParser {
    1.64 +
    1.65 +    private static final int HEAD_LINE    = 0;
    1.66 +    private static final int HEADERS      = 1;
    1.67 +
    1.68 +    private final SessionInputBuffer sessionBuffer;
    1.69 +    private final int maxHeaderCount;
    1.70 +    private final int maxLineLen;
    1.71 +    private final List headerLines;
    1.72 +    protected final LineParser lineParser;
    1.73 +
    1.74 +    private int state;
    1.75 +    private HttpMessage message;
    1.76 +
    1.77 +    /**
    1.78 +     * Creates an instance of this class.
    1.79 +     *
    1.80 +     * @param buffer the session input buffer.
    1.81 +     * @param parser the line parser.
    1.82 +     * @param params HTTP parameters.
    1.83 +     */
    1.84 +    public AbstractMessageParser(
    1.85 +            final SessionInputBuffer buffer,
    1.86 +            final LineParser parser,
    1.87 +            final HttpParams params) {
    1.88 +        super();
    1.89 +        if (buffer == null) {
    1.90 +            throw new IllegalArgumentException("Session input buffer may not be null");
    1.91 +        }
    1.92 +        if (params == null) {
    1.93 +            throw new IllegalArgumentException("HTTP parameters may not be null");
    1.94 +        }
    1.95 +        this.sessionBuffer = buffer;
    1.96 +        this.maxHeaderCount = params.getIntParameter(
    1.97 +                CoreConnectionPNames.MAX_HEADER_COUNT, -1);
    1.98 +        this.maxLineLen = params.getIntParameter(
    1.99 +                CoreConnectionPNames.MAX_LINE_LENGTH, -1);
   1.100 +        this.lineParser = (parser != null) ? parser : BasicLineParser.DEFAULT;
   1.101 +        this.headerLines = new ArrayList();
   1.102 +        this.state = HEAD_LINE;
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Parses HTTP headers from the data receiver stream according to the generic
   1.107 +     * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
   1.108 +     *
   1.109 +     * @param inbuffer Session input buffer
   1.110 +     * @param maxHeaderCount maximum number of headers allowed. If the number
   1.111 +     *  of headers received from the data stream exceeds maxCount value, an
   1.112 +     *  IOException will be thrown. Setting this parameter to a negative value
   1.113 +     *  or zero will disable the check.
   1.114 +     * @param maxLineLen maximum number of characters for a header line,
   1.115 +     *  including the continuation lines. Setting this parameter to a negative
   1.116 +     *  value or zero will disable the check.
   1.117 +     * @return array of HTTP headers
   1.118 +     * @param parser line parser to use. Can be <code>null</code>, in which case
   1.119 +     *  the default implementation of this interface will be used.
   1.120 +     *
   1.121 +     * @throws IOException in case of an I/O error
   1.122 +     * @throws HttpException in case of HTTP protocol violation
   1.123 +     */
   1.124 +    public static Header[] parseHeaders(
   1.125 +            final SessionInputBuffer inbuffer,
   1.126 +            int maxHeaderCount,
   1.127 +            int maxLineLen,
   1.128 +            LineParser parser)
   1.129 +        throws HttpException, IOException {
   1.130 +        if (parser == null) {
   1.131 +            parser = BasicLineParser.DEFAULT;
   1.132 +        }
   1.133 +        List headerLines = new ArrayList();
   1.134 +        return parseHeaders(inbuffer, maxHeaderCount, maxLineLen, parser, headerLines);
   1.135 +    }
   1.136 +
   1.137 +    /**
   1.138 +     * Parses HTTP headers from the data receiver stream according to the generic
   1.139 +     * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
   1.140 +     *
   1.141 +     * @param inbuffer Session input buffer
   1.142 +     * @param maxHeaderCount maximum number of headers allowed. If the number
   1.143 +     *  of headers received from the data stream exceeds maxCount value, an
   1.144 +     *  IOException will be thrown. Setting this parameter to a negative value
   1.145 +     *  or zero will disable the check.
   1.146 +     * @param maxLineLen maximum number of characters for a header line,
   1.147 +     *  including the continuation lines. Setting this parameter to a negative
   1.148 +     *  value or zero will disable the check.
   1.149 +     * @param parser line parser to use.
   1.150 +     * @param headerLines List of header lines. This list will be used to store
   1.151 +     *   intermediate results. This makes it possible to resume parsing of
   1.152 +     *   headers in case of a {@link java.io.InterruptedIOException}.
   1.153 +     *
   1.154 +     * @return array of HTTP headers
   1.155 +     *
   1.156 +     * @throws IOException in case of an I/O error
   1.157 +     * @throws HttpException in case of HTTP protocol violation
   1.158 +     *
   1.159 +     * @since 4.1
   1.160 +     */
   1.161 +    public static Header[] parseHeaders(
   1.162 +            final SessionInputBuffer inbuffer,
   1.163 +            int maxHeaderCount,
   1.164 +            int maxLineLen,
   1.165 +            final LineParser parser,
   1.166 +            final List headerLines)
   1.167 +        throws HttpException, IOException {
   1.168 +
   1.169 +        if (inbuffer == null) {
   1.170 +            throw new IllegalArgumentException("Session input buffer may not be null");
   1.171 +        }
   1.172 +        if (parser == null) {
   1.173 +            throw new IllegalArgumentException("Line parser may not be null");
   1.174 +        }
   1.175 +        if (headerLines == null) {
   1.176 +            throw new IllegalArgumentException("Header line list may not be null");
   1.177 +        }
   1.178 +
   1.179 +        CharArrayBuffer current = null;
   1.180 +        CharArrayBuffer previous = null;
   1.181 +        for (;;) {
   1.182 +            if (current == null) {
   1.183 +                current = new CharArrayBuffer(64);
   1.184 +            } else {
   1.185 +                current.clear();
   1.186 +            }
   1.187 +            int l = inbuffer.readLine(current);
   1.188 +            if (l == -1 || current.length() < 1) {
   1.189 +                break;
   1.190 +            }
   1.191 +            // Parse the header name and value
   1.192 +            // Check for folded headers first
   1.193 +            // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
   1.194 +            // discussion on folded headers
   1.195 +            if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
   1.196 +                // we have continuation folded header
   1.197 +                // so append value
   1.198 +                int i = 0;
   1.199 +                while (i < current.length()) {
   1.200 +                    char ch = current.charAt(i);
   1.201 +                    if (ch != ' ' && ch != '\t') {
   1.202 +                        break;
   1.203 +                    }
   1.204 +                    i++;
   1.205 +                }
   1.206 +                if (maxLineLen > 0
   1.207 +                        && previous.length() + 1 + current.length() - i > maxLineLen) {
   1.208 +                    throw new IOException("Maximum line length limit exceeded");
   1.209 +                }
   1.210 +                previous.append(' ');
   1.211 +                previous.append(current, i, current.length() - i);
   1.212 +            } else {
   1.213 +                headerLines.add(current);
   1.214 +                previous = current;
   1.215 +                current = null;
   1.216 +            }
   1.217 +            if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
   1.218 +                throw new IOException("Maximum header count exceeded");
   1.219 +            }
   1.220 +        }
   1.221 +        Header[] headers = new Header[headerLines.size()];
   1.222 +        for (int i = 0; i < headerLines.size(); i++) {
   1.223 +            CharArrayBuffer buffer = (CharArrayBuffer) headerLines.get(i);
   1.224 +            try {
   1.225 +                headers[i] = parser.parseHeader(buffer);
   1.226 +            } catch (ParseException ex) {
   1.227 +                throw new ProtocolException(ex.getMessage());
   1.228 +            }
   1.229 +        }
   1.230 +        return headers;
   1.231 +    }
   1.232 +
   1.233 +    /**
   1.234 +     * Subclasses must override this method to generate an instance of
   1.235 +     * {@link HttpMessage} based on the initial input from the session buffer.
   1.236 +     * <p>
   1.237 +     * Usually this method is expected to read just the very first line or
   1.238 +     * the very first valid from the data stream and based on the input generate
   1.239 +     * an appropriate instance of {@link HttpMessage}.
   1.240 +     *
   1.241 +     * @param sessionBuffer the session input buffer.
   1.242 +     * @return HTTP message based on the input from the session buffer.
   1.243 +     * @throws IOException in case of an I/O error.
   1.244 +     * @throws HttpException in case of HTTP protocol violation.
   1.245 +     * @throws ParseException in case of a parse error.
   1.246 +     */
   1.247 +    protected abstract HttpMessage parseHead(SessionInputBuffer sessionBuffer)
   1.248 +        throws IOException, HttpException, ParseException;
   1.249 +
   1.250 +    public HttpMessage parse() throws IOException, HttpException {
   1.251 +        int st = this.state;
   1.252 +        switch (st) {
   1.253 +        case HEAD_LINE:
   1.254 +            try {
   1.255 +                this.message = parseHead(this.sessionBuffer);
   1.256 +            } catch (ParseException px) {
   1.257 +                throw new ProtocolException(px.getMessage(), px);
   1.258 +            }
   1.259 +            this.state = HEADERS;
   1.260 +            //$FALL-THROUGH$
   1.261 +        case HEADERS:
   1.262 +            Header[] headers = AbstractMessageParser.parseHeaders(
   1.263 +                    this.sessionBuffer,
   1.264 +                    this.maxHeaderCount,
   1.265 +                    this.maxLineLen,
   1.266 +                    this.lineParser,
   1.267 +                    this.headerLines);
   1.268 +            this.message.setHeaders(headers);
   1.269 +            HttpMessage result = this.message;
   1.270 +            this.message = null;
   1.271 +            this.headerLines.clear();
   1.272 +            this.state = HEAD_LINE;
   1.273 +            return result;
   1.274 +        default:
   1.275 +            throw new IllegalStateException("Inconsistent parser state");
   1.276 +        }
   1.277 +    }
   1.278 +
   1.279 +}

mercurial