Search in sources :

Example 1 with HttpURI

use of org.eclipse.jetty.http.HttpURI 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 2 with HttpURI

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

the class ProxyProtocolTest method test_PROXY_GET_v1.

@Test
public void test_PROXY_GET_v1() throws Exception {
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                Assert.assertEquals("1.2.3.4", request.getRemoteAddr());
                Assert.assertEquals(1111, request.getRemotePort());
                Assert.assertEquals("5.6.7.8", request.getLocalAddr());
                Assert.assertEquals(2222, request.getLocalPort());
            } catch (Throwable th) {
                th.printStackTrace();
                response.setStatus(500);
            }
            baseRequest.setHandled(true);
        }
    });
    String request1 = "PROXY TCP4 1.2.3.4 5.6.7.8 1111 2222\r\n";
    SocketChannel channel = SocketChannel.open();
    channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
    channel.write(ByteBuffer.wrap(request1.getBytes(StandardCharsets.UTF_8)));
    FuturePromise<Session> promise = new FuturePromise<>();
    client.accept(null, channel, new Session.Listener.Adapter(), promise);
    Session session = promise.get(5, TimeUnit.SECONDS);
    HttpFields fields = new HttpFields();
    String uri = "http://localhost:" + connector.getLocalPort() + "/";
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 3 with HttpURI

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

the class Client method main.

public static void main(String[] args) throws Exception {
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory();
    client.addBean(sslContextFactory);
    client.start();
    String host = "webtide.com";
    int port = 443;
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(5, 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);
    final Phaser phaser = new Phaser(2);
    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())
                phaser.arrive();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            System.err.println(frame);
            callback.succeeded();
            if (frame.isEndStream())
                phaser.arrive();
        }

        @Override
        public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            System.err.println(frame);
            phaser.register();
            return this;
        }
    });
    phaser.awaitAdvanceInterruptibly(phaser.arrive(), 5, TimeUnit.SECONDS);
    client.stop();
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Phaser(java.util.concurrent.Phaser) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HttpURI(org.eclipse.jetty.http.HttpURI) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session)

Example 4 with HttpURI

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

the class AbstractDoSFilterTest method startServer.

public void startServer(Class<? extends Filter> filter) throws Exception {
    _tester = new ServletTester("/ctx");
    HttpURI uri = new HttpURI(_tester.createConnector(true));
    _host = uri.getHost();
    _port = uri.getPort();
    _tester.getContext().addServlet(TestServlet.class, "/*");
    FilterHolder dosFilter = _tester.getContext().addFilter(filter, "/dos/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    dosFilter.setInitParameter("maxRequestsPerSec", "4");
    dosFilter.setInitParameter("delayMs", "200");
    dosFilter.setInitParameter("throttledRequests", "1");
    dosFilter.setInitParameter("waitMs", "10");
    dosFilter.setInitParameter("throttleMs", "4000");
    dosFilter.setInitParameter("remotePort", "false");
    dosFilter.setInitParameter("insertHeaders", "true");
    FilterHolder timeoutFilter = _tester.getContext().addFilter(filter, "/timeout/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    timeoutFilter.setInitParameter("maxRequestsPerSec", "4");
    timeoutFilter.setInitParameter("delayMs", "200");
    timeoutFilter.setInitParameter("throttledRequests", "1");
    timeoutFilter.setInitParameter("waitMs", "10");
    timeoutFilter.setInitParameter("throttleMs", "4000");
    timeoutFilter.setInitParameter("remotePort", "false");
    timeoutFilter.setInitParameter("insertHeaders", "true");
    timeoutFilter.setInitParameter("maxRequestMs", _requestMaxTime + "");
    _tester.start();
}
Also used : ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) HttpURI(org.eclipse.jetty.http.HttpURI)

Example 5 with HttpURI

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

the class Dispatcher method forward.

protected void forward(ServletRequest request, ServletResponse response, DispatcherType dispatch) throws ServletException, IOException {
    Request baseRequest = Request.getBaseRequest(request);
    Response base_response = baseRequest.getResponse();
    base_response.resetForForward();
    if (!(request instanceof HttpServletRequest))
        request = new ServletRequestHttpWrapper(request);
    if (!(response instanceof HttpServletResponse))
        response = new ServletResponseHttpWrapper(response);
    final HttpURI old_uri = baseRequest.getHttpURI();
    final String old_context_path = baseRequest.getContextPath();
    final String old_servlet_path = baseRequest.getServletPath();
    final String old_path_info = baseRequest.getPathInfo();
    final MultiMap<String> old_query_params = baseRequest.getQueryParameters();
    final Attributes old_attr = baseRequest.getAttributes();
    final DispatcherType old_type = baseRequest.getDispatcherType();
    try {
        baseRequest.setDispatcherType(dispatch);
        if (_named != null) {
            _contextHandler.handle(_named, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
        } else {
            ForwardAttributes attr = new ForwardAttributes(old_attr);
            //for queryString is allowed to be null, but cannot be null for the other values.
            if (old_attr.getAttribute(FORWARD_REQUEST_URI) != null) {
                attr._pathInfo = (String) old_attr.getAttribute(FORWARD_PATH_INFO);
                attr._query = (String) old_attr.getAttribute(FORWARD_QUERY_STRING);
                attr._requestURI = (String) old_attr.getAttribute(FORWARD_REQUEST_URI);
                attr._contextPath = (String) old_attr.getAttribute(FORWARD_CONTEXT_PATH);
                attr._servletPath = (String) old_attr.getAttribute(FORWARD_SERVLET_PATH);
            } else {
                attr._pathInfo = old_path_info;
                attr._query = old_uri.getQuery();
                attr._requestURI = old_uri.getPath();
                attr._contextPath = old_context_path;
                attr._servletPath = old_servlet_path;
            }
            HttpURI uri = new HttpURI(old_uri.getScheme(), old_uri.getHost(), old_uri.getPort(), _uri.getPath(), _uri.getParam(), _uri.getQuery(), _uri.getFragment());
            baseRequest.setHttpURI(uri);
            baseRequest.setContextPath(_contextHandler.getContextPath());
            baseRequest.setServletPath(null);
            baseRequest.setPathInfo(_pathInContext);
            if (_uri.getQuery() != null || old_uri.getQuery() != null)
                baseRequest.mergeQueryParameters(old_uri.getQuery(), _uri.getQuery(), true);
            baseRequest.setAttributes(attr);
            _contextHandler.handle(_pathInContext, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
            if (!baseRequest.getHttpChannelState().isAsync())
                commitResponse(response, baseRequest);
        }
    } finally {
        baseRequest.setHttpURI(old_uri);
        baseRequest.setContextPath(old_context_path);
        baseRequest.setServletPath(old_servlet_path);
        baseRequest.setPathInfo(old_path_info);
        baseRequest.setQueryParameters(old_query_params);
        baseRequest.resetParameters();
        baseRequest.setAttributes(old_attr);
        baseRequest.setDispatcherType(old_type);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Attributes(org.eclipse.jetty.util.Attributes) HttpServletResponse(javax.servlet.http.HttpServletResponse) DispatcherType(javax.servlet.DispatcherType) HttpURI(org.eclipse.jetty.http.HttpURI)

Aggregations

HttpURI (org.eclipse.jetty.http.HttpURI)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 MetaData (org.eclipse.jetty.http.MetaData)10 Request (org.eclipse.jetty.server.Request)10 HttpFields (org.eclipse.jetty.http.HttpFields)9 Stream (org.eclipse.jetty.http2.api.Stream)8 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Test (org.junit.Test)7 InetSocketAddress (java.net.InetSocketAddress)6 Session (org.eclipse.jetty.http2.api.Session)6 FuturePromise (org.eclipse.jetty.util.FuturePromise)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Promise (org.eclipse.jetty.util.Promise)5 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)4 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)4 ServerConfigService (com.thoughtworks.go.server.service.ServerConfigService)3 HttpChannel (org.eclipse.jetty.server.HttpChannel)3 HttpInput (org.eclipse.jetty.server.HttpInput)3 IOException (java.io.IOException)2