1111import io .netty .buffer .ByteBuf ;
1212import io .netty .buffer .Unpooled ;
1313import io .netty .handler .codec .http .DefaultFullHttpRequest ;
14- import io .netty .handler .codec .http .DefaultHttpHeaders ;
1514import io .netty .handler .codec .http .FullHttpRequest ;
1615import io .netty .handler .codec .http .HttpHeaderNames ;
1716import io .netty .handler .codec .http .HttpHeaders ;
@@ -41,59 +40,35 @@ public class Netty4HttpRequest implements HttpRequest {
4140
4241 private final FullHttpRequest request ;
4342 private final BytesReference content ;
44- private final HttpHeadersMap headers ;
43+ private final Map < String , List < String >> headers ;
4544 private final AtomicBoolean released ;
4645 private final Exception inboundException ;
4746 private final boolean pooled ;
48-
4947 private final int sequence ;
5048
5149 Netty4HttpRequest (int sequence , FullHttpRequest request ) {
52- this (
53- sequence ,
54- request ,
55- new HttpHeadersMap (request .headers ()),
56- new AtomicBoolean (false ),
57- true ,
58- Netty4Utils .toBytesReference (request .content ())
59- );
50+ this (sequence , request , new AtomicBoolean (false ), true , Netty4Utils .toBytesReference (request .content ()));
6051 }
6152
6253 Netty4HttpRequest (int sequence , FullHttpRequest request , Exception inboundException ) {
63- this (
64- sequence ,
65- request ,
66- new HttpHeadersMap (request .headers ()),
67- new AtomicBoolean (false ),
68- true ,
69- Netty4Utils .toBytesReference (request .content ()),
70- inboundException
71- );
54+ this (sequence , request , new AtomicBoolean (false ), true , Netty4Utils .toBytesReference (request .content ()), inboundException );
7255 }
7356
74- private Netty4HttpRequest (
75- int sequence ,
76- FullHttpRequest request ,
77- HttpHeadersMap headers ,
78- AtomicBoolean released ,
79- boolean pooled ,
80- BytesReference content
81- ) {
82- this (sequence , request , headers , released , pooled , content , null );
57+ private Netty4HttpRequest (int sequence , FullHttpRequest request , AtomicBoolean released , boolean pooled , BytesReference content ) {
58+ this (sequence , request , released , pooled , content , null );
8359 }
8460
8561 private Netty4HttpRequest (
8662 int sequence ,
8763 FullHttpRequest request ,
88- HttpHeadersMap headers ,
8964 AtomicBoolean released ,
9065 boolean pooled ,
9166 BytesReference content ,
9267 Exception inboundException
9368 ) {
9469 this .sequence = sequence ;
9570 this .request = request ;
96- this .headers = headers ;
71+ this .headers = getHttpHeadersAsMap ( request . headers ()) ;
9772 this .content = content ;
9873 this .pooled = pooled ;
9974 this .released = released ;
@@ -102,36 +77,7 @@ private Netty4HttpRequest(
10277
10378 @ Override
10479 public RestRequest .Method method () {
105- HttpMethod httpMethod = request .method ();
106- if (httpMethod == HttpMethod .GET ) return RestRequest .Method .GET ;
107-
108- if (httpMethod == HttpMethod .POST ) return RestRequest .Method .POST ;
109-
110- if (httpMethod == HttpMethod .PUT ) return RestRequest .Method .PUT ;
111-
112- if (httpMethod == HttpMethod .DELETE ) return RestRequest .Method .DELETE ;
113-
114- if (httpMethod == HttpMethod .HEAD ) {
115- return RestRequest .Method .HEAD ;
116- }
117-
118- if (httpMethod == HttpMethod .OPTIONS ) {
119- return RestRequest .Method .OPTIONS ;
120- }
121-
122- if (httpMethod == HttpMethod .PATCH ) {
123- return RestRequest .Method .PATCH ;
124- }
125-
126- if (httpMethod == HttpMethod .TRACE ) {
127- return RestRequest .Method .TRACE ;
128- }
129-
130- if (httpMethod == HttpMethod .CONNECT ) {
131- return RestRequest .Method .CONNECT ;
132- }
133-
134- throw new IllegalArgumentException ("Unexpected http method: " + httpMethod );
80+ return translateRequestMethod (request .method ());
13581 }
13682
13783 @ Override
@@ -170,7 +116,6 @@ public HttpRequest releaseAndCopy() {
170116 request .headers (),
171117 request .trailingHeaders ()
172118 ),
173- headers ,
174119 new AtomicBoolean (false ),
175120 false ,
176121 Netty4Utils .toBytesReference (copiedContent )
@@ -210,28 +155,19 @@ public HttpVersion protocolVersion() {
210155
211156 @ Override
212157 public HttpRequest removeHeader (String header ) {
213- HttpHeaders headersWithoutContentTypeHeader = new DefaultHttpHeaders ();
214- headersWithoutContentTypeHeader .add (request .headers ());
215- headersWithoutContentTypeHeader .remove (header );
216- HttpHeaders trailingHeaders = new DefaultHttpHeaders ();
217- trailingHeaders .add (request .trailingHeaders ());
218- trailingHeaders .remove (header );
158+ HttpHeaders copiedHeadersWithout = request .headers ().copy ();
159+ copiedHeadersWithout .remove (header );
160+ HttpHeaders copiedTrailingHeadersWithout = request .trailingHeaders ().copy ();
161+ copiedTrailingHeadersWithout .remove (header );
219162 FullHttpRequest requestWithoutHeader = new DefaultFullHttpRequest (
220163 request .protocolVersion (),
221164 request .method (),
222165 request .uri (),
223166 request .content (),
224- headersWithoutContentTypeHeader ,
225- trailingHeaders
226- );
227- return new Netty4HttpRequest (
228- sequence ,
229- requestWithoutHeader ,
230- new HttpHeadersMap (requestWithoutHeader .headers ()),
231- released ,
232- pooled ,
233- content
167+ copiedHeadersWithout ,
168+ copiedTrailingHeadersWithout
234169 );
170+ return new Netty4HttpRequest (sequence , requestWithoutHeader , released , pooled , content );
235171 }
236172
237173 @ Override
@@ -249,6 +185,46 @@ public Exception getInboundException() {
249185 return inboundException ;
250186 }
251187
188+ public io .netty .handler .codec .http .HttpRequest getNettyRequest () {
189+ return request ;
190+ }
191+
192+ public static RestRequest .Method translateRequestMethod (HttpMethod httpMethod ) {
193+ if (httpMethod == HttpMethod .GET ) return RestRequest .Method .GET ;
194+
195+ if (httpMethod == HttpMethod .POST ) return RestRequest .Method .POST ;
196+
197+ if (httpMethod == HttpMethod .PUT ) return RestRequest .Method .PUT ;
198+
199+ if (httpMethod == HttpMethod .DELETE ) return RestRequest .Method .DELETE ;
200+
201+ if (httpMethod == HttpMethod .HEAD ) {
202+ return RestRequest .Method .HEAD ;
203+ }
204+
205+ if (httpMethod == HttpMethod .OPTIONS ) {
206+ return RestRequest .Method .OPTIONS ;
207+ }
208+
209+ if (httpMethod == HttpMethod .PATCH ) {
210+ return RestRequest .Method .PATCH ;
211+ }
212+
213+ if (httpMethod == HttpMethod .TRACE ) {
214+ return RestRequest .Method .TRACE ;
215+ }
216+
217+ if (httpMethod == HttpMethod .CONNECT ) {
218+ return RestRequest .Method .CONNECT ;
219+ }
220+
221+ throw new IllegalArgumentException ("Unexpected http method: " + httpMethod );
222+ }
223+
224+ public static Map <String , List <String >> getHttpHeadersAsMap (HttpHeaders httpHeaders ) {
225+ return new HttpHeadersMap (httpHeaders );
226+ }
227+
252228 /**
253229 * A wrapper of {@link HttpHeaders} that implements a map to prevent copying unnecessarily. This class does not support modifications
254230 * and due to the underlying implementation, it performs case insensitive lookups of key to values.
0 commit comments