1
1
package top .meethigher .proxy .http ;
2
2
3
- import io .vertx .core .*;
3
+ import io .vertx .core .AsyncResult ;
4
+ import io .vertx .core .Future ;
5
+ import io .vertx .core .Handler ;
6
+ import io .vertx .core .Vertx ;
4
7
import io .vertx .core .http .*;
5
8
import io .vertx .core .json .JsonObject ;
6
- import io .vertx .core .net .SocketAddress ;
7
9
import io .vertx .ext .web .Route ;
8
10
import io .vertx .ext .web .Router ;
9
11
import io .vertx .ext .web .RoutingContext ;
@@ -412,10 +414,8 @@ protected boolean isHopByHopHeader(String headerName) {
412
414
* 复制请求头。复制的过程中忽略逐跳标头
413
415
*/
414
416
protected void copyRequestHeaders (RoutingContext ctx , HttpServerRequest realReq , HttpClientRequest proxyReq ) {
415
- MultiMap realHeaders = realReq .headers ();
416
- MultiMap proxyHeaders = proxyReq .headers ();
417
- proxyHeaders .clear ();
418
- for (String headerName : realHeaders .names ()) {
417
+ proxyReq .headers ().clear ();
418
+ for (String headerName : realReq .headers ().names ()) {
419
419
// 若是逐跳标头,则跳过
420
420
if (isHopByHopHeader (headerName )) {
421
421
continue ;
@@ -424,7 +424,7 @@ protected void copyRequestHeaders(RoutingContext ctx, HttpServerRequest realReq,
424
424
if ("host" .equalsIgnoreCase (headerName )) {
425
425
continue ;
426
426
}
427
- proxyHeaders . set (headerName , realHeaders .get (headerName ));
427
+ proxyReq . putHeader (headerName , realReq . headers () .get (headerName ));
428
428
}
429
429
430
430
// 传递真实客户端信息
@@ -451,28 +451,26 @@ protected void copyRequestHeaders(RoutingContext ctx, HttpServerRequest realReq,
451
451
* 复制响应头。复制的过程中忽略逐跳标头
452
452
*/
453
453
protected void copyResponseHeaders (RoutingContext ctx , HttpServerRequest realReq , HttpServerResponse realResp , HttpClientResponse proxyResp ) {
454
- MultiMap proxyHeaders = proxyResp .headers ();
455
- MultiMap realHeaders = realResp .headers ();
456
- realHeaders .clear ();
454
+ realResp .headers ().clear ();
457
455
458
456
Map <String , String > needSetHeaderMap = new LinkedHashMap <>();
459
- for (String headerName : proxyHeaders .names ()) {
457
+ for (String headerName : proxyResp . headers () .names ()) {
460
458
// 若是逐跳标头,则跳过
461
459
if (isHopByHopHeader (headerName )) {
462
460
continue ;
463
461
}
464
462
// 保留Cookie
465
463
if ("Set-Cookie" .equalsIgnoreCase (headerName ) || "Set-Cookie2" .equalsIgnoreCase (headerName )) {
466
464
if (getContextData (ctx , P_PRESERVE_COOKIES ) != null && Boolean .parseBoolean (getContextData (ctx , P_PRESERVE_COOKIES ).toString ())) {
467
- needSetHeaderMap .put (headerName , proxyHeaders .get (headerName ));
465
+ needSetHeaderMap .put (headerName , proxyResp . headers () .get (headerName ));
468
466
}
469
467
}
470
468
// 重写重定向Location
471
469
else if ("Location" .equalsIgnoreCase (headerName )) {
472
- String value = rewriteLocation (ctx , realReq .absoluteURI (), proxyHeaders .get (headerName ));
470
+ String value = rewriteLocation (ctx , realReq .absoluteURI (), proxyResp . headers () .get (headerName ));
473
471
needSetHeaderMap .put (headerName , value );
474
472
} else {
475
- needSetHeaderMap .put (headerName , proxyHeaders .get (headerName ));
473
+ needSetHeaderMap .put (headerName , proxyResp . headers () .get (headerName ));
476
474
}
477
475
}
478
476
// 跨域由代理掌控
@@ -504,7 +502,7 @@ else if ("Location".equalsIgnoreCase(headerName)) {
504
502
505
503
506
504
for (String key : needSetHeaderMap .keySet ()) {
507
- realHeaders .set (key , needSetHeaderMap .get (key ));
505
+ realResp . headers () .set (key , needSetHeaderMap .get (key ));
508
506
}
509
507
}
510
508
@@ -570,10 +568,8 @@ protected Handler<AsyncResult<HttpClientRequest>> connectHandler(RoutingContext
570
568
setContextData (ctx , INTERNAL_PROXY_SERVER_CONNECTION_OPEN , true );
571
569
572
570
// 注册客户端与代理服务之间连接的断开监听事件。可监听主动关闭和被动关闭
573
- HttpConnection connection = clientReq .connection ();
574
- SocketAddress localAddress = connection .localAddress ();
575
- setContextData (ctx , INTERNAL_CLIENT_LOCAL_ADDR , localAddress .hostAddress () + ":" + localAddress .port ());
576
- connection .closeHandler (v -> {
571
+ setContextData (ctx , INTERNAL_CLIENT_LOCAL_ADDR , clientReq .connection ().localAddress ().toString ());
572
+ clientReq .connection ().closeHandler (v -> {
577
573
setContextData (ctx , INTERNAL_PROXY_SERVER_CONNECTION_OPEN , false );
578
574
log .debug ("proxyClient local connection {} closed" ,
579
575
getContextData (ctx , INTERNAL_CLIENT_LOCAL_ADDR ).toString ());
@@ -592,7 +588,7 @@ protected Handler<AsyncResult<HttpClientRequest>> connectHandler(RoutingContext
592
588
}
593
589
} else if ((boolean ) getContextData (ctx , INTERNAL_PROXY_SERVER_CONNECTION_OPEN ) && !(boolean ) getContextData (ctx , INTERNAL_CLIENT_CONNECTION_OPEN )) {
594
590
// 整体链路连接不可用,释放资源
595
- connection .close ();
591
+ clientReq . connection () .close ();
596
592
}
597
593
} else {
598
594
badGateway (ctx , serverResp );
@@ -619,74 +615,67 @@ protected Handler<RoutingContext> routingContextHandler(HttpClient httpClient) {
619
615
// vertx的uri()是包含query参数的。而path()才是我们常说的不带有query的uri
620
616
// route不是线程安全的。route里的metadata应以路由为单元存储,而不是以请求为单元存储。一个路由会有很多请求。
621
617
// 若想要以请求为单元存储数据,应该使用routingContext.put
622
- Route route = ctx .currentRoute ();
623
618
// 将路由原数据,复制到请求上下文
624
- for (String key : route .metadata ().keySet ()) {
625
- setContextData (ctx , key , route .getMetadata (key ));
619
+ for (String key : ctx . currentRoute () .metadata ().keySet ()) {
620
+ setContextData (ctx , key , ctx . currentRoute () .getMetadata (key ));
626
621
}
627
622
628
623
// 记录请求开始时间
629
624
setContextData (ctx , INTERNAL_SEND_TIMESTAMP , System .currentTimeMillis ());
630
625
// 记录连接状态
631
626
setContextData (ctx , INTERNAL_CLIENT_CONNECTION_OPEN , true );
632
627
633
-
634
- HttpServerRequest serverReq = ctx .request ();
635
- HttpServerResponse serverResp = ctx .response ();
636
-
637
628
// 暂停流读取
638
- serverReq .pause ();
629
+ ctx . request () .pause ();
639
630
640
631
641
632
// 获取代理地址
642
- String proxyUrl = getProxyUrl (ctx , serverReq , serverResp );
633
+ String proxyUrl = getProxyUrl (ctx , ctx . request (), ctx . response () );
643
634
setContextData (ctx , INTERNAL_PROXY_URL , proxyUrl );
644
- setContextData (ctx , INTERNAL_SERVER_HTTP_VERSION , serverReq .version ().alpnName ());
645
- setContextData (ctx , INTERNAL_METHOD , serverReq .method ().name ());
646
- setContextData (ctx , INTERNAL_USER_AGENT , serverReq .getHeader ("User-Agent" ));
647
- setContextData (ctx , INTERNAL_SOURCE_URI , serverReq .uri ());
635
+ setContextData (ctx , INTERNAL_SERVER_HTTP_VERSION , ctx . request () .version ().alpnName ());
636
+ setContextData (ctx , INTERNAL_METHOD , ctx . request () .method ().name ());
637
+ setContextData (ctx , INTERNAL_USER_AGENT , ctx . request () .getHeader ("User-Agent" ));
638
+ setContextData (ctx , INTERNAL_SOURCE_URI , ctx . request () .uri ());
648
639
649
640
650
641
// 构建请求参数
651
642
RequestOptions requestOptions = new RequestOptions ();
652
643
requestOptions .setAbsoluteURI (proxyUrl );
653
- requestOptions .setMethod (serverReq .method ());
644
+ requestOptions .setMethod (ctx . request () .method ());
654
645
requestOptions .setFollowRedirects (getContextData (ctx , P_FOLLOW_REDIRECTS ) != null && Boolean .parseBoolean (getContextData (ctx , P_FOLLOW_REDIRECTS ).toString ()));
655
646
656
647
657
648
// 注册客户端与代理服务之间连接的断开监听事件。可监听主动关闭和被动关闭
658
- HttpConnection connection = serverReq .connection ();
659
- SocketAddress remoteAddress = connection .remoteAddress ();
660
- setContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR , remoteAddress .hostAddress () + ":" + remoteAddress .port ());
649
+ setContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR , ctx .request ().connection ().remoteAddress ().toString ());
661
650
662
- connection .closeHandler (v -> {
651
+ ctx . request (). connection () .closeHandler (v -> {
663
652
setContextData (ctx , INTERNAL_CLIENT_CONNECTION_OPEN , false );
664
653
log .debug ("proxyServer remote connection {} closed" , getContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR ).toString ());
665
654
});
666
655
667
656
// 如果跨域由代理服务接管,那么针对跨域使用的OPTIONS预检请求,就由代理服务接管,而不经过实际的后端服务
668
- if (HttpMethod .OPTIONS .name ().equalsIgnoreCase (serverReq .method ().name ()) &&
657
+ if (HttpMethod .OPTIONS .name ().equalsIgnoreCase (ctx . request () .method ().name ()) &&
669
658
getContextData (ctx , P_CORS_CONTROL ) != null && Boolean .parseBoolean (getContextData (ctx , P_CORS_CONTROL ).toString ()) &&
670
659
getContextData (ctx , P_ALLOW_CORS ) != null && Boolean .parseBoolean (getContextData (ctx , P_ALLOW_CORS ).toString ())
671
660
) {
672
- String header = serverReq .getHeader ("origin" );
661
+ String header = ctx . request () .getHeader ("origin" );
673
662
if (header == null || header .isEmpty ()) {
674
- serverResp .putHeader ("Access-Control-Allow-Origin" , "*" );
663
+ ctx . response () .putHeader ("Access-Control-Allow-Origin" , "*" );
675
664
} else {
676
- serverResp .putHeader ("Access-Control-Allow-Origin" , header );
665
+ ctx . response () .putHeader ("Access-Control-Allow-Origin" , header );
677
666
}
678
- serverResp .putHeader ("Access-Control-Allow-Methods" , "*" );
679
- serverResp .putHeader ("Access-Control-Allow-Headers" , "*" );
680
- serverResp .putHeader ("Access-Control-Allow-Credentials" , "true" );
681
- serverResp .putHeader ("Access-Control-Expose-Headers" , "*" );
682
- setStatusCode (ctx , serverResp , 200 ).end ();
667
+ ctx . response () .putHeader ("Access-Control-Allow-Methods" , "*" );
668
+ ctx . response () .putHeader ("Access-Control-Allow-Headers" , "*" );
669
+ ctx . response () .putHeader ("Access-Control-Allow-Credentials" , "true" );
670
+ ctx . response () .putHeader ("Access-Control-Expose-Headers" , "*" );
671
+ setStatusCode (ctx , ctx . response () , 200 ).end ();
683
672
doLog (ctx );
684
673
return ;
685
674
}
686
675
687
676
// 请求
688
677
if ((boolean ) getContextData (ctx , INTERNAL_CLIENT_CONNECTION_OPEN )) {
689
- httpClient .request (requestOptions ).onComplete (connectHandler (ctx , serverReq , serverResp , proxyUrl ));
678
+ httpClient .request (requestOptions ).onComplete (connectHandler (ctx , ctx . request (), ctx . response () , proxyUrl ));
690
679
}
691
680
};
692
681
}
0 commit comments