mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
add response mime type
This commit is contained in:
parent
554d39807b
commit
347bacdd85
1 changed files with 48 additions and 16 deletions
|
@ -1,5 +1,6 @@
|
||||||
package me.kavin.piped;
|
package me.kavin.piped;
|
||||||
|
|
||||||
|
import io.netty.util.AsciiString;
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.extractor.localization.Localization;
|
import org.schabi.newpipe.extractor.localization.Localization;
|
||||||
|
@ -23,6 +24,7 @@ import reactor.netty.http.server.HttpServerResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import static io.netty.handler.codec.http.HttpHeaderNames.*;
|
import static io.netty.handler.codec.http.HttpHeaderNames.*;
|
||||||
|
import static io.netty.handler.codec.http.HttpHeaderValues.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
@ -38,7 +40,7 @@ public class Main {
|
||||||
QueryStringDecoder query = new QueryStringDecoder(req.uri());
|
QueryStringDecoder query = new QueryStringDecoder(req.uri());
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
||||||
|
@ -64,7 +66,7 @@ public class Main {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return writeResponse(res, "ok", 200, "private", start);
|
return writeResponse(res, "ok", TEXT_PLAIN, 200, "private", start);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
||||||
|
@ -222,24 +224,54 @@ public class Main {
|
||||||
Thread.sleep(Long.MAX_VALUE);
|
Thread.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) {
|
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);
|
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) {
|
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) {
|
||||||
return res.compression(true)
|
return writeResponse(res, resp, APPLICATION_JSON, code, cache, time);
|
||||||
.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, 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) {
|
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, int code, String cache) {
|
||||||
return res.compression(true)
|
return writeResponse(res, resp, APPLICATION_JSON, code, cache);
|
||||||
.status(code)
|
|
||||||
.addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
|
|
||||||
.addHeader(CACHE_CONTROL, cache)
|
|
||||||
.send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue