Skip to content

Commit a531a5c

Browse files
authored
#1903: merge issue 1903's fix from 2.7.0 to 2.6.x (#2668)
1 parent dca9574 commit a531a5c

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ protected Object decode(Channel channel, ChannelBuffer buffer, int readable, byt
139139

140140
protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
141141
byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
142-
Serialization s = CodecSupport.getSerialization(channel.getUrl(), proto);
143-
ObjectInput in = s.deserialize(channel.getUrl(), is);
144142
// get request id.
145143
long id = Bytes.bytes2long(header, 4);
146144
if ((flag & FLAG_REQUEST) == 0) {
@@ -152,8 +150,9 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
152150
// get status.
153151
byte status = header[3];
154152
res.setStatus(status);
155-
if (status == Response.OK) {
156-
try {
153+
try {
154+
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
155+
if (status == Response.OK) {
157156
Object data;
158157
if (res.isHeartbeat()) {
159158
data = decodeHeartbeatData(channel, in);
@@ -163,12 +162,12 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
163162
data = decodeResponseData(channel, in, getRequestData(id));
164163
}
165164
res.setResult(data);
166-
} catch (Throwable t) {
167-
res.setStatus(Response.CLIENT_ERROR);
168-
res.setErrorMessage(StringUtils.toString(t));
165+
} else {
166+
res.setErrorMessage(in.readUTF());
169167
}
170-
} else {
171-
res.setErrorMessage(in.readUTF());
168+
} catch (Throwable t) {
169+
res.setStatus(Response.CLIENT_ERROR);
170+
res.setErrorMessage(StringUtils.toString(t));
172171
}
173172
return res;
174173
} else {
@@ -180,6 +179,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
180179
req.setEvent(Request.HEARTBEAT_EVENT);
181180
}
182181
try {
182+
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
183183
Object data;
184184
if (req.isHeartbeat()) {
185185
data = decodeHeartbeatData(channel, in);

dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/transport/CodecSupport.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import com.alibaba.dubbo.common.extension.ExtensionLoader;
2323
import com.alibaba.dubbo.common.logger.Logger;
2424
import com.alibaba.dubbo.common.logger.LoggerFactory;
25+
import com.alibaba.dubbo.common.serialize.ObjectInput;
2526
import com.alibaba.dubbo.common.serialize.Serialization;
2627

2728
import java.io.IOException;
29+
import java.io.InputStream;
2830
import java.util.HashMap;
2931
import java.util.Map;
3032
import java.util.Set;
@@ -75,4 +77,9 @@ public static Serialization getSerialization(URL url, Byte id) throws IOExceptio
7577
return serialization;
7678
}
7779

80+
public static ObjectInput deserialize(URL url, InputStream is, byte proto) throws IOException {
81+
Serialization s = getSerialization(url, proto);
82+
return s.deserialize(url, is);
83+
}
84+
7885
}

dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/codec/ExchangeCodecTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ public void test_Decode_Error_Response_Object() throws IOException {
147147
Assert.assertEquals(90, obj.getStatus());
148148
}
149149

150+
@Test
151+
public void testInvalidSerializaitonId() throws Exception {
152+
byte[] header = new byte[]{MAGIC_HIGH, MAGIC_LOW, (byte)0x8F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
153+
Object obj = decode(header);
154+
Assert.assertTrue(obj instanceof Request);
155+
Request request = (Request) obj;
156+
Assert.assertTrue(request.isBroken());
157+
Assert.assertTrue(request.getData() instanceof IOException);
158+
header = new byte[]{MAGIC_HIGH, MAGIC_LOW, (byte)0x1F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
159+
160+
obj = decode(header);
161+
Assert.assertTrue(obj instanceof Response);
162+
Response response = (Response) obj;
163+
Assert.assertEquals(response.getStatus(), Response.CLIENT_ERROR);
164+
Assert.assertTrue(response.getErrorMessage().contains("IOException"));
165+
}
166+
150167
@Test
151168
public void test_Decode_Check_Payload() throws IOException {
152169
byte[] header = new byte[]{MAGIC_HIGH, MAGIC_LOW, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/transport/codec/DeprecatedExchangeCodec.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ protected Object decode(Channel channel, InputStream is, int readable, byte[] he
130130

131131
protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
132132
byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
133-
Serialization s = CodecSupport.getSerialization(channel.getUrl(), proto);
134-
ObjectInput in = s.deserialize(channel.getUrl(), is);
135133
// get request id.
136134
long id = Bytes.bytes2long(header, 4);
137135
if ((flag & FLAG_REQUEST) == 0) {
@@ -143,8 +141,9 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
143141
// get status.
144142
byte status = header[3];
145143
res.setStatus(status);
146-
if (status == Response.OK) {
147-
try {
144+
try {
145+
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
146+
if (status == Response.OK) {
148147
Object data;
149148
if (res.isHeartbeat()) {
150149
data = decodeHeartbeatData(channel, in);
@@ -154,12 +153,12 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
154153
data = decodeResponseData(channel, in, getRequestData(id));
155154
}
156155
res.setResult(data);
157-
} catch (Throwable t) {
158-
res.setStatus(Response.CLIENT_ERROR);
159-
res.setErrorMessage(StringUtils.toString(t));
156+
} else {
157+
res.setErrorMessage(in.readUTF());
160158
}
161-
} else {
162-
res.setErrorMessage(in.readUTF());
159+
} catch (Throwable t) {
160+
res.setStatus(Response.CLIENT_ERROR);
161+
res.setErrorMessage(StringUtils.toString(t));
163162
}
164163
return res;
165164
} else {
@@ -171,6 +170,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
171170
req.setEvent(Request.HEARTBEAT_EVENT);
172171
}
173172
try {
173+
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
174174
Object data;
175175
if (req.isHeartbeat()) {
176176
data = decodeHeartbeatData(channel, in);

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboCodec.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
package com.alibaba.dubbo.rpc.protocol.dubbo;
1818

1919
import com.alibaba.dubbo.common.Constants;
20-
import com.alibaba.dubbo.common.URL;
2120
import com.alibaba.dubbo.common.Version;
2221
import com.alibaba.dubbo.common.io.Bytes;
2322
import com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream;
2423
import com.alibaba.dubbo.common.logger.Logger;
2524
import com.alibaba.dubbo.common.logger.LoggerFactory;
2625
import com.alibaba.dubbo.common.serialize.ObjectInput;
2726
import com.alibaba.dubbo.common.serialize.ObjectOutput;
28-
import com.alibaba.dubbo.common.serialize.Serialization;
2927
import com.alibaba.dubbo.common.utils.ReflectUtils;
3028
import com.alibaba.dubbo.common.utils.StringUtils;
3129
import com.alibaba.dubbo.remoting.Channel;
@@ -63,7 +61,6 @@ public class DubboCodec extends ExchangeCodec implements Codec2 {
6361
@Override
6462
protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
6563
byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
66-
Serialization s = CodecSupport.getSerialization(channel.getUrl(), proto);
6764
// get request id.
6865
long id = Bytes.bytes2long(header, 4);
6966
if ((flag & FLAG_REQUEST) == 0) {
@@ -75,13 +72,14 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
7572
// get status.
7673
byte status = header[3];
7774
res.setStatus(status);
78-
if (status == Response.OK) {
79-
try {
75+
try {
76+
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
77+
if (status == Response.OK) {
8078
Object data;
8179
if (res.isHeartbeat()) {
82-
data = decodeHeartbeatData(channel, deserialize(s, channel.getUrl(), is));
80+
data = decodeHeartbeatData(channel, in);
8381
} else if (res.isEvent()) {
84-
data = decodeEventData(channel, deserialize(s, channel.getUrl(), is));
82+
data = decodeEventData(channel, in);
8583
} else {
8684
DecodeableRpcResult result;
8785
if (channel.getUrl().getParameter(
@@ -98,15 +96,15 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
9896
data = result;
9997
}
10098
res.setResult(data);
101-
} catch (Throwable t) {
102-
if (log.isWarnEnabled()) {
103-
log.warn("Decode response failed: " + t.getMessage(), t);
104-
}
105-
res.setStatus(Response.CLIENT_ERROR);
106-
res.setErrorMessage(StringUtils.toString(t));
99+
} else {
100+
res.setErrorMessage(in.readUTF());
107101
}
108-
} else {
109-
res.setErrorMessage(deserialize(s, channel.getUrl(), is).readUTF());
102+
} catch (Throwable t) {
103+
if (log.isWarnEnabled()) {
104+
log.warn("Decode response failed: " + t.getMessage(), t);
105+
}
106+
res.setStatus(Response.CLIENT_ERROR);
107+
res.setErrorMessage(StringUtils.toString(t));
110108
}
111109
return res;
112110
} else {
@@ -119,10 +117,11 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
119117
}
120118
try {
121119
Object data;
120+
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
122121
if (req.isHeartbeat()) {
123-
data = decodeHeartbeatData(channel, deserialize(s, channel.getUrl(), is));
122+
data = decodeHeartbeatData(channel, in);
124123
} else if (req.isEvent()) {
125-
data = decodeEventData(channel, deserialize(s, channel.getUrl(), is));
124+
data = decodeEventData(channel, in);
126125
} else {
127126
DecodeableRpcInvocation inv;
128127
if (channel.getUrl().getParameter(
@@ -149,11 +148,6 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
149148
}
150149
}
151150

152-
private ObjectInput deserialize(Serialization serialization, URL url, InputStream is)
153-
throws IOException {
154-
return serialization.deserialize(url, is);
155-
}
156-
157151
private byte[] readMessageData(InputStream is) throws IOException {
158152
if (is.available() > 0) {
159153
byte[] result = new byte[is.available()];

0 commit comments

Comments
 (0)