Skip to content

Commit c2ec843

Browse files
committed
add allow header for response code 405
1 parent 5a54372 commit c2ec843

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

netty/src/main/java/io/grpc/netty/NettyServerHandler.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import io.netty.channel.ChannelFutureListener;
6161
import io.netty.channel.ChannelHandlerContext;
6262
import io.netty.channel.ChannelPromise;
63+
import io.netty.handler.codec.http.HttpHeaderNames;
6364
import io.netty.handler.codec.http2.DecoratingHttp2ConnectionEncoder;
6465
import io.netty.handler.codec.http2.DecoratingHttp2FrameWriter;
6566
import io.netty.handler.codec.http2.DefaultHttp2Connection;
@@ -70,6 +71,7 @@
7071
import io.netty.handler.codec.http2.DefaultHttp2Headers;
7172
import io.netty.handler.codec.http2.DefaultHttp2LocalFlowController;
7273
import io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController;
74+
import io.netty.handler.codec.http2.EmptyHttp2Headers;
7375
import io.netty.handler.codec.http2.Http2Connection;
7476
import io.netty.handler.codec.http2.Http2ConnectionAdapter;
7577
import io.netty.handler.codec.http2.Http2ConnectionDecoder;
@@ -480,8 +482,10 @@ private void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers
480482
}
481483

482484
if (!HTTP_METHOD.contentEquals(headers.method())) {
485+
Http2Headers extraHeaders = new DefaultHttp2Headers();
486+
extraHeaders.add(HttpHeaderNames.ALLOW, HTTP_METHOD);
483487
respondWithHttpError(ctx, streamId, 405, Status.Code.INTERNAL,
484-
String.format("Method '%s' is not supported", headers.method()));
488+
String.format("Method '%s' is not supported", headers.method()), extraHeaders);
485489
return;
486490
}
487491

@@ -868,7 +872,13 @@ public boolean visit(Http2Stream stream) throws Http2Exception {
868872
}
869873

870874
private void respondWithHttpError(
871-
ChannelHandlerContext ctx, int streamId, int code, Status.Code statusCode, String msg) {
875+
ChannelHandlerContext ctx, int streamId, int code, Status.Code statusCode, String msg) {
876+
respondWithHttpError(ctx, streamId, code, statusCode, msg, EmptyHttp2Headers.INSTANCE);
877+
}
878+
879+
private void respondWithHttpError(
880+
ChannelHandlerContext ctx, int streamId, int code, Status.Code statusCode, String msg,
881+
Http2Headers extraHeaders) {
872882
Metadata metadata = new Metadata();
873883
metadata.put(InternalStatus.CODE_KEY, statusCode.toStatus());
874884
metadata.put(InternalStatus.MESSAGE_KEY, msg);
@@ -880,6 +890,7 @@ private void respondWithHttpError(
880890
for (int i = 0; i < serialized.length; i += 2) {
881891
headers.add(new AsciiString(serialized[i], false), new AsciiString(serialized[i + 1], false));
882892
}
893+
headers.add(extraHeaders);
883894
encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
884895
ByteBuf msgBuf = ByteBufUtil.writeUtf8(ctx.alloc(), msg);
885896
encoder().writeData(ctx, streamId, msgBuf, 0, true, ctx.newPromise());

okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static io.grpc.okhttp.OkHttpServerBuilder.MAX_CONNECTION_IDLE_NANOS_DISABLED;
2121

2222
import com.google.common.base.Preconditions;
23+
import com.google.common.collect.Lists;
2324
import com.google.common.util.concurrent.Futures;
2425
import com.google.common.util.concurrent.ListenableFuture;
2526
import com.google.errorprone.annotations.concurrent.GuardedBy;
@@ -52,6 +53,7 @@
5253
import java.io.IOException;
5354
import java.net.Socket;
5455
import java.net.SocketException;
56+
import java.util.Collections;
5557
import java.util.List;
5658
import java.util.Locale;
5759
import java.util.Map;
@@ -772,8 +774,9 @@ public void headers(boolean outFinished,
772774
}
773775

774776
if (!POST_METHOD.equals(httpMethod)) {
777+
List<Header> extraHeaders = Lists.newArrayList(new Header(ByteString.encodeUtf8("allow"), POST_METHOD));
775778
respondWithHttpError(streamId, inFinished, 405, Status.Code.INTERNAL,
776-
"HTTP Method is not supported: " + asciiString(httpMethod));
779+
"HTTP Method is not supported: " + asciiString(httpMethod), extraHeaders);
777780
return;
778781
}
779782

@@ -1065,12 +1068,18 @@ private void streamError(int streamId, ErrorCode errorCode, String reason) {
10651068
}
10661069

10671070
private void respondWithHttpError(
1068-
int streamId, boolean inFinished, int httpCode, Status.Code statusCode, String msg) {
1071+
int streamId, boolean inFinished, int httpCode, Status.Code statusCode, String msg) {
1072+
respondWithHttpError(streamId, inFinished, httpCode, statusCode, msg, Collections.emptyList());
1073+
}
1074+
1075+
private void respondWithHttpError(
1076+
int streamId, boolean inFinished, int httpCode, Status.Code statusCode, String msg, List<Header> extraHeaders) {
10691077
Metadata metadata = new Metadata();
10701078
metadata.put(InternalStatus.CODE_KEY, statusCode.toStatus());
10711079
metadata.put(InternalStatus.MESSAGE_KEY, msg);
10721080
List<Header> headers =
10731081
Headers.createHttpResponseHeaders(httpCode, "text/plain; charset=utf-8", metadata);
1082+
headers.addAll(extraHeaders);
10741083
Buffer data = new Buffer().writeUtf8(msg);
10751084

10761085
synchronized (lock) {

0 commit comments

Comments
 (0)