/
AbstractNettyHttpRequest.java
265 lines (239 loc) · 8.62 KB
/
AbstractNettyHttpRequest.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.netty;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.HttpParameters;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpVersion;
import io.micronaut.http.MediaType;
import io.micronaut.http.netty.stream.DefaultStreamedHttpRequest;
import io.micronaut.http.netty.stream.StreamedHttpRequest;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultLastHttpContent;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http2.HttpConversionUtil;
import io.netty.util.AsciiString;
import io.netty.util.DefaultAttributeMap;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Locale;
import java.util.Optional;
/**
* Abstract implementation of {@link HttpRequest} for Netty.
*
* @param <B> The body
* @author Graeme Rocher
* @since 1.0
*/
@Internal
public abstract class AbstractNettyHttpRequest<B> extends DefaultAttributeMap implements HttpRequest<B>, NettyHttpRequestBuilder {
public static final AsciiString STREAM_ID = HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text();
public static final AsciiString HTTP2_SCHEME = HttpConversionUtil.ExtensionHeaderNames.SCHEME.text();
protected final io.netty.handler.codec.http.HttpRequest nettyRequest;
protected final ConversionService<?> conversionService;
protected final HttpMethod httpMethod;
protected final URI uri;
protected final String httpMethodName;
private NettyHttpParameters httpParameters;
private Optional<MediaType> mediaType;
private Charset charset;
private Optional<Locale> locale;
private String path;
private Collection<MediaType> accept;
/**
* @param nettyRequest The Http netty request
* @param conversionService The conversion service
*/
public AbstractNettyHttpRequest(io.netty.handler.codec.http.HttpRequest nettyRequest, ConversionService conversionService) {
this.nettyRequest = nettyRequest;
this.conversionService = conversionService;
URI fullUri = URI.create(nettyRequest.uri());
if (fullUri.getAuthority() != null || fullUri.getScheme() != null) {
// http://example.com/foo -> /foo
try {
fullUri = new URI(
null, // scheme
null, // authority
fullUri.getPath(),
fullUri.getQuery(),
fullUri.getFragment()
);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
this.uri = fullUri;
this.httpMethodName = nettyRequest.method().name();
this.httpMethod = HttpMethod.parse(httpMethodName);
}
@NonNull
@Override
public io.netty.handler.codec.http.HttpRequest toHttpRequest() {
return this.nettyRequest;
}
@NonNull
@Override
public io.netty.handler.codec.http.FullHttpRequest toFullHttpRequest() {
if (this.nettyRequest instanceof io.netty.handler.codec.http.FullHttpRequest) {
return (io.netty.handler.codec.http.FullHttpRequest) this.nettyRequest;
}
DefaultFullHttpRequest httpRequest = new DefaultFullHttpRequest(
this.nettyRequest.protocolVersion(),
this.nettyRequest.method(),
this.nettyRequest.uri()
);
httpRequest.headers().setAll(this.nettyRequest.headers());
return httpRequest;
}
@NonNull
@Override
public StreamedHttpRequest toStreamHttpRequest() {
if (isStream()) {
return (StreamedHttpRequest) this.nettyRequest;
} else {
if (this.nettyRequest instanceof io.netty.handler.codec.http.FullHttpRequest) {
return new DefaultStreamedHttpRequest(
io.netty.handler.codec.http.HttpVersion.HTTP_1_1,
this.nettyRequest.method(),
this.nettyRequest.uri(),
true,
Publishers.just(new DefaultLastHttpContent(((io.netty.handler.codec.http.FullHttpRequest) this.nettyRequest).content()))
);
} else {
return new DefaultStreamedHttpRequest(
io.netty.handler.codec.http.HttpVersion.HTTP_1_1,
this.nettyRequest.method(),
this.nettyRequest.uri(),
true,
Publishers.just(LastHttpContent.EMPTY_LAST_CONTENT)
);
}
}
}
@Override
public boolean isStream() {
return this.nettyRequest instanceof StreamedHttpRequest;
}
@Override
public HttpVersion getHttpVersion() {
if (nettyRequest.headers().contains(STREAM_ID)) {
return HttpVersion.HTTP_2_0;
}
return HttpVersion.HTTP_1_1;
}
/**
* @return The native netty request
*/
public io.netty.handler.codec.http.HttpRequest getNettyRequest() {
return nettyRequest;
}
@Override
public HttpParameters getParameters() {
NettyHttpParameters httpParameters = this.httpParameters;
if (httpParameters == null) {
synchronized (this) { // double check
httpParameters = this.httpParameters;
if (httpParameters == null) {
httpParameters = decodeParameters();
this.httpParameters = httpParameters;
}
}
}
return httpParameters;
}
@Override
public Collection<MediaType> accept() {
if (accept == null) {
accept = HttpRequest.super.accept();
}
return accept;
}
@Override
public Optional<MediaType> getContentType() {
if (mediaType == null) {
mediaType = HttpRequest.super.getContentType();
}
return mediaType;
}
@Override
public Charset getCharacterEncoding() {
if (charset == null) {
charset = initCharset(HttpRequest.super.getCharacterEncoding());
}
return charset;
}
@Override
public Optional<Locale> getLocale() {
if (locale == null) {
locale = HttpRequest.super.getLocale();
}
return locale;
}
@Override
public HttpMethod getMethod() {
return httpMethod;
}
@Override
public URI getUri() {
return this.uri;
}
@Override
public String getPath() {
String path = this.path;
if (path == null) {
synchronized (this) { // double check
path = this.path;
if (path == null) {
path = decodePath();
this.path = path;
}
}
}
return path;
}
/**
* @param characterEncoding The charactger encoding
* @return The Charset
*/
protected abstract Charset initCharset(Charset characterEncoding);
/**
* @param uri The URI
* @return The query string decoder
*/
protected final QueryStringDecoder createDecoder(URI uri) {
Charset charset = getCharacterEncoding();
return charset != null ? new QueryStringDecoder(uri, charset) : new QueryStringDecoder(uri);
}
private String decodePath() {
QueryStringDecoder queryStringDecoder = createDecoder(uri);
return queryStringDecoder.rawPath();
}
private NettyHttpParameters decodeParameters() {
QueryStringDecoder queryStringDecoder = createDecoder(uri);
return new NettyHttpParameters(queryStringDecoder.parameters(), conversionService, null);
}
@Override
public String getMethodName() {
return httpMethodName;
}
}