86
86
import org .springframework .http .MediaType ;
87
87
import org .springframework .util .CollectionUtils ;
88
88
import org .springframework .util .StringUtils ;
89
+ import org .springframework .web .util .UriUtils ;
89
90
import org .xml .sax .InputSource ;
90
91
import org .w3c .dom .Document ;
91
92
import org .w3c .dom .Node ;
@@ -509,13 +510,37 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
509
510
}
510
511
// params
511
512
Map <String , String > params = httpProtocol .getParams ();
513
+ boolean enableUrlEncoding = Boolean .parseBoolean (httpProtocol .getEnableUrlEncoding ());
514
+ StringBuilder queryParams = new StringBuilder ();
515
+
512
516
if (params != null && !params .isEmpty ()) {
513
517
for (Map .Entry <String , String > param : params .entrySet ()) {
514
- if (StringUtils .hasText (param .getValue ())) {
515
- requestBuilder .addParameter (param .getKey (), TimeExpressionUtil .calculate (param .getValue ()));
518
+ String key = param .getKey ();
519
+ String value = param .getValue ();
520
+
521
+ if (!StringUtils .hasText (key )) {
522
+ continue ;
523
+ }
524
+
525
+ if (queryParams .length () > 0 ) {
526
+ queryParams .append ("&" );
527
+ }
528
+
529
+ if (enableUrlEncoding ) {
530
+ key = UriUtils .encodeQueryParam (key , "UTF-8" );
531
+ }
532
+ queryParams .append (key );
533
+
534
+ if (StringUtils .hasText (value )) {
535
+ String calculatedValue = TimeExpressionUtil .calculate (value );
536
+ if (enableUrlEncoding ) {
537
+ calculatedValue = UriUtils .encodeQueryParam (calculatedValue , "UTF-8" );
538
+ }
539
+ queryParams .append ("=" ).append (calculatedValue );
516
540
}
517
541
}
518
542
}
543
+
519
544
// The default request header can be overridden if customized
520
545
// keep-alive
521
546
requestBuilder .addHeader (HttpHeaders .CONNECTION , NetworkConstants .KEEP_ALIVE );
@@ -525,8 +550,7 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
525
550
if (headers != null && !headers .isEmpty ()) {
526
551
for (Map .Entry <String , String > header : headers .entrySet ()) {
527
552
if (StringUtils .hasText (header .getValue ())) {
528
- requestBuilder .addHeader (CollectUtil .replaceUriSpecialChar (header .getKey ()),
529
- CollectUtil .replaceUriSpecialChar (header .getValue ()));
553
+ requestBuilder .addHeader (header .getKey (), header .getValue ());
530
554
}
531
555
}
532
556
}
@@ -560,23 +584,48 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
560
584
requestBuilder .setEntity (new StringEntity (httpProtocol .getPayload (), StandardCharsets .UTF_8 ));
561
585
}
562
586
563
- // uri
564
- String uri = CollectUtil .replaceUriSpecialChar (httpProtocol .getUrl ());
565
- if (IpDomainUtil .isHasSchema (httpProtocol .getHost ())) {
587
+ // uri encode
588
+ String uri ;
589
+ if (enableUrlEncoding ) {
590
+ // if the url contains parameters directly
591
+ if (httpProtocol .getUrl ().contains ("?" )) {
592
+ String path = httpProtocol .getUrl ().substring (0 , httpProtocol .getUrl ().indexOf ("?" ));
593
+ String query = httpProtocol .getUrl ().substring (httpProtocol .getUrl ().indexOf ("?" ) + 1 );
594
+ uri = UriUtils .encodePath (path , "UTF-8" ) + "?" + UriUtils .encodeQuery (query , "UTF-8" );
595
+ } else {
596
+ uri = UriUtils .encodePath (httpProtocol .getUrl (), "UTF-8" );
597
+ }
598
+ } else {
599
+ uri = httpProtocol .getUrl ();
600
+ }
566
601
567
- requestBuilder .setUri (httpProtocol .getHost () + ":" + httpProtocol .getPort () + uri );
602
+ // append query params
603
+ if (queryParams .length () > 0 ) {
604
+ uri += (uri .contains ("?" ) ? "&" : "?" ) + queryParams .toString ();
605
+ }
606
+
607
+ String finalUri ;
608
+ if (IpDomainUtil .isHasSchema (httpProtocol .getHost ())) {
609
+ finalUri = httpProtocol .getHost () + ":" + httpProtocol .getPort () + uri ;
568
610
} else {
569
611
String ipAddressType = IpDomainUtil .checkIpAddressType (httpProtocol .getHost ());
570
612
String baseUri = NetworkConstants .IPV6 .equals (ipAddressType )
571
613
? String .format ("[%s]:%s%s" , httpProtocol .getHost (), httpProtocol .getPort (), uri )
572
614
: String .format ("%s:%s%s" , httpProtocol .getHost (), httpProtocol .getPort (), uri );
573
615
boolean ssl = Boolean .parseBoolean (httpProtocol .getSsl ());
574
616
if (ssl ) {
575
- requestBuilder . setUri ( NetworkConstants .HTTPS_HEADER + baseUri ) ;
617
+ finalUri = NetworkConstants .HTTPS_HEADER + baseUri ;
576
618
} else {
577
- requestBuilder . setUri ( NetworkConstants .HTTP_HEADER + baseUri ) ;
619
+ finalUri = NetworkConstants .HTTP_HEADER + baseUri ;
578
620
}
579
621
}
622
+
623
+ try {
624
+ requestBuilder .setUri (finalUri );
625
+ } catch (IllegalArgumentException e ) {
626
+ log .warn ("Invalid URI with illegal characters: {}. User has disabled URL encoding, not applying any encoding." , finalUri );
627
+ throw e ;
628
+ }
580
629
581
630
// custom timeout
582
631
int timeout = CollectUtil .getTimeout (httpProtocol .getTimeout (), 0 );
0 commit comments