Search in sources :

Example 11 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 12 with HttpURI

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

the class InstrumentingProxyServlet method service.

@Override
public void service(final ServletRequest req, final ServletResponse res) throws ServletException, IOException {
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;
    if ("CONNECT".equalsIgnoreCase(request.getMethod())) {
        handleConnect(request, response);
        return;
    }
    final InputStream in = request.getInputStream();
    final Continuation continuation = ContinuationSupport.getContinuation(request);
    if (!continuation.isInitial()) {
        // Need better test that isInitial
        response.sendError(HttpServletResponse.SC_GATEWAY_TIMEOUT);
        return;
    }
    String uri = request.getRequestURI();
    if (request.getQueryString() != null) {
        uri += "?" + request.getQueryString();
    }
    final HttpURI url = proxyHttpURI(request, uri);
    logger.debug("proxy {}-->{}", uri, url);
    if (url == null) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final HttpExchange exchange = new CustomHttpExchange(continuation, response, request);
    exchange.setScheme(HttpSchemes.HTTPS.equals(request.getScheme()) ? HttpSchemes.HTTPS_BUFFER : HttpSchemes.HTTP_BUFFER);
    exchange.setMethod(request.getMethod());
    exchange.setURL(url.toString());
    exchange.setVersion(request.getProtocol());
    logger.debug("{} {} {}", request.getMethod(), url, request.getProtocol());
    // check connection header
    String connectionHdr = request.getHeader("Connection");
    if (connectionHdr != null) {
        connectionHdr = connectionHdr.toLowerCase(Locale.ENGLISH);
        if (!connectionHdr.contains("keep-alive") && !connectionHdr.contains("close")) {
            connectionHdr = null;
        }
    }
    // force host
    if (_hostHeader != null) {
        exchange.setRequestHeader("Host", _hostHeader);
    }
    // copy headers
    boolean xForwardedFor = false;
    boolean hasContent = false;
    long contentLength = -1;
    Enumeration<?> enm = request.getHeaderNames();
    while (enm.hasMoreElements()) {
        // TODO could be better than this!
        String hdr = (String) enm.nextElement();
        String lhdr = hdr.toLowerCase(Locale.ENGLISH);
        if ("transfer-encoding".equals(lhdr)) {
            if (request.getHeader("transfer-encoding").contains("chunk")) {
                hasContent = true;
            }
        }
        if (_DontProxyHeaders.contains(lhdr)) {
            continue;
        }
        if (connectionHdr != null && connectionHdr.contains(lhdr)) {
            continue;
        }
        if (_hostHeader != null && "host".equals(lhdr)) {
            continue;
        }
        if ("content-type".equals(lhdr)) {
            hasContent = true;
        } else if ("content-length".equals(lhdr)) {
            contentLength = request.getContentLength();
            exchange.setRequestHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(contentLength));
            if (contentLength > 0) {
                hasContent = true;
            }
        } else if ("x-forwarded-for".equals(lhdr)) {
            xForwardedFor = true;
        }
        Enumeration<?> vals = request.getHeaders(hdr);
        while (vals.hasMoreElements()) {
            String val = (String) vals.nextElement();
            if (val != null) {
                logger.debug("{}: {}", hdr, val);
                exchange.setRequestHeader(hdr, val);
            }
        }
    }
    // Proxy headers
    exchange.setRequestHeader("Via", "1.1 (jetty)");
    if (!xForwardedFor) {
        exchange.addRequestHeader("X-Forwarded-For", request.getRemoteAddr());
        exchange.addRequestHeader("X-Forwarded-Proto", request.getScheme());
        exchange.addRequestHeader("X-Forwarded-Host", request.getHeader("Host"));
        exchange.addRequestHeader("X-Forwarded-Server", request.getLocalName());
    }
    if (hasContent) {
        exchange.setRequestContentSource(in);
    }
    customizeExchange(exchange, request);
    /*
         * we need to set the timeout on the continuation to take into
         * account the timeout of the HttpClient and the HttpExchange
         */
    long ctimeout = (_client.getTimeout() > exchange.getTimeout()) ? _client.getTimeout() : exchange.getTimeout();
    // should fail/expire first from exchange
    if (ctimeout == 0) {
        // ideally never times out
        continuation.setTimeout(0);
    } else {
        continuation.setTimeout(ctimeout + 1000);
    }
    customizeContinuation(continuation);
    continuation.suspend(response);
    _client.send(exchange);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Continuation(org.eclipse.jetty.continuation.Continuation) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpExchange(org.eclipse.jetty.client.HttpExchange) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpURI(org.eclipse.jetty.http.HttpURI)

Example 13 with HttpURI

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

the class DeploymentContextWriterTest method shouldSetSecureSiteURLWhenSiteUrlIsConfigured.

@Test
public void shouldSetSecureSiteURLWhenSiteUrlIsConfigured() throws URISyntaxException {
    final ServerConfigService serverConfigService = mock(ServerConfigService.class);
    when(serverConfigService.hasAnyUrlConfigured()).thenReturn(true);
    when(serverConfigService.siteUrlFor("http://url/go/admin?tab=oAuth", true)).thenReturn("https://url/go/admin?tab=oAuth");
    Request request = new Request(mock(HttpChannel.class), mock(HttpInput.class));
    request.setUri(new HttpURI("/go/admin?tab=oAuth"));
    request.setServerName("url");
    DeploymentContextWriter writer = new DeploymentContextWriter() {

        @Override
        protected BaseUrlProvider getBaseUrlProvider(HttpServletRequest req) {
            return serverConfigService;
        }
    };
    writer.writeSecureSiteUrl(request);
    assertThat(request.getAttribute("secure_site"), is("https://url/go/admin?tab=oAuth"));
    assertThat(request.getAttribute("force_ssl"), is("true"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConfigService(com.thoughtworks.go.server.service.ServerConfigService) HttpInput(org.eclipse.jetty.server.HttpInput) Request(org.eclipse.jetty.server.Request) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpChannel(org.eclipse.jetty.server.HttpChannel) HttpURI(org.eclipse.jetty.http.HttpURI) Test(org.junit.Test)

Example 14 with HttpURI

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

the class DeploymentContextWriterTest method shouldSkipRedirectWhenSiteUrlIsNotConfigured.

@Test
public void shouldSkipRedirectWhenSiteUrlIsNotConfigured() throws URISyntaxException {
    final ServerConfigService serverConfigService = mock(ServerConfigService.class);
    when(serverConfigService.hasAnyUrlConfigured()).thenReturn(false);
    Request req = new Request(mock(HttpChannel.class), mock(HttpInput.class));
    req.setUri(new HttpURI("/go/admin?tab=oAuth"));
    req.setServerName("url");
    req.setServerPort(8153);
    //req.setProtocol("http");
    DeploymentContextWriter writer = new DeploymentContextWriter() {

        @Override
        protected BaseUrlProvider getBaseUrlProvider(HttpServletRequest req) {
            return serverConfigService;
        }
    };
    writer.writeSecureSiteUrl(req);
    assertThat(req.getAttribute("secure_site"), is(nullValue()));
    assertThat(req.getAttribute("force_ssl"), is(nullValue()));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConfigService(com.thoughtworks.go.server.service.ServerConfigService) HttpInput(org.eclipse.jetty.server.HttpInput) Request(org.eclipse.jetty.server.Request) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpChannel(org.eclipse.jetty.server.HttpChannel) HttpURI(org.eclipse.jetty.http.HttpURI) Test(org.junit.Test)

Example 15 with HttpURI

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

the class Jetty9RequestTest method setUp.

@Before
public void setUp() throws Exception {
    request = mock(Request.class);
    jetty9Request = new Jetty9Request(request);
    when(request.getUri()).thenReturn(new HttpURI("foo/bar/baz"));
    when(request.getRootURL()).thenReturn(new StringBuilder("http://junk/"));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpURI(org.eclipse.jetty.http.HttpURI) Before(org.junit.Before)

Aggregations

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