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);
}
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;
}
Aggregations