Skip to content

Commit

Permalink
fix: transparent info use bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
wardseptember committed Dec 28, 2023
1 parent ebbbe98 commit 68c0f52
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.tencent.trpc.core.rpc.Response;
import com.tencent.trpc.core.utils.RpcUtils;
import com.tencent.trpc.proto.http.common.HttpConstants;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -94,11 +95,12 @@ private Response handleResponse(Request request, SimpleHttpResponse simpleHttpRe
}
// handle http header
// Parse the data passed through from the server to the client.
// In the tRPC protocol, the value of the attachment is stored and used as a byte array to maintain consistency.
Map<String, Object> respAttachments = new HashMap<>();
for (Header header : simpleHttpResponse.getHeaders()) {
String name = header.getName();
String value = header.getValue();
respAttachments.put(name, value);
respAttachments.put(name, value.getBytes(StandardCharsets.UTF_8));
}

Header contentLengthHdr = simpleHttpResponse.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
Expand Down Expand Up @@ -202,7 +204,7 @@ private SimpleHttpRequest buildRequest(Request request, int requestTimeout) thro
if (jsonString != null) {
simpleHttpRequest.setBody(jsonString, ContentType.APPLICATION_JSON);
}
// set custom business headers, consistent with the TRPC protocol, only process String and byte[]
// Set custom business headers, consistent with the TRPC protocol, only process String and byte[]
request.getAttachments().forEach((k, v) -> {
if (Objects.equal(k, HttpHeaders.TRANSFER_ENCODING) || Objects.equal(k, HttpHeaders.CONTENT_LENGTH)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ private Response handleResponse(Request request, CloseableHttpResponse httpRespo
throw TRpcException.newBizException(statusCode,
httpResponse.getStatusLine().getReasonPhrase());
}
//handle http header
//Parse the data passed through from the server to the client.
// handle http header
// Parse the data passed through from the server to the client.
// In the tRPC protocol, the value of the attachment is stored and used as a byte array to maintain consistency.
Map<String, Object> respAttachments = new HashMap<>();
for (Header header : httpResponse.getAllHeaders()) {
String name = header.getName();
for (HeaderElement element : header.getElements()) {
String value = element.getName();
respAttachments.put(name, value);
respAttachments.put(name, value.getBytes(StandardCharsets.UTF_8));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private void fillResponseHeaderWithAttachments(HttpServletResponse response, Res
if (attachments == null || attachments.isEmpty()) {
return;
}
// set custom business headers, consistent with the TRPC protocol, only process String and byte[]
// Set custom business headers, consistent with the TRPC protocol, only process String and byte[]
attachments.forEach((name, value) -> {
if (value instanceof String) {
response.setHeader(name, String.valueOf(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.tencent.trpc.core.common.config.ServiceConfig;
import com.tencent.trpc.core.rpc.RpcClientContext;
import com.tencent.trpc.core.utils.NetUtils;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import org.apache.http.HttpHeaders;
import org.junit.AfterClass;
Expand Down Expand Up @@ -131,10 +132,10 @@ public void testHttp2RpcClient() {
HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE));
// get server tran info
byte[] bytesRspValue = (byte[]) context.getRspAttachMap().get(TEST_BYTES_RSP_KEY);
Assert.assertEquals(bytesRspValue, TEST_BYTES_RSP_VALUE);
Assert.assertArrayEquals(bytesRspValue, TEST_BYTES_RSP_VALUE);

String stringRspValue = (String) context.getRspAttachMap().get(TEST_STRING_RSP_KEY);
Assert.assertEquals(stringRspValue, TEST_STRING_RSP_VALUE);
byte[] stringRspValue = (byte[]) context.getRspAttachMap().get(TEST_STRING_RSP_KEY);
Assert.assertEquals(new String(stringRspValue, StandardCharsets.UTF_8), TEST_STRING_RSP_VALUE);

Assert.assertNotNull(helloResponse);
String rspMessage = helloResponse.getMessage();
Expand Down Expand Up @@ -166,10 +167,10 @@ public void testHttpRpcClient() {
HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE));
// get server tran info
byte[] bytesRspValue = (byte[]) context.getRspAttachMap().get(TEST_BYTES_RSP_KEY);
Assert.assertEquals(bytesRspValue, TEST_BYTES_RSP_VALUE);
Assert.assertArrayEquals(bytesRspValue, TEST_BYTES_RSP_VALUE);

String stringRspValue = (String) context.getRspAttachMap().get(TEST_STRING_RSP_KEY);
Assert.assertEquals(stringRspValue, TEST_STRING_RSP_VALUE);
byte[] stringRspValue = (byte[]) context.getRspAttachMap().get(TEST_STRING_RSP_KEY);
Assert.assertEquals(new String(stringRspValue, StandardCharsets.UTF_8), TEST_STRING_RSP_VALUE);

Assert.assertNotNull(helloResponse);
String rspMessage = helloResponse.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class Constant {
public static final String TEST_MESSAGE = "tRPC-Java!";
public static final String TEST_RSP_MESSAGE = "Hello tRPC-Java!";

public static final String TEST_STRING_REQ_KEY = "stringReqKey";
public static final String TEST_STRING_REQ_KEY = "string-req-key";
public static final String TEST_STRING_REQ_VALUE = "stringReqValue";

public static final String TEST_BYTES_REQ_KEY = "bytesReqKey";
public static final String TEST_BYTES_REQ_KEY = "bytes-req-key";
public static final byte[] TEST_BYTES_REQ_VALUE = "bytesReqValue".getBytes(StandardCharsets.UTF_8);

public static final String TEST_STRING_RSP_KEY = "stringRspKey";
public static final String TEST_STRING_RSP_KEY = "string-rsp-key";
public static final String TEST_STRING_RSP_VALUE = "stringRspValue";

public static final String TEST_BYTES_RSP_KEY = "bytesRspKey";
public static final String TEST_BYTES_RSP_KEY = "bytes-rsp-key";
public static final byte[] TEST_BYTES_RSP_VALUE = "bytesRspValue".getBytes(StandardCharsets.UTF_8);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.tencent.trpc.proto.http.constant.Constant.TEST_STRING_RSP_VALUE;

import com.tencent.trpc.core.rpc.RpcContext;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import tests.service.GreeterService;
import tests.service.HelloRequestProtocol;
Expand All @@ -33,11 +34,11 @@ public class GreeterServiceImpl2 implements GreeterService {

@Override
public HelloRequestProtocol.HelloResponse sayHello(RpcContext context, HelloRequestProtocol.HelloRequest request) {
String stringReqValue = (String) context.getReqAttachMap().get(TEST_STRING_REQ_KEY);
Assert.assertEquals(stringReqValue, TEST_STRING_REQ_VALUE);
byte[] stringReqValue = (byte[]) context.getReqAttachMap().get(TEST_STRING_REQ_KEY);
Assert.assertEquals(new String(stringReqValue, StandardCharsets.UTF_8), TEST_STRING_REQ_VALUE);

byte[] bytesReqValue = (byte[]) context.getReqAttachMap().get(TEST_BYTES_REQ_KEY);
Assert.assertEquals(TEST_BYTES_REQ_VALUE, bytesReqValue);
Assert.assertArrayEquals(TEST_BYTES_REQ_VALUE, bytesReqValue);

String message = request.getMessage();
Assert.assertEquals(TEST_MESSAGE, message);
Expand Down

0 comments on commit 68c0f52

Please sign in to comment.