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.http.impl.io;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.ConnectionClosedException;
33  import org.apache.http.HttpException;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpRequestFactory;
36  import org.apache.http.ParseException;
37  import org.apache.http.RequestLine;
38  import org.apache.http.config.MessageConstraints;
39  import org.apache.http.impl.DefaultHttpRequestFactory;
40  import org.apache.http.io.SessionInputBuffer;
41  import org.apache.http.message.LineParser;
42  import org.apache.http.message.ParserCursor;
43  import org.apache.http.params.HttpParams;
44  import org.apache.http.util.Args;
45  import org.apache.http.util.CharArrayBuffer;
46  
47  /**
48   * HTTP request parser that obtain its input from an instance
49   * of {@link SessionInputBuffer}.
50   *
51   * @since 4.2
52   */
53  @SuppressWarnings("deprecation")
54  public class DefaultHttpRequestParser extends AbstractMessageParser<HttpRequest> {
55  
56      private final HttpRequestFactory requestFactory;
57      private final CharArrayBuffer lineBuf;
58  
59      /**
60       * Creates an instance of this class.
61       *
62       * @param buffer the session input buffer.
63       * @param lineParser the line parser.
64       * @param requestFactory the factory to use to create
65       *    {@link HttpRequest}s.
66       * @param params HTTP parameters.
67       *
68       * @deprecated (4.3) use
69       *   {@link DefaultHttpRequestParser#DefaultHttpRequestParser(SessionInputBuffer, LineParser,
70       *     HttpRequestFactory, MessageConstraints)}
71       */
72      @Deprecated
73      public DefaultHttpRequestParser(
74              final SessionInputBuffer buffer,
75              final LineParser lineParser,
76              final HttpRequestFactory requestFactory,
77              final HttpParams params) {
78          super(buffer, lineParser, params);
79          this.requestFactory = Args.notNull(requestFactory, "Request factory");
80          this.lineBuf = new CharArrayBuffer(128);
81      }
82  
83      /**
84       * Creates new instance of DefaultHttpRequestParser.
85       *
86       * @param buffer the session input buffer.
87       * @param lineParser the line parser. If {@code null}
88       *   {@link org.apache.http.message.BasicLineParser#INSTANCE} will be used.
89       * @param requestFactory the response factory. If {@code null}
90       *   {@link DefaultHttpRequestFactory#INSTANCE} will be used.
91       * @param constraints the message constraints. If {@code null}
92       *   {@link MessageConstraints#DEFAULT} will be used.
93       *
94       * @since 4.3
95       */
96      public DefaultHttpRequestParser(
97              final SessionInputBuffer buffer,
98              final LineParser lineParser,
99              final HttpRequestFactory requestFactory,
100             final MessageConstraints constraints) {
101         super(buffer, lineParser, constraints);
102         this.requestFactory = requestFactory != null ? requestFactory :
103             DefaultHttpRequestFactory.INSTANCE;
104         this.lineBuf = new CharArrayBuffer(128);
105     }
106 
107     /**
108      * @since 4.3
109      */
110     public DefaultHttpRequestParser(
111             final SessionInputBuffer buffer,
112             final MessageConstraints constraints) {
113         this(buffer, null, null, constraints);
114     }
115 
116     /**
117      * @since 4.3
118      */
119     public DefaultHttpRequestParser(final SessionInputBuffer buffer) {
120         this(buffer, null, null, MessageConstraints.DEFAULT);
121     }
122 
123     @Override
124     protected HttpRequest parseHead(
125             final SessionInputBuffer sessionBuffer)
126         throws IOException, HttpException, ParseException {
127 
128         this.lineBuf.clear();
129         final int readLen = sessionBuffer.readLine(this.lineBuf);
130         if (readLen == -1) {
131             throw new ConnectionClosedException("Client closed connection");
132         }
133         final ParserCursorursor.html#ParserCursor">ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
134         final RequestLine requestline = this.lineParser.parseRequestLine(this.lineBuf, cursor);
135         return this.requestFactory.newHttpRequest(requestline);
136     }
137 
138 }