org.webpieces.httpclientx.impl.Http2SocketImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http2to1_1-client Show documentation
Show all versions of http2to1_1-client Show documentation
HTTP/2 client that talks HTTP/1.1 in case you want to convert from HTTP/2 to HTTP/1.1 without changing code
package org.webpieces.httpclientx.impl;
import java.net.InetSocketAddress;
import org.webpieces.nio.api.channels.HostWithPort;
import org.webpieces.util.futures.XFuture;
import org.webpieces.http2client.api.Http2Socket;
import org.webpieces.http2client.api.dto.FullRequest;
import org.webpieces.http2client.api.dto.FullResponse;
import org.webpieces.http2translations.api.Http2ToHttp11;
import org.webpieces.httpclient11.api.HttpFullRequest;
import org.webpieces.httpclient11.api.HttpSocket;
import org.webpieces.httpclient11.api.HttpStreamRef;
import org.webpieces.httpparser.api.dto.HttpRequest;
import com.webpieces.http2.api.dto.highlevel.Http2Request;
import com.webpieces.http2.api.dto.lowlevel.CancelReason;
import com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName;
import com.webpieces.http2.api.streaming.RequestStreamHandle;
import com.webpieces.http2.api.streaming.ResponseStreamHandle;
import com.webpieces.http2.api.streaming.StreamRef;
import com.webpieces.http2.api.streaming.StreamWriter;
public class Http2SocketImpl implements Http2Socket {
private HttpSocket socket11;
public Http2SocketImpl(HttpSocket socket11) {
this.socket11 = socket11;
}
@Override
public XFuture connect(HostWithPort addr) {
return socket11.connect(addr);
}
/**
* @deprecated
*/
@Deprecated
@Override
public XFuture connect(InetSocketAddress addr) {
return socket11.connect(addr);
}
@Override
public RequestStreamHandle openStream() {
return new StreamImpl();
}
private class StreamImpl implements RequestStreamHandle {
@Override
public StreamRef process(Http2Request request, ResponseStreamHandle responseListener) {
HttpRequest req = Http2ToHttp11.translateRequest(request);
HttpStreamRef streamRef = socket11.send(req, new ResponseListener(socket11+"", responseListener));
XFuture newWriter = streamRef.getWriter().thenApply(s -> new StreamWriterImpl(s, req));
return new MyStreamRef(newWriter, request);
}
}
public class MyStreamRef implements StreamRef {
private XFuture writer;
private Http2Request request;
public MyStreamRef(XFuture writer, Http2Request request) {
this.writer = writer;
this.request = request;
}
@Override
public XFuture getWriter() {
return writer;
}
@Override
public XFuture cancel(CancelReason reason) {
String value = request.getSingleHeaderValue(Http2HeaderName.CONNECTION);
if("keep-alive".equals(value)) //do nothing as keep-alive was set so we can't really cancel in http1.1 for this case
return XFuture.completedFuture(null);
//keep alive not set so close the socket for http1.1
return socket11.close();
}
}
@Override
public XFuture send(FullRequest request) {
HttpFullRequest req = Translations2.translate(request);
return socket11.send(req).thenApply(r -> Translations2.translate(r));
}
@Override
public XFuture close() {
return socket11.close();
}
@Override
public XFuture sendPing() {
throw new UnsupportedOperationException("Http1.1 does not support ping");
}
@Override
public String toString() {
return "Http2-1.1Socket[socket=" + socket11 + "]";
}
}