1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
47
48
49
50
51 public class DefaultHttpRequestParser extends AbstractMessageParser<ClassicHttpRequest> {
52
53 private final HttpRequestFactory<ClassicHttpRequest> requestFactory;
54
55
56
57
58
59
60
61
62
63
64
65
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
77
78 public DefaultHttpRequestParser(final Http1Config http1Config) {
79 this(null, null, http1Config);
80 }
81
82
83
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 }