use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class TestHttp11Backpressure method initialize.
private void initialize() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
XFuture<Void> future2 = mockChannel.sendToSvrAsync(req);
// The default return was a completed future so no backpressure here
Assert.assertTrue(future2.isDone());
PassedIn in1 = mockListener.getSingleRequest();
HttpResponse resp1 = Requests.createResponse(1);
Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
XFuture<StreamWriter> future = in1.stream.process(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
// server should add content-length 0 for firefox
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Assert.assertEquals(resp1, payload);
future.get(2, TimeUnit.SECONDS);
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class TestHttp11Errors method testCloseAfter2ndRequest.
@Test
public void testCloseAfter2ndRequest() 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());
byte[] buf = new byte[10];
DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, true);
DataFrame data = (DataFrame) Http11ToHttp2.translateData(data1);
writer.processPiece(data);
fut1.get(2, TimeUnit.SECONDS);
Assert.assertFalse(mockStreamRefA.isCancelled());
Assert.assertFalse(mockStreamRefB.isCancelled());
mockChannel.simulateClose();
// this request is done, nothing to cancel
Assert.assertFalse(mockStreamRefA.isCancelled());
Assert.assertTrue(mockStreamRefB.isCancelled());
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class Requests method createPostRequest.
public static List<HttpPayload> createPostRequest(String url, String... argTuples) {
if (argTuples.length % 2 != 0)
throw new IllegalArgumentException("argTuples.length must be of even size (key/value)");
HttpUri httpUri = new HttpUri(url);
HttpRequestLine requestLine = new HttpRequestLine();
requestLine.setMethod(KnownHttpMethod.POST);
requestLine.setUri(httpUri);
HttpRequest req = new HttpRequest();
req.setRequestLine(requestLine);
req.addHeader(new Header(KnownHeaderName.HOST, "myhost.com"));
String encodedParams = "";
for (int i = 0; i < argTuples.length; i += 2) {
String key = URLEncoder.encode(argTuples[i], StandardCharsets.UTF_8);
String value = URLEncoder.encode(argTuples[i + 1], StandardCharsets.UTF_8);
if (!"".equals(encodedParams))
encodedParams += "&";
encodedParams += key + "=" + value;
}
HttpData data = new HttpData();
data.setEndOfData(true);
byte[] bytes = encodedParams.getBytes(StandardCharsets.UTF_8);
DataWrapper body = gen.wrapByteArray(bytes);
data.setBody(body);
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + body.getReadableSize()));
req.addHeader(new Header(KnownHeaderName.CONTENT_TYPE, "application/x-www-form-urlencoded"));
List<HttpPayload> payloads = new ArrayList<>();
payloads.add(req);
payloads.add(data);
return payloads;
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamFirst.
@Test
public void testSendTwoRequestsStreamFirst() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "20"));
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
byte[] buf = new byte[10];
DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, false);
mockChannel.sendToSvr(data1);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
DataWrapper dataWrapper2 = DATA_GEN.wrapByteArray(buf);
HttpData data2 = new HttpData(dataWrapper2, true);
mockChannel.sendToSvr(data2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
XFuture<Void> fut = mockChannel.sendToSvrAsync(req2);
Assert.assertFalse(fut.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
// send back request2's response first!!!! BUT verify it does not go to client per http11 pipelining rules
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
in1.stream.process(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
fut.get(2, TimeUnit.SECONDS);
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http11ToHttp2.responseToHeaders(resp2);
in2.stream.process(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class Layer2Http11Handler method processWithBackpressure.
public XFuture<Void> processWithBackpressure(FrontendSocketImpl socket, int newDataSize, int numBytesRead) {
Memento state = socket.getHttp11ParseState();
List<HttpPayload> parsed = state.getParsedMessages();
WebSession session = (WebSession) socket.getSession().get(WEB_SESSION_KEY);
if (session == null) {
session = new WebSession();
socket.getSession().put(WEB_SESSION_KEY, session);
}
// ALL of the below MUST happen AFTER the previous processing happened
// which may not have finished so chain the below with the previous future
XFuture<Void> future = session.getProcessFuture();
for (HttpPayload payload : parsed) {
// VERY IMPORTANT: Writing the code like this would slam through calling process N times
// BUT it doesn't give the clients a chance to seet a flag between packets
// Mainly done for exceptions and streaming so you can log exc, set a boolean so you
// don't get 100 exceptions while something is happening like socket disconnect
// In these 2 lines of code, processCorrectly is CALLED N times RIGHT NOW
// The code below this only calls them right now IF AND ONLY IF the client returns
// a completed future each time!!!
// XFuture<Void> fut = processCorrectly(socket, payload);
// future = future.thenCompose(s -> fut);
future = future.thenCompose(s -> processCorrectly(socket, payload));
}
// replace with new future so it blocks any future pieces if these are not processed
session.setProcessFuture(future);
return future;
}
Aggregations