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