use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class ResponseCacher method run.
public XFuture<FullResponse> run(FullRequest request) {
SingleResponseListener responseListener = new SingleResponseListener();
RequestStreamHandle streamHandle = openStreamFunc.get();
Http2Request req = request.getHeaders();
if (request.getPayload() == null) {
request.getHeaders().setEndOfStream(true);
streamHandle.process(req, responseListener);
return responseListener.fetchResponseFuture();
} else if (request.getTrailingHeaders() == null) {
request.getHeaders().setEndOfStream(false);
DataFrame data = createData(request, true);
StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
return streamRef.getWriter().thenCompose(writer -> {
data.setStreamId(req.getStreamId());
return writer.processPiece(data);
}).thenCompose(writer -> responseListener.fetchResponseFuture());
}
request.getHeaders().setEndOfStream(false);
DataFrame data = createData(request, false);
Http2Trailers trailers = request.getTrailingHeaders();
trailers.setEndOfStream(true);
StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);
return streamRef.getWriter().thenCompose(writer -> writeStuff(writer, req, data, trailers, responseListener));
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestStreaming method testAsyncJsonGet.
@Test
public void testAsyncJsonGet() {
String bodyStr = "asdlfkjsldfkjlsfkjdlksjfffffflsdkjfffffldksjflkdjsfldsjf";
byte[] bytes = bodyStr.getBytes();
DataWrapper body = gen.wrapByteArray(bytes);
Http2Request request = Requests.createRequest("/json/streaming", body);
RequestStreamHandle stream = http2Socket.openStream();
StreamRef ref = stream.process(request, mockResponseListener);
XFuture<StreamWriter> writer = ref.getWriter();
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class EchoStreamingClient method stream.
public StreamRef stream(ResponseStreamHandle handle) {
ProxyStreamHandle h = (ProxyStreamHandle) handle;
Http2Request req = Current.request().originalRequest;
Http2Response response = h.createBaseResponse(req, "application/ndjson", 200, "OK");
try {
StreamWriter writer = h.process(response).get();
return new ProxyStreamRef(new EchoWriter(writer));
} catch (InterruptedException | ExecutionException e) {
throw SneakyThrow.sneak(e);
}
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class HttpsJsonClient method sendHttpRequest.
/**
* <b>DO NOT USE FOR PUBLIC HTTP REQUEST THIS IS FOR INTERNAL USE ONLY</b>
*/
public <T> XFuture<T> sendHttpRequest(Method method, Object request, Endpoint endpoint, Class<T> responseType) {
InetSocketAddress apiAddress = endpoint.getServerAddress();
String httpMethod = endpoint.getHttpMethod();
String endpointPath = endpoint.getUrlPath();
Http2Request httpReq = createHttpReq(apiAddress, httpMethod, endpointPath);
RequestCloseListener closeListener = new RequestCloseListener(schedulerSvc);
Http2Socket httpSocket = createSocket(apiAddress, closeListener);
XFuture<Void> connect = httpSocket.connect(apiAddress);
String jsonRequest = marshal(request);
byte[] reqAsBytes = jsonRequest.getBytes(StandardCharsets.UTF_8);
if (jsonRequest.equals("null")) {
// hack
reqAsBytes = new byte[0];
}
DataWrapper data = WRAPPER_GEN.wrapByteArray(reqAsBytes);
if (httpReq.getKnownMethod() == Http2Method.POST) {
httpReq.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, String.valueOf(data.getReadableSize())));
}
httpReq.addHeader(new Http2Header(Http2HeaderName.SCHEME, "https"));
FullRequest fullRequest = new FullRequest(httpReq, data, null);
log.info("curl request on socket(" + httpSocket + ")" + createCurl(fullRequest, apiAddress.getPort()));
Map<String, Object> fullContext = Context.getContext();
if (fullContext == null) {
throw new IllegalStateException("Missing webserver filters? Context.getFullContext() must contain data");
}
Map<String, String> ctxMap = MDC.getCopyOfContextMap();
Contexts contexts = new Contexts(ctxMap, fullContext);
long start = System.currentTimeMillis();
XFuture<T> future = futureUtil.catchBlockWrap(() -> sendAndTranslate(contexts, apiAddress, responseType, httpSocket, connect, fullRequest, jsonRequest), (t) -> translateException(httpReq, t));
// // Track metrics with future.handle()
// // If method is null, then no need to track metrics
// // If monitoring is null, then this call probably came from OrderlyTest
// if (method != null && monitoring != null) {
// future = future.handle((r, e) -> {
// String clientId = context.getRequest().getRequestState(OrderlyHeaders.CLIENT_ID.getHeaderName());
// monitoring.endHttpClientTimer(method, clientId, endpoint, start);
//
// if (e != null) {
// monitoring.incrementHttpClientExceptionMetric(method, clientId, endpoint, e.getClass().getSimpleName());
// return XFuture.<T>failedFuture(e);
// }
//
// monitoring.incrementHttpClientSuccessMetric(method, clientId, endpoint);
// return XFuture.completedFuture(r);
// }).thenCompose(Function.identity());
// }
// so we can cancel the future exactly when the socket closes
closeListener.setFuture(future);
return future;
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class HttpsJsonClient method createCurl.
private String createCurl(FullRequest request, int port) {
DataWrapper data = request.getPayload();
String body = data.createStringFromUtf8(0, data.getReadableSize());
Http2Request req = request.getHeaders();
return createCurl2(port, req, () -> ("--data '" + body + "'"));
}
Aggregations