56
56
@ NoArgsConstructor
57
57
public class HttpRequest implements Request {
58
58
59
- private static final HttpDataFactory factory = new DefaultHttpDataFactory (DefaultHttpDataFactory .MINSIZE ); // Disk if size exceed
59
+ private static final HttpDataFactory factory =
60
+ new DefaultHttpDataFactory (DefaultHttpDataFactory .MINSIZE ); // Disk if size exceed
60
61
61
62
private static final ByteBuf EMPTY_BUF = Unpooled .copiedBuffer ("" , CharsetUtil .UTF_8 );
62
63
@@ -73,13 +74,15 @@ public class HttpRequest implements Request {
73
74
private String url ;
74
75
private String protocol ;
75
76
private String method ;
77
+ private String cookieString ;
76
78
private boolean keepAlive ;
77
79
private Session session ;
78
80
79
81
private boolean isRequestPart ;
80
82
private boolean isChunked ;
81
83
private boolean isMultipart ;
82
84
private boolean isEnd ;
85
+ private boolean initCookie ;
83
86
84
87
private Map <String , String > headers = null ;
85
88
private Map <String , Object > attributes = null ;
@@ -147,10 +150,10 @@ public Map<String, String> pathParams() {
147
150
148
151
@ Override
149
152
public String queryString () {
150
- if (null != this . url && this . url .contains ("?" )) {
151
- return this . url . substring ( this . url . indexOf ( "?" ) + 1 ) ;
153
+ if (null == url || ! url .contains ("?" )) {
154
+ return "" ;
152
155
}
153
- return "" ;
156
+ return url . substring ( url . indexOf ( "?" ) + 1 ) ;
154
157
}
155
158
156
159
@ Override
@@ -180,10 +183,14 @@ public HttpMethod httpMethod() {
180
183
181
184
@ Override
182
185
public boolean useGZIP () {
183
- boolean useGZIP = WebContext .blade ().environment ().getBoolean (Const .ENV_KEY_GZIP_ENABLE , false );
186
+
187
+ boolean useGZIP = WebContext .blade ().environment ()
188
+ .getBoolean (Const .ENV_KEY_GZIP_ENABLE , false );
189
+
184
190
if (!useGZIP ) {
185
191
return false ;
186
192
}
193
+
187
194
String acceptEncoding = this .header (HttpConst .ACCEPT_ENCODING );
188
195
if (StringKit .isEmpty (acceptEncoding )) {
189
196
return false ;
@@ -209,6 +216,10 @@ public boolean isIE() {
209
216
210
217
@ Override
211
218
public Map <String , Cookie > cookies () {
219
+ if (!initCookie && StringKit .isNotEmpty (cookieString )) {
220
+ initCookie = true ;
221
+ ServerCookieDecoder .LAX .decode (cookieString ).forEach (this ::parseCookie );
222
+ }
212
223
return this .cookies ;
213
224
}
214
225
@@ -336,80 +347,76 @@ public static HttpRequest build(String remoteAddress, HttpObject msg) {
336
347
request .uri = cleanUri ;
337
348
}
338
349
339
- SessionManager sessionManager = WebContext .blade ().sessionManager ();
340
- if (null != sessionManager ) {
341
- request .session = SESSION_HANDLER .createSession (request );
350
+ if (!HttpServerHandler .PERFORMANCE ) {
351
+ SessionManager sessionManager = WebContext .blade ().sessionManager ();
352
+ if (null != sessionManager ) {
353
+ request .session = SESSION_HANDLER .createSession (request );
354
+ }
342
355
}
343
356
344
357
HttpServerHandler .setLocalContext (new LocalContext (msg , request , decoder ));
345
358
return request ;
346
359
}
347
360
348
- private static HttpPostRequestDecoder initRequest (HttpRequest request , io .netty .handler .codec .http .HttpRequest nettyRequest ) {
361
+ private static HttpPostRequestDecoder initRequest (
362
+ HttpRequest request ,
363
+ io .netty .handler .codec .http .HttpRequest nettyRequest ) {
364
+
349
365
// headers
350
366
var httpHeaders = nettyRequest .headers ();
351
367
if (httpHeaders .isEmpty ()) {
352
368
request .headers = new HashMap <>();
353
369
} else {
354
370
request .headers = new HashMap <>(httpHeaders .size ());
355
371
356
- httpHeaders .forEach (entry -> request .headers .put (entry .getKey (), entry .getValue ()));
357
-
358
- // Iterator<Map.Entry<String, String>> entryIterator = httpHeaders.iteratorAsString();
359
- // while (entryIterator.hasNext()) {
360
- // var entry = entryIterator.next();
361
- // request.headers.put(entry.getKey(), entry.getValue());
362
- // }
372
+ Iterator <Map .Entry <String , String >> entryIterator = httpHeaders .iteratorAsString ();
373
+ while (entryIterator .hasNext ()) {
374
+ Map .Entry <String , String > next = entryIterator .next ();
375
+ request .headers .put (next .getKey (), next .getValue ());
376
+ }
363
377
}
364
378
365
379
// request query parameters
366
- var parameters = new QueryStringDecoder (request .url (), CharsetUtil .UTF_8 ).parameters ();
367
- if (null != parameters ) {
368
- request .parameters = new HashMap <>();
369
- request .parameters .putAll (parameters );
380
+ if (request .url ().contains ("?" )) {
381
+ var parameters = new QueryStringDecoder (request .url (), CharsetUtil .UTF_8 )
382
+ .parameters ();
383
+
384
+ if (null != parameters ) {
385
+ request .parameters .putAll (parameters );
386
+ }
370
387
}
371
388
372
- request .initCookie ();
389
+ // cookies
390
+ request .cookieString = request .header (HttpConst .COOKIE_STRING );
373
391
374
392
if ("GET" .equals (request .method ())) {
375
393
return null ;
376
394
}
377
395
378
396
try {
379
- HttpPostRequestDecoder decoder = new HttpPostRequestDecoder (factory , nettyRequest );
397
+ HttpPostRequestDecoder decoder =
398
+ new HttpPostRequestDecoder (factory , nettyRequest );
399
+
380
400
request .isMultipart = decoder .isMultipart ();
381
401
return decoder ;
382
402
} catch (Exception e ) {
383
403
throw new HttpParseException ("build decoder fail" , e );
384
404
}
385
405
}
386
406
387
- private void initCookie () {
388
- // cookies
389
- var cookie = this .header (HttpConst .COOKIE_STRING );
390
- cookie = cookie .length () > 0 ? cookie : this .header (HttpConst .COOKIE_STRING .toLowerCase ());
391
- if (null != cookie && cookie .length () > 0 ) {
392
- ServerCookieDecoder .LAX .decode (cookie ).forEach (this ::parseCookie );
393
- }
394
- }
395
-
396
407
/**
397
- * Example of reading request by chunk and getting values from chunk to chunk
408
+ * Reading request by chunk and getting values from chunk
398
409
*/
399
- private boolean readHttpDataChunkByChunk (HttpPostRequestDecoder decoder ) {
410
+ private void readHttpDataChunkByChunk (HttpPostRequestDecoder decoder ) {
400
411
try {
401
- boolean read = false ;
402
412
while (decoder .hasNext ()) {
403
- read = true ;
404
413
InterfaceHttpData data = decoder .next ();
405
414
if (data != null ) {
406
415
parseData (data );
407
416
}
408
417
}
409
- return read ;
410
418
} catch (HttpPostRequestDecoder .EndOfDataDecoderException e ) {
411
419
// ignore
412
- return true ;
413
420
} catch (Exception e ) {
414
421
throw new HttpParseException (e );
415
422
}
@@ -453,7 +460,6 @@ private void parseAttribute(Attribute attribute) throws IOException {
453
460
* Parse FileUpload to {@link FileItem}.
454
461
*
455
462
* @param fileUpload netty http file upload
456
- * @throws IOException
457
463
*/
458
464
private void parseFileUpload (FileUpload fileUpload ) throws IOException {
459
465
if (!fileUpload .isCompleted ()) {
@@ -466,8 +472,11 @@ private void parseFileUpload(FileUpload fileUpload) throws IOException {
466
472
// Upload the file is moved to the specified temporary file,
467
473
// because FileUpload will be release after completion of the analysis.
468
474
// tmpFile will be deleted automatically if they are used.
469
- Path tmpFile = Files .createTempFile (Paths .get (fileUpload .getFile ().getParent ()), "blade_" , "_upload" );
470
- Files .move (Paths .get (fileUpload .getFile ().getPath ()), tmpFile , StandardCopyOption .REPLACE_EXISTING );
475
+ Path tmpFile = Files .createTempFile (
476
+ Paths .get (fileUpload .getFile ().getParent ()), "blade_" , "_upload" );
477
+
478
+ Path fileUploadPath = Paths .get (fileUpload .getFile ().getPath ());
479
+ Files .move (fileUploadPath , tmpFile , StandardCopyOption .REPLACE_EXISTING );
471
480
472
481
fileItem .setFile (tmpFile .toFile ());
473
482
fileItem .setPath (tmpFile .toFile ().getPath ());
0 commit comments