Search in sources :

Example 1 with FullRequest

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());
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) FullResponse(org.webpieces.http2client.api.dto.FullResponse) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) FullRequest(org.webpieces.http2client.api.dto.FullRequest) Test(org.junit.Test)

Example 2 with FullRequest

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));
}
Also used : FullRequest(org.webpieces.http2client.api.dto.FullRequest) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) XFuture(org.webpieces.util.futures.XFuture) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) StreamRef(com.webpieces.http2.api.streaming.StreamRef) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) DataWrapper(org.webpieces.data.api.DataWrapper) FullResponse(org.webpieces.http2client.api.dto.FullResponse) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Supplier(java.util.function.Supplier) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Trailers(com.webpieces.http2.api.dto.highlevel.Http2Trailers) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Example 3 with FullRequest

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");
}
Also used : HttpFullResponse(org.webpieces.httpclient11.api.HttpFullResponse) FullResponse(org.webpieces.http2client.api.dto.FullResponse) ResponseWrapperHttp2(org.webpieces.webserver.test.http2.ResponseWrapperHttp2) HttpFullRequest(org.webpieces.httpclient11.api.HttpFullRequest) FullRequest(org.webpieces.http2client.api.dto.FullRequest) AbstractHttp2Test(org.webpieces.webserver.test.http2.AbstractHttp2Test) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 4 with FullRequest

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;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Contexts(org.webpieces.util.context.Contexts) DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Socket(org.webpieces.http2client.api.Http2Socket) FullRequest(org.webpieces.http2client.api.dto.FullRequest)

Example 5 with FullRequest

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());
}
Also used : Logger(org.webpieces.util.logging.Logger) Http2Trailers(com.webpieces.hpack.api.dto.Http2Trailers) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) FullRequest(org.webpieces.http2client.api.dto.FullRequest) Http2Request(com.webpieces.hpack.api.dto.Http2Request) Http2ClientEngineFactory(com.webpieces.http2engine.api.client.Http2ClientEngineFactory) DataWrapper(org.webpieces.data.api.DataWrapper) FullResponse(org.webpieces.http2client.api.dto.FullResponse) LoggerFactory(org.webpieces.util.logging.LoggerFactory) StreamHandle(com.webpieces.http2engine.api.StreamHandle) Http2ClientEngine(com.webpieces.http2engine.api.client.Http2ClientEngine) Http2Socket(org.webpieces.http2client.api.Http2Socket) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Http2Request(com.webpieces.hpack.api.dto.Http2Request) StreamHandle(com.webpieces.http2engine.api.StreamHandle) Http2Trailers(com.webpieces.hpack.api.dto.Http2Trailers) DataFrame(com.webpieces.http2parser.api.dto.DataFrame)

Aggregations

FullRequest (org.webpieces.http2client.api.dto.FullRequest)14 FullResponse (org.webpieces.http2client.api.dto.FullResponse)9 Test (org.junit.Test)6 DataWrapper (org.webpieces.data.api.DataWrapper)5 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)4 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)3 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)3 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)3 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)3 Http2Trailers (com.webpieces.http2.api.dto.highlevel.Http2Trailers)2 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)2 InetSocketAddress (java.net.InetSocketAddress)2 Http2Socket (org.webpieces.http2client.api.Http2Socket)2 HttpFullResponse (org.webpieces.httpclient11.api.HttpFullResponse)2 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)2 AbstractHttp2Test (org.webpieces.webserver.test.http2.AbstractHttp2Test)2 ResponseWrapperHttp2 (org.webpieces.webserver.test.http2.ResponseWrapperHttp2)2 Http2Request (com.webpieces.hpack.api.dto.Http2Request)1 Http2Trailers (com.webpieces.hpack.api.dto.Http2Trailers)1 Http2Headers (com.webpieces.http2.api.dto.highlevel.Http2Headers)1