From e8c7b44e8ec8a15297af85b131aa29cad3979b6b Mon Sep 17 00:00:00 2001 From: bopol Date: Fri, 5 Feb 2021 18:22:08 +0100 Subject: [PATCH 1/4] Use HttpHeaderNames for HTTP headers --- src/main/java/me/kavin/piped/Main.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 1cb0545..a5cb610 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -20,6 +20,10 @@ import reactor.netty.NettyOutbound; import reactor.netty.http.server.HttpServer; import reactor.netty.http.server.HttpServerResponse; +import java.nio.charset.StandardCharsets; + +import static io.netty.handler.codec.http.HttpHeaderNames.*; + public class Main { public static void main(String[] args) throws Exception { @@ -219,20 +223,17 @@ public class Main { } public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) { - return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache) - .addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0) - .send(ByteBufFlux.fromString(Flux.just(resp), java.nio.charset.StandardCharsets.UTF_8, - ByteBufAllocator.DEFAULT)); + return writeResponse(res, resp.getBytes(StandardCharsets.UTF_8), code, cache, time); } public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) { - return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache) + return res.compression(true).addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*").addHeader(CACHE_CONTROL, cache) .addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0) .sendByteArray(Flux.just(resp)); } public static NettyOutbound writeResponse(HttpServerResponse res, Flux resp, int code, String cache) { - return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache) + return res.compression(true).addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*").addHeader(CACHE_CONTROL, cache) .send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT)); } } From 554d39807bbddeee003a795996b2556f078c5610 Mon Sep 17 00:00:00 2001 From: bopol Date: Fri, 5 Feb 2021 18:53:05 +0100 Subject: [PATCH 2/4] add status code to server response --- src/main/java/me/kavin/piped/Main.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index a5cb610..a1e8563 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -227,13 +227,19 @@ public class Main { } public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) { - return res.compression(true).addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*").addHeader(CACHE_CONTROL, cache) + return res.compression(true) + .status(code) + .addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*") + .addHeader(CACHE_CONTROL, cache) .addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0) .sendByteArray(Flux.just(resp)); } public static NettyOutbound writeResponse(HttpServerResponse res, Flux resp, int code, String cache) { - return res.compression(true).addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*").addHeader(CACHE_CONTROL, cache) + return res.compression(true) + .status(code) + .addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*") + .addHeader(CACHE_CONTROL, cache) .send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT)); } } From 347bacdd857032496e0292a4ad6159e2cf62d2c9 Mon Sep 17 00:00:00 2001 From: bopol Date: Fri, 5 Feb 2021 19:28:02 +0100 Subject: [PATCH 3/4] add response mime type --- src/main/java/me/kavin/piped/Main.java | 64 +++++++++++++++++++------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index a1e8563..64738d3 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -1,5 +1,6 @@ package me.kavin.piped; +import io.netty.util.AsciiString; import org.apache.commons.lang3.exception.ExceptionUtils; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.Localization; @@ -23,6 +24,7 @@ import reactor.netty.http.server.HttpServerResponse; import java.nio.charset.StandardCharsets; import static io.netty.handler.codec.http.HttpHeaderNames.*; +import static io.netty.handler.codec.http.HttpHeaderValues.*; public class Main { @@ -38,7 +40,7 @@ public class Main { QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { - return writeResponse(res, query.parameters().get("hub.challenge").get(0), 200, "private", start); + return writeResponse(res, query.parameters().get("hub.challenge").get(0), APPLICATION_XML, 200, "private", start); } catch (Exception e) { e.printStackTrace(); return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); @@ -64,7 +66,7 @@ public class Main { e.printStackTrace(); } }); - return writeResponse(res, "ok", 200, "private", start); + return writeResponse(res, "ok", TEXT_PLAIN, 200, "private", start); } catch (Exception e) { e.printStackTrace(); return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start); @@ -222,24 +224,54 @@ public class Main { Thread.sleep(Long.MAX_VALUE); } - public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) { - return writeResponse(res, resp.getBytes(StandardCharsets.UTF_8), code, cache, time); - } + public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) { + return writeResponse(res, resp, APPLICATION_JSON, code, cache, time); + } + + public static NettyOutbound writeResponse(HttpServerResponse res, String resp, AsciiString mimeType, int code, String cache, long time) { + return writeResponse(res, resp, mimeType.toString(), code, cache, time); + } + + public static NettyOutbound writeResponse(HttpServerResponse res, String resp, String mimeType, int code, String cache, long time) { + return writeResponse(res, resp.getBytes(StandardCharsets.UTF_8), mimeType, code, cache, time); + } + + public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) { - return res.compression(true) - .status(code) - .addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*") - .addHeader(CACHE_CONTROL, cache) - .addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0) - .sendByteArray(Flux.just(resp)); + return writeResponse(res, resp, APPLICATION_JSON, code, cache, time); } + public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, AsciiString mimeType, int code, String cache, long time) { + return writeResponse(res, resp, mimeType.toString(), code, cache, time); + } + + public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, String mimeType, int code, String cache, long time) { + return res.compression(true) + .status(code) + .addHeader(CONTENT_TYPE, mimeType) + .addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*") + .addHeader(CACHE_CONTROL, cache) + .addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0) + .sendByteArray(Flux.just(resp)); + } + + + public static NettyOutbound writeResponse(HttpServerResponse res, Flux resp, int code, String cache) { - return res.compression(true) - .status(code) - .addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*") - .addHeader(CACHE_CONTROL, cache) - .send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT)); + return writeResponse(res, resp, APPLICATION_JSON, code, cache); } + + public static NettyOutbound writeResponse(HttpServerResponse res, Flux resp, AsciiString mimeType, int code, String cache) { + return writeResponse(res, resp, mimeType.toString(), code, cache); + } + + public static NettyOutbound writeResponse(HttpServerResponse res, Flux resp, String mimeType, int code, String cache) { + return res.compression(true) + .status(code) + .addHeader(CONTENT_TYPE, mimeType) + .addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*") + .addHeader(CACHE_CONTROL, cache) + .send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT)); + } } From 7e5256134b383db3a2d61853a864ff843d8d68f1 Mon Sep 17 00:00:00 2001 From: bopol Date: Fri, 5 Feb 2021 20:33:21 +0100 Subject: [PATCH 4/4] Change pubsub mimetype --- src/main/java/me/kavin/piped/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 64738d3..46f63b7 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -40,7 +40,8 @@ public class Main { QueryStringDecoder query = new QueryStringDecoder(req.uri()); try { - return writeResponse(res, query.parameters().get("hub.challenge").get(0), APPLICATION_XML, 200, "private", start); + return writeResponse(res, query.parameters().get("hub.challenge").get(0), TEXT_PLAIN, 200, "private", start); + } catch (Exception e) { e.printStackTrace(); return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);