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       * @since 5.3
56       */
57      public DefaultHttpRequestParser(
58              final Http1Config http1Config,
59              final LineParser lineParser,
60              final HttpRequestFactory<ClassicHttpRequest> requestFactory) {
61          super(http1Config, lineParser);
62          this.requestFactory = requestFactory != null ? requestFactory : DefaultClassicHttpRequestFactory.INSTANCE;
63      }
64  
65      /**
66       * @deprecated Use {@link #DefaultHttpRequestParser(Http1Config, LineParser, HttpRequestFactory)}
67       */
68      @Deprecated
69      public DefaultHttpRequestParser(
70              final LineParser lineParser,
71              final HttpRequestFactory<ClassicHttpRequest> requestFactory,
72              final Http1Config http1Config) {
73          this(http1Config, lineParser, requestFactory);
74      }
75  
76      /**
77       * @since 4.3
78       */
79      public DefaultHttpRequestParser(final Http1Config http1Config) {
80          this(http1Config, null, null);
81      }
82  
83      /**
84       * @since 4.3
85       */
86      public DefaultHttpRequestParser() {
87          this(Http1Config.DEFAULT, null, null);
88      }
89  
90      @Override
91      public ClassicHttpRequest parse(
92              final SessionInputBuffer buffer, final InputStream inputStream) throws IOException, HttpException {
93          try {
94              return super.parse(buffer, inputStream);
95          } catch (final MessageConstraintException ex) {
96              throw new RequestHeaderFieldsTooLargeException(ex.getMessage(), ex);
97          }
98      }
99  
100     @Override
101     protected ClassicHttpRequest createMessage(final CharArrayBuffer buffer) throws IOException, HttpException {
102         final RequestLine requestLine = getLineParser().parseRequestLine(buffer);
103         final ClassicHttpRequest request = this.requestFactory.newHttpRequest(requestLine.getMethod(), requestLine.getUri());
104         request.setVersion(requestLine.getProtocolVersion());
105         return request;
106     }
107 
108 }