Search in sources :

Example 11 with HttpFields

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);
    }
}
Also used : HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) IStream(org.eclipse.jetty.http2.IStream)

Example 12 with HttpFields

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());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 13 with HttpFields

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());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 14 with HttpFields

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));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpURI(org.eclipse.jetty.http.HttpURI) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 15 with HttpFields

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);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpFields(org.eclipse.jetty.http.HttpFields) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)185 Test (org.junit.Test)143 MetaData (org.eclipse.jetty.http.MetaData)118 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)106 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)94 Session (org.eclipse.jetty.http2.api.Session)90 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)80 FuturePromise (org.eclipse.jetty.util.FuturePromise)70 HttpServletResponse (javax.servlet.http.HttpServletResponse)62 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)53 ByteBuffer (java.nio.ByteBuffer)52 Promise (org.eclipse.jetty.util.Promise)49 HttpServletRequest (javax.servlet.http.HttpServletRequest)48 IOException (java.io.IOException)43 ServletException (javax.servlet.ServletException)40 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32