web: Set content length header

This commit is contained in:
Johannes Frohnmeyer 2022-01-01 11:55:24 +01:00
parent 9065660523
commit c7b9ee38c4
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 23 additions and 1 deletions

View File

@ -47,6 +47,7 @@ public class JfWebServer implements WebServer {
register(webPath, s -> {
HttpResponse resp = new HttpResponse(HttpStatusCode.OK);
resp.addHeader("Content-Type", Files.probeContentType(file));
resp.addHeader("Content-Length", String.valueOf(Files.size(file)));
FileInputStream fs = new FileInputStream(file.toFile());
resp.setData(fs);
return resp;
@ -63,6 +64,7 @@ public class JfWebServer implements WebServer {
return register(webPath, s -> {
HttpResponse resp = new HttpResponse(HttpStatusCode.OK);
resp.addHeader("Content-Type", contentType);
resp.addHeader("Content-Length", String.valueOf(data.length));
ByteArrayInputStream fs = new ByteArrayInputStream(data);
resp.setData(fs);
return resp;
@ -95,6 +97,7 @@ public class JfWebServer implements WebServer {
if (c[0]) {
resp = new HttpResponse(HttpStatusCode.OK);
resp.addHeader("Content-Type", Files.probeContentType(p_f[0]));
resp.addHeader("Content-Length", String.valueOf(Files.size(p_f[0])));
FileInputStream fs = new FileInputStream(p_f[0].toFile());
resp.setData(fs);
} else {

View File

@ -5,10 +5,22 @@ import io.gitlab.jfronny.libjf.web.api.WebInit;
import io.gitlab.jfronny.libjf.web.api.WebServer;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpResponse;
import io.gitlab.jfronny.libjf.web.impl.util.bluemapcore.HttpStatusCode;
import net.fabricmc.loader.api.FabricLoader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class WebTest implements WebInit {
@Override
public void register(WebServer api) {
LibJf.LOGGER.info(api.register("/test.html", request -> new HttpResponse(HttpStatusCode.OK).setData("<html><head><title>Hello, World!</title></head><body>Hello,<br>World!</body></html>")));
Path sourcePath = FabricLoader.getInstance().getModContainer("libjf-web-v0-testmod").get().getPath("test.html");
LibJf.LOGGER.info(api.register("/test/0.html", request -> new HttpResponse(HttpStatusCode.OK).setData(Files.readString(sourcePath))));
try {
LibJf.LOGGER.info(api.registerFile("/test/1.html", sourcePath, false));
LibJf.LOGGER.info(api.registerFile("/test/2.html", sourcePath, true));
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,7 @@
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
Hello,<br>World!</body>
</html>