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;
}
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());
}
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());
}
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);
}
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);
}
Aggregations