use of org.webpieces.http2client.api.dto.FullRequest in project webpieces by deanhiller.
the class TestCBasicRequestResponse method testWithNoData.
@Test
public void testWithNoData() throws InterruptedException, ExecutionException, TimeoutException {
FullRequest request1 = new FullRequest();
request1.setHeaders(Requests.createRequest());
XFuture<FullResponse> future = httpSocket.send(request1);
Assert.assertFalse(future.isDone());
Http2Headers frame = (Http2Headers) mockChannel.getFrameAndClear();
Assert.assertEquals(1, frame.getStreamId());
Http2Response resp = Requests.createResponse(request1.getHeaders().getStreamId());
resp.setEndOfStream(true);
mockChannel.write(resp);
FullResponse response = future.get(2, TimeUnit.SECONDS);
Assert.assertEquals(0, response.getPayload().getReadableSize());
}
use of org.webpieces.http2client.api.dto.FullRequest 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 org.webpieces.http2client.api.dto.FullRequest in project webpieces by deanhiller.
the class TestKeepContextHttp2 method testSeperationOfMDCFromServer.
@Test
public void testSeperationOfMDCFromServer() {
String mdcKey = "test";
String mdcValue = "value";
// test out "something":null converts to "" in java....
String json = "{ `meta`: { `numResults`: 4 }, `testValidation`:`notBlank` }".replace("`", "\"");
FullRequest req = org.webpieces.webserver.test.http2.Requests.createJsonRequest("POST", "/json/simple", json);
MDC.put(mdcKey, mdcValue);
// not exactly part of this test but checking for leak of server context into client
// (only in embedded modes does this occur)
Assert.assertEquals(0, Context.getContext().size());
XFuture<FullResponse> respFuture = http2Socket.send(req);
ResponseWrapperHttp2 response = ResponseExtract.waitAndWrap(respFuture);
// not exactly part of this test but checking for leak of server context into client
// (only in embedded modes does this occur)
Assert.assertEquals(0, Context.getContext().size());
// validate that MDC was not blown away
Assert.assertEquals(mdcValue, MDC.get(mdcKey));
response.assertStatusCode(StatusCode.HTTP_200_OK);
response.assertContentType("application/json");
}
use of org.webpieces.http2client.api.dto.FullRequest 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 org.webpieces.http2client.api.dto.FullRequest in project webpieces by deanhiller.
the class Http2SocketImpl method send.
/**
* Can't specifically backpressure with this method(ie. On the other method, if you do not ack, eventually
* with too many bytes, the channelmanager disregisters and stops reading from the socket placing backpressure
* on the socket)
*/
@Override
public CompletableFuture<FullResponse> send(FullRequest request) {
SingleResponseListener responseListener = new SingleResponseListener();
StreamHandle streamHandle = openStream();
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);
return streamHandle.process(request.getHeaders(), responseListener).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);
return streamHandle.process(request.getHeaders(), responseListener).thenCompose(writer -> {
data.setStreamId(req.getStreamId());
return writer.processPiece(data);
}).thenCompose(writer -> {
trailers.setStreamId(req.getStreamId());
return writer.processPiece(trailers);
}).thenCompose(writer -> responseListener.fetchResponseFuture());
}
Aggregations