private static String resolveRequestUri(HttpServerRequest request) { String uri = request.uri(); for (int i = 0; i < uri.length(); i++) { char c = uri.charAt(i); if (c == '/' || c == '?' || c == '#') { break; } if (c == ':' && (i + 2 < uri.length())) { if (uri.charAt(i + 1) == '/' && uri.charAt(i + 2) == '/') { for (int j = i + 3; j < uri.length(); j++) { c = uri.charAt(j); if (c == '/' || c == '?' || c == '#') { return uri.substring(j); } } return ""; } } } return uri; }
private static String resolveRequestUri(HttpServerRequest request) { String uri = request.uri(); for (int i = 0; i < uri.length(); i++) { char c = uri.charAt(i); if (c == '/' || c == '?' || c == '#') { break; } if (c == ':' && (i + 2 < uri.length())) { if (uri.charAt(i + 1) == '/' && uri.charAt(i + 2) == '/') { for (int j = i + 3; j < uri.length(); j++) { c = uri.charAt(j); if (c == '/' || c == '?' || c == '#') { return uri.substring(j); } } return ""; } } } return uri; }
private static String resolveRequestUri(HttpServerRequest request) { String uri = request.uri(); for (int i = 0; i < uri.length(); i++) { char c = uri.charAt(i); if (c == '/' || c == '?' || c == '#') { break; } if (c == ':' && (i + 2 < uri.length())) { if (uri.charAt(i + 1) == '/' && uri.charAt(i + 2) == '/') { for (int j = i + 3; j < uri.length(); j++) { c = uri.charAt(j); if (c == '/' || c == '?' || c == '#') { return uri.substring(j); } } return ""; } } } return uri; }
private static String resolveRequestUri(HttpServerRequest request) { String uri = request.uri(); for (int i = 0; i < uri.length(); i++) { char c = uri.charAt(i); if (c == '/' || c == '?' || c == '#') { break; } if (c == ':' && (i + 2 < uri.length())) { if (uri.charAt(i + 1) == '/' && uri.charAt(i + 2) == '/') { for (int j = i + 3; j < uri.length(); j++) { c = uri.charAt(j); if (c == '/' || c == '?' || c == '#') { return uri.substring(j); } } return ""; } } } return uri; }
@Override public boolean test(HttpServerRequest key) { return (method == null || method.equals(key.method())) && key.uri() .startsWith( prefix); } }
@Override public boolean test(HttpServerRequest key) { return (method == null || method.equals(key.method())) && key.uri() .startsWith( prefix); } }
@Override public HttpServerRoutes directory(String uri, Path directory, Function<HttpServerResponse, HttpServerResponse> interceptor) { Objects.requireNonNull(directory, "directory"); return route(HttpPredicate.prefix(uri), (req, resp) -> { String prefix = URI.create(req.uri()) .getPath() .replaceFirst(uri, ""); if(prefix.charAt(0) == '/'){ prefix = prefix.substring(1); } Path p = directory.resolve(prefix); if (Files.isReadable(p)) { if (interceptor != null) { return interceptor.apply(resp) .sendFile(p); } return resp.sendFile(p); } return resp.sendNotFound(); }); }
@Override public HttpServerRoutes directory(String uri, Path directory, Function<HttpServerResponse, HttpServerResponse> interceptor) { Objects.requireNonNull(directory, "directory"); return route(HttpPredicate.prefix(uri), (req, resp) -> { String prefix = URI.create(req.uri()) .getPath() .replaceFirst(uri, ""); if(prefix.charAt(0) == '/'){ prefix = prefix.substring(1); } Path p = directory.resolve(prefix); if (Files.isReadable(p)) { if (interceptor != null) { return interceptor.apply(resp) .sendFile(p); } return resp.sendFile(p); } return resp.sendNotFound(); }); }
@Override public final boolean test(HttpServerRequest key) { return (protocol == null || protocol.equals(key.version())) && (method == null || method.equals( key.method())) && (template == null || template.matches(key.uri())); }
@Override public final boolean test(HttpServerRequest key) { return (protocol == null || protocol.equals(key.version())) && (method == null || method.equals( key.method())) && (template == null || template.matches(key.uri())); }
/** * Listen for WebSocket with the given route predicate to invoke the matching handlers * * @param condition a predicate given each inbound request * @param handler an handler to invoke for the given condition * @param protocols sub-protocol to use in WS handshake signature * * @return a new handler */ @SuppressWarnings("unchecked") default HttpServerRoutes ws(Predicate<? super HttpServerRequest> condition, BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends Publisher<Void>> handler, @Nullable String protocols, int maxFramePayloadLength) { return route(condition, (req, resp) -> { if (req.requestHeaders() .contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) { HttpServerOperations ops = (HttpServerOperations) req; return ops.withWebsocketSupport(req.uri(), protocols, maxFramePayloadLength, handler); } return resp.sendNotFound(); }); }
/** * Listen for WebSocket with the given route predicate to invoke the matching handlers * * @param condition a predicate given each inbound request * @param handler an handler to invoke for the given condition * @param protocols sub-protocol to use in WS handshake signature * * @return a new handler */ @SuppressWarnings("unchecked") default HttpServerRoutes ws(Predicate<? super HttpServerRequest> condition, BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends Publisher<Void>> handler, @Nullable String protocols, int maxFramePayloadLength) { return route(condition, (req, resp) -> { if (req.requestHeaders() .contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) { HttpServerOperations ops = (HttpServerOperations) req; return ops.withWebsocketSupport(req.uri(), protocols, maxFramePayloadLength, handler); } return resp.sendNotFound(); }); }
HttpServer.create() .host("localhost") .route(r -> r.route(req -> req.uri().startsWith("/1"), (req, res) -> res.sendString(Mono.just("OK"))) .route(req -> req.uri().startsWith("/2"), (req, res) -> res.chunkedTransfer(false) .sendString(Mono.just("OK"))) .route(req -> req.uri().startsWith("/3"), (req, res) -> { res.responseHeaders().set("Content-Length", 2); return res.sendString(Mono.just("OK")); }) .route(req -> req.uri().startsWith("/4"), (req, res) -> res.sendHeaders()) .route(req -> req.uri().startsWith("/5"), (req, res) -> res.chunkedTransfer(false) .sendHeaders()) .route(req -> req.uri().startsWith("/6"), (req, res) -> { res.responseHeaders().set("Content-Length", 2); return res.sendHeaders(); }) .route(req -> req.uri().startsWith("/7"), (req, res) -> res.send()) .route(req -> req.uri().startsWith("/8"), (req, res) -> res.chunkedTransfer(false) .send()) .route(req -> req.uri().startsWith("/9"), (req, res) -> {
private Mono<Void> handleRequest( ByteBuf content, HttpServerRequest httpRequest, HttpServerResponse httpResponse) { String qualifier = httpRequest.uri(); Builder builder = ServiceMessage.builder().qualifier(qualifier).data(content); enrichRequest(httpRequest.requestHeaders(), builder); return serviceCall .requestOne(builder.build()) .doOnNext(message -> metrics.markServiceResponse()) .switchIfEmpty( Mono.defer(() -> Mono.just(ServiceMessage.builder().qualifier(qualifier).build()))) .flatMap( response -> { enrichResponse(httpResponse, response); return Mono.defer( () -> ExceptionProcessor.isError(response) // check error ? error(httpResponse, response) : response.hasData() // check data ? ok(httpResponse, response) : noContent(httpResponse)); }); }
private Mono<Void> handleRequest( ByteBuf content, HttpServerRequest httpRequest, HttpServerResponse httpResponse) { String qualifier = httpRequest.uri(); Builder builder = ServiceMessage.builder().qualifier(qualifier).data(content); enrichRequest(httpRequest.requestHeaders(), builder); return serviceCall .requestOne(builder.build()) .doOnNext(message -> metrics.markServiceResponse()) .switchIfEmpty( Mono.defer(() -> Mono.just(ServiceMessage.builder().qualifier(qualifier).build()))) .flatMap( response -> { enrichResponse(httpResponse, response); return Mono.defer( () -> response.isError() // check error ? error(httpResponse, response) : response.hasData() // check data ? ok(httpResponse, response) : noContent(httpResponse)); }); }
@Test public void sslExchangeRelativeGet() throws CertificateException, SSLException { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()) .build(); SslContext sslClient = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); DisposableServer context = HttpServer.create() .secure(ssl -> ssl.sslContext(sslServer)) .handle((req, resp) -> resp.sendString(Flux.just("hello ", req.uri()))) .wiretap(true) .bindNow(); String responseString = createHttpClientForContextWithAddress(context) .secure(ssl -> ssl.sslContext(sslClient)) .get() .uri("/foo") .responseSingle((res, buf) -> buf.asString(CharsetUtil.UTF_8)) .block(Duration.ofMillis(200)); context.disposeNow(); assertThat(responseString).isEqualTo("hello /foo"); }
@Test public void sslExchangeAbsoluteGet() throws CertificateException, SSLException { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); SslContext sslClient = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); DisposableServer context = HttpServer.create() .secure(ssl -> ssl.sslContext(sslServer)) .handle((req, resp) -> resp.sendString(Flux.just("hello ", req.uri()))) .wiretap(true) .bindNow(); String responseString = createHttpClientForContextWithAddress(context) .secure(ssl -> ssl.sslContext(sslClient)) .get() .uri("/foo") .responseSingle((res, buf) -> buf.asString(CharsetUtil.UTF_8)) .block(); context.disposeNow(); assertThat(responseString).isEqualTo("hello /foo"); }
.port(0) .handle((req, res) -> { if (req.uri().contains("/login") && req.method().equals(HttpMethod.POST) && counter.getAndDecrement() > 0) {