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));
}
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);
}
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"));
}
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()));
}
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/"));
}
Aggregations