use of org.webpieces.httpclient11.api.HttpStreamRef in project webpieces by deanhiller.
the class TestCancelStream method testClientCancelNoKeepAlive.
@Test
public void testClientCancelNoKeepAlive() {
XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
MockResponseListener mockListener = new MockResponseListener();
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/home", false);
mockChannel.addWriteResponse(XFuture.completedFuture(null));
HttpStreamRef ref = httpSocket.send(req, mockListener);
XFuture<Void> cancelDone = ref.cancel("some reason");
Assert.assertTrue(cancelDone.isDone());
// Assert the socket is NOT closed
Assert.assertTrue(mockChannel.isClosed());
}
use of org.webpieces.httpclient11.api.HttpStreamRef in project webpieces by deanhiller.
the class TestCancelStream method testClientCancelWithKeepAlive.
// @Test
// public void testRequestResponseXFutureCancelNoKeepAlive() {
// throw new UnsupportedOperationException("not done yet");
// }
//
// @Test
// public void testRequestResponseXFutureCancelWithKeepAlive() {
// throw new UnsupportedOperationException("not done yet");
// }
@Test
public void testClientCancelWithKeepAlive() {
XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
MockResponseListener mockListener = new MockResponseListener();
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/home", false);
req.addHeader(new Header(KnownHeaderName.CONNECTION, "keep-alive"));
mockChannel.addWriteResponse(XFuture.completedFuture(null));
HttpStreamRef ref = httpSocket.send(req, mockListener);
XFuture<Void> cancelDone = ref.cancel("some reason");
Assert.assertTrue(cancelDone.isDone());
// Assert the socket is NOT closed
Assert.assertFalse(mockChannel.isClosed());
}
use of org.webpieces.httpclient11.api.HttpStreamRef in project webpieces by deanhiller.
the class HttpSocketImpl method actuallySendRequest.
private HttpStreamRef actuallySendRequest(HttpRequest request, HttpResponseListener listener) {
HttpResponseListener l = new CatchResponseListener(listener, svrSocket);
ByteBuffer wrap = parser.marshalToByteBuffer(state, request);
isConnect = false;
if (request.getRequestLine().getMethod().getKnownStatus() == KnownHttpMethod.CONNECT)
isConnect = true;
// put this on the queue before the write to be completed from the listener below
responsesToComplete.offer(l);
boolean canSendChunks = false;
Header header = request.getHeaderLookupStruct().getHeader(KnownHeaderName.TRANSFER_ENCODING);
if (header != null && "chunked".equals(header.getValue()))
canSendChunks = true;
boolean canSendTheChunks = canSendChunks;
XFuture<HttpDataWriter> writer = channel.write(wrap).thenApply(v -> new HttpChunkWriterImpl(channel, parser, state, isConnect, canSendTheChunks));
return new MyStreamRefImpl(writer, request);
}
use of org.webpieces.httpclient11.api.HttpStreamRef in project webpieces by deanhiller.
the class HttpSocketImpl method send.
@Override
public XFuture<HttpFullResponse> send(HttpFullRequest request) {
Integer contentLength = request.getRequest().getContentLength();
if (request.getData() == null || request.getData().getReadableSize() == 0) {
if (contentLength != null && contentLength != 0)
throw new IllegalArgumentException("HttpRequest has 0 Content-Length but readable size=" + request.getData().getReadableSize());
} else if (!request.getRequest().isHasNonZeroContentLength())
throw new IllegalArgumentException("HttpRequest must have Content-Length header");
else if (request.getRequest().getContentLength() != request.getData().getReadableSize())
throw new IllegalArgumentException("HttpRequest Content-Length header value=" + request.getRequest().getContentLength() + " does not match payload size=" + request.getData().getReadableSize());
XFuture<HttpFullResponse> future = new XFuture<HttpFullResponse>();
HttpResponseListener l = new CompletableListener(future);
HttpStreamRef streamRef = send(request.getRequest(), l);
if (request.getData() != null && request.getData().getReadableSize() > 0) {
HttpData data = new HttpData(request.getData(), true);
streamRef.getWriter().thenCompose(w -> {
return w.send(data);
});
}
future.exceptionally(t -> {
// we can only cancel if it is NOT keepalive or else we have to keep socket open
if (t instanceof CancellationException && !isKeepAliveRequest(request.getRequest())) {
streamRef.cancel("XFuture cancelled by client, so cancel request");
}
return null;
});
return future;
}
Aggregations