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  
33  import org.apache.hc.core5.http.ClassicHttpRequest;
34  import org.apache.hc.core5.http.HttpException;
35  import org.apache.hc.core5.http.HttpRequestFactory;
36  import org.apache.hc.core5.http.MessageConstraintException;
37  import org.apache.hc.core5.http.RequestHeaderFieldsTooLargeException;
38  import org.apache.hc.core5.http.config.Http1Config;
39  import org.apache.hc.core5.http.io.SessionInputBuffer;
40  import org.apache.hc.core5.http.message.LineParser;
41  import org.apache.hc.core5.http.message.RequestLine;
42  import org.apache.hc.core5.util.CharArrayBuffer;
43  
44  /**
45   * HTTP request parser that obtain its input from an instance
46   * of {@link org.apache.hc.core5.http.io.SessionInputBuffer}.
47   *
48   * @since 4.2
49   */
50  public class DefaultHttpRequestParser extends AbstractMessageParser<ClassicHttpRequest> {
51  
52      private final HttpRequestFactory<ClassicHttpRequest> requestFactory;
53  
54      /**
55       * Creates new instance of DefaultHttpRequestParser.
56       *
57       * @param lineParser the line parser. If {@code null}
58       *   {@link org.apache.hc.core5.http.message.LazyLineParser#INSTANCE} will be used.
59       * @param requestFactory the response factory. If {@code null}
60       *   {@link DefaultClassicHttpRequestFactory#INSTANCE} will be used.
61       * @param http1Config the message http1Config. If {@code null}
62       *   {@link Http1Config#DEFAULT} will be used.
63       *
64       * @since 4.3
65       */
66      public DefaultHttpRequestParser(
67              final LineParser lineParser,
68              final HttpRequestFactory<ClassicHttpRequest> requestFactory,
69              final Http1Config http1Config) {
70          super(lineParser, http1Config);
71          this.requestFactory = requestFactory != null ? requestFactory : DefaultClassicHttpRequestFactory.INSTANCE;
72      }
73  
74      /**
75       * @since 4.3
76       */
77      public DefaultHttpRequestParser(final Http1Config http1Config) {
78          this(null, null, http1Config);
79      }
80  
81      /**
82       * @since 4.3
83       */
84      public DefaultHttpRequestParser() {
85          this(Http1Config.DEFAULT);
86      }
87  
88      @Override
89      public ClassicHttpRequest parse(
90              final SessionInputBuffer buffer, final InputStream inputStream) throws IOException, HttpException {
91          try {
92              return super.parse(buffer, inputStream);
93          } catch (final MessageConstraintException ex) {
94              throw new RequestHeaderFieldsTooLargeException(ex.getMessage(), ex);
95          }
96      }
97  
98      @Override
99      protected ClassicHttpRequest createMessage(final CharArrayBuffer buffer) throws IOException, HttpException {
100         final RequestLine requestLine = getLineParser().parseRequestLine(buffer);
101         final ClassicHttpRequest request = this.requestFactory.newHttpRequest(requestLine.getMethod(), requestLine.getUri());
102         request.setVersion(requestLine.getProtocolVersion());
103         return request;
104     }
105 
106 }