use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.
the class HTTP2ServerTest method testNonISOHeader.
@Test
public void testNonISOHeader() throws Exception {
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Invalid header name, the connection must be closed.
response.setHeader("Euro_(€)", "42");
}
});
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));
output.flush();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
boolean closed = parseResponse(client, parser);
Assert.assertTrue(closed);
}
}
}
use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.
the class HTTP2ServerTest method testNoPrefaceBytes.
@Test
public void testNoPrefaceBytes() throws Exception {
startServer(new HttpServlet() {
});
// No preface bytes.
MetaData.Request metaData = newRequest("GET", new HttpFields());
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
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 CountDownLatch latch = new CountDownLatch(1);
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onGoAway(GoAwayFrame frame) {
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestWithContinuationFrames.
@Test
public void testRequestWithContinuationFrames() throws Exception {
testRequestWithContinuationFrames(null, () -> {
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));
return lease;
});
}
use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestResponseContent.
@Test
public void testRequestResponseContent() throws Exception {
final byte[] content = "Hello, world!".getBytes(StandardCharsets.UTF_8);
final CountDownLatch latch = new CountDownLatch(4);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
latch.countDown();
resp.getOutputStream().write(content);
}
});
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> headersRef = new AtomicReference<>();
final AtomicReference<DataFrame> dataRef = 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) {
headersRef.set(frame);
latch.countDown();
}
@Override
public void onData(DataFrame frame) {
dataRef.set(frame);
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
HeadersFrame response = headersRef.get();
Assert.assertNotNull(response);
MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
Assert.assertEquals(200, responseMetaData.getStatus());
DataFrame responseData = dataRef.get();
Assert.assertNotNull(responseData);
Assert.assertArrayEquals(content, BufferUtil.toArray(responseData.getData()));
}
}
use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.
the class HttpChannelOverHTTP2 method onRequest.
public Runnable onRequest(HeadersFrame frame) {
try {
MetaData.Request request = (MetaData.Request) frame.getMetaData();
HttpFields fields = request.getFields();
// pseudo-header, so we need to synthesize a Host header.
if (!fields.contains(HttpHeader.HOST)) {
String authority = request.getURI().getAuthority();
if (authority != null) {
// Lower-case to be consistent with other HTTP/2 headers.
fields.put("host", authority);
}
}
_expect100Continue = fields.contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString());
HttpFields response = getResponse().getHttpFields();
if (getHttpConfiguration().getSendServerVersion())
response.add(SERVER_VERSION);
if (getHttpConfiguration().getSendXPoweredBy())
response.add(POWERED_BY);
onRequest(request);
boolean endStream = frame.isEndStream();
if (endStream) {
onContentComplete();
onRequestComplete();
}
_delayedUntilContent = getHttpConfiguration().isDelayDispatchUntilContent() && !endStream && !_expect100Continue;
_handled = !_delayedUntilContent;
if (LOG.isDebugEnabled()) {
Stream stream = getStream();
LOG.debug("HTTP2 Request #{}/{}, delayed={}:{}{} {} {}{}{}", stream.getId(), Integer.toHexString(stream.getSession().hashCode()), _delayedUntilContent, System.lineSeparator(), request.getMethod(), request.getURI(), request.getHttpVersion(), System.lineSeparator(), fields);
}
return _delayedUntilContent ? null : this;
} catch (BadMessageException x) {
onBadMessage(x.getCode(), x.getReason());
return null;
} catch (Throwable x) {
onBadMessage(HttpStatus.INTERNAL_SERVER_ERROR_500, null);
return null;
}
}
Aggregations