Skip to content

Commit

Permalink
Check buffer usage, use constant bytes instead of strings, remove com…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
niloc132 committed Aug 29, 2022
1 parent 5054a9c commit d291b9c
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,17 @@ public void complete() {
payload.putInt(trailerLength);
for (Map.Entry<String, String> entry : map.entrySet()) {
payload.put(entry.getKey().getBytes(StandardCharsets.US_ASCII));
payload.put(": ".getBytes(StandardCharsets.US_ASCII));
payload.put((byte) ':');
payload.put((byte) ' ');
payload.put(entry.getValue().getBytes(StandardCharsets.US_ASCII));
payload.put("\r\n".getBytes(StandardCharsets.US_ASCII));
payload.put((byte) '\r');
payload.put((byte) '\n');
}
if (payload.hasRemaining()) {
// Normally we must not throw, but this is an exceptional case. Complete
// the stream, _then_ throw.
super.complete();
throw new IllegalStateException("Incorrectly sized buffer, trailer payload will be sized wrong");
}
wrappedResponse.getOutputStream().write(payload.array());
}
Expand Down Expand Up @@ -105,8 +113,7 @@ private static boolean isGrpcWeb(ServletRequest request) {
return request.getContentType() != null && request.getContentType().startsWith(CONTENT_TYPE_GRPC_WEB);
}

// Technically we should throw away content-length too, but the impl won't care
public static class GrpcWebHttpResponse extends HttpServletResponseWrapper {
private static class GrpcWebHttpResponse extends HttpServletResponseWrapper {
private Supplier<Map<String, String>> trailers;

public GrpcWebHttpResponse(HttpServletResponse response) {
Expand Down

0 comments on commit d291b9c

Please sign in to comment.