Search in sources :

Example 1 with HttpResponse

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

the class TestResponseParsing method testPartialHttpMessage.

@Test
public void testPartialHttpMessage() {
    HttpResponse response = createOkResponse();
    byte[] payload = unwrap(parser.marshalToByteBuffer(state, response));
    byte[] firstPart = new byte[10];
    byte[] secondPart = new byte[payload.length - firstPart.length];
    //let's split the payload up into two pieces
    System.arraycopy(payload, 0, firstPart, 0, firstPart.length);
    System.arraycopy(payload, firstPart.length, secondPart, 0, secondPart.length);
    DataWrapper data1 = dataGen.wrapByteArray(firstPart);
    DataWrapper data2 = dataGen.wrapByteArray(secondPart);
    Memento memento = parser.prepareToParse();
    memento = parser.parse(memento, data1);
    Assert.assertEquals(0, memento.getParsedMessages().size());
    memento = parser.parse(memento, data2);
    Assert.assertEquals(1, memento.getParsedMessages().size());
    HttpPayload httpMessage = memento.getParsedMessages().get(0);
    Assert.assertEquals(response, httpMessage);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpPayload(org.webpieces.httpparser.api.dto.HttpPayload) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) Test(org.junit.Test)

Example 2 with HttpResponse

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

the class HttpParserImpl method parseResponse.

private HttpMessage parseResponse(MementoImpl memento, List<String> lines) {
    //remove first line...
    String firstLine = lines.remove(0);
    //In the case of response, a reason may contain spaces so we must split on first and second
    //whitespace only
    int indexOf = firstLine.indexOf(" ");
    if (indexOf < 0)
        throw new IllegalArgumentException("The first line of http request is invalid=" + firstLine);
    String versionStr = firstLine.substring(0, indexOf).trim();
    String tail = firstLine.substring(indexOf).trim();
    int indexOf2 = tail.indexOf(" ");
    if (indexOf2 < 0)
        throw new IllegalArgumentException("The first line of http request is invalid=" + firstLine);
    String codeStr = tail.substring(0, indexOf2).trim();
    String reason = tail.substring(indexOf2).trim();
    HttpVersion version2 = parseVersion(versionStr, firstLine);
    HttpResponseStatus status = new HttpResponseStatus();
    Integer codeVal = toInteger(codeStr, firstLine);
    if (codeVal <= 0 || codeVal >= 1000)
        throw new IllegalArgumentException("invalid status code.  response line=" + firstLine);
    status.setCode(codeVal);
    status.setReason(reason);
    HttpResponseStatusLine httpRequestLine = new HttpResponseStatusLine();
    httpRequestLine.setStatus(status);
    httpRequestLine.setVersion(version2);
    HttpResponse response = new HttpResponse();
    response.setStatusLine(httpRequestLine);
    parseHeaders(lines, response);
    memento.addMessage(response);
    return response;
}
Also used : HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine) HttpVersion(org.webpieces.httpparser.api.dto.HttpVersion)

Example 3 with HttpResponse

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

the class TestChunkedParsing method create400Response.

static HttpResponse create400Response() {
    Header header1 = new Header();
    header1.setName(KnownHeaderName.AGE);
    header1.setValue("CooolValue");
    HttpResponseStatus status = new HttpResponseStatus();
    status.setKnownStatus(KnownStatusCode.HTTP_400_BADREQUEST);
    HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
    statusLine.setStatus(status);
    HttpResponse resp = new HttpResponse();
    resp.setStatusLine(statusLine);
    resp.addHeader(header1);
    return resp;
}
Also used : Header(org.webpieces.httpparser.api.common.Header) HttpResponseStatus(org.webpieces.httpparser.api.dto.HttpResponseStatus) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) HttpResponseStatusLine(org.webpieces.httpparser.api.dto.HttpResponseStatusLine)

Example 4 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 5 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)

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