Search in sources :

Example 41 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class IdleTimeoutTest method testServerEnforcingIdleTimeoutWithUnrespondedStream.

@Test
public void testServerEnforcingIdleTimeoutWithUnrespondedStream() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            return null;
        }
    });
    connector.setIdleTimeout(idleTimeout);
    final CountDownLatch latch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            latch.countDown();
        }
    });
    // The request is not replied, and the server should idle timeout.
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, true);
    session.newStream(requestFrame, new Promise.Adapter<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(10 * idleTimeout);
        }
    }, new Stream.Listener.Adapter());
    Assert.assertTrue(latch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 42 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class IdleTimeoutTest method testClientEnforcingIdleTimeoutWithUnrespondedStream.

@Test
public void testClientEnforcingIdleTimeoutWithUnrespondedStream() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            return null;
        }

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    client.setIdleTimeout(idleTimeout);
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, true);
    session.newStream(requestFrame, new Promise.Adapter<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(10 * idleTimeout);
        }
    }, new Stream.Listener.Adapter());
    Assert.assertTrue(closeLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 43 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class JDK9HTTP2Client method main.

public static void main(String[] args) throws Exception {
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    client.addBean(sslContextFactory);
    client.start();
    String host = "localhost";
    int port = 8443;
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new Session.Listener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(555, TimeUnit.SECONDS);
    HttpFields requestFields = new HttpFields();
    requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
    HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            System.err.println(frame);
            if (frame.isEndStream())
                latch.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            System.err.println(frame);
            callback.succeeded();
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    latch.await(5, TimeUnit.SECONDS);
    client.stop();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session)

Example 44 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class HttpReceiverOverHTTP2 method onHeaders.

@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return;
    HttpResponse response = exchange.getResponse();
    MetaData.Response metaData = (MetaData.Response) frame.getMetaData();
    response.version(metaData.getHttpVersion()).status(metaData.getStatus()).reason(metaData.getReason());
    if (responseBegin(exchange)) {
        HttpFields headers = metaData.getFields();
        for (HttpField header : headers) {
            if (!responseHeader(exchange, header))
                return;
        }
        if (responseHeaders(exchange)) {
            int status = metaData.getStatus();
            boolean informational = HttpStatus.isInformational(status) && status != HttpStatus.SWITCHING_PROTOCOLS_101;
            if (frame.isEndStream() || informational)
                responseSuccess(exchange);
        }
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) HttpResponse(org.eclipse.jetty.client.HttpResponse)

Example 45 with MetaData

use of org.eclipse.jetty.http.MetaData in project jetty.project by eclipse.

the class HttpReceiverOverHTTP2 method onPush.

@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return null;
    HttpRequest request = exchange.getRequest();
    MetaData.Request metaData = (MetaData.Request) frame.getMetaData();
    HttpRequest pushRequest = (HttpRequest) getHttpDestination().getHttpClient().newRequest(metaData.getURIString());
    BiFunction<Request, Request, Response.CompleteListener> pushListener = request.getPushListener();
    if (pushListener != null) {
        Response.CompleteListener listener = pushListener.apply(request, pushRequest);
        if (listener != null) {
            HttpChannelOverHTTP2 pushChannel = getHttpChannel().getHttpConnection().newHttpChannel(true);
            List<Response.ResponseListener> listeners = Collections.singletonList(listener);
            HttpExchange pushExchange = new HttpExchange(getHttpDestination(), pushRequest, listeners);
            pushChannel.associate(pushExchange);
            pushChannel.setStream(stream);
            // TODO: idle timeout ?
            pushExchange.requestComplete(null);
            pushExchange.terminateRequest();
            return pushChannel.getStreamListener();
        }
    }
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
    return null;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpExchange(org.eclipse.jetty.client.HttpExchange) Request(org.eclipse.jetty.client.api.Request) HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame)

Aggregations

MetaData (org.eclipse.jetty.http.MetaData)99 HttpFields (org.eclipse.jetty.http.HttpFields)87 Test (org.junit.Test)79 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)75 CountDownLatch (java.util.concurrent.CountDownLatch)64 Stream (org.eclipse.jetty.http2.api.Stream)61 Session (org.eclipse.jetty.http2.api.Session)59 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)57 FuturePromise (org.eclipse.jetty.util.FuturePromise)51 ByteBuffer (java.nio.ByteBuffer)39 HttpServletResponse (javax.servlet.http.HttpServletResponse)36 Promise (org.eclipse.jetty.util.Promise)36 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)35 Callback (org.eclipse.jetty.util.Callback)30 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)29 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)26 IOException (java.io.IOException)24 HashMap (java.util.HashMap)24 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 ServletException (javax.servlet.ServletException)23