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.hc.core5.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import org.apache.hc.core5.http.ConnectionClosedException;
36  import org.apache.hc.core5.http.Header;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpMessage;
39  import org.apache.hc.core5.http.MessageConstraintException;
40  import org.apache.hc.core5.http.config.Http1Config;
41  import org.apache.hc.core5.http.io.HttpMessageParser;
42  import org.apache.hc.core5.http.io.SessionInputBuffer;
43  import org.apache.hc.core5.http.message.LazyLineParser;
44  import org.apache.hc.core5.http.message.LineParser;
45  import org.apache.hc.core5.util.Args;
46  import org.apache.hc.core5.util.CharArrayBuffer;
47  
48  /**
49   * Abstract base class for HTTP message parsers that obtain input from
50   * an instance of {@link org.apache.hc.core5.http.io.SessionInputBuffer}.
51   *
52   * @since 4.0
53   */
54  public abstract class AbstractMessageParser<T extends HttpMessage> implements HttpMessageParser<T> {
55  
56      private static final int HEAD_LINE    = 0;
57      private static final int HEADERS      = 1;
58  
59      private final Http1Config http1Config;
60      private final List<CharArrayBuffer> headerLines;
61      private final CharArrayBuffer headLine;
62      private final LineParser lineParser;
63  
64      private int state;
65      private T message;
66  
67      /**
68       * @since 5.3
69       */
70      public AbstractMessageParser(final Http1Config http1Config, final LineParser lineParser) {
71          super();
72          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
73          this.lineParser = lineParser != null ? lineParser : LazyLineParser.INSTANCE;
74          this.headerLines = new ArrayList<>();
75          this.headLine = new CharArrayBuffer(128);
76          this.state = HEAD_LINE;
77      }
78  
79      /**
80       * @deprecated Use {@link #AbstractMessageParser(Http1Config, LineParser)}
81       */
82      @Deprecated
83      public AbstractMessageParser(final LineParser lineParser, final Http1Config http1Config) {
84          this(http1Config, lineParser);
85      }
86  
87      LineParser getLineParser() {
88          return this.lineParser;
89      }
90  
91      /**
92       * Parses HTTP headers from the data receiver stream according to the generic
93       * format as specified by the HTTP/1.1 protocol specification.
94       *
95       * @param inBuffer Session input buffer
96       * @param inputStream Input stream
97       * @param maxHeaderCount maximum number of headers allowed. If the number
98       *  of headers received from the data stream exceeds maxCount value, an
99       *  IOException will be thrown. Setting this parameter to a negative value
100      *  or zero will disable the check.
101      * @param maxLineLen maximum number of characters for a header line,
102      *  including the continuation lines. Setting this parameter to a negative
103      *  value or zero will disable the check.
104      * @return array of HTTP headers
105      * @param lineParser the line parser. If {@code null}
106      *   {@link org.apache.hc.core5.http.message.LazyLineParser#INSTANCE} will be used
107      *
108      * @throws IOException in case of an I/O error
109      * @throws HttpException in case of HTTP protocol violation
110      */
111     public static Header[] parseHeaders(
112             final SessionInputBuffer inBuffer,
113             final InputStream inputStream,
114             final int maxHeaderCount,
115             final int maxLineLen,
116             final LineParser lineParser) throws HttpException, IOException {
117         final List<CharArrayBuffer> headerLines = new ArrayList<>();
118         return parseHeaders(inBuffer, inputStream, maxHeaderCount, maxLineLen,
119                 lineParser != null ? lineParser : LazyLineParser.INSTANCE, headerLines);
120     }
121 
122     /**
123      * Parses HTTP headers from the data receiver stream according to the generic
124      * format as specified by the HTTP/1.1 protocol specification.
125      *
126      * @param inBuffer Session input buffer
127      * @param inputStream Input stream
128      * @param maxHeaderCount maximum number of headers allowed. If the number
129      *  of headers received from the data stream exceeds maxCount value, an
130      *  IOException will be thrown. Setting this parameter to a negative value
131      *  or zero will disable the check.
132      * @param maxLineLen maximum number of characters for a header line,
133      *  including the continuation lines. Setting this parameter to a negative
134      *  value or zero will disable the check.
135      * @param parser line parser to use.
136      * @param headerLines List of header lines. This list will be used to store
137      *   intermediate results. This makes it possible to resume parsing of
138      *   headers in case of a {@link java.io.InterruptedIOException}.
139      *
140      * @return array of HTTP headers
141      *
142      * @throws IOException in case of an I/O error
143      * @throws HttpException in case of HTTP protocol violation
144      *
145      * @since 4.1
146      */
147     public static Header[] parseHeaders(
148             final SessionInputBuffer inBuffer,
149             final InputStream inputStream,
150             final int maxHeaderCount,
151             final int maxLineLen,
152             final LineParser parser,
153             final List<CharArrayBuffer> headerLines) throws HttpException, IOException {
154         Args.notNull(inBuffer, "Session input buffer");
155         Args.notNull(inputStream, "Input stream");
156         Args.notNull(parser, "Line parser");
157         Args.notNull(headerLines, "Header line list");
158 
159         CharArrayBuffer current = null;
160         CharArrayBuffer previous = null;
161         for (;;) {
162             if (current == null) {
163                 current = new CharArrayBuffer(64);
164             } else {
165                 current.clear();
166             }
167             final int readLen = inBuffer.readLine(current, inputStream);
168             if (readLen == -1 || current.length() < 1) {
169                 break;
170             }
171             // Parse the header name and value
172             // Check for folded headers first
173             // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
174             // discussion on folded headers
175             if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
176                 // we have continuation folded header
177                 // so append value
178                 int i = 0;
179                 while (i < current.length()) {
180                     final char ch = current.charAt(i);
181                     if (ch != ' ' && ch != '\t') {
182                         break;
183                     }
184                     i++;
185                 }
186                 if (maxLineLen > 0
187                         && previous.length() + 1 + current.length() - i > maxLineLen) {
188                     throw new MessageConstraintException("Maximum line length limit exceeded");
189                 }
190                 previous.append(' ');
191                 previous.append(current, i, current.length() - i);
192             } else {
193                 headerLines.add(current);
194                 previous = current;
195                 current = null;
196             }
197             if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
198                 throw new MessageConstraintException("Maximum header count exceeded");
199             }
200         }
201         final Header[] headers = new Header[headerLines.size()];
202         for (int i = 0; i < headerLines.size(); i++) {
203             final CharArrayBuffer buffer = headerLines.get(i);
204             headers[i] = parser.parseHeader(buffer);
205         }
206         return headers;
207     }
208 
209     /**
210      * Subclasses must override this method to generate an instance of
211      * {@link HttpMessage} based on the initial input from the session buffer.
212      * <p>
213      * Usually this method is expected to read just the very first line or
214      * the very first valid from the data stream and based on the input generate
215      * an appropriate instance of {@link HttpMessage}.
216      *
217      * @param buffer the session input buffer.
218      * @return HTTP message based on the input from the session buffer.
219      * @throws IOException in case of an I/O error.
220      * @throws HttpException in case of HTTP protocol violation.
221      *
222      * @since 5.0
223      */
224     protected abstract T createMessage(CharArrayBuffer buffer) throws IOException, HttpException;
225 
226     /**
227      * Subclasses must override this method to generate an appropriate exception
228      * in case of unexpected connection termination by the peer endpoint.
229      *
230      * @since 5.0
231      *
232      * @deprecated do not use.
233      */
234     @Deprecated
235     protected IOException createConnectionClosedException() {
236         return new ConnectionClosedException();
237     }
238 
239     @Override
240     public T parse(final SessionInputBuffer buffer, final InputStream inputStream) throws IOException, HttpException {
241         Args.notNull(buffer, "Session input buffer");
242         Args.notNull(inputStream, "Input stream");
243         final int st = this.state;
244         switch (st) {
245         case HEAD_LINE:
246             for (int n = 0; n < this.http1Config.getMaxEmptyLineCount(); n++) {
247                 this.headLine.clear();
248                 final int i = buffer.readLine(this.headLine, inputStream);
249                 if (i == -1) {
250                     return null;
251                 }
252                 if (this.headLine.length() > 0) {
253                     this.message = createMessage(this.headLine);
254                     if (this.message != null) {
255                         break;
256                     }
257                 }
258             }
259             if (this.message == null) {
260                 throw new MessageConstraintException("Maximum empty line limit exceeded");
261             }
262             this.state = HEADERS;
263             //$FALL-THROUGH$
264         case HEADERS:
265             final Header[] headers = AbstractMessageParser.parseHeaders(
266                     buffer,
267                     inputStream,
268                     this.http1Config.getMaxHeaderCount(),
269                     this.http1Config.getMaxLineLength(),
270                     this.lineParser,
271                     this.headerLines);
272             this.message.setHeaders(headers);
273             final T result = this.message;
274             this.message = null;
275             this.headerLines.clear();
276             this.state = HEAD_LINE;
277             return result;
278         default:
279             throw new IllegalStateException("Inconsistent parser state");
280         }
281     }
282 
283 }