Skip to content

Commit b81a63e

Browse files
forfreedayliukai
andauthored
feat(http): optimize parameter parsing (#5483)
--------- Co-authored-by: liukai <kayle.liu@tron.network>
1 parent 99bef82 commit b81a63e

3 files changed

Lines changed: 47 additions & 11 deletions

File tree

framework/src/main/java/org/tron/core/services/http/Util.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.commons.lang3.StringUtils;
3131
import org.bouncycastle.util.encoders.Hex;
3232
import org.eclipse.jetty.http.HttpMethod;
33+
import org.eclipse.jetty.http.MimeTypes;
3334
import org.eclipse.jetty.util.MultiMap;
3435
import org.eclipse.jetty.util.StringUtil;
3536
import org.eclipse.jetty.util.UrlEncoded;
@@ -78,8 +79,6 @@ public class Util {
7879
public static final String FUNCTION_SELECTOR = "function_selector";
7980
public static final String FUNCTION_PARAMETER = "parameter";
8081
public static final String CALL_DATA = "data";
81-
public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";
82-
public static final String APPLICATION_JSON = "application/json";
8382

8483
public static String printTransactionFee(String transactionFee) {
8584
JSONObject jsonObject = new JSONObject();
@@ -525,7 +524,6 @@ public static byte[] getAddress(HttpServletRequest request) throws Exception {
525524

526525
private static String checkGetParam(HttpServletRequest request, String key) throws Exception {
527526
String method = request.getMethod();
528-
String value = null;
529527

530528
if (HttpMethod.GET.toString().toUpperCase().equalsIgnoreCase(method)) {
531529
return request.getParameter(key);
@@ -535,8 +533,10 @@ private static String checkGetParam(HttpServletRequest request, String key) thro
535533
if (StringUtils.isBlank(contentType)) {
536534
return null;
537535
}
538-
if (APPLICATION_JSON.toLowerCase().contains(contentType)) {
539-
value = getRequestValue(request);
536+
if (contentType.contains(MimeTypes.Type.FORM_ENCODED.asString())) {
537+
return request.getParameter(key);
538+
} else {
539+
String value = getRequestValue(request);
540540
if (StringUtils.isBlank(value)) {
541541
return null;
542542
}
@@ -545,13 +545,10 @@ private static String checkGetParam(HttpServletRequest request, String key) thro
545545
if (jsonObject != null) {
546546
return jsonObject.getString(key);
547547
}
548-
} else if (APPLICATION_FORM_URLENCODED.toLowerCase().contains(contentType)) {
549-
return request.getParameter(key);
550-
} else {
551548
return null;
552549
}
553550
}
554-
return value;
551+
return null;
555552
}
556553

557554
public static String getRequestValue(HttpServletRequest request) throws IOException {

framework/src/test/java/org/tron/core/services/http/GetBrokerageServletTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ public void getBrokerageValueByJsonTest() {
5252
}
5353
}
5454

55+
56+
@Test
57+
public void getBrokerageByJsonUTF8Test() {
58+
int expect = 20;
59+
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}";
60+
MockHttpServletRequest request = createRequest("application/json; charset=utf-8");
61+
request.setContent(jsonParam.getBytes());
62+
MockHttpServletResponse response = new MockHttpServletResponse();
63+
getBrokerageServlet.doPost(request, response);
64+
try {
65+
String contentAsString = response.getContentAsString();
66+
JSONObject result = JSONObject.parseObject(contentAsString);
67+
int brokerage = (int)result.get("brokerage");
68+
Assert.assertEquals(expect, brokerage);
69+
} catch (UnsupportedEncodingException e) {
70+
Assert.fail(e.getMessage());
71+
}
72+
}
73+
5574
@Test
5675
public void getBrokerageValueTest() {
5776
int expect = 20;

framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import java.io.UnsupportedEncodingException;
99
import javax.annotation.Resource;
1010

11+
import lombok.extern.slf4j.Slf4j;
1112
import org.apache.commons.lang3.StringUtils;
1213
import org.junit.After;
1314
import org.junit.Assert;
15+
import org.junit.Before;
1416
import org.junit.Test;
1517
import org.springframework.mock.web.MockHttpServletRequest;
1618
import org.springframework.mock.web.MockHttpServletResponse;
@@ -22,6 +24,7 @@
2224
import org.tron.core.service.MortgageService;
2325
import org.tron.core.store.DelegationStore;
2426

27+
@Slf4j
2528
public class GetRewardServletTest extends BaseTest {
2629

2730
@Resource
@@ -52,6 +55,7 @@ public MockHttpServletRequest createRequest(String contentType) {
5255
return request;
5356
}
5457

58+
@Before
5559
public void init() {
5660
manager.getDynamicPropertiesStore().saveChangeDelegation(1);
5761
byte[] sr = decodeFromBase58Check("27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh");
@@ -61,7 +65,6 @@ public void init() {
6165

6266
@Test
6367
public void getRewardValueByJsonTest() {
64-
init();
6568
int expect = 138181;
6669
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}";
6770
MockHttpServletRequest request = createRequest("application/json");
@@ -78,9 +81,26 @@ public void getRewardValueByJsonTest() {
7881
}
7982
}
8083

84+
@Test
85+
public void getRewardByJsonUTF8Test() {
86+
int expect = 138181;
87+
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}";
88+
MockHttpServletRequest request = createRequest("application/json; charset=utf-8");
89+
MockHttpServletResponse response = new MockHttpServletResponse();
90+
request.setContent(jsonParam.getBytes());
91+
try {
92+
getRewardServlet.doPost(request, response);
93+
String contentAsString = response.getContentAsString();
94+
JSONObject result = JSONObject.parseObject(contentAsString);
95+
int reward = (int)result.get("reward");
96+
Assert.assertEquals(expect, reward);
97+
} catch (UnsupportedEncodingException e) {
98+
Assert.fail(e.getMessage());
99+
}
100+
}
101+
81102
@Test
82103
public void getRewardValueTest() {
83-
init();
84104
int expect = 138181;
85105
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
86106
MockHttpServletResponse response = new MockHttpServletResponse();

0 commit comments

Comments
 (0)