5656@ NoArgsConstructor
5757public class HttpRequest implements Request {
5858
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
6061
6162 private static final ByteBuf EMPTY_BUF = Unpooled .copiedBuffer ("" , CharsetUtil .UTF_8 );
6263
@@ -73,13 +74,15 @@ public class HttpRequest implements Request {
7374 private String url ;
7475 private String protocol ;
7576 private String method ;
77+ private String cookieString ;
7678 private boolean keepAlive ;
7779 private Session session ;
7880
7981 private boolean isRequestPart ;
8082 private boolean isChunked ;
8183 private boolean isMultipart ;
8284 private boolean isEnd ;
85+ private boolean initCookie ;
8386
8487 private Map <String , String > headers = null ;
8588 private Map <String , Object > attributes = null ;
@@ -147,10 +150,10 @@ public Map<String, String> pathParams() {
147150
148151 @ Override
149152 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 "" ;
152155 }
153- return "" ;
156+ return url . substring ( url . indexOf ( "?" ) + 1 ) ;
154157 }
155158
156159 @ Override
@@ -180,10 +183,14 @@ public HttpMethod httpMethod() {
180183
181184 @ Override
182185 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+
184190 if (!useGZIP ) {
185191 return false ;
186192 }
193+
187194 String acceptEncoding = this .header (HttpConst .ACCEPT_ENCODING );
188195 if (StringKit .isEmpty (acceptEncoding )) {
189196 return false ;
@@ -209,6 +216,10 @@ public boolean isIE() {
209216
210217 @ Override
211218 public Map <String , Cookie > cookies () {
219+ if (!initCookie && StringKit .isNotEmpty (cookieString )) {
220+ initCookie = true ;
221+ ServerCookieDecoder .LAX .decode (cookieString ).forEach (this ::parseCookie );
222+ }
212223 return this .cookies ;
213224 }
214225
@@ -336,80 +347,76 @@ public static HttpRequest build(String remoteAddress, HttpObject msg) {
336347 request .uri = cleanUri ;
337348 }
338349
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+ }
342355 }
343356
344357 HttpServerHandler .setLocalContext (new LocalContext (msg , request , decoder ));
345358 return request ;
346359 }
347360
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+
349365 // headers
350366 var httpHeaders = nettyRequest .headers ();
351367 if (httpHeaders .isEmpty ()) {
352368 request .headers = new HashMap <>();
353369 } else {
354370 request .headers = new HashMap <>(httpHeaders .size ());
355371
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+ }
363377 }
364378
365379 // 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+ }
370387 }
371388
372- request .initCookie ();
389+ // cookies
390+ request .cookieString = request .header (HttpConst .COOKIE_STRING );
373391
374392 if ("GET" .equals (request .method ())) {
375393 return null ;
376394 }
377395
378396 try {
379- HttpPostRequestDecoder decoder = new HttpPostRequestDecoder (factory , nettyRequest );
397+ HttpPostRequestDecoder decoder =
398+ new HttpPostRequestDecoder (factory , nettyRequest );
399+
380400 request .isMultipart = decoder .isMultipart ();
381401 return decoder ;
382402 } catch (Exception e ) {
383403 throw new HttpParseException ("build decoder fail" , e );
384404 }
385405 }
386406
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-
396407 /**
397- * Example of reading request by chunk and getting values from chunk to chunk
408+ * Reading request by chunk and getting values from chunk
398409 */
399- private boolean readHttpDataChunkByChunk (HttpPostRequestDecoder decoder ) {
410+ private void readHttpDataChunkByChunk (HttpPostRequestDecoder decoder ) {
400411 try {
401- boolean read = false ;
402412 while (decoder .hasNext ()) {
403- read = true ;
404413 InterfaceHttpData data = decoder .next ();
405414 if (data != null ) {
406415 parseData (data );
407416 }
408417 }
409- return read ;
410418 } catch (HttpPostRequestDecoder .EndOfDataDecoderException e ) {
411419 // ignore
412- return true ;
413420 } catch (Exception e ) {
414421 throw new HttpParseException (e );
415422 }
@@ -453,7 +460,6 @@ private void parseAttribute(Attribute attribute) throws IOException {
453460 * Parse FileUpload to {@link FileItem}.
454461 *
455462 * @param fileUpload netty http file upload
456- * @throws IOException
457463 */
458464 private void parseFileUpload (FileUpload fileUpload ) throws IOException {
459465 if (!fileUpload .isCompleted ()) {
@@ -466,8 +472,11 @@ private void parseFileUpload(FileUpload fileUpload) throws IOException {
466472 // Upload the file is moved to the specified temporary file,
467473 // because FileUpload will be release after completion of the analysis.
468474 // 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 );
471480
472481 fileItem .setFile (tmpFile .toFile ());
473482 fileItem .setPath (tmpFile .toFile ().getPath ());
0 commit comments