Search in sources :

Example 11 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class Http1_1StreamImpl method sendResponse.

@Override
public CompletableFuture<StreamWriter> sendResponse(Http2Response headers) {
    closeCheck();
    HttpResponse response = Http2Translations.translateResponse(headers);
    if (headers.isEndOfStream()) {
        validateHeader(response);
        remove(headers);
        return write(response).thenApply(w -> {
            permitQueue.releasePermit();
            return new NoWritesWriter();
        });
    } else if (contentLengthGreaterThanZero(headers)) {
        return write(response).thenApply(w -> new ContentLengthResponseWriter(headers));
    }
    response.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
    return write(response).thenApply(c -> new Http11ChunkedWriter());
}
Also used : Channel(org.webpieces.nio.api.channels.Channel) HttpChunk(org.webpieces.httpparser.api.dto.HttpChunk) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) ByteBuffer(java.nio.ByteBuffer) Http2Msg(com.webpieces.http2parser.api.dto.lib.Http2Msg) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpStream(org.webpieces.frontend2.api.HttpStream) HttpData(org.webpieces.httpparser.api.dto.HttpData) FrontendSocket(org.webpieces.frontend2.api.FrontendSocket) Logger(org.webpieces.util.logging.Logger) Header(org.webpieces.httpparser.api.common.Header) PermitQueue(com.webpieces.util.locking.PermitQueue) Http2Translations(org.webpieces.frontend2.impl.translation.Http2Translations) HttpParser(org.webpieces.httpparser.api.HttpParser) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) KnownHeaderName(org.webpieces.httpparser.api.common.KnownHeaderName) StreamSession(org.webpieces.frontend2.api.StreamSession) Http2HeaderName(com.webpieces.http2parser.api.dto.lib.Http2HeaderName) HttpLastChunk(org.webpieces.httpparser.api.dto.HttpLastChunk) PushStreamHandle(com.webpieces.http2engine.api.PushStreamHandle) StreamMsg(com.webpieces.http2parser.api.dto.lib.StreamMsg) StreamWriter(com.webpieces.http2engine.api.StreamWriter) LoggerFactory(org.webpieces.util.logging.LoggerFactory) Http2Response(com.webpieces.hpack.api.dto.Http2Response) ResponseStream(org.webpieces.frontend2.api.ResponseStream) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Header(org.webpieces.httpparser.api.common.Header) Http2Header(com.webpieces.http2parser.api.dto.lib.Http2Header) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse)

Example 12 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class MockTcpChannel method write.

@Override
public CompletableFuture<Channel> write(ByteBuffer b) {
    DataWrapper data = dataGen.wrapByteBuffer(b);
    memento = parser.parse(memento, data);
    List<HttpPayload> parsedMessages = memento.getParsedMessages();
    for (HttpPayload payload : parsedMessages) {
        if (payload instanceof HttpResponse) {
            sendResponse((HttpResponse) payload);
        } else {
            sendData((HttpData) payload);
        }
    }
    return CompletableFuture.completedFuture(this);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse)

Example 13 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class TestHttp11Basic method testSendTwoRequestsStreamFirst.

@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
    mockChannel.write(req);
    PassedIn in1 = mockListener.getSingleRequest();
    byte[] buf = new byte[10];
    DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
    HttpData data1 = new HttpData(dataWrapper, false);
    mockChannel.write(data1);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    DataWrapper dataWrapper2 = dataGen.wrapByteArray(buf);
    HttpData data2 = new HttpData(dataWrapper2, true);
    mockChannel.write(data2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    mockChannel.write(req2);
    Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
    //send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
    HttpResponse resp1 = Requests.createResponse(1);
    resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
    in1.stream.sendResponse(headers1);
    HttpPayload payload = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, payload);
    PassedIn in2 = mockListener.getSingleRequest();
    HttpResponse resp2 = Requests.createResponse(2);
    resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
    Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
    in2.stream.sendResponse(headers2);
    HttpPayload payload2 = mockChannel.getFrameAndClear();
    Assert.assertEquals(resp2, payload2);
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) DataWrapper(org.webpieces.data.api.DataWrapper) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpData(org.webpieces.httpparser.api.dto.HttpData) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 14 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class TestHttp11Basic method testUploadWithBody.

@Test
public void testUploadWithBody() throws InterruptedException, ExecutionException, TimeoutException {
    String bodyStr = "hi there, how are you";
    DataWrapper dataWrapper = dataGen.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
    HttpData body = new HttpData(dataWrapper, true);
    req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + dataWrapper.getReadableSize()));
    mockChannel.write(req);
    mockChannel.write(body);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2Translations.translateRequest(in1.request);
    Assert.assertEquals(req, req1);
    DataFrame frame = (DataFrame) mockStreamWriter.getSingleFrame();
    DataWrapper data = frame.getData();
    Assert.assertEquals(bodyStr, data.createStringFromUtf8(0, data.getReadableSize()));
    Assert.assertTrue(frame.isEndOfStream());
    HttpResponse resp = Requests.createNobodyResponse();
    Http2Response http2Resp = Http2Translations.responseToHeaders(resp);
    CompletableFuture<StreamWriter> fut = in1.stream.sendResponse(http2Resp);
    fut.get(2, TimeUnit.SECONDS);
    HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, respToClient);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData) StreamWriter(com.webpieces.http2engine.api.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 15 with HttpResponse

use of org.webpieces.httpparser.api.dto.HttpResponse in project webpieces by deanhiller.

the class HttpSocketImpl method send.

@Override
public CompletableFuture<HttpResponse> send(HttpRequest request) {
    CompletableFuture<HttpResponse> future = new CompletableFuture<HttpResponse>();
    HttpResponseListener l = new CompletableListener(future);
    send(request, l);
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseListener(org.webpieces.httpclient.api.HttpResponseListener)

Aggregations

HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)26 Test (org.junit.Test)17 Header (org.webpieces.httpparser.api.common.Header)15 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)13 DataWrapper (org.webpieces.data.api.DataWrapper)12 Http2Response (com.webpieces.hpack.api.dto.Http2Response)7 HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)7 HttpResponseStatusLine (org.webpieces.httpparser.api.dto.HttpResponseStatusLine)7 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)6 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)6 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)6 HttpData (org.webpieces.httpparser.api.dto.HttpData)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)4 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)4 HttpLastChunk (org.webpieces.httpparser.api.dto.HttpLastChunk)4 CompletableFuture (java.util.concurrent.CompletableFuture)2 PushStreamHandle (com.webpieces.http2engine.api.PushStreamHandle)1 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)1 Http2Msg (com.webpieces.http2parser.api.dto.lib.Http2Msg)1