Skip to content

Commit

Permalink
Remove rpc context to avoid http2 headers override (#7345)
Browse files Browse the repository at this point in the history
  • Loading branch information
guohao committed Mar 9, 2021
1 parent 0df1fdc commit 2e7151e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ protected Map<String, Object> parseHeadersToMap(Http2Headers headers) {
Map<String, Object> attachments = new HashMap<>();
for (Map.Entry<CharSequence, CharSequence> header : headers) {
String key = header.getKey().toString();
if(Http2Headers.PseudoHeaderName.isPseudoHeader(key)){
continue;
}

if (ENABLE_ATTACHMENT_WRAP) {
if (key.endsWith("-tw-bin") && key.length() > 7) {
Expand All @@ -156,6 +159,9 @@ protected Map<String, Object> parseHeadersToMap(Http2Headers headers) {
protected void convertAttachment(Http2Headers trailers, Map<String, Object> attachments) throws IOException {
for (Map.Entry<String, Object> entry : attachments.entrySet()) {
final String key = entry.getKey().toLowerCase(Locale.ROOT);
if(Http2Headers.PseudoHeaderName.isPseudoHeader(key)){
continue;
}
final Object v = entry.getValue();
if (!ENABLE_ATTACHMENT_WRAP) {
if (v instanceof String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.MethodDescriptor;
Expand Down Expand Up @@ -183,12 +184,12 @@ private void unaryInvoke() {
ClassLoadUtil.switchContextLoader(tccl);
}

final Http2Headers trailers = new DefaultHttp2Headers()
.setInt(TripleConstant.STATUS_KEY, GrpcStatus.Code.OK.code);
final Http2Headers trailers = new DefaultHttp2Headers();
final Map<String, Object> attachments = response.getObjectAttachments();
if (attachments != null) {
convertAttachment(trailers, attachments);
}
trailers.setInt(TripleConstant.STATUS_KEY, GrpcStatus.Code.OK.code);
ctx.write(new DefaultHttp2HeadersFrame(http2Headers));
final DefaultHttp2DataFrame data = new DefaultHttp2DataFrame(buf);
ctx.write(data);
Expand All @@ -206,6 +207,7 @@ private void unaryInvoke() {
};

future.whenComplete(onComplete);
RpcContext.removeContext();
}


Expand Down Expand Up @@ -277,6 +279,15 @@ private Invocation buildInvocation() {
inv.setParameterTypes(methodDescriptor.getParameterClasses());
inv.setReturnTypes(methodDescriptor.getReturnTypes());
final Map<String, Object> attachments = parseHeadersToMap(getHeaders());
attachments.remove("content-type");
attachments.remove("interface");
attachments.remove("tri-service-version");
attachments.remove("tri-service-group");
attachments.remove("serialization");
attachments.remove("te");
attachments.remove("path");
attachments.remove("grpc-status");
attachments.remove("grpc-message");
inv.setObjectAttachments(attachments);
return inv;
}
Expand Down

0 comments on commit 2e7151e

Please sign in to comment.