This is an automated email from the ASF dual-hosted git repository. jerrick pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push: new 895a4dd Judge null for key and value in attachment in RpcContextFilter. (#2171) 895a4dd is described below commit 895a4dd690f8190a972e9d4ae370bf5b1c8700c7 Author: 时无两丶 <442367...@qq.com> AuthorDate: Thu Aug 9 11:28:12 2018 +0800 Judge null for key and value in attachment in RpcContextFilter. (#2171) * Judge null for key and value in attachment in RpcContextFilter. If not, NullPointerException will be thrown in RpcContextFilter in some case. * optimize code --- .../dubbo/rpc/protocol/rest/RpcContextFilter.java | 25 +++++++++++++++++----- .../dubbo/rpc/protocol/rest/RestProtocolTest.java | 21 ++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java index f585dc7..ae9e813 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcContextFilter.java @@ -18,7 +18,6 @@ package org.apache.dubbo.rpc.protocol.rest; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.RpcContext; - import org.jboss.resteasy.spi.ResteasyProviderFactory; import javax.annotation.Priority; @@ -70,19 +69,35 @@ public class RpcContextFilter implements ContainerRequestFilter, ClientRequestFi public void filter(ClientRequestContext requestContext) throws IOException { int size = 0; for (Map.Entry<String, String> entry : RpcContext.getContext().getAttachments().entrySet()) { - if (entry.getValue().contains(",") || entry.getValue().contains("=") - || entry.getKey().contains(",") || entry.getKey().contains("=")) { + String key = entry.getKey(); + String value = entry.getKey(); + if (illegalForRest(key) || illegalForRest(value)) { throw new IllegalArgumentException("The attachments of " + RpcContext.class.getSimpleName() + " must not contain ',' or '=' when using rest protocol"); } // TODO for now we don't consider the differences of encoding and server limit - size += entry.getValue().getBytes("UTF-8").length; + if (value != null) { + size += value.getBytes("UTF-8").length; + } if (size > MAX_HEADER_SIZE) { throw new IllegalArgumentException("The attachments of " + RpcContext.class.getSimpleName() + " is too big"); } - String attachments = entry.getKey() + "=" + entry.getValue(); + String attachments = key + "=" + value; requestContext.getHeaders().add(DUBBO_ATTACHMENT_HEADER, attachments); } } + + /** + * If a string value illegal for rest protocol(',' and '=' is illegal for rest protocol). + * + * @param v string value + * @return true for illegal + */ + private boolean illegalForRest(String v) { + if (StringUtils.isNotEmpty(v)) { + return v.contains(",") || v.contains("="); + } + return false; + } } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index 671cdf5..a698d0c 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -128,6 +128,27 @@ public class RestProtocolTest { exporter.unexport(); } + @Test + public void testRpcContextFilter() { + ServiceClassHolder.getInstance().pushServiceClass(DemoService.class); + + // use RpcContextFilter + URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty") + .addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.RpcContextFilter"); + Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl)); + + IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl)); + + String value = null; + // put a null value into attachment. + RpcContext.getContext().setAttachment("key", value); + Integer result = demoService.hello(1, 2); + + assertThat(result, is(3)); + + exporter.unexport(); + } + @Test(expected = RuntimeException.class) public void testRegFail() { ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);