use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class Http11ToHttp2 method responseToHeaders.
public static Http2Response responseToHeaders(HttpResponse response) {
List<Http2Header> headers = new ArrayList<>();
headers.add(new Http2Header(Http2HeaderName.STATUS, response.getStatusLine().getStatus().getCode().toString()));
for (Header header : response.getHeaders()) {
if (header.getKnownName() == KnownHeaderName.TRANSFER_ENCODING) {
if ("chunked".equals(header.getValue())) {
// skip as http2 does not allow this header
continue;
}
}
headers.add(new Http2Header(header.getName(), header.getValue()));
}
Http2Response resp = new Http2Response(headers);
Header header = response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
if (header != null) {
int len = Integer.parseInt(header.getValue());
if (len == 0) {
resp.setEndOfStream(true);
} else {
resp.setEndOfStream(false);
}
} else if (response.isHasChunkedTransferHeader()) {
resp.setEndOfStream(false);
} else
resp.setEndOfStream(true);
return resp;
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class Http2ToHttp11 method translateResponse.
public static HttpResponse translateResponse(Http2Response headers) {
HttpResponseStatus status = new HttpResponseStatus();
HttpResponseStatusLine statusLine = new HttpResponseStatusLine();
statusLine.setStatus(status);
HttpResponse response = new HttpResponse();
response.setStatusLine(statusLine);
for (Http2Header header : headers.getHeaders()) {
if (header.getKnownName() == Http2HeaderName.STATUS) {
fillStatus(header, status);
} else if ("reason".equals(header.getName())) {
fillReason(header, status);
} else if (header.getKnownName() == Http2HeaderName.SCHEME) {
// do nothing and drop it
} else {
Header http1Header = convertHeader(header);
response.addHeader(http1Header);
}
}
if (headers.getHeaderLookupStruct().getHeader(Http2HeaderName.CONTENT_LENGTH) == null) {
if (headers.isEndOfStream()) {
// firefox really needs content length!!! if end of stream
response.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
} else {
response.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
}
}
if (status.getCode() == null)
throw new IllegalArgumentException("The header :status is required to send the response");
// Do a queue instead with last few frames that can be enabled
log.debug("response=" + response);
return response;
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class TestHttp11Errors method testCloseBeforeFirstRequestCompletes.
@Test
public void testCloseBeforeFirstRequestCompletes() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
XFuture<StreamWriter> futA = XFuture.completedFuture(null);
MockStreamRef mockStreamRefA = new MockStreamRef(futA);
mockListener.addMockStreamToReturn(mockStreamRefA);
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
XFuture<StreamWriter> futB = XFuture.completedFuture(null);
MockStreamRef mockStreamRefB = new MockStreamRef(futB);
mockListener.addMockStreamToReturn(mockStreamRefB);
XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
XFuture<StreamWriter> future = in1.stream.process(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
Assert.assertFalse(mockStreamRefA.isCancelled());
Assert.assertFalse(mockStreamRefB.isCancelled());
mockChannel.simulateClose();
// this request is done, nothing to cancel
Assert.assertTrue(mockStreamRefA.isCancelled());
Assert.assertFalse(mockStreamRefB.isCancelled());
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class TestS4FrameSizeAndHeaders method fillHeaders.
private void fillHeaders(Http2Response response1) {
String value = "heaheaheaheaheaheahahoz.zhxheh,h,he,he,heaheaeaheaheahoahoahozzoqorqzro.zo.zrszaroatroathoathoathoathoatoh";
for (int i = 0; i < 10; i++) {
value = value + value;
response1.addHeader(new Http2Header("eaheahaheaheaeha" + i, value));
}
}
use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.
the class TestS4FrameSizeAndHeaders method createInterleavedFrames.
private List<Http2Frame> createInterleavedFrames() {
Http2Response response1 = new Http2Response();
response1.setStreamId(1);
response1.setEndOfStream(true);
fillHeaders(response1);
HeaderEncoding encoding = new HeaderEncoding();
List<Http2Frame> frames1 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response1);
Http2Response response2 = new Http2Response();
response2.setStreamId(3);
response1.setEndOfStream(true);
response2.addHeader(new Http2Header(Http2HeaderName.ACCEPT, "value"));
List<Http2Frame> frames2 = encoding.translateToFrames(localSettings.getMaxFrameSize(), new Encoder(localSettings.getHeaderTableSize()), response2);
List<Http2Frame> frames = new ArrayList<>();
frames.addAll(frames1);
frames.add(1, frames2.get(0));
return frames;
}
Aggregations