Search in sources :

Example 1 with HttpResponseListener

use of org.webpieces.httpclient11.api.HttpResponseListener 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 2 with HttpResponseListener

use of org.webpieces.httpclient11.api.HttpResponseListener 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

HttpResponseListener (org.webpieces.httpclient11.api.HttpResponseListener)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 HttpStreamRef (org.webpieces.httpclient11.api.HttpStreamRef)1 Header (org.webpieces.httpparser.api.common.Header)1 HttpData (org.webpieces.httpparser.api.dto.HttpData)1 XFuture (org.webpieces.util.futures.XFuture)1