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