Skip to content

Commit 55be2ff

Browse files
authored
Merge pull request #22 from threadly/HttpClientRequestFinishFix
Http client request finish fix
2 parents 9d1be81 + f730779 commit 55be2ff

File tree

14 files changed

+138
-90
lines changed

14 files changed

+138
-90
lines changed

client/src/main/java/org/threadly/litesockets/client/http/HTTPClient.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.threadly.litesockets.protocols.http.response.HTTPResponseProcessor.HTTPResponseCallback;
3737
import org.threadly.litesockets.protocols.http.shared.HTTPAddress;
3838
import 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;
4040
import org.threadly.litesockets.protocols.http.shared.HTTPResponseCode;
4141
import org.threadly.litesockets.protocols.ws.WebSocketFrameParser.WebSocketFrame;
4242
import 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() {

client/src/main/java/org/threadly/litesockets/client/http/HTTPStreamClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public HTTPStreamClient(TCPClient client, String host) {
7373
this.host = host;
7474
port = client.getRemoteSocketAddress().getPort();
7575
client.addCloseListener(classCloser);
76-
httpProcessor = new HTTPResponseProcessor();
76+
httpProcessor = new HTTPResponseProcessor(false);
7777
httpProcessor.addHTTPResponseCallback(requestCB);
7878
slfResponse = new SettableListenableFuture<HTTPResponse>();
7979
isConnected = true;
@@ -95,7 +95,7 @@ public HTTPStreamClient(SocketExecuter se, String host, int port) throws IOExcep
9595
client = se.createTCPClient(host, port);
9696
client.setConnectionTimeout(DEFAULT_TIMEOUT);
9797
client.addCloseListener(classCloser);
98-
httpProcessor = new HTTPResponseProcessor();
98+
httpProcessor = new HTTPResponseProcessor(false);
9999
httpProcessor.addHTTPResponseCallback(requestCB);
100100
}
101101

client/src/main/java/org/threadly/litesockets/client/ws/WebSocketClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.threadly.litesockets.protocols.http.response.HTTPResponse;
2424
import org.threadly.litesockets.protocols.http.response.HTTPResponseBuilder;
2525
import org.threadly.litesockets.protocols.http.shared.HTTPConstants;
26-
import org.threadly.litesockets.protocols.http.shared.HTTPRequestType;
26+
import org.threadly.litesockets.protocols.http.shared.HTTPRequestMethod;
2727
import org.threadly.litesockets.protocols.http.shared.HTTPResponseCode;
2828
import org.threadly.litesockets.protocols.ws.WebSocketFrameParser;
2929
import org.threadly.litesockets.protocols.ws.WebSocketFrameParser.WebSocketFrame;
@@ -45,7 +45,7 @@ public class WebSocketClient implements StreamingClient {
4545
.setHeader(HTTPConstants.HTTP_KEY_WEBSOCKET_ACCEPT, "123456")
4646
.build();
4747
public static final HTTPRequest DEFAULT_WS_REQUEST = new HTTPRequestBuilder()
48-
.setRequestType(HTTPRequestType.GET)
48+
.setRequestMethod(HTTPRequestMethod.GET)
4949
.setHeader(HTTPConstants.HTTP_KEY_UPGRADE, "websocket")
5050
.setHeader(HTTPConstants.HTTP_KEY_CONNECTION, "Upgrade")
5151
.setHeader(HTTPConstants.HTTP_KEY_WEBSOCKET_VERSION, "13")

client/src/test/java/org/threadly/litesockets/client/http/HTTPClientTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ public void noContentLengthWithBody() throws IOException, HTTPParsingException {
291291
assertEquals("TEST123", hrs.getBodyAsString());
292292
}
293293

294+
@Test
295+
public void contentLengthOnHeadRequest() throws IOException, HTTPParsingException {
296+
int port = PortUtils.findTCPPort(); // TODO
297+
fakeServer = new TestHTTPServer(port, RESPONSE_CL, "", false, true);
298+
final HTTPRequestBuilder hrb = new HTTPRequestBuilder(new URL("http://localhost:"+port));
299+
hrb.setRequestMethod("HEAD");
300+
301+
final HTTPClient httpClient = new HTTPClient();
302+
httpClient.start();
303+
HTTPResponseData hrs = httpClient.request(hrb.buildClientHTTPRequest());
304+
System.out.println(hrs.getResponse());
305+
assertEquals(CONTENT.length(), hrs.getContentLength());
306+
assertEquals("", hrs.getBodyAsString());
307+
}
308+
294309
@Test
295310
public void closeBeforeLength() throws IOException, HTTPParsingException {
296311
int port = PortUtils.findTCPPort();

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group = org.threadly
2-
version = 0.14
2+
version = 0.15
33
threadlyVersion = 5.16
44
litesocketsVersion = 4.3
55
org.gradle.parallel=true

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/HTTPRequestBuilder.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.threadly.litesockets.protocols.http.shared.HTTPAddress;
1313
import org.threadly.litesockets.protocols.http.shared.HTTPConstants;
1414
import org.threadly.litesockets.protocols.http.shared.HTTPHeaders;
15-
import org.threadly.litesockets.protocols.http.shared.HTTPRequestType;
15+
import org.threadly.litesockets.protocols.http.shared.HTTPRequestMethod;
1616
import org.threadly.litesockets.protocols.http.shared.HTTPUtils;
1717
import org.threadly.util.ArgumentVerifier;
1818

@@ -67,9 +67,9 @@ public HTTPRequestBuilder setURL(final URL url) {
6767
}
6868
String q = url.getQuery();
6969
if(q != null) {
70-
request = new HTTPRequestHeader(request.getRequestType(), tmpPath, HTTPUtils.queryToMap(q), request.getHttpVersion());
70+
request = new HTTPRequestHeader(request.getRequestMethod(), tmpPath, HTTPUtils.queryToMap(q), request.getHttpVersion());
7171
} else {
72-
request = new HTTPRequestHeader(request.getRequestType(), tmpPath, null, request.getHttpVersion());
72+
request = new HTTPRequestHeader(request.getRequestMethod(), tmpPath, null, request.getHttpVersion());
7373
}
7474
if(url.getProtocol().equalsIgnoreCase("https")) {
7575
doSSL = true;
@@ -91,13 +91,14 @@ public HTTPRequestBuilder setHTTPRequestHeader(final HTTPRequestHeader hrh) {
9191
}
9292

9393
/**
94-
* Sets the {@link HTTPRequestType} for this request. This will accept non-stander strings in the for the request.
94+
* Sets the {@link HTTPRequestMethod} for this request. This will accept non-stander strings in the for the request.
9595
*
96-
* @param rt the RequestType to set this request too.
96+
* @param rm the http request method to set this request too.
9797
* @return the current {@link HTTPRequestBuilder} object.
9898
*/
99-
public HTTPRequestBuilder setRequestType(final String rt) {
100-
this.request = new HTTPRequestHeader(rt, request.getRequestPath(), request.getRequestQuery(), request.getHttpVersion());
99+
public HTTPRequestBuilder setRequestMethod(final String rm) {
100+
this.request = new HTTPRequestHeader(rm, request.getRequestPath(), request.getRequestQuery(),
101+
request.getHttpVersion());
101102
return this;
102103
}
103104

@@ -108,7 +109,8 @@ public HTTPRequestBuilder setRequestType(final String rt) {
108109
* @return the current {@link HTTPRequestBuilder} object.
109110
*/
110111
public HTTPRequestBuilder setHTTPVersion(final String version) {
111-
this.request = new HTTPRequestHeader(request.getRequestType(), request.getRequestPath(), request.getRequestQuery(), version);
112+
this.request = new HTTPRequestHeader(request.getRequestMethod(), request.getRequestPath(),
113+
request.getRequestQuery(), version);
112114
return this;
113115
}
114116

@@ -122,9 +124,11 @@ public HTTPRequestBuilder setHTTPVersion(final String version) {
122124
*/
123125
public HTTPRequestBuilder setPath(final String path) {
124126
if(path.contains("?")) {
125-
this.request = new HTTPRequestHeader(request.getRequestType(), path, HTTPUtils.queryToMap(path), request.getHttpVersion());
127+
this.request = new HTTPRequestHeader(request.getRequestMethod(), path,
128+
HTTPUtils.queryToMap(path), request.getHttpVersion());
126129
} else {
127-
this.request = new HTTPRequestHeader(request.getRequestType(), path, request.getRequestQuery(), request.getHttpVersion());
130+
this.request = new HTTPRequestHeader(request.getRequestMethod(), path,
131+
request.getRequestQuery(), request.getHttpVersion());
128132
}
129133
return this;
130134
}
@@ -136,7 +140,7 @@ public HTTPRequestBuilder setPath(final String path) {
136140
* @return the current {@link HTTPRequestBuilder} object.
137141
*/
138142
public HTTPRequestBuilder setQueryString(final String query) {
139-
this.request = new HTTPRequestHeader(request.getRequestType(), request.getRequestPath(), HTTPUtils.queryToMap(query), request.getHttpVersion());
143+
this.request = new HTTPRequestHeader(request.getRequestMethod(), request.getRequestPath(), HTTPUtils.queryToMap(query), request.getHttpVersion());
140144
return this;
141145
}
142146

@@ -150,7 +154,7 @@ public HTTPRequestBuilder setQueryString(final String query) {
150154
public HTTPRequestBuilder appendQuery(final String key, final String value) {
151155
HashMap<String, String> map = new HashMap<String, String>(request.getRequestQuery());
152156
map.put(key, value);
153-
this.request = new HTTPRequestHeader(request.getRequestType(), request.getRequestPath(), map, request.getHttpVersion());
157+
this.request = new HTTPRequestHeader(request.getRequestMethod(), request.getRequestPath(), map, request.getHttpVersion());
154158
return this;
155159
}
156160

@@ -163,7 +167,7 @@ public HTTPRequestBuilder appendQuery(final String key, final String value) {
163167
public HTTPRequestBuilder removeQuery(final String key) {
164168
HashMap<String, String> map = new HashMap<String, String>(request.getRequestQuery());
165169
map.remove(key);
166-
this.request = new HTTPRequestHeader(request.getRequestType(), request.getRequestPath(), map, request.getHttpVersion());
170+
this.request = new HTTPRequestHeader(request.getRequestMethod(), request.getRequestPath(), map, request.getHttpVersion());
167171
return this;
168172
}
169173

@@ -301,13 +305,14 @@ public HTTPRequestBuilder replaceHTTPHeaders(final HTTPHeaders hh) {
301305

302306

303307
/**
304-
* Sets the {@link HTTPRequestType} for this request. This uses the standard http request types enum.
308+
* Sets the {@link HTTPRequestMethod} for this request. This uses the standard http request methods enum.
305309
*
306-
* @param rt the RequestType to set this request too.
310+
* @param rm the http request method to set this request too.
307311
* @return the current {@link HTTPRequestBuilder} object.
308312
*/
309-
public HTTPRequestBuilder setRequestType(final HTTPRequestType rt) {
310-
this.request = new HTTPRequestHeader(rt, request.getRequestPath(), request.getRequestQuery(), request.getHttpVersion());
313+
public HTTPRequestBuilder setRequestMethod(final HTTPRequestMethod rm) {
314+
this.request = new HTTPRequestHeader(rm, request.getRequestPath(), request.getRequestQuery(),
315+
request.getHttpVersion());
311316
return this;
312317
}
313318

0 commit comments

Comments
 (0)