Search in sources :

Example 1 with HttpStreamRef

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());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) InetSocketAddress(java.net.InetSocketAddress) HttpStreamRef(org.webpieces.httpclient11.api.HttpStreamRef) MockResponseListener(org.webpieces.httpclient.mocks.MockResponseListener) Test(org.junit.Test)

Example 2 with HttpStreamRef

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());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Header(org.webpieces.httpparser.api.common.Header) InetSocketAddress(java.net.InetSocketAddress) HttpStreamRef(org.webpieces.httpclient11.api.HttpStreamRef) MockResponseListener(org.webpieces.httpclient.mocks.MockResponseListener) Test(org.junit.Test)

Example 3 with HttpStreamRef

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);
}
Also used : Header(org.webpieces.httpparser.api.common.Header) HttpDataWriter(org.webpieces.httpclient11.api.HttpDataWriter) ByteBuffer(java.nio.ByteBuffer) HttpResponseListener(org.webpieces.httpclient11.api.HttpResponseListener)

Example 4 with HttpStreamRef

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;
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) XFuture(org.webpieces.util.futures.XFuture) CancellationException(java.util.concurrent.CancellationException) HttpStreamRef(org.webpieces.httpclient11.api.HttpStreamRef) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpResponseListener(org.webpieces.httpclient11.api.HttpResponseListener)

Aggregations

HttpStreamRef (org.webpieces.httpclient11.api.HttpStreamRef)3 InetSocketAddress (java.net.InetSocketAddress)2 Test (org.junit.Test)2 MockResponseListener (org.webpieces.httpclient.mocks.MockResponseListener)2 HttpResponseListener (org.webpieces.httpclient11.api.HttpResponseListener)2 Header (org.webpieces.httpparser.api.common.Header)2 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)2 ByteBuffer (java.nio.ByteBuffer)1 CancellationException (java.util.concurrent.CancellationException)1 HttpDataWriter (org.webpieces.httpclient11.api.HttpDataWriter)1 HttpFullResponse (org.webpieces.httpclient11.api.HttpFullResponse)1 HttpData (org.webpieces.httpparser.api.dto.HttpData)1 XFuture (org.webpieces.util.futures.XFuture)1