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

feat(http): optimize parameter parsing #5483

Merged

Conversation

forfreeday
Copy link
Contributor

What does this PR do?
optimize incorrect content-type comparisons.

Why are these changes required?
When a client requests an interface, if it is an application/json request, it may be written in a variety of ways, including:

  1. application/json
  2. application/json;charset=iso-8859-1
  3. application/json;charset=utf-8

When determining the content-type of a request, the interface needs to be compatible with the variable writing ways of the requests.

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details

@forfreeday forfreeday changed the base branch from develop to release_v4.7.3 September 13, 2023 05:31
@codecov-commenter
Copy link

codecov-commenter commented Sep 13, 2023

Codecov Report

Merging #5483 (54884dc) into release_v4.7.3 (326b782) will increase coverage by 0.04%.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@                 Coverage Diff                  @@
##             release_v4.7.3    #5483      +/-   ##
====================================================
+ Coverage             64.58%   64.62%   +0.04%     
  Complexity             9684     9684              
====================================================
  Files                   867      867              
  Lines                 51726    51724       -2     
  Branches               5755     5754       -1     
====================================================
+ Hits                  33407    33428      +21     
+ Misses                15714    15693      -21     
+ Partials               2605     2603       -2     
Files Changed Coverage Δ
...rc/main/java/org/tron/core/services/http/Util.java 46.05% <100.00%> (-0.03%) ⬇️

... and 8 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -535,7 +534,9 @@ private static String checkGetParam(HttpServletRequest request, String key) thro
if (StringUtils.isBlank(contentType)) {
return null;
}
if (APPLICATION_JSON.toLowerCase().contains(contentType)) {
if (contentType.contains(MimeTypes.Type.APPLICATION_JSON.asString())
Copy link
Contributor

@halibobo1205 halibobo1205 Sep 13, 2023

Choose a reason for hiding this comment

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

if contentType.contains(MimeTypes.Type.APPLICATION_JSON_UTF_8.asString()) == ture
then contentType.contains(MimeTypes.Type.APPLICATION_JSON.asString() must be true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The content-type will be true if the condition is met.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean the last two rulings are not necessary, if you insist on doing this, all the scenes need to be added

@halibobo1205
Copy link
Contributor

@forfreeday Please add test for validation.

@@ -545,7 +546,7 @@ private static String checkGetParam(HttpServletRequest request, String key) thro
if (jsonObject != null) {
return jsonObject.getString(key);
}
} else if (APPLICATION_FORM_URLENCODED.toLowerCase().contains(contentType)) {
} else if (contentType.contains(MimeTypes.Type.FORM_ENCODED.asString())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why reverse the contains()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The characteristics of String.contains() dictate that when using contains(), you need to pay attention to its their containment relationship; longer characters can contain shorter characters, and conversely, shorter characters cannot contain longer characters.

String str1 = "abc";
String str2 = "ab";
str1.contains(str2);

is valid, the reverse is not.
The content-type of the request could be any of the following:
application/json;
application/json;charset=utf-8;
application/json; charset=utf-8;
So you need to compare the constants with the content-type of the request.

Copy link
Contributor

@halibobo1205 halibobo1205 Sep 13, 2023

Choose a reason for hiding this comment

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

charset=gb2312;application/JSON is also legal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually contentType.contains(MimeTypes.Type.APPLICATION_JSON.asString() is compatible with all scenarios, more judgment seems redundant

@@ -78,8 +79,6 @@ public class Util {
public static final String FUNCTION_SELECTOR = "function_selector";
public static final String FUNCTION_PARAMETER = "parameter";
public static final String CALL_DATA = "data";
public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String APPLICATION_JSON = "application/json";

public static String printTransactionFee(String transactionFee) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Better add unit test

@@ -535,7 +534,9 @@ private static String checkGetParam(HttpServletRequest request, String key) thro
if (StringUtils.isBlank(contentType)) {
return null;
}
if (APPLICATION_JSON.toLowerCase().contains(contentType)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the contentType to lower case too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

defaults to lowercase.

@halibobo1205 halibobo1205 added area:http topic: gRPC/HTTP api rpc/http related issue type: Enhancement minor enhancement labels Sep 14, 2023
@halibobo1205 halibobo1205 changed the title feat(Util): optimize util feat(http): optimize parameter parsing Sep 14, 2023
@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.7.3 milestone Sep 14, 2023
@halibobo1205 halibobo1205 merged commit b81a63e into tronprotocol:release_v4.7.3 Sep 14, 2023
5 checks passed
@@ -535,8 +533,10 @@ private static String checkGetParam(HttpServletRequest request, String key) thro
if (StringUtils.isBlank(contentType)) {
return null;
}
if (APPLICATION_JSON.toLowerCase().contains(contentType)) {
value = getRequestValue(request);
if (contentType.contains(MimeTypes.Type.FORM_ENCODED.asString())) {
Copy link
Contributor

@lurais lurais Oct 9, 2023

Choose a reason for hiding this comment

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

Shall we add some explanation about this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:http topic: gRPC/HTTP api rpc/http related issue type: Enhancement minor enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants