1313import io .vproxy .base .http .HttpRespParser ;
1414import io .vproxy .base .processor .http1 .entity .Response ;
1515import io .vproxy .base .selector .SelectorEventLoop ;
16+ import io .vproxy .base .util .ByteArray ;
1617import io .vproxy .base .util .RingBuffer ;
1718import io .vproxy .base .util .RingBufferETHandler ;
1819import io .vproxy .base .util .Utils ;
1920import io .vproxy .base .util .callback .BlockCallback ;
21+ import io .vproxy .base .util .callback .Callback ;
2022import io .vproxy .base .util .nio .ByteArrayChannel ;
2123import io .vproxy .base .util .ringbuffer .SSLUnwrapRingBuffer ;
2224import io .vproxy .base .util .ringbuffer .SSLUtils ;
2325import io .vproxy .base .util .ringbuffer .SSLWrapRingBuffer ;
2426import io .vproxy .base .util .ringbuffer .SimpleRingBuffer ;
2527import io .vproxy .base .util .ringbuffer .ssl .VSSLContext ;
28+ import io .vproxy .base .util .thread .VProxyThread ;
2629import io .vproxy .component .app .TcpLB ;
2730import io .vproxy .component .secure .SecurityGroup ;
2831import io .vproxy .component .ssl .CertKey ;
3740import javax .net .ssl .*;
3841import java .io .IOException ;
3942import java .net .ServerSocket ;
40- import java .net .Socket ;
43+ import java .net .URI ;
4144import java .net .UnknownHostException ;
45+ import java .net .http .HttpRequest ;
46+ import java .net .http .HttpResponse ;
4247import java .security .KeyStore ;
4348import java .util .concurrent .ConcurrentLinkedQueue ;
4449
@@ -115,8 +120,8 @@ public static void tearDownClass() {
115120 SelectorEventLoop selectorEventLoop ;
116121
117122 @ Test
118- public void requestSite () throws Exception {
119- String url = "https://www.bing.com " ;
123+ public void requestSite () throws Throwable {
124+ String url = "https://vproxy.io " ;
120125 String host = url .substring ("https://" .length ());
121126 int port = 443 ;
122127 BlockCallback <IP , UnknownHostException > cb = new BlockCallback <>();
@@ -126,19 +131,22 @@ public void requestSite() throws Exception {
126131 inet = cb .block ();
127132 } catch (UnknownHostException e ) {
128133 System .out .println ("we are not able to resolve the address, " +
129- "may be network is wrong, we ignore this case" );
134+ "may be network is wrong, we ignore this case" );
130135 throw new AssumptionViolatedException (e .getMessage ());
131136 }
132137 var remote = new IPPort (inet , port );
133138 // make a socket to test whether it's accessible
134- {
135- try (Socket sock = new Socket ()) {
136- sock .connect (remote .toInetSocketAddress ());
137- } catch (IOException e ) {
138- System .out .println ("we cannot connect to the remote," +
139- "may be network is wrong, we ignore this case" );
140- throw new AssumptionViolatedException (e .getMessage ());
139+ try (var client = java .net .http .HttpClient .newHttpClient ()) {
140+ var req = HttpRequest .newBuilder ()
141+ .GET ().uri (URI .create ("https://" + inet .formatToIPString ()))
142+ .header ("Host" , host )
143+ .build ();
144+ var resp = client .send (req , HttpResponse .BodyHandlers .discarding ());
145+ if (resp .statusCode () < 200 || resp .statusCode () >= 500 ) {
146+ throw new Exception ("failed to request " + url + ", status : " + resp .statusCode ());
141147 }
148+ } catch (Throwable t ) {
149+ throw new AssumptionViolatedException (t .getMessage ());
142150 }
143151
144152 // create loop
@@ -163,52 +171,73 @@ public void requestSite() throws Exception {
163171 pair .left , pair .right
164172 );
165173 conn .setTimeout (5_000 );
166- loop .addConnectableConnection (conn , null , new MySSLConnectableConnectionHandler (host ));
167174
175+ var cb2 = new BlockCallback <Void , Throwable >();
176+ loop .addConnectableConnection (conn , null , new MySSLConnectableConnectionHandler (host , cb2 ));
168177 // start
169- selectorEventLoop .loop ();
178+ selectorEventLoop .loop (r -> VProxyThread .create (r , "requestSite" ));
179+ cb2 .block ();
180+ selectorEventLoop .close ();
170181 }
171182
172- class MySSLConnectableConnectionHandler implements ConnectableConnectionHandler {
183+ static class MySSLConnectableConnectionHandler implements ConnectableConnectionHandler {
173184 private final ByteArrayChannel chnl ;
174185 private final HttpRespParser parser ;
186+ private final Callback <Void , Throwable > cb ;
175187
176- MySSLConnectableConnectionHandler (String host ) {
188+ MySSLConnectableConnectionHandler (String host , Callback < Void , Throwable > cb ) {
177189 chnl = ByteArrayChannel .fromFull (("" +
178190 "GET / HTTP/1.1\r \n " +
179191 "Host: " + host + "\r \n " +
180192 "User-Agent: curl/vproxy\r \n " + // add curl agent to get json response
181193 "\r \n " ).getBytes ());
182194 parser = new HttpRespParser (true );
195+ this .cb = cb ;
183196 }
184197
198+ private boolean isConnected = false ;
199+
185200 @ Override
186201 public void connected (ConnectableConnectionHandlerContext ctx ) {
202+ isConnected = true ;
187203 // send http request
188204 ctx .connection .getOutBuffer ().storeBytesFrom (chnl );
189205 }
190206
207+ private ByteArray readData = null ;
208+
191209 @ Override
192210 public void readable (ConnectionHandlerContext ctx ) {
211+ var bytes = ByteArray .from (ctx .connection .getInBuffer ().getBytes ());
212+ if (readData == null ) {
213+ readData = bytes ;
214+ } else {
215+ readData = readData .concat (bytes );
216+ }
217+
193218 int res = parser .feed (ctx .connection .getInBuffer ());
194219 if (res != 0 ) {
195220 String err = parser .getErrorMessage ();
196- assertNull ("got error: " + err , err );
221+ try {
222+ assertNull ("got error: " + err , err );
223+ } catch (Throwable t ) {
224+ cb .failed (t );
225+ }
197226 return ;
198227 }
199228 // headers done
200229 Response resp = parser .getResult ();
201- assertTrue ("failed: " + resp .toString (), 200 == resp .statusCode || 302 == resp .statusCode );
230+ try {
231+ assertTrue ("failed: " + resp .toString (), 200 <= resp .statusCode && resp .statusCode < 500 );
232+ } catch (Throwable t ) {
233+ cb .failed (t );
234+ return ;
235+ }
202236 System .out .println ("===============\n " + resp + "\n =============" );
203237 // the body is truncated
204238 // we actually do not need that
205239 // successfully parsing the status and headers means that
206240 // this lib is working properly
207- try {
208- selectorEventLoop .close ();
209- } catch (IOException e ) {
210- fail (e .getMessage ());
211- }
212241 }
213242
214243 @ Override
@@ -218,7 +247,11 @@ public void writable(ConnectionHandlerContext ctx) {
218247
219248 @ Override
220249 public void exception (ConnectionHandlerContext ctx , IOException err ) {
221- fail ();
250+ try {
251+ fail ("exception occurred: " + err + ", isConnected: " + isConnected + ", readData: " + readData );
252+ } catch (Throwable t ) {
253+ cb .failed (t );
254+ }
222255 }
223256
224257 @ Override
0 commit comments