Simplify pack validation and merge response mixins
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package online.astradev.packs;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
@@ -15,9 +18,6 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
/** Serves only published archives, with expiring links and bounded downloads. */
|
||||
public final class PackHost implements AutoCloseable {
|
||||
private record Grant(String hash, long expires) {}
|
||||
@@ -112,9 +112,12 @@ public final class PackHost implements AutoCloseable {
|
||||
for (String parameter : query.split("&")) {
|
||||
if (parameter.startsWith("token=")) {
|
||||
try {
|
||||
String token = URLDecoder.decode(parameter.substring(6), StandardCharsets.UTF_8);
|
||||
String token =
|
||||
URLDecoder.decode(parameter.substring(6), StandardCharsets.UTF_8);
|
||||
Grant grant = grants.get(token);
|
||||
return grant != null && grant.hash().equals(hash) && grant.expires() >= System.currentTimeMillis();
|
||||
return grant != null
|
||||
&& grant.hash().equals(hash)
|
||||
&& grant.expires() >= System.currentTimeMillis();
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
@@ -123,7 +126,8 @@ public final class PackHost implements AutoCloseable {
|
||||
return false;
|
||||
}
|
||||
|
||||
private void sendFile(HttpExchange exchange, Path file, String hash) throws IOException, InterruptedException {
|
||||
private void sendFile(HttpExchange exchange, Path file, String hash)
|
||||
throws IOException, InterruptedException {
|
||||
var headers = exchange.getResponseHeaders();
|
||||
headers.set("Content-Type", "application/zip");
|
||||
headers.set("ETag", '"' + hash + '"');
|
||||
@@ -144,7 +148,9 @@ public final class PackHost implements AutoCloseable {
|
||||
int read;
|
||||
while ((read = input.read(buffer)) != -1) {
|
||||
transferred += read;
|
||||
long delay = transferred * 1_000_000_000L / bytesPerSecond - (System.nanoTime() - started);
|
||||
long delay =
|
||||
transferred * 1_000_000_000L / bytesPerSecond
|
||||
- (System.nanoTime() - started);
|
||||
if (delay > 0) {
|
||||
TimeUnit.NANOSECONDS.sleep(delay);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user