Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
Platform 3.28

* HTTP Server

- We now disallow TLS renegotiation.

- We have added HTTPServerModule.enableVirtualThreads(), which enables
Jetty's support for Java 21 virtual threads.

- We now synthesize the :authority pseudoheader on HTTP/2.

- We now synthesize the Host header on HTTP/1.0 and HTTP/2.

- Remote errors, when detected, are now notified to async applications.

Platform 3.27

* Java version support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.proofpoint.discovery.client;

import com.google.common.io.ByteStreams;
import com.google.common.net.HttpHeaders;
import com.google.common.util.concurrent.ListenableFuture;
import com.proofpoint.http.client.CacheControl;
Expand All @@ -32,6 +31,7 @@
import org.weakref.jmx.Flatten;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.concurrent.CancellationException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -128,8 +128,8 @@ public ServiceDescriptors handle(Request request, Response response)
}

byte[] json;
try {
json = ByteStreams.toByteArray(response.getInputStream());
try (InputStream stream = response.getInputStream()) {
json = stream.readAllBytes();
}
catch (IOException e) {
throw new DiscoveryException(format("Lookup of %s failed", type), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package com.proofpoint.http.client;

import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import com.google.common.net.MediaType;
import com.google.common.primitives.Ints;
import com.proofpoint.json.JsonCodec;

import java.io.InputStream;
import java.util.Set;

import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
Expand Down Expand Up @@ -72,9 +72,9 @@ public T handle(Request request, Response response)
if (contentType != null && !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) {
return defaultValue;
}
try {
return jsonCodec.fromJson(ByteStreams.toByteArray(response.getInputStream()));
}
try (InputStream inputStream = response.getInputStream()) {
return jsonCodec.fromJson(inputStream.readAllBytes());
}
catch (Exception e) {
return defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@

import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.io.ByteStreams;
import com.google.common.net.MediaType;
import com.proofpoint.http.client.FullJsonResponseHandler.JsonResponse;
import com.proofpoint.json.JsonCodec;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
import static com.proofpoint.http.client.ResponseHandlerUtils.readResponseBytes;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -61,24 +60,14 @@ public JsonResponse<T> handleException(Request request, Exception exception)
@Override
public JsonResponse<T> handle(Request request, Response response)
{
byte[] bytes = readResponseBytes(response);
byte[] bytes = readResponseBytes(request, response);
String contentType = response.getHeader(CONTENT_TYPE);
if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) {
return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), bytes);
}
return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, bytes);
}

private static byte[] readResponseBytes(Response response)
{
try {
return ByteStreams.toByteArray(response.getInputStream());
}
catch (IOException e) {
throw new RuntimeException("Error reading response from server", e);
}
}

public static class JsonResponse<T>
{
private final int statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
package com.proofpoint.http.client;

import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import com.google.common.net.MediaType;
import com.google.common.primitives.Ints;
import com.proofpoint.json.JsonCodec;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;
import java.util.Set;

import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
import static com.proofpoint.http.client.ResponseHandlerUtils.readResponseBytes;
import static java.nio.charset.StandardCharsets.UTF_8;

public class JsonResponseHandler<T> implements ResponseHandler<T, RuntimeException>
Expand Down Expand Up @@ -80,13 +79,9 @@ public T handle(Request request, Response response)
if (!MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) {
throw new UnexpectedResponseException("Expected application/json response from server but got " + contentType, request, response);
}
byte[] bytes;
try {
bytes = ByteStreams.toByteArray(response.getInputStream());
}
catch (IOException e) {
throw new RuntimeException("Error reading JSON response from server", e);
}

byte[] bytes = readResponseBytes(request, response);

try {
return jsonCodec.fromJson(bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,19 @@ public static RuntimeException propagate(Request request, Throwable exception)
throwIfUnchecked(exception);
throw new RuntimeException(exception);
}

public static byte[] readResponseBytes(Request request, Response response)
{
try {
return response.getInputStream().readAllBytes();
}
catch (IOException e) {
throw new UncheckedIOException("Failed reading response from server: " + urlFor(request), e);
}
}

private static String urlFor(Request request)
{
return request.getUri().toASCIIString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.io.ByteStreams;
import com.google.common.net.MediaType;
import com.proofpoint.http.client.StringResponseHandler.StringResponse;
import jakarta.annotation.Nullable;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Optional;

import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
import static com.proofpoint.http.client.ResponseHandlerUtils.readResponseBytes;
import static java.nio.charset.StandardCharsets.UTF_8;

public class StringResponseHandler implements ResponseHandler<StringResponse, RuntimeException>
Expand All @@ -53,27 +52,18 @@ public StringResponse handleException(Request request, Exception exception)
@Override
public StringResponse handle(Request request, Response response)
{
try {
String contentType = response.getHeader(CONTENT_TYPE);

if (contentType != null) {
MediaType mediaType = MediaType.parse(contentType);
return new StringResponse(
response.getStatusCode(),
response.getStatusMessage(),
response.getHeaders(),
new String(ByteStreams.toByteArray(response.getInputStream()), mediaType.charset().or(UTF_8)));
}

return new StringResponse(
response.getStatusCode(),
response.getStatusMessage(),
response.getHeaders(),
new String(toByteArray(response.getInputStream()), UTF_8));
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
byte[] bytes = readResponseBytes(request, response);

Charset charset = Optional.ofNullable(response.getHeader(CONTENT_TYPE))
.map(MediaType::parse)
.flatMap(mediaType -> mediaType.charset().toJavaUtil())
.orElse(UTF_8);

return new StringResponse(
response.getStatusCode(),
response.getStatusMessage(),
response.getHeaders(),
new String(bytes, charset));
}

public static class StringResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.io.ByteStreams;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -61,7 +61,9 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
requestHeaders.putAll(HeaderName.of(name), Collections.list(request.getHeaders(name)));
}

requestBytes = ByteStreams.toByteArray(request.getInputStream());
try (InputStream inputStream = request.getInputStream()) {
requestBytes = inputStream.readAllBytes();
}

if (responseStatusMessage != null) {
response.sendError(responseStatusCode, responseStatusMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;

import static com.google.common.net.MediaType.JSON_UTF_8;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.proofpoint.http.client.FullJsonResponseHandler.JsonResponse;
import static com.proofpoint.http.client.FullJsonResponseHandler.createFullJsonResponseHandler;
import static com.proofpoint.http.client.HttpStatus.INTERNAL_SERVER_ERROR;
import static com.proofpoint.http.client.Request.Builder.prepareGet;
import static com.proofpoint.http.client.testing.TestingResponse.mockResponse;
import static com.proofpoint.testing.Assertions.assertInstanceOf;
import static java.lang.String.format;
Expand Down Expand Up @@ -161,14 +163,14 @@ public void testJsonReadException()
when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(expectedException);

try {
handler.handle(null, mockResponse()
handler.handle(prepareGet().setUri(URI.create("https://invalid.invalid/test")).build(), mockResponse()
.contentType(JSON_UTF_8)
.body(inputStream)
.build());
fail("expected exception");
}
catch (RuntimeException e) {
assertEquals(e.getMessage(), "Error reading response from server");
assertEquals(e.getMessage(), "Failed reading response from server: https://invalid.invalid/test");
assertSame(e.getCause(), expectedException);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import static com.google.common.net.MediaType.JSON_UTF_8;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.proofpoint.http.client.HttpStatus.INTERNAL_SERVER_ERROR;
import static com.proofpoint.http.client.JsonResponseHandler.createJsonResponseHandler;
import static com.proofpoint.http.client.Request.Builder.prepareGet;
import static com.proofpoint.http.client.TestFullJsonResponseHandler.User;
import static com.proofpoint.http.client.testing.TestingResponse.mockResponse;
import static com.proofpoint.testing.Assertions.assertInstanceOf;
Expand Down Expand Up @@ -96,14 +98,14 @@ public void testJsonReadException()
when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(expectedException);

try {
handler.handle(null, mockResponse()
handler.handle(prepareGet().setUri(URI.create("https://invalid.invalid/test")).build(), mockResponse()
.contentType(JSON_UTF_8)
.body(inputStream)
.build());
fail("expected exception");
}
catch (RuntimeException e) {
assertEquals(e.getMessage(), "Error reading JSON response from server");
assertEquals(e.getMessage(), "Failed reading response from server: https://invalid.invalid/test");
assertSame(e.getCause(), expectedException);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Set;
import java.util.function.Function;

import static com.google.common.io.ByteStreams.toByteArray;
import static com.proofpoint.http.client.testing.TestingResponse.mockResponse;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -218,7 +217,7 @@ private static void assertResponse(Response response, HttpStatus status, Immutab
}
assertEquals(response.getHeaders(), builder.build());
try {
assertEquals(toByteArray(response.getInputStream()), body);
assertEquals(response.getInputStream().readAllBytes(), body);
}
catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Loading