use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class HttpChannelOverHTTP2 method onRequestTrailers.
public void onRequestTrailers(HeadersFrame frame) {
HttpFields trailers = frame.getMetaData().getFields();
onTrailers(trailers);
onRequestComplete();
if (LOG.isDebugEnabled()) {
Stream stream = getStream();
LOG.debug("HTTP2 Request #{}/{}, trailers:{}{}", stream.getId(), Integer.toHexString(stream.getSession().hashCode()), System.lineSeparator(), trailers);
}
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class CloseTest method testClientAbruptlyClosesConnection.
@Test
public void testClientAbruptlyClosesConnection() throws Exception {
final CountDownLatch closeLatch = new CountDownLatch(1);
final AtomicReference<Session> sessionRef = new AtomicReference<>();
startServer(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
try {
sessionRef.set(stream.getSession());
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
// Reply with HEADERS.
stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
closeLatch.await(5, TimeUnit.SECONDS);
return null;
} catch (InterruptedException x) {
return 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));
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 onHeaders(HeadersFrame frame) {
try {
// Close the connection just after
// receiving the response headers.
client.close();
closeLatch.countDown();
} catch (IOException x) {
throw new RuntimeIOException(x);
}
}
}, 4096, 8192);
parseResponse(client, parser);
// We need to give some time to the server to receive and process the TCP FIN.
Thread.sleep(1000);
Session session = sessionRef.get();
Assert.assertTrue(session.isClosed());
Assert.assertTrue(((HTTP2Session) session).isDisconnected());
}
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class CloseTest method testClientSendsGoAwayButDoesNotCloseConnectionServerCloses.
@Test
public void testClientSendsGoAwayButDoesNotCloseConnectionServerCloses() throws Exception {
final AtomicReference<Session> sessionRef = new AtomicReference<>();
startServer(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
sessionRef.set(stream.getSession());
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
return 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));
generator.control(lease, new GoAwayFrame(1, ErrorCode.NO_ERROR.code, "OK".getBytes("UTF-8")));
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) {
output.write(BufferUtil.toArray(buffer));
}
// Don't close the connection; the server should close.
final CountDownLatch responseLatch = new CountDownLatch(1);
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onHeaders(HeadersFrame frame) {
// Even if we sent the GO_AWAY immediately after the
// HEADERS, the server is able to send us the response.
responseLatch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
// Wait for the server to close.
Thread.sleep(1000);
// Client received the TCP FIN from server.
Assert.assertEquals(-1, client.getInputStream().read());
// Server is closed.
Session session = sessionRef.get();
Assert.assertTrue(session.isClosed());
Assert.assertTrue(((HTTP2Session) session).isDisconnected());
}
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class PushedResourcesTest method testPushedResourceCancelled.
@Test
public void testPushedResourceCancelled() throws Exception {
String pushPath = "/secondary";
CountDownLatch latch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
HttpURI pushURI = new HttpURI("http://localhost:" + connector.getLocalPort() + pushPath);
MetaData.Request pushRequest = new MetaData.Request(HttpMethod.GET.asString(), pushURI, HttpVersion.HTTP_2, new HttpFields());
stream.push(new PushPromiseFrame(stream.getId(), 0, pushRequest), new Promise.Adapter<Stream>() {
@Override
public void succeeded(Stream pushStream) {
// Just send the normal response and wait for the reset.
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
}
}, new Stream.Listener.Adapter() {
@Override
public void onReset(Stream stream, ResetFrame frame) {
latch.countDown();
}
});
return null;
}
});
HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> null).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.
the class ResponseTest method testSetRFC2965Cookie.
@Test
public void testSetRFC2965Cookie() throws Exception {
Response response = _channel.getResponse();
HttpFields fields = response.getHttpFields();
response.addSetRFC2965Cookie("null", null, null, null, -1, null, false, false, -1);
assertEquals("null=", fields.get("Set-Cookie"));
fields.clear();
response.addSetRFC2965Cookie("minimal", "value", null, null, -1, null, false, false, -1);
assertEquals("minimal=value", fields.get("Set-Cookie"));
fields.clear();
//test cookies with same name, domain and path
response.addSetRFC2965Cookie("everything", "something", "domain", "path", 0, "noncomment", true, true, 0);
response.addSetRFC2965Cookie("everything", "value", "domain", "path", 0, "comment", true, true, 0);
Enumeration<String> e = fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=something;Version=1;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=noncomment", e.nextElement());
assertEquals("everything=value;Version=1;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment", e.nextElement());
assertFalse(e.hasMoreElements());
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", fields.get("Expires"));
assertFalse(e.hasMoreElements());
//test cookies with same name, different domain
fields.clear();
response.addSetRFC2965Cookie("everything", "other", "domain1", "path", 0, "blah", true, true, 0);
response.addSetRFC2965Cookie("everything", "value", "domain2", "path", 0, "comment", true, true, 0);
e = fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=other;Version=1;Path=path;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=blah", e.nextElement());
assertTrue(e.hasMoreElements());
assertEquals("everything=value;Version=1;Path=path;Domain=domain2;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment", e.nextElement());
assertFalse(e.hasMoreElements());
//test cookies with same name, same path, one with domain, one without
fields.clear();
response.addSetRFC2965Cookie("everything", "other", "domain1", "path", 0, "blah", true, true, 0);
response.addSetRFC2965Cookie("everything", "value", "", "path", 0, "comment", true, true, 0);
e = fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=other;Version=1;Path=path;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=blah", e.nextElement());
assertTrue(e.hasMoreElements());
assertEquals("everything=value;Version=1;Path=path;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment", e.nextElement());
assertFalse(e.hasMoreElements());
//test cookies with same name, different path
fields.clear();
response.addSetRFC2965Cookie("everything", "other", "domain1", "path1", 0, "blah", true, true, 0);
response.addSetRFC2965Cookie("everything", "value", "domain1", "path2", 0, "comment", true, true, 0);
e = fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=other;Version=1;Path=path1;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=blah", e.nextElement());
assertTrue(e.hasMoreElements());
assertEquals("everything=value;Version=1;Path=path2;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment", e.nextElement());
assertFalse(e.hasMoreElements());
//test cookies with same name, same domain, one with path, one without
fields.clear();
response.addSetRFC2965Cookie("everything", "other", "domain1", "path1", 0, "blah", true, true, 0);
response.addSetRFC2965Cookie("everything", "value", "domain1", "", 0, "comment", true, true, 0);
e = fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=other;Version=1;Path=path1;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=blah", e.nextElement());
assertTrue(e.hasMoreElements());
assertEquals("everything=value;Version=1;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment", e.nextElement());
assertFalse(e.hasMoreElements());
//test cookies same name only, no path, no domain
fields.clear();
response.addSetRFC2965Cookie("everything", "other", "", "", 0, "blah", true, true, 0);
response.addSetRFC2965Cookie("everything", "value", "", "", 0, "comment", true, true, 0);
e = fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=other;Version=1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=blah", e.nextElement());
assertEquals("everything=value;Version=1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly;Comment=comment", e.nextElement());
assertFalse(e.hasMoreElements());
fields.clear();
response.addSetRFC2965Cookie("ev erything", "va lue", "do main", "pa th", 1, "co mment", true, true, 1);
String setCookie = fields.get("Set-Cookie");
assertThat(setCookie, Matchers.startsWith("\"ev erything\"=\"va lue\";Version=1;Path=\"pa th\";Domain=\"do main\";Expires="));
assertThat(setCookie, Matchers.endsWith(" GMT;Max-Age=1;Secure;HttpOnly;Comment=\"co mment\""));
fields.clear();
response.addSetRFC2965Cookie("name", "value", null, null, -1, null, false, false, 0);
setCookie = fields.get("Set-Cookie");
assertEquals(-1, setCookie.indexOf("Version="));
fields.clear();
response.addSetRFC2965Cookie("name", "v a l u e", null, null, -1, null, false, false, 0);
setCookie = fields.get("Set-Cookie");
fields.clear();
response.addSetRFC2965Cookie("json", "{\"services\":[\"cwa\", \"aa\"]}", null, null, -1, null, false, false, -1);
assertEquals("json=\"{\\\"services\\\":[\\\"cwa\\\", \\\"aa\\\"]}\"", fields.get("Set-Cookie"));
fields.clear();
response.addSetRFC2965Cookie("name", "value", "domain", null, -1, null, false, false, -1);
response.addSetRFC2965Cookie("name", "other", "domain", null, -1, null, false, false, -1);
response.addSetRFC2965Cookie("name", "more", "domain", null, -1, null, false, false, -1);
e = fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertThat(e.nextElement(), Matchers.startsWith("name=value"));
assertThat(e.nextElement(), Matchers.startsWith("name=other"));
assertThat(e.nextElement(), Matchers.startsWith("name=more"));
response.addSetRFC2965Cookie("foo", "bar", "domain", null, -1, null, false, false, -1);
response.addSetRFC2965Cookie("foo", "bob", "domain", null, -1, null, false, false, -1);
assertThat(fields.get("Set-Cookie"), Matchers.startsWith("name=value"));
fields.clear();
response.addSetRFC2965Cookie("name", "value%=", null, null, -1, null, false, false, 0);
setCookie = fields.get("Set-Cookie");
assertEquals("name=value%=", setCookie);
}
Aggregations