Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.ResponseBuilder;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.StreamingOutput;
import jakarta.ws.rs.core.UriInfo;

import org.apache.commons.httpclient.Header;
Expand Down Expand Up @@ -399,6 +400,33 @@ protected <T extends HttpMethodBase> Response forward(T method, Consumer<T> prep
}
responseBuilder.header(name, header.getValue());
}
return responseBuilder.entity(method.getResponseBodyAsStream()).build();

responseBuilder.header("X-Proxy-Streaming", "1");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marker: lets callers verify the streaming patch is loaded


InputStream responseBodyStream = method.getResponseBodyAsStream();
if (responseBodyStream == null) {
method.releaseConnection();
return responseBuilder.build();
}

StreamingOutput streamingBody = output -> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy the upstream body chunk by chunk, flushing after every read so each chunk reaches the socket immediately instead of accumulating in the servlet output buffer until the upstream response completes.

try {
byte[] buffer = new byte[8192];
int read;
while ((read = responseBodyStream.read(buffer)) >= 0) {
if (read > 0) {
output.write(buffer, 0, read);
output.flush();
}
}
} finally {
try {
responseBodyStream.close();
} catch (IOException ignored) {
}
method.releaseConnection();
}
};
return responseBuilder.entity(streamingBody).build();
}
}