@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 Publisher<Void> apply(HttpServerRequest request, HttpServerResponse response) { final Iterator<HttpRouteHandler> iterator = handlers.iterator(); HttpRouteHandler cursor; try { while (iterator.hasNext()) { cursor = iterator.next(); if (cursor.test(request)) { return cursor.apply(request, response); } } } catch (Throwable t) { Exceptions.throwIfFatal(t); return Mono.error(t); //500 } return response.sendNotFound(); }
@Override public Publisher<Void> apply(HttpServerRequest request, HttpServerResponse response) { final Iterator<HttpRouteHandler> iterator = handlers.iterator(); HttpRouteHandler cursor; try { while (iterator.hasNext()) { cursor = iterator.next(); if (cursor.test(request)) { return cursor.apply(request, response); } } } catch (Throwable t) { Exceptions.throwIfFatal(t); return Mono.error(t); //500 } return response.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(); }); }
/** * 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(); }); }
@Test public void httpPort() { DisposableServer blockingFacade = HttpServer.create() .port(8080) .handle((req, resp) -> resp.sendNotFound()) .wiretap(true) .bindNow(); blockingFacade.disposeNow(); assertThat(blockingFacade.address().getPort()) .isEqualTo(8080); }
@Test public void httpPortWithAddress() { DisposableServer blockingFacade = HttpServer.create() .port(8080) .host("localhost") .handle((req, resp) -> resp.sendNotFound()) .wiretap(true) .bindNow(); blockingFacade.disposeNow(); assertThat(blockingFacade.address().getPort()) .isEqualTo(8080); }
.validateHeaders(false) .initialBufferSize(10)) .handle((req, resp) -> req.receive().then(resp.sendNotFound())) .tcpConfiguration(tcp -> tcp.doOnConnection(c -> {