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