3636import org .threadly .litesockets .protocols .http .response .HTTPResponseProcessor .HTTPResponseCallback ;
3737import org .threadly .litesockets .protocols .http .shared .HTTPAddress ;
3838import org .threadly .litesockets .protocols .http .shared .HTTPParsingException ;
39- import org .threadly .litesockets .protocols .http .shared .HTTPRequestType ;
39+ import org .threadly .litesockets .protocols .http .shared .HTTPRequestMethod ;
4040import org .threadly .litesockets .protocols .http .shared .HTTPResponseCode ;
4141import org .threadly .litesockets .protocols .ws .WebSocketFrameParser .WebSocketFrame ;
4242import org .threadly .litesockets .utils .IOUtils ;
@@ -173,7 +173,8 @@ public void closeAllClients() {
173173 * @param timeout time in milliseconds to wait for HTTPRequests to finish.
174174 */
175175 public void setTimeout (long timeout , TimeUnit unit ) {
176- this .defaultTimeoutMS = Math .min (Math .max (unit .toMillis (timeout ),HTTPRequest .MIN_TIMEOUT_MS ), HTTPRequest .MAX_TIMEOUT_MS );
176+ this .defaultTimeoutMS = Math .min (Math .max (unit .toMillis (timeout ),HTTPRequest .MIN_TIMEOUT_MS ),
177+ HTTPRequest .MAX_TIMEOUT_MS );
177178 }
178179
179180 public long getMaxIdleTimeout () {
@@ -210,22 +211,22 @@ public void run() {
210211 * @throws HTTPParsingException is thrown if the server sends back protocol or a response that is larger then allowed.
211212 */
212213 public HTTPResponseData request (final URL url ) throws HTTPParsingException {
213- return request (url , HTTPRequestType .GET , IOUtils .EMPTY_BYTEBUFFER );
214+ return request (url , HTTPRequestMethod .GET , IOUtils .EMPTY_BYTEBUFFER );
214215 }
215216
216217 /**
217218 * Sends a blocking HTTP request.
218219 *
219220 * @param url the url to send the request too.
220- * @param rt the {@link HTTPRequestType } to use on the request.
221+ * @param rm the {@link HTTPRequestMethod } to use on the request.
221222 * @param bb the data to put in the body for this request.
222223 * @return an {@link HTTPResponseData} object containing the headers and content of the response.
223224 * @throws HTTPParsingException is thrown if the server sends back protocol or a response that is larger then allowed.
224225 */
225- public HTTPResponseData request (final URL url , final HTTPRequestType rt , final ByteBuffer bb ) throws HTTPParsingException {
226+ public HTTPResponseData request (final URL url , final HTTPRequestMethod rm , final ByteBuffer bb ) throws HTTPParsingException {
226227 HTTPResponseData hr = null ;
227228 try {
228- hr = requestAsync (url , rt , bb ).get ();
229+ hr = requestAsync (url , rm , bb ).get ();
229230 } catch (InterruptedException e ) {
230231 Thread .currentThread ().interrupt ();
231232 } catch (Exception e ) {
@@ -275,21 +276,22 @@ public HTTPResponseData request(final ClientHTTPRequest request) throws HTTPPars
275276 * successfully or with errors.
276277 */
277278 public ListenableFuture <HTTPResponseData > requestAsync (final URL url ) {
278- return requestAsync (url , HTTPRequestType .GET , IOUtils .EMPTY_BYTEBUFFER );
279+ return requestAsync (url , HTTPRequestMethod .GET , IOUtils .EMPTY_BYTEBUFFER );
279280 }
280281
281282 /**
282283 * Sends an asynchronous HTTP request.
283284 *
284285 * @param url the {@link URL} to send the request too.
285- * @param rt the {@link HTTPRequestType } to use on the request.
286+ * @param rm the {@link HTTPRequestMethod } to use on the request.
286287 * @param bb the data to put in the body for this request.
287288 * @return an {@link ListenableFuture} containing a {@link HTTPResponseData} object that will be completed when the request is finished,
288289 * successfully or with errors.
289290 */
290- public ListenableFuture <HTTPResponseData > requestAsync (final URL url , final HTTPRequestType rt , final ByteBuffer bb ) {
291+ public ListenableFuture <HTTPResponseData > requestAsync (final URL url , final HTTPRequestMethod rm ,
292+ final ByteBuffer bb ) {
291293 HTTPRequestBuilder hrb = new HTTPRequestBuilder (url );
292- hrb .setRequestType ( rt );
294+ hrb .setRequestMethod ( rm );
293295 hrb .setTimeout (this .defaultTimeoutMS , TimeUnit .MILLISECONDS );
294296 if (bb != null && bb .hasRemaining ()) {
295297 hrb .setBody (bb );
@@ -498,14 +500,17 @@ public void onRead(Client client) {
498500 */
499501 private class HTTPRequestWrapper implements HTTPResponseCallback {
500502 private final SettableListenableFuture <HTTPResponseData > slf = new SettableListenableFuture <>(false );
501- private final HTTPResponseProcessor hrp = new HTTPResponseProcessor () ;
503+ private final HTTPResponseProcessor hrp ;
502504 private final ClientHTTPRequest chr ;
503505 private HTTPResponse response ;
504506 private ReuseableMergedByteBuffers responseMBB = new ReuseableMergedByteBuffers ();
505507 private TCPClient client ;
506508 private long lastRead = Clock .lastKnownForwardProgressingMillis ();
507509
508510 public HTTPRequestWrapper (ClientHTTPRequest chr ) {
511+ hrp = new HTTPResponseProcessor (chr .getHTTPRequest ()
512+ .getHTTPRequestHeader ()
513+ .getRequestMethod ().equals ("HEAD" ));
509514 hrp .addHTTPResponseCallback (this );
510515 this .chr = chr ;
511516 }
@@ -534,7 +539,8 @@ public void bodyData(ByteBuffer bb) {
534539
535540 @ Override
536541 public void finished () {
537- slf .setResult (new HTTPResponseData (HTTPClient .this , chr .getHTTPRequest (), response , responseMBB .duplicateAndClean ()));
542+ slf .setResult (new HTTPResponseData (HTTPClient .this , chr .getHTTPRequest (), response ,
543+ responseMBB .duplicateAndClean ()));
538544 hrp .removeHTTPResponseCallback (this );
539545 inProcess .remove (client );
540546 addBackTCPClient (chr .getHTTPAddress (), client );
@@ -563,7 +569,8 @@ public static class HTTPResponseData {
563569 private final MergedByteBuffers body ;
564570 private final HTTPClient client ;
565571
566- public HTTPResponseData (HTTPClient client , HTTPRequest origRequest , HTTPResponse hr , MergedByteBuffers bb ) {
572+ public HTTPResponseData (HTTPClient client , HTTPRequest origRequest , HTTPResponse hr ,
573+ MergedByteBuffers bb ) {
567574 this .client = client ;
568575 this .hr = hr ;
569576 this .body = bb ;
@@ -587,7 +594,16 @@ public HTTPResponseCode getResponseCode() {
587594 }
588595
589596 public long getContentLength () {
590- return body .remaining ();
597+ long result = body .remaining ();
598+ if (result > 0 ) {
599+ return result ;
600+ }
601+ result = hr .getHeaders ().getContentLength ();
602+ if (result > 0 ) {
603+ return result ;
604+ } else {
605+ return 0 ;
606+ }
591607 }
592608
593609 public MergedByteBuffers getBody () {
0 commit comments