use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestCancelStream method testClientCancelNoKeepAlive.
@Test
public void testClientCancelNoKeepAlive() {
XFuture<Void> connect = httpSocket.connect(new InetSocketAddress(8555));
MockResponseListener mockListener = new MockResponseListener();
Http2Request req = Requests.createRequest(false);
mockChannel.addWriteResponse(XFuture.completedFuture(null));
RequestStreamHandle requestStream = httpSocket.openStream();
StreamRef ref = requestStream.process(req, mockListener);
CancelReason reason = new RstStreamFrame();
XFuture<Void> cancelDone = ref.cancel(reason);
Assert.assertTrue(cancelDone.isDone());
// Assert the socket is NOT closed
Assert.assertTrue(mockChannel.isClosed());
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestHttp1Backpressure method testBasicBackpressure.
@Test
public void testBasicBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
MockResponseListener listener = new MockResponseListener();
RequestStreamHandle handle = socket.openStream();
mockChannel.addWriteResponse(XFuture.completedFuture(null));
Http2Request request = Requests.createRequest();
StreamRef streamRef = handle.process(request, listener);
XFuture<StreamWriter> writer = streamRef.getWriter();
Assert.assertTrue(writer.isDone());
Assert.assertEquals(request, mockChannel.getLastWriteParam());
Http2Response response1 = Requests.createResponse(1, 250);
HttpData response2 = Requests.createHttpData(250);
List<ByteBuffer> buffers = create3BuffersWithTwoMessags(response1, response2);
DataListener dataListener = mockChannel.getConnectedListener();
XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
listener.addProcessResponse(requestFuture);
XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
// resolved since data is cached in parser (client doesn't receive this data)
Assert.assertTrue(fut1.isDone());
// This next one is confusing BUT in http1.1 parsing terms, data is data for content length so this results
// in a full HttpData packet actually...
XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
// not resolved yet since client only has part of the data
Assert.assertFalse(fut2.isDone());
MockStreamWriter mockWriter = new MockStreamWriter();
XFuture<Void> streamWriterFuture = new XFuture<Void>();
mockWriter.addProcessResponse(streamWriterFuture);
// This releases BOTH packets above to be processed!!!(not just the one)
requestFuture.complete(mockWriter);
Assert.assertFalse(fut2.isDone());
streamWriterFuture.complete(null);
// NOW it's resolved
fut2.get(2, TimeUnit.SECONDS);
// feed the last buffer in
XFuture<Void> future3 = new XFuture<Void>();
mockWriter.addProcessResponse(future3);
XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
Assert.assertFalse(fut3.isDone());
future3.complete(null);
fut3.get(2, TimeUnit.SECONDS);
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class Requests method createRequest.
static Http2Request createRequest(boolean eos) {
List<Http2Header> headers = new ArrayList<>();
headers.add(new Http2Header(Http2HeaderName.METHOD, "GET"));
headers.add(new Http2Header(Http2HeaderName.PATH, "/"));
headers.add(new Http2Header(Http2HeaderName.SCHEME, "http"));
headers.add(new Http2Header(Http2HeaderName.AUTHORITY, "somehost.com"));
headers.add(new Http2Header("serverid", "1"));
headers.add(new Http2Header(Http2HeaderName.ACCEPT, "*/*"));
headers.add(new Http2Header(Http2HeaderName.ACCEPT_ENCODING, "gzip, deflate"));
headers.add(new Http2Header(Http2HeaderName.USER_AGENT, "webpieces/1.15.0"));
Http2Request request = new Http2Request(headers);
request.setEndOfStream(eos);
return request;
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class TestWriteReads method testBasicReadWrite.
@Test
public void testBasicReadWrite() throws InterruptedException, ExecutionException, TimeoutException {
MockResponseListener listener = new MockResponseListener();
RequestStreamHandle handle = socket.openStream();
mockChannel.addWriteResponse(XFuture.completedFuture(null));
Http2Request request = Requests.createRequest();
StreamRef streamRef = handle.process(request, listener);
XFuture<StreamWriter> writer = streamRef.getWriter();
Assert.assertTrue(writer.isDone());
Assert.assertEquals(request, mockChannel.getLastWriteParam());
MockResponseListener listener2 = new MockResponseListener();
request.getHeaderLookupStruct().getHeader("serverid").setValue("2");
mockChannel.addWriteResponse(XFuture.completedFuture(null));
StreamRef streamRef1 = handle.process(request, listener2);
XFuture<StreamWriter> writer2 = streamRef1.getWriter();
Assert.assertTrue(writer2.isDone());
Assert.assertEquals(request, mockChannel.getLastWriteParam());
Http2Response response1 = Requests.createResponse(1, 0);
listener.addProcessResponse(XFuture.completedFuture(null));
XFuture<Void> fut1 = mockChannel.writeResponse(response1);
// throws if exception occurred and ensures future resolved
fut1.get(2, TimeUnit.SECONDS);
Http2Response msg = listener.getIncomingMsg();
Assert.assertEquals(response1, msg);
Http2Response response2 = Requests.createResponse(2, 0);
listener2.addProcessResponse(XFuture.completedFuture(null));
XFuture<Void> fut2 = mockChannel.writeResponse(response2);
// throws if exception occurred and ensures future resolved
fut2.get(2, TimeUnit.SECONDS);
Http2Response msg2 = listener2.getIncomingMsg();
Assert.assertEquals(response2, msg2);
}
use of com.webpieces.http2.api.dto.highlevel.Http2Request in project webpieces by deanhiller.
the class Http11ToHttp2 method requestToHeaders.
private static Http2Request requestToHeaders(HttpRequest request, boolean fromSslChannel) {
HttpRequestLine requestLine = request.getRequestLine();
List<Header> requestHeaders = request.getHeaders();
LinkedList<Http2Header> headerList = new LinkedList<>();
// add special headers
headerList.add(new Http2Header(":method", requestLine.getMethod().getMethodAsString()));
UrlInfo urlInfo = requestLine.getUri().getUriBreakdown();
headerList.add(new Http2Header(":path", requestLine.getUri().getUri()));
// Figure out scheme
if (urlInfo.getPrefix() != null) {
headerList.add(new Http2Header(":scheme", urlInfo.getPrefix()));
} else if (fromSslChannel) {
headerList.add(new Http2Header(":scheme", "https"));
} else {
headerList.add(new Http2Header(":scheme", "http"));
}
// Figure out authority
Header hostHeader = request.getHeaderLookupStruct().getHeader(KnownHeaderName.HOST);
if (hostHeader == null)
throw new IllegalArgumentException("Host header is required in http1.1");
// Add regular headers
for (Header header : requestHeaders) {
if (header.getKnownName() == KnownHeaderName.HOST) {
// keeps headers in order of http1 headers
String h = hostHeader.getValue();
headerList.add(new Http2Header(":authority", h));
continue;
}
headerList.add(new Http2Header(header.getName().toLowerCase(), header.getValue()));
}
Http2Request headers = new Http2Request(headerList);
Header contentLen = request.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH);
if (request.isHasChunkedTransferHeader()) {
headers.setEndOfStream(false);
} else if (contentLenGreaterThanZero(contentLen)) {
headers.setEndOfStream(false);
} else
headers.setEndOfStream(true);
return headers;
}
Aggregations