11
11
import io .vertx .ext .web .client .WebClientOptions ;
12
12
import org .apache .commons .lang3 .StringUtils ;
13
13
import org .code13k .zeroproxy .app .Env ;
14
- import org .code13k .zeroproxy .config .ProxyConfig ;
15
- import org .code13k .zeroproxy .model .ProxyResponse ;
16
- import org .code13k .zeroproxy .model .config .proxy .ProxyInfo ;
14
+ import org .code13k .zeroproxy .model .ProxyHttpResponse ;
15
+ import org .code13k .zeroproxy .model .config .proxy .ProxyHttpInfo ;
17
16
import org .slf4j .Logger ;
18
17
import org .slf4j .LoggerFactory ;
19
18
@@ -30,15 +29,14 @@ public class ProxyHttpClient {
30
29
31
30
// Data
32
31
private WebClient mWebClient ;
33
- private ProxyInfo mProxyInfo ;
34
- private int mRoundRobinIndex = 0 ;
32
+ private ProxyHttpInfo mProxyHttpInfo ;
35
33
36
34
/**
37
35
* Constructor
38
36
*/
39
- public ProxyHttpClient (ProxyInfo proxyInfo , int eventLoopPoolSize ) {
40
- mProxyInfo = proxyInfo ;
41
- mLogger .trace ("ProxyHttpClient() # " + mProxyInfo );
37
+ public ProxyHttpClient (ProxyHttpInfo proxyHttpInfo , int eventLoopPoolSize ) {
38
+ mProxyHttpInfo = proxyHttpInfo ;
39
+ mLogger .trace ("ProxyHttpClient() # " + mProxyHttpInfo );
42
40
43
41
// Set user-agent
44
42
String userAgent = "Code13k-ZeroProxy/" + Env .getInstance ().getVersionString ();
@@ -50,8 +48,8 @@ public ProxyHttpClient(ProxyInfo proxyInfo, int eventLoopPoolSize) {
50
48
webClientOptions .setTrustAll (true );
51
49
webClientOptions .setSsl (true );
52
50
webClientOptions .setTryUseCompression (true );
53
- webClientOptions .setConnectTimeout (mProxyInfo .getConnectTimeout ());
54
- webClientOptions .setIdleTimeout (mProxyInfo .getIdleTimeout ());
51
+ webClientOptions .setConnectTimeout (mProxyHttpInfo .getConnectTimeout ());
52
+ webClientOptions .setIdleTimeout (mProxyHttpInfo .getIdleTimeout ());
55
53
56
54
// Init VertxOptions
57
55
VertxOptions vertxOptions = new VertxOptions ();
@@ -64,7 +62,7 @@ public ProxyHttpClient(ProxyInfo proxyInfo, int eventLoopPoolSize) {
64
62
/**
65
63
* Proxy
66
64
*/
67
- public void proxy (String originPath , HttpMethod originMethod , MultiMap originHeaders , Buffer originBody , Consumer <ProxyResponse > consumer ) {
65
+ public void proxy (String originPath , HttpMethod originMethod , MultiMap originHeaders , Buffer originBody , Consumer <ProxyHttpResponse > consumer ) {
68
66
// Headers
69
67
final MultiMap headers = MultiMap .caseInsensitiveMultiMap ();
70
68
headers .addAll (originHeaders );
@@ -87,55 +85,31 @@ public void proxy(String originPath, HttpMethod originMethod, MultiMap originHea
87
85
requestTarget (uri , method , headers , body , new Consumer <HttpResponse <Buffer >>() {
88
86
@ Override
89
87
public void accept (HttpResponse <Buffer > response ) {
90
- /**
91
- * Multiple Request
92
- */
93
- if (ProxyConfig .isMultipleRequest (mProxyInfo ) == true ) {
94
- Map <String , Object > resultItem = new HashMap <>();
95
- if (response == null ) {
96
- resultItem .put ("uri" , uri );
97
- resultItem .put ("statusCode" , 504 );
98
- resultItem .put ("statusMessage" , "Gateway Time-out" );
99
- resultItem .put ("headers" , makeDefaultHeaders ());
100
- resultItem .put ("body" , "" );
101
- } else {
102
- resultItem .put ("uri" , uri );
103
- resultItem .put ("statusCode" , response .statusCode ());
104
- resultItem .put ("statusMessage" , response .statusMessage ());
105
- resultItem .put ("headers" , convertHeaders (response .headers ()));
106
- resultItem .put ("body" , response .bodyAsString ());
107
- }
108
- result .add (resultItem );
109
-
110
- // END
111
- if (processingCount .decrementAndGet () == 0 ) {
112
- Buffer proxyBody = Buffer .buffer (new GsonBuilder ().create ().toJson (result ));
113
- ProxyResponse proxyResponse = new ProxyResponse ();
114
- proxyResponse .setStatusCode (200 );
115
- proxyResponse .setStatusMessage ("OK" );
116
- proxyResponse .setHeaders (makeProxyResponseHeaders ());
117
- proxyResponse .setBody (proxyBody );
118
- consumer .accept (proxyResponse );
119
- }
88
+ Map <String , Object > resultItem = new HashMap <>();
89
+ if (response == null ) {
90
+ resultItem .put ("uri" , uri );
91
+ resultItem .put ("statusCode" , 504 );
92
+ resultItem .put ("statusMessage" , "Gateway Time-out" );
93
+ resultItem .put ("headers" , makeDefaultHeaders ());
94
+ resultItem .put ("body" , "" );
95
+ } else {
96
+ resultItem .put ("uri" , uri );
97
+ resultItem .put ("statusCode" , response .statusCode ());
98
+ resultItem .put ("statusMessage" , response .statusMessage ());
99
+ resultItem .put ("headers" , convertHeaders (response .headers ()));
100
+ resultItem .put ("body" , response .bodyAsString ());
120
101
}
121
-
122
- /**
123
- * Single Request
124
- */
125
- else {
126
- ProxyResponse proxyResponse = new ProxyResponse ();
127
- if (response == null ) {
128
- proxyResponse .setStatusCode (504 );
129
- proxyResponse .setStatusMessage ("Gateway Time-out" );
130
- proxyResponse .setHeaders (makeDefaultHeaders ());
131
- proxyResponse .setBody (null );
132
- } else {
133
- proxyResponse .setStatusCode (response .statusCode ());
134
- proxyResponse .setStatusMessage (response .statusMessage ());
135
- proxyResponse .setHeaders (response .headers ());
136
- proxyResponse .setBody (response .body ());
137
- }
138
- consumer .accept (proxyResponse );
102
+ result .add (resultItem );
103
+
104
+ // END
105
+ if (processingCount .decrementAndGet () == 0 ) {
106
+ Buffer proxyBody = Buffer .buffer (new GsonBuilder ().create ().toJson (result ));
107
+ ProxyHttpResponse proxyHttpResponse = new ProxyHttpResponse ();
108
+ proxyHttpResponse .setStatusCode (200 );
109
+ proxyHttpResponse .setStatusMessage ("OK" );
110
+ proxyHttpResponse .setHeaders (makeProxyResponseHeaders ());
111
+ proxyHttpResponse .setBody (proxyBody );
112
+ consumer .accept (proxyHttpResponse );
139
113
}
140
114
}
141
115
});
@@ -179,21 +153,11 @@ private MultiMap makeProxyResponseHeaders() {
179
153
private ArrayList <String > getTargetURI (String originPath ) {
180
154
ArrayList <String > result = new ArrayList <>();
181
155
ArrayList <String > uriList = new ArrayList <>();
182
- mProxyInfo .getTargets ().forEach (targetBaseUri -> {
156
+ mProxyHttpInfo .getTargets ().forEach (targetBaseUri -> {
183
157
String uri = targetBaseUri + "/" + originPath ;
184
158
uriList .add (uri );
185
159
});
186
- if (mProxyInfo .getType ().equalsIgnoreCase (ProxyConfig .ChannelType .ROUND_ROBIN )) {
187
- String uri = uriList .get (mRoundRobinIndex );
188
- mRoundRobinIndex = (mRoundRobinIndex + 1 ) % mProxyInfo .getTargets ().size ();
189
- result .add (uri );
190
- } else if (mProxyInfo .getType ().equalsIgnoreCase (ProxyConfig .ChannelType .RANDOM )) {
191
- int index = (int ) (System .nanoTime () % mProxyInfo .getTargets ().size ());
192
- mLogger .trace ("index = " + index );
193
- result .add (mProxyInfo .getTargets ().get (index ));
194
- } else if (mProxyInfo .getType ().equalsIgnoreCase (ProxyConfig .ChannelType .ALL )) {
195
- result .addAll (uriList );
196
- }
160
+ result .addAll (uriList );
197
161
return result ;
198
162
}
199
163
0 commit comments