add response mime type

This commit is contained in:
bopol 2021-02-05 19:28:02 +01:00
parent 554d39807b
commit 347bacdd85

View file

@ -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<String> 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<String> resp, AsciiString mimeType, int code, String cache) {
return writeResponse(res, resp, mimeType.toString(), code, cache);
}
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> 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));
}
}