Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix rest Chinese encoding #13617

Merged
merged 11 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,51 @@ public void testMethod() {
strings.toArray(new String[0]),
requestTemplate.getParam("param").toArray(new String[0]));
}

@Test
void testBuildURL() throws Exception {
int port = NetUtils.getAvailablePort();
URL url = new ServiceConfigURL(
"http", "localhost", port, new String[] {Constants.BIND_PORT_KEY, String.valueOf(port)});
HttpServer httpServer = new JettyHttpServer(url, new HttpHandler<HttpServletRequest, HttpServletResponse>() {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.getWriter().write(request.getQueryString());
}
});

RequestTemplate requestTemplate = new RequestTemplate(null, "POST", "localhost:" + port);

requestTemplate.addParam("name", "李强");
requestTemplate.addParam("age", "18");
requestTemplate.path("/hello/world");

// When using the OKHttpRestClient, parameters will be encoded with UTF-8 and appended to the URL
RestClient restClient = new OKHttpRestClient(new HttpClientConfig());

CompletableFuture<RestResult> send = restClient.send(requestTemplate);

RestResult restResult = send.get();

assertThat(new String(restResult.getBody()), is("name=%E6%9D%8E%E5%BC%BA&age=18"));

// When using the HttpClientRestClient, parameters will be encoded with UTF-8 and appended to the URL
restClient = new HttpClientRestClient(new HttpClientConfig());

send = restClient.send(requestTemplate);

restResult = send.get();

assertThat(new String(restResult.getBody()), is("name=%E6%9D%8E%E5%BC%BA&age=18"));

// When using the URLConnectionRestClient, parameters won't be encoded and still appended to the URL
restClient = new URLConnectionRestClient(new HttpClientConfig());

send = restClient.send(requestTemplate);

restResult = send.get();

assertThat(new String(restResult.getBody(), StandardCharsets.UTF_8), is("name=李强&age=18"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.dubbo.rpc.protocol.rest.deploy.ServiceDeployer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -56,6 +58,17 @@ protected void initHeaders() {}
protected void initParameters() {
String requestURI = getRequestURI();

String decodedRequestURI = requestURI;
try {
decodedRequestURI = URLDecoder.decode(decodedRequestURI, "UTF-8");
} catch (UnsupportedEncodingException e) {
// do nothing
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hardcoded, we can set utf-8 by default, but it must come from Accept-Charset first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll make a adjustment

Copy link
Contributor Author

@MajaChen MajaChen Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made an adjustment according to your opinon, please take a look again, take your time


if (StringUtils.isNotEmpty(decodedRequestURI)) {
requestURI = decodedRequestURI;
}

if (requestURI != null && requestURI.contains("?")) {

String queryString = requestURI.substring(requestURI.indexOf("?") + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class NettyRequestFacadeTest {

@Test
Expand Down Expand Up @@ -95,4 +98,34 @@ void testMethod() {

Assertions.assertEquals("GET", nettyRequestFacade.getMethod());
}

@Test
void testChineseDecoding() {
String uri = "/hello/world?name=%E6%9D%8E%E5%BC%BA&age=18";
DefaultFullHttpRequest defaultFullHttpRequest =
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);

NettyRequestFacade nettyRequestFacade = new NettyRequestFacade(defaultFullHttpRequest, null);
assertThat(nettyRequestFacade.getPath(), is("/hello/world"));
assertThat(nettyRequestFacade.getParameter("name"), is("李强"));
assertThat(nettyRequestFacade.getParameter("age"), is("18"));

// Applying the decode method to the URI is acceptable, even if the URI is not encoded.
uri = "/hello/world?name=lily&age=18";
defaultFullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);

nettyRequestFacade = new NettyRequestFacade(defaultFullHttpRequest, null);
assertThat(nettyRequestFacade.getPath(), is("/hello/world"));
assertThat(nettyRequestFacade.getParameter("name"), is("lily"));
assertThat(nettyRequestFacade.getParameter("age"), is("18"));

// When using URLConnectionRestClient, the URI won't be encoded, but it's still acceptable.
uri = "/hello/world?name=李强&age=18";
defaultFullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);

nettyRequestFacade = new NettyRequestFacade(defaultFullHttpRequest, null);
assertThat(nettyRequestFacade.getPath(), is("/hello/world"));
assertThat(nettyRequestFacade.getParameter("name"), is("李强"));
assertThat(nettyRequestFacade.getParameter("age"), is("18"));
}
}
Loading