use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestResponseNoContent.
@Test
public void testRequestResponseNoContent() throws Exception {
final CountDownLatch latch = new CountDownLatch(3);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
latch.countDown();
}
});
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
MetaData.Request metaData = newRequest("GET", new HttpFields());
generator.control(lease, new HeadersFrame(1, metaData, null, true));
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) {
output.write(BufferUtil.toArray(buffer));
}
final AtomicReference<HeadersFrame> frameRef = new AtomicReference<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onSettings(SettingsFrame frame) {
latch.countDown();
}
@Override
public void onHeaders(HeadersFrame frame) {
frameRef.set(frame);
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
HeadersFrame response = frameRef.get();
Assert.assertNotNull(response);
MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
Assert.assertEquals(200, responseMetaData.getStatus());
}
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class HTTP2ServerTest method testBadPingWrongPayload.
@Test
public void testBadPingWrongPayload() throws Exception {
startServer(new HttpServlet() {
});
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
generator.control(lease, new PingFrame(new byte[8], false));
// Modify the length of the frame to a wrong one.
lease.getByteBuffers().get(2).putShort(0, (short) 7);
final CountDownLatch latch = new CountDownLatch(1);
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) {
output.write(BufferUtil.toArray(buffer));
}
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onGoAway(GoAwayFrame frame) {
Assert.assertEquals(ErrorCode.FRAME_SIZE_ERROR.code, frame.getError());
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class HTTP2ServerTest method testCommitFailure.
@Test
public void testCommitFailure() throws Exception {
final long delay = 1000;
final AtomicBoolean broken = new AtomicBoolean();
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Wait for the SETTINGS frames to be exchanged.
Thread.sleep(delay);
broken.set(true);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
server.stop();
ServerConnector connector2 = new ServerConnector(server, new HTTP2ServerConnectionFactory(new HttpConfiguration())) {
@Override
protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {
@Override
public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException {
if (broken.get())
callback.failed(new IOException("explicitly_thrown_by_test"));
else
super.write(callback, buffers);
}
};
}
};
server.addConnector(connector2);
server.start();
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
MetaData.Request metaData = newRequest("GET", new HttpFields());
generator.control(lease, new HeadersFrame(1, metaData, null, true));
try (Socket client = new Socket("localhost", connector2.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
// The server will close the connection abruptly since it
// cannot write and therefore cannot even send the GO_AWAY.
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
boolean closed = parseResponse(client, parser, 2 * delay);
Assert.assertTrue(closed);
}
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class HTTP2ServerTest method testBadPingWrongStreamId.
@Test
public void testBadPingWrongStreamId() throws Exception {
startServer(new HttpServlet() {
});
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
generator.control(lease, new PingFrame(new byte[8], false));
// Modify the streamId of the frame to non zero.
lease.getByteBuffers().get(2).putInt(4, 1);
final CountDownLatch latch = new CountDownLatch(1);
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) {
output.write(BufferUtil.toArray(buffer));
}
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onGoAway(GoAwayFrame frame) {
Assert.assertEquals(ErrorCode.PROTOCOL_ERROR.code, frame.getError());
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testAfterContentTransformerDoNoTransform.
private void testAfterContentTransformerDoNoTransform(final boolean readSource, final boolean useDisk) throws Exception {
final String key0 = "id";
long value0 = 1;
final String key1 = "channel";
String value1 = "foo";
final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new AfterContentTransformer() {
{
if (useDisk)
setMaxInputBufferSize(0);
}
@Override
public boolean transform(Source source, Sink sink) throws IOException {
if (readSource) {
InputStream input = source.getInputStream();
JSON.parse(new InputStreamReader(input, "UTF-8"));
}
// No transformation.
return false;
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
@SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
Assert.assertNotNull(obj);
Assert.assertEquals(2, obj.size());
Assert.assertEquals(value0, obj.get(key0));
Assert.assertEquals(value1, obj.get(key1));
}
Aggregations