2727import org .threadly .litesockets .TCPClient ;
2828import org .threadly .litesockets .buffers .MergedByteBuffers ;
2929import org .threadly .litesockets .buffers .ReuseableMergedByteBuffers ;
30+ import org .threadly .litesockets .protocols .http .request .ClientHTTPRequest ;
3031import org .threadly .litesockets .protocols .http .request .HTTPRequest ;
3132import org .threadly .litesockets .protocols .http .request .HTTPRequestBuilder ;
3233import org .threadly .litesockets .protocols .http .response .HTTPResponse ;
3334import org .threadly .litesockets .protocols .http .response .HTTPResponseProcessor ;
3435import org .threadly .litesockets .protocols .http .response .HTTPResponseProcessor .HTTPResponseCallback ;
3536import org .threadly .litesockets .protocols .http .shared .HTTPAddress ;
36- import org .threadly .litesockets .protocols .http .shared .HTTPConstants ;
3737import org .threadly .litesockets .protocols .http .shared .HTTPParsingException ;
3838import org .threadly .litesockets .protocols .http .shared .HTTPRequestType ;
3939import org .threadly .litesockets .protocols .http .shared .HTTPResponseCode ;
@@ -63,7 +63,7 @@ public class HTTPClient extends AbstractService {
6363 private final MainClientProcessor mcp = new MainClientProcessor ();
6464 private final RunSocket runSocketTask ;
6565 private final int maxConcurrent ;
66- private volatile int defaultTimeout = DEFAULT_TIMEOUT ;
66+ private volatile int defaultTimeoutMS = HTTPRequest . DEFAULT_TIMEOUT_MS ;
6767 private volatile SSLContext sslContext = SSLUtils .OPEN_SSL_CTX ;
6868
6969 private NoThreadSocketExecuter ntse = null ;
@@ -168,8 +168,8 @@ public void closeAllClients() {
168168 *
169169 * @param timeout time in milliseconds to wait for HTTPRequests to finish.
170170 */
171- public void setTimeout (int timeout ) {
172- this .defaultTimeout = timeout ;
171+ public void setTimeout (TimeUnit unit , int timeout ) {
172+ this .defaultTimeoutMS = ( int ) Math . min ( Math . max ( unit . toMillis ( timeout ), HTTPRequest . MIN_TIMEOUT_MS ), HTTPRequest . MAX_TIMEOUT_MS ) ;
173173 }
174174
175175 /**
@@ -208,31 +208,6 @@ public HTTPResponseData request(final URL url, final HTTPRequestType rt, final B
208208 return hr ;
209209 }
210210
211- /**
212- * Sends a blocking HTTP request.
213- *
214- * @param ha the {@link HTTPAddress} to connect to, any hostname in the actual HTTPRequest will just be sent in the protocol.
215- * @param request the {@link HTTPRequest} to send the server once connected.
216- * @return an {@link HTTPResponseData} object containing the headers and content of the response.
217- * @throws HTTPParsingException is thrown if the server sends back protocol or a response that is larger then allowed.
218- */
219- public HTTPResponseData request (final HTTPAddress ha , final HTTPRequest request ) throws HTTPParsingException {
220- return request (ha , request , IOUtils .EMPTY_BYTEBUFFER );
221- }
222-
223- /**
224- * Sends a blocking HTTP request.
225- *
226- * @param ha the {@link HTTPAddress} to connect to, any hostname in the actual HTTPRequest will just be sent in the protocol.
227- * @param request the {@link HTTPRequest} to send the server once connected.
228- * @param body the body to send with this request. You must have set the {@link HTTPRequest} correctly for this body.
229- * @return an {@link HTTPResponseData} object containing the headers and content of the response.
230- * @throws HTTPParsingException is thrown if the server sends back protocol or a response that is larger then allowed.
231- */
232- public HTTPResponseData request (final HTTPAddress ha , final HTTPRequest request , final ByteBuffer body ) throws HTTPParsingException {
233- return request (ha , request , body , TimeUnit .MILLISECONDS , defaultTimeout );
234- }
235-
236211 /**
237212 * Sends a blocking HTTP request.
238213 *
@@ -244,11 +219,10 @@ public HTTPResponseData request(final HTTPAddress ha, final HTTPRequest request,
244219 * @return an {@link HTTPResponseData} object containing the headers and content of the response.
245220 * @throws HTTPParsingException is thrown if the server sends back protocol or a response that is larger then allowed.
246221 */
247- public HTTPResponseData request (final HTTPAddress ha , final HTTPRequest request , final ByteBuffer body , final TimeUnit unit , final long timeout )
248- throws HTTPParsingException {
222+ public HTTPResponseData request (final ClientHTTPRequest request ) throws HTTPParsingException {
249223 HTTPResponseData hr = null ;
250224 try {
251- hr = requestAsync (ha , request , body , unit , timeout ).get ();
225+ hr = requestAsync (request ).get ();
252226 } catch (InterruptedException e ) {
253227 Thread .currentThread ().interrupt ();
254228 } catch (Exception e ) {
@@ -284,45 +258,12 @@ public ListenableFuture<HTTPResponseData> requestAsync(final URL url) {
284258 * successfully or with errors.
285259 */
286260 public ListenableFuture <HTTPResponseData > requestAsync (final URL url , final HTTPRequestType rt , final ByteBuffer bb ) {
287- boolean ssl = false ;
288- int port = HTTPConstants .DEFAULT_HTTP_PORT ;
289- String host = url .getHost ();
290- if (url .getProtocol ().equalsIgnoreCase ("https" )) {
291- port = HTTPConstants .DEFAULT_HTTPS_PORT ;
292- ssl = true ;
293- }
294- if (url .getPort () != -1 ) {
295- port = url .getPort ();
296- }
297261 HTTPRequestBuilder hrb = new HTTPRequestBuilder (url );
298262 hrb .setRequestType (rt );
299- return requestAsync (new HTTPAddress (host , port , ssl ), hrb .build (), bb );
263+ hrb .setTimeout (TimeUnit .MILLISECONDS , this .defaultTimeoutMS );
264+ return requestAsync (hrb .buildClientHTTPRequest ());
300265 }
301266
302- /**
303- * Sends an asynchronous HTTP request.
304- *
305- * @param ha the {@link HTTPAddress} to connect to, any hostname in the actual HTTPRequest will just be sent in the protocol.
306- * @param request the {@link HTTPRequest} to send the server once connected.
307- * @return an {@link ListenableFuture} containing a {@link HTTPResponseData} object that will be completed when the request is finished,
308- * successfully or with errors.
309- */
310- public ListenableFuture <HTTPResponseData > requestAsync (final HTTPAddress ha , final HTTPRequest request ) {
311- return requestAsync (ha , request , IOUtils .EMPTY_BYTEBUFFER );
312- }
313-
314- /**
315- * Sends an asynchronous HTTP request.
316- *
317- * @param ha the {@link HTTPAddress} to connect to, any hostname in the actual HTTPRequest will just be sent in the protocol.
318- * @param request the {@link HTTPRequest} to send the server once connected.
319- * @param body the body to send with this request. You must have set the {@link HTTPRequest} correctly for this body.
320- * @return an {@link ListenableFuture} containing a {@link HTTPResponseData} object that will be completed when the request is finished,
321- * successfully or with errors.
322- */
323- public ListenableFuture <HTTPResponseData > requestAsync (final HTTPAddress ha , final HTTPRequest request , final ByteBuffer body ) {
324- return requestAsync (ha , request , body , TimeUnit .MILLISECONDS , defaultTimeout );
325- }
326267
327268 /**
328269 * Sends an asynchronous HTTP request.
@@ -335,9 +276,8 @@ public ListenableFuture<HTTPResponseData> requestAsync(final HTTPAddress ha, fin
335276 * @return an {@link ListenableFuture} containing a {@link HTTPResponseData} object that will be completed when the request is finished,
336277 * successfully or with errors.
337278 */
338- public ListenableFuture <HTTPResponseData > requestAsync (final HTTPAddress ha ,
339- final HTTPRequest request , final ByteBuffer body , final TimeUnit unit , final long timeout ) {
340- HTTPRequestWrapper hrw = new HTTPRequestWrapper (request , ha , body , unit .toMillis (timeout ));
279+ public ListenableFuture <HTTPResponseData > requestAsync (final ClientHTTPRequest request ) {
280+ HTTPRequestWrapper hrw = new HTTPRequestWrapper (request );
341281 final ListenableFuture <HTTPResponseData > lf = hrw .slf ;
342282 queue .add (hrw );
343283 if (ntse != null ) {
@@ -363,10 +303,10 @@ private void process(HTTPRequestWrapper hrw) {
363303 sei .watchFuture (hrw .slf , hrw .timeTillExpired ()+1 );
364304
365305 hrw .updateReadTime ();
366- hrw .client = getTCPClient (hrw .ha );
306+ hrw .client = getTCPClient (hrw .chr . getHTTPAddress () );
367307 inProcess .put (hrw .client , hrw );
368- hrw .client .write (hrw .hr .getByteBuffer ());
369- hrw .client .write (hrw .body .duplicate ());
308+ hrw .client .write (hrw .chr . getHTTPRequest () .getByteBuffer ());
309+ hrw .client .write (hrw .chr . getBodyBuffer () .duplicate ());
370310 } catch (Exception e ) {
371311 //Have to catch all here or we dont keep processing if NoThreadSE is in use
372312 //hrw.slf.setFailure(e);
@@ -505,29 +445,23 @@ public void onRead(Client client) {
505445 private class HTTPRequestWrapper implements HTTPResponseCallback {
506446 private final SettableListenableFuture <HTTPResponseData > slf = new SettableListenableFuture <>(false );
507447 private final HTTPResponseProcessor hrp = new HTTPResponseProcessor ();
508- private final HTTPRequest hr ;
509- private final HTTPAddress ha ;
510- private final long timeout ;
511- private final ByteBuffer body ;
448+ private final ClientHTTPRequest chr ;
512449 private HTTPResponse response ;
513450 private ReuseableMergedByteBuffers responseMBB = new ReuseableMergedByteBuffers ();
514451 private TCPClient client ;
515452 private long lastRead = Clock .lastKnownForwardProgressingMillis ();
516453
517- public HTTPRequestWrapper (HTTPRequest hr , HTTPAddress ha , ByteBuffer body , long timeout ) {
454+ public HTTPRequestWrapper (ClientHTTPRequest chr ) {
518455 hrp .addHTTPResponseCallback (this );
519- this .hr = hr ;
520- this .ha = ha ;
521- this .body = body ;
522- this .timeout = timeout ;
456+ this .chr = chr ;
523457 }
524458
525459 public void updateReadTime () {
526460 lastRead = Clock .lastKnownForwardProgressingMillis ();
527461 }
528462
529463 public long timeTillExpired () {
530- return timeout - (Clock .lastKnownForwardProgressingMillis () - lastRead );
464+ return chr . getTimeoutMS () - (Clock .lastKnownForwardProgressingMillis () - lastRead );
531465 }
532466
533467 @ Override
@@ -546,10 +480,10 @@ public void bodyData(ByteBuffer bb) {
546480
547481 @ Override
548482 public void finished () {
549- slf .setResult (new HTTPResponseData (HTTPClient .this , hr , response , responseMBB .duplicateAndClean ()));
483+ slf .setResult (new HTTPResponseData (HTTPClient .this , chr . getHTTPRequest () , response , responseMBB .duplicateAndClean ()));
550484 hrp .removeHTTPResponseCallback (this );
551485 inProcess .remove (client );
552- addBackTCPClient (ha , client );
486+ addBackTCPClient (chr . getHTTPAddress () , client );
553487 processQueue ();
554488 }
555489
0 commit comments