Search in sources :

Example 6 with Http2Request

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));
}
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 7 with Http2Request

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();
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) AbstractHttp2Test(org.webpieces.webserver.test.http2.AbstractHttp2Test) PrivateWebserverForTest(org.webpieces.webserver.PrivateWebserverForTest) Test(org.junit.Test)

Example 8 with Http2Request

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);
    }
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with Http2Request

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;
}
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 10 with Http2Request

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 + "'"));
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request)

Aggregations

Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)71 Test (org.junit.Test)39 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)36 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)36 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)33 StreamRef (com.webpieces.http2.api.streaming.StreamRef)32 DataWrapper (org.webpieces.data.api.DataWrapper)18 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)14 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)14 XFuture (org.webpieces.util.futures.XFuture)13 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)10 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)10 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)9 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)9 InetSocketAddress (java.net.InetSocketAddress)8 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)7 MockStreamWriter (org.webpieces.httpfrontend2.api.mock2.MockStreamWriter)7 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)6