Search in sources :

Example 11 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class HttpLastChunk method createTrailer.

@Override
public String createTrailer() {
    String lastPart = "";
    for (Header header : getHeaders()) {
        lastPart += header.getName() + ": " + header.getValue() + TRAILER_STR;
    }
    lastPart += TRAILER_STR;
    return lastPart;
}
Also used : Header(org.webpieces.httpparser.api.common.Header)

Example 12 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class TestSyncWebServer method testAbsoluteHtmlPath.

@Test
public void testAbsoluteHtmlPath() {
    HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/myroute2");
    http11Socket.send(req);
    FullResponse response = ResponseExtract.assertSingleResponse(http11Socket);
    response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
    response.assertContains("This is the first raw html page");
    response.assertContentType("text/html; charset=utf-8");
    List<Header> headers = response.getResponse().getHeaderLookupStruct().getHeaders(KnownHeaderName.CONTENT_TYPE);
    Assert.assertEquals(1, headers.size());
}
Also used : HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) FullResponse(org.webpieces.webserver.test.FullResponse) Header(org.webpieces.httpparser.api.common.Header) Test(org.junit.Test) AbstractWebpiecesTest(org.webpieces.webserver.test.AbstractWebpiecesTest) WebserverForTest(org.webpieces.webserver.WebserverForTest)

Example 13 with Header

use of org.webpieces.httpparser.api.common.Header 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 14 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class FullResponse method createCookieRequestHeader.

/**
	 * Example request cookie from chrome
	 * Cookie: webSession=1-gzvc03bKRP2YYvWySwgENREwFSg=:__ST=3a2fda5dad7547d3b15b1f61bd3d12f5; webFlash=1:_message=Invalid+values+below&user.address.zipCode=Text+instead+of+number&__secureToken=3a2fda5dad7547d3b15b1f61bd3d12f5&user.firstName=Dean+Hiller; webErrors=1:user.address.zipCode=Could+not+convert+value
	 * @return
	 */
public Header createCookieRequestHeader() {
    List<Header> headers = response.getHeaderLookupStruct().getHeaders(KnownHeaderName.SET_COOKIE);
    String fullRequestCookie = "";
    boolean firstLine = true;
    for (Header header : headers) {
        String value = header.getValue();
        if (value.contains(";")) {
            String[] split = value.split(";");
            value = split[0];
        }
        String[] keyVal = value.split("=");
        if (keyVal.length <= 1)
            //skip adding this cookie as it was cleared out
            continue;
        if (firstLine) {
            firstLine = false;
            fullRequestCookie += value;
        } else
            fullRequestCookie += "; " + value;
    }
    return new Header(KnownHeaderName.COOKIE, fullRequestCookie);
}
Also used : Header(org.webpieces.httpparser.api.common.Header)

Example 15 with Header

use of org.webpieces.httpparser.api.common.Header in project webpieces by deanhiller.

the class FullResponse method uncompressBodyAndAssertContainsString.

public void uncompressBodyAndAssertContainsString(String text) {
    Header header = getResponse().getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_ENCODING);
    if (header == null)
        throw new IllegalStateException("Body is not compressed as no CONTENT_ENCODING header field exists");
    else if (!"gzip".equals(header.getValue()))
        throw new IllegalStateException("Body has wrong compression type=" + header.getValue() + " in CONTENT_ENCODING header field");
    DataWrapper wrapper = getBody();
    byte[] compressed = wrapper.createByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(compressed);
    byte[] out = new byte[10000];
    DataWrapper output = dataGen.emptyWrapper();
    try (GZIPInputStream str = new GZIPInputStream(in)) {
        int read = 0;
        while ((read = str.read(out)) > 0) {
            ByteBuffer buffer = ByteBuffer.wrap(out, 0, read);
            DataWrapper byteWrapper = dataGen.wrapByteBuffer(buffer);
            output = dataGen.chainDataWrappers(output, byteWrapper);
            out = new byte[10000];
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Charset charset = extractCharset();
    String bodyAsString = output.createStringFrom(0, output.getReadableSize(), charset);
    if (!bodyAsString.contains(text))
        throw new IllegalStateException("Expected compressed body to contain='" + text + "' but body was=" + bodyAsString);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.webpieces.httpparser.api.common.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Header (org.webpieces.httpparser.api.common.Header)64 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)32 Test (org.junit.Test)24 DataWrapper (org.webpieces.data.api.DataWrapper)18 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)16 FullResponse (org.webpieces.webserver.test.FullResponse)16 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)13 WebserverForTest (org.webpieces.webserver.WebserverForTest)12 HttpData (org.webpieces.httpparser.api.dto.HttpData)11 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)10 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)10 HttpRequestLine (org.webpieces.httpparser.api.dto.HttpRequestLine)10 HttpUri (org.webpieces.httpparser.api.dto.HttpUri)9 HttpDummyRequest (org.webpieces.webserver.test.HttpDummyRequest)9 Http2Response (com.webpieces.hpack.api.dto.Http2Response)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)7 HttpChunk (org.webpieces.httpparser.api.dto.HttpChunk)7 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)5 HttpResponseStatus (org.webpieces.httpparser.api.dto.HttpResponseStatus)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4