From 02688a2f4701534714735ce85b3f81c82a70173b Mon Sep 17 00:00:00 2001 From: CodingPhoenixx Date: Mon, 15 Jun 2026 07:50:17 +0200 Subject: [PATCH] Reformat code for improved readability and consistency across classes --- .../nextusweb/server/HttpRequestHandler.java | 43 +++---- .../dev/coph/nextusweb/server/HttpServer.java | 10 +- .../nextusweb/server/auth/AuthConfig.java | 1 + .../server/ratelimit/LeakyBucketLimiter.java | 106 +++++++++--------- 4 files changed, 72 insertions(+), 88 deletions(-) diff --git a/src/main/java/dev/coph/nextusweb/server/HttpRequestHandler.java b/src/main/java/dev/coph/nextusweb/server/HttpRequestHandler.java index c31ca99..01e9b1b 100644 --- a/src/main/java/dev/coph/nextusweb/server/HttpRequestHandler.java +++ b/src/main/java/dev/coph/nextusweb/server/HttpRequestHandler.java @@ -63,8 +63,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler params = resolution instanceof Router.Resolution.Match m - ? m.pathParams() : Map.of(); + Map params = resolution instanceof Router.Resolution.Match m ? m.pathParams() : Map.of(); Request request = new Request(raw, params); request.clientIp(clientIp); @@ -346,11 +332,11 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler { try { - for (var mw : router.middlewares()) mw.accept(request, res); + for (var mw : router.middlewares()) + mw.accept(request, res); m.handler().handle(request, res); } catch (BadRequestException e) { - res.status(400).json(Map.of("error", - e.getMessage() == null ? "Bad Request" : e.getMessage())); + res.status(400).json(Map.of("error", e.getMessage() == null ? "Bad Request" : e.getMessage())); } catch (Exception e) { LOG.log(Level.ERROR, "Handler failed for " + raw.method() + " " + path, e); res.status(500).json("{\"error\":\"Internal Server Error\"}"); @@ -403,8 +389,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler 0"); + if (bytes <= 0) + throw new IllegalArgumentException("maxHttpContentLength must be > 0"); this.maxHttpContentLength = bytes; return this; } @@ -292,14 +293,11 @@ public final class HttpServer { pipeline.addLast("ssl", tlsCfg.newHandler(ch.alloc())); } if (readTimeoutSeconds > 0) { - pipeline.addLast("read-timeout", - new ReadTimeoutHandler(readTimeoutSeconds, TimeUnit.SECONDS)); + pipeline.addLast("read-timeout", new ReadTimeoutHandler(readTimeoutSeconds, TimeUnit.SECONDS)); } pipeline.addLast(new HttpServerCodec()) .addLast(new HttpObjectAggregator(maxContent)) - .addLast(new HttpRequestHandler(router, corsHandler, rateLimitGate, - auth, proxies, websocketRouter, websocketConfig, - secHeaders, tlsEnabled)); + .addLast(new HttpRequestHandler(router, corsHandler, rateLimitGate, auth, proxies, websocketRouter, websocketConfig, secHeaders, tlsEnabled)); } }) .bind(port).sync().channel().closeFuture().sync(); diff --git a/src/main/java/dev/coph/nextusweb/server/auth/AuthConfig.java b/src/main/java/dev/coph/nextusweb/server/auth/AuthConfig.java index 60f0997..8d71c73 100644 --- a/src/main/java/dev/coph/nextusweb/server/auth/AuthConfig.java +++ b/src/main/java/dev/coph/nextusweb/server/auth/AuthConfig.java @@ -37,6 +37,7 @@ public final class AuthConfig { * Optional {@code WWW-Authenticate} challenge sent with {@code 401} responses. */ private final String challenge; + private AuthConfig(Builder b) { this.globalRule = b.globalRule; this.exactPathRules = Map.copyOf(b.exactPathRules); diff --git a/src/main/java/dev/coph/nextusweb/server/ratelimit/LeakyBucketLimiter.java b/src/main/java/dev/coph/nextusweb/server/ratelimit/LeakyBucketLimiter.java index 1aa5ca1..e32308a 100644 --- a/src/main/java/dev/coph/nextusweb/server/ratelimit/LeakyBucketLimiter.java +++ b/src/main/java/dev/coph/nextusweb/server/ratelimit/LeakyBucketLimiter.java @@ -71,66 +71,66 @@ public final class LeakyBucketLimiter implements RateLimiter { * * @param state Holds the current {@code (waterLevel, lastLeakNanos)} pair as one atomic unit. */ - private record LeakyBucket(AtomicReference state) { - /** - * Creates an empty bucket. - * - * @param state the creation timestamp in nanoseconds - */ - private LeakyBucket(long state) { - this.state = new AtomicReference<>(new State(0, state)); - } + private record LeakyBucket(AtomicReference state) { + /** + * Creates an empty bucket. + * + * @param state the creation timestamp in nanoseconds + */ + private LeakyBucket(long state) { + this.state = new AtomicReference<>(new State(0, state)); + } - /** - * Returns the timestamp leakage was last accounted to, used by {@link #cleanup(long)}. - * - * @return the last-leak timestamp in nanoseconds - */ - long lastLeak() { - return state.get().lastLeakNanos(); - } + /** + * Returns the timestamp leakage was last accounted to, used by {@link #cleanup(long)}. + * + * @return the last-leak timestamp in nanoseconds + */ + long lastLeak() { + return state.get().lastLeakNanos(); + } - /** - * Applies elapsed leakage and, if there is room, adds one unit of water. The new level and - * the timestamp it was leaked to are swapped in together, so the previous race where the - * level advanced but the timestamp update was lost (drifting the leak accounting) can no - * longer occur. - * - * @param now the current time in nanoseconds - * @param capacity the bucket capacity - * @param leakIntervalNanos the nanoseconds per leaked unit - * @return an allow result with the remaining headroom, or a deny result with a retry - * hint when the bucket is full - */ - Result tryAcquire(long now, long capacity, long leakIntervalNanos) { - while (true) { - State current = state.get(); + /** + * Applies elapsed leakage and, if there is room, adds one unit of water. The new level and + * the timestamp it was leaked to are swapped in together, so the previous race where the + * level advanced but the timestamp update was lost (drifting the leak accounting) can no + * longer occur. + * + * @param now the current time in nanoseconds + * @param capacity the bucket capacity + * @param leakIntervalNanos the nanoseconds per leaked unit + * @return an allow result with the remaining headroom, or a deny result with a retry + * hint when the bucket is full + */ + Result tryAcquire(long now, long capacity, long leakIntervalNanos) { + while (true) { + State current = state.get(); - long leaked = (now - current.lastLeakNanos()) / leakIntervalNanos; - long newLevel = Math.max(0, current.waterLevel() - leaked); + long leaked = (now - current.lastLeakNanos()) / leakIntervalNanos; + long newLevel = Math.max(0, current.waterLevel() - leaked); - if (newLevel >= capacity) { - long retryMs = leakIntervalNanos / 1_000_000L; - return Result.deny(capacity, retryMs); - } + if (newLevel >= capacity) { + long retryMs = leakIntervalNanos / 1_000_000L; + return Result.deny(capacity, retryMs); + } - long newLastLeak = leaked > 0 - ? current.lastLeakNanos() + leaked * leakIntervalNanos - : current.lastLeakNanos(); + long newLastLeak = leaked > 0 + ? current.lastLeakNanos() + leaked * leakIntervalNanos + : current.lastLeakNanos(); - if (state.compareAndSet(current, new State(newLevel + 1, newLastLeak))) { - return Result.allow(capacity - newLevel - 1, capacity); - } + if (state.compareAndSet(current, new State(newLevel + 1, newLastLeak))) { + return Result.allow(capacity - newLevel - 1, capacity); } } - - /** - * Immutable snapshot of a bucket's mutable state. - * - * @param waterLevel current water level (number of units in the bucket) - * @param lastLeakNanos timestamp leakage has been applied up to, in nanoseconds - */ - private record State(long waterLevel, long lastLeakNanos) { - } } + + /** + * Immutable snapshot of a bucket's mutable state. + * + * @param waterLevel current water level (number of units in the bucket) + * @param lastLeakNanos timestamp leakage has been applied up to, in nanoseconds + */ + private record State(long waterLevel, long lastLeakNanos) { + } + } }