feat: bound async HTTP attempts and track remaining security work
This commit is contained in:
1 parent
d994ffb9a5
commit
926aa64ba3
13 files changed
+479
-8
No files matched your search
+114
@@ -0,0 +1,114 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpHeaders;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Flow;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/** One bounded HTTP attempt. No credentials, route selection, protocol mapping, retry, risk or billing policy. */
|
||||
public final class BoundedHttpTransport {
|
||||
private static final int ERROR_BODY_LIMIT = 64 * 1024;
|
||||
private final HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10))
|
||||
.followRedirects(HttpClient.Redirect.NEVER).build();
|
||||
|
||||
public record Response(int statusCode, HttpHeaders headers, byte[] body) {
|
||||
@Override public String toString() { return "TransportResponse[status=" + statusCode + ", bytes=" + body.length + "]"; }
|
||||
}
|
||||
public enum Reason { TIMEOUT, INTERRUPTED, BODY_TOO_LARGE, IO }
|
||||
public static final class Failure extends IOException {
|
||||
private final Reason reason;
|
||||
private final Integer observedStatus;
|
||||
private Failure(Reason reason, Integer status) {
|
||||
super("Bounded HTTP attempt failed: " + reason);
|
||||
this.reason = reason; this.observedStatus = status;
|
||||
}
|
||||
public Reason reason() { return reason; }
|
||||
public Integer observedStatus() { return observedStatus; }
|
||||
}
|
||||
|
||||
public Response send(HttpRequest request, int maxResponseBytes) throws Failure {
|
||||
if (maxResponseBytes < 1 || maxResponseBytes > 32 * 1024 * 1024) throw new IllegalArgumentException("Invalid response limit");
|
||||
Duration timeout = request.timeout().orElseThrow(() -> new IllegalArgumentException("An explicit total HTTP deadline is required"));
|
||||
var status = new AtomicInteger();
|
||||
var subscriber = new AtomicReference<LimitedBody>();
|
||||
CompletableFuture<HttpResponse<byte[]>> attempt = client.sendAsync(request, info -> {
|
||||
status.set(info.statusCode());
|
||||
int limit = info.statusCode() >= 200 && info.statusCode() < 300 ? maxResponseBytes : Math.min(maxResponseBytes, ERROR_BODY_LIMIT);
|
||||
var body = new LimitedBody(limit, info.statusCode(), info.headers().firstValueAsLong("Content-Length").orElse(-1));
|
||||
subscriber.set(body);
|
||||
return body;
|
||||
});
|
||||
try {
|
||||
// Unlike a header-only timeout, this also bounds a server that drips or never finishes the body.
|
||||
HttpResponse<byte[]> response = attempt.get(Math.max(1, timeout.toMillis()), TimeUnit.MILLISECONDS);
|
||||
return new Response(response.statusCode(), response.headers(), response.body());
|
||||
} catch (TimeoutException exception) {
|
||||
throw cancel(attempt, subscriber.get(), Reason.TIMEOUT, status.get());
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw cancel(attempt, subscriber.get(), Reason.INTERRUPTED, status.get());
|
||||
} catch (ExecutionException exception) {
|
||||
Throwable cause = exception.getCause();
|
||||
while (cause instanceof java.util.concurrent.CompletionException && cause.getCause() != null) cause = cause.getCause();
|
||||
if (cause instanceof Failure failure) throw failure;
|
||||
throw cancel(attempt, subscriber.get(), cause instanceof java.net.http.HttpTimeoutException ? Reason.TIMEOUT : Reason.IO, status.get());
|
||||
}
|
||||
}
|
||||
|
||||
private Failure cancel(CompletableFuture<?> attempt, LimitedBody body, Reason reason, int status) {
|
||||
Failure failure = new Failure(reason, status == 0 ? null : status);
|
||||
if (body != null) body.cancel(failure);
|
||||
attempt.cancel(true);
|
||||
return failure;
|
||||
}
|
||||
|
||||
private static final class LimitedBody implements HttpResponse.BodySubscriber<byte[]> {
|
||||
private final CompletableFuture<byte[]> completed = new CompletableFuture<>();
|
||||
private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
private final int limit, status;
|
||||
private final long declaredLength;
|
||||
private Flow.Subscription subscription;
|
||||
private int received;
|
||||
|
||||
LimitedBody(int limit, int status, long declaredLength) { this.limit = limit; this.status = status; this.declaredLength = declaredLength; }
|
||||
@Override public CompletionStage<byte[]> getBody() { return completed; }
|
||||
@Override public void onSubscribe(Flow.Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
if (completed.isDone()) { subscription.cancel(); return; }
|
||||
if (declaredLength > limit) { cancel(new Failure(Reason.BODY_TOO_LARGE, status)); return; }
|
||||
subscription.request(1);
|
||||
}
|
||||
@Override public void onNext(List<ByteBuffer> buffers) {
|
||||
if (completed.isDone()) { if (subscription != null) subscription.cancel(); return; }
|
||||
long chunkBytes = 0;
|
||||
for (ByteBuffer buffer : buffers) chunkBytes += buffer.remaining();
|
||||
if (chunkBytes > limit - received) { cancel(new Failure(Reason.BODY_TOO_LARGE, status)); return; }
|
||||
received += (int) chunkBytes;
|
||||
byte[] part = new byte[8192];
|
||||
for (ByteBuffer buffer : buffers) while (buffer.hasRemaining()) {
|
||||
int count = Math.min(part.length, buffer.remaining());
|
||||
buffer.get(part, 0, count); bytes.write(part, 0, count);
|
||||
}
|
||||
subscription.request(1);
|
||||
}
|
||||
@Override public void onError(Throwable error) { completed.completeExceptionally(new Failure(Reason.IO, status)); }
|
||||
@Override public void onComplete() { completed.complete(bytes.toByteArray()); }
|
||||
void cancel(Failure failure) {
|
||||
completed.completeExceptionally(failure);
|
||||
if (subscription != null) subscription.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-7
@@ -7,11 +7,10 @@ import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
|
||||
import com.moyi.controlplane.provider.transport.ProviderRequestException;
|
||||
import com.moyi.controlplane.provider.transport.BoundedHttpTransport;
|
||||
import java.net.URI;
|
||||
import java.net.InetAddress;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -42,7 +41,7 @@ public class AsyncProviderClient {
|
||||
/** Keep rotation state across submit/poll calls; rebuilding a Provider on
|
||||
* every request otherwise selected the first upstream key forever. */
|
||||
private final ConcurrentMap<String, AtomicInteger> keyCursors = new ConcurrentHashMap<>();
|
||||
private final HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
||||
private final BoundedHttpTransport transport = new BoundedHttpTransport();
|
||||
|
||||
public AsyncProviderClient(JdbcTemplate jdbc, SecretCipher cipher, ObjectMapper mapper, Environment environment) {
|
||||
this.jdbc = jdbc; this.cipher = cipher; this.mapper = mapper; this.environment = environment;
|
||||
@@ -321,7 +320,7 @@ public class AsyncProviderClient {
|
||||
.header("Authorization", "Bearer " + key).header("Content-Type", "application/json");
|
||||
HttpRequest request = builder.POST(HttpRequest.BodyPublishers.ofString(write(body))).build();
|
||||
sending = true;
|
||||
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||
var response = transport.send(request, 8 * 1024 * 1024);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
|
||||
byte[] bytes = response.body();
|
||||
if (bytes.length == 0 || bytes.length > 8 * 1024 * 1024) throw new ApiException("UPSTREAM_MEDIA_TOO_LARGE", message + "(返回音频超过 8 MiB 或为空)", 502);
|
||||
@@ -361,7 +360,7 @@ public class AsyncProviderClient {
|
||||
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(out.toByteArray())).build();
|
||||
sending = true;
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
var response = transport.send(request, 8 * 1024 * 1024);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
|
||||
Map<String,Object> parsed = mapper.readValue(response.body(), new TypeReference<>() {});
|
||||
Object code = parsed.get("code");
|
||||
@@ -379,6 +378,10 @@ public class AsyncProviderClient {
|
||||
private ProviderRequestException requestFailure(Exception exception, boolean sending, String message) {
|
||||
if (exception instanceof InterruptedException) Thread.currentThread().interrupt();
|
||||
if (exception instanceof ProviderRequestException failure) return failure;
|
||||
if (exception instanceof BoundedHttpTransport.Failure failure && failure.observedStatus() != null
|
||||
&& (failure.observedStatus() < 200 || failure.observedStatus() >= 300)) {
|
||||
return ProviderRequestException.http(failure.observedStatus(), message);
|
||||
}
|
||||
if (sending) return ProviderRequestException.unknown("UPSTREAM_PROVIDER_ERROR", message);
|
||||
if (exception instanceof ApiException error) return ProviderRequestException.notSent(error.getCode(), error.getMessage(), error.getStatus());
|
||||
return ProviderRequestException.notSent("UPSTREAM_REQUEST_NOT_SENT", "请求准备失败,尚未提交上游生成", 502);
|
||||
@@ -409,7 +412,12 @@ public class AsyncProviderClient {
|
||||
}
|
||||
}
|
||||
HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(15)).GET().build();
|
||||
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||
BoundedHttpTransport.Response response;
|
||||
try { response = transport.send(request, 8 * 1024 * 1024); }
|
||||
catch (BoundedHttpTransport.Failure failure) {
|
||||
if (failure.reason() == BoundedHttpTransport.Reason.BODY_TOO_LARGE) throw new ApiException("MEDIA_TOO_LARGE", "媒体输入超过 8 MiB", 400);
|
||||
throw new ApiException("MEDIA_FETCH_FAILED", "无法在期限内读取媒体输入", 400);
|
||||
}
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new ApiException("MEDIA_FETCH_FAILED", "无法读取媒体输入(HTTP " + response.statusCode() + ")", 400);
|
||||
}
|
||||
@@ -447,7 +455,7 @@ public class AsyncProviderClient {
|
||||
else builder.GET();
|
||||
HttpRequest request = builder.build();
|
||||
sending = true;
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
var response = transport.send(request, 8 * 1024 * 1024);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
|
||||
Map<String,Object> parsed = mapper.readValue(response.body(), new TypeReference<>() {});
|
||||
Object code = parsed.get("code");
|
||||
|
||||
Reference in new issue
Block a user