static String absoluteURI(String serverOrigin, HttpServerRequest req) throws URISyntaxException { String absoluteURI; URI uri = new URI(req.uri()); String scheme = uri.getScheme(); if (scheme != null && (scheme.equals("http") || scheme.equals("https"))) { absoluteURI = uri.toString(); } else { String host = req.host(); if (host != null) { absoluteURI = req.scheme() + "://" + host + uri; } else { // Fall back to the server origin absoluteURI = serverOrigin + uri; } } return absoluteURI; }
@Test public void testHostHeaderOverridePossible() { server.requestHandler(req -> { assertEquals("localhost:4444", req.host()); req.response().end(); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete()); req.setHost("localhost:4444"); req.end(); })); await(); }
@Override public String host() { return delegate.host(); }
@Test public void testOverrideAuthority() throws Exception { server.requestHandler(req -> { assertEquals("localhost:4444", req.host()); req.response().end(); }); startServer(); client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> { testComplete(); }) .setHost("localhost:4444") .exceptionHandler(this::fail) .end(); await(); }
MultiMap headers = req.headers(); String upgrade = headers.get("upgrade"); assertEquals(DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT, req.host()); if ("h2c".equals(upgrade)) { req.response().setStatusCode(400).end();
assertEquals(HttpMethod.GET, req.method()); assertEquals("/somepath", req.path()); assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.host()); assertEquals("foo_request_value", req.getHeader("Foo_request")); assertEquals("bar_request_value", req.getHeader("bar_request"));
static String absoluteURI(String serverOrigin, HttpServerRequest req) throws URISyntaxException { String absoluteURI; URI uri = new URI(req.uri()); String scheme = uri.getScheme(); if (scheme != null && (scheme.equals("http") || scheme.equals("https"))) { absoluteURI = uri.toString(); } else { String host = req.host(); if (host != null) { absoluteURI = req.scheme() + "://" + host + uri; } else { // Fall back to the server origin absoluteURI = serverOrigin + uri; } } return absoluteURI; }
@Override public void handle(RoutingContext ctx) { String host = ctx.request().host(); if (host == null) { ctx.next(); } else { boolean match = false; for (String h : host.split(":")) { if (regex.matcher(h).matches()) { match = true; break; } } if (match) { handler.handle(ctx); } else { ctx.next(); } } } }
public static ResteasyUriInfo extractUriInfo(HttpServerRequest req, String contextPath) { String host = req.host(); if (host == null) { host = "unknown"; } String uri = req.absoluteURI(); String protocol = req.scheme(); String uriString; // If we appear to have an absolute URL, don't try to recreate it from the host and request line. if (uri.startsWith(protocol + "://")) { uriString = uri; } else { uriString = protocol + "://" + host + uri; } URI absoluteURI = URI.create(uriString); return new ResteasyUriInfo(uriString, absoluteURI.getRawQuery(), contextPath); }
/** * @return the request host. For HTTP2 it returns the pseudo header otherwise it returns the header */ public String host() { String ret = delegate.host(); return ret; }
/** * @return the request host. For HTTP2 it returns the pseudo header otherwise it returns the header */ public String host() { String ret = delegate.host(); return ret; }
@Test public void testHostHeaderOverridePossible() { server.requestHandler(req -> { assertEquals("localhost:4444", req.host()); req.response().end(); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete()); req.setHost("localhost:4444"); req.end(); })); await(); }
@Override public String host() { return delegate.host(); }
when(request.uri()).thenReturn("http://localhost/path"); when(request.absoluteURI()).thenReturn("http://localhost/path"); when(request.host()).thenReturn("localhost"); when(request.path()).thenReturn("/path"); when(request.response()).thenReturn(response);
VertxHttpServerRequest(HttpServerRequest httpServerRequest) { this.httpServerRequest = httpServerRequest; this.timestamp = System.currentTimeMillis(); this.id = UUID.toString(UUID.random()); this.metrics = Metrics.on(timestamp).build(); this.metrics.setRequestId(id()); this.metrics.setHttpMethod(method()); this.metrics.setLocalAddress(localAddress()); this.metrics.setRemoteAddress(remoteAddress()); this.metrics.setHost(httpServerRequest.host()); this.metrics.setUri(uri()); }
@Test public void testOverrideAuthority() throws Exception { server.requestHandler(req -> { assertEquals("localhost:4444", req.host()); req.response().end(); }); startServer(); client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> { testComplete(); }) .setHost("localhost:4444") .exceptionHandler(this::fail) .end(); await(); }
MultiMap headers = req.headers(); String upgrade = headers.get("upgrade"); assertEquals(DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT, req.host()); if ("h2c".equals(upgrade)) { req.response().setStatusCode(400).end();
assertEquals(HttpMethod.GET, req.method()); assertEquals("/somepath", req.path()); assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.host()); assertEquals("foo_request_value", req.getHeader("Foo_request")); assertEquals("bar_request_value", req.getHeader("bar_request"));
@Override public void setLocation(String basePath) { String protocol = "http://"; if (rc.request().isSSL()) { protocol = "https://"; } String hostAndPort = rc.request().host(); rc.response().putHeader(HttpHeaders.LOCATION, protocol + hostAndPort + basePath); }
@Test public void testVirtualHost() throws Exception { server.requestHandler(req -> { assertEquals("another-host:8080", req.host()); req.response().end(); }); startServer(); HttpRequest<Buffer> req = client.get("/test").virtualHost("another-host"); req.send(onSuccess(resp -> testComplete())); await(); }