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

fix(http): fix java.lang.IllegalStateException: STREAMED #5367

Merged
merged 13 commits into from
Aug 18, 2023

Conversation

forfreeday
Copy link
Contributor

What does this PR do?

  1. Optimized Util getAddress method
  2. add GetBrokerageServletTest test case
  3. add GetRewardServletTest test case

Why are these changes required?
Fix incorrect interface returns, add test cases

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details

1. Optimized Util getAddress method
2. add `GetBrokerageServletTest` test case
3. add `GetRewardServletTest` test case
@forfreeday forfreeday changed the title feat(Servlet): Optimize Http Servlet feat(Servlet): optimize the http request interface Jul 20, 2023
@lvs007 lvs007 requested review from xxo1shine and halibobo1205 July 21, 2023 09:09
@@ -499,6 +500,12 @@ public static byte[] getAddress(HttpServletRequest request) throws Exception {
byte[] address = null;
String addressParam = "address";
String addressStr = request.getParameter(addressParam);

if (APPLICATION_FORM_URLENCODED.equals(request.getContentType())
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not clear. Is this the only scene?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is recommended to make a clear distinction between GET, POST (application/json, application/x-www-form-urlencoded)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no need to add other types of content types, and these types are supported even if other types of judgments are not explicitly added.

@forfreeday
Copy link
Contributor Author

@halibobo1205

With the addition of parameter checks, if the uploaded parameter is an illegal parameter, an error will be returned.

request:

curl --location 'http://127.0.0.1:8090/wallet/getReward' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'address='

expected Result:

{
    "Error": "INVALID address, invalid request parameter"
}

Otherwise, normal parameter requests for JSON, GET, and POST are not affected.

1. add parameter checking methods
if (HttpMethod.GET.toString().toUpperCase() .equalsIgnoreCase(method)) {
value = request.getParameter(key);
if (StringUtils.isBlank(value)) {
throw new IllegalArgumentException("Invalid request parameter");
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to keep compatibility

}
if (keyValue[0].equals("address")) {
return keyValue[1];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to keep compatibility

@halibobo1205
Copy link
Contributor

@forfreeday Is there any progress on this PR?

@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.7.3 milestone Aug 9, 2023
@forfreeday
Copy link
Contributor Author

@halibobo1205
Adjusted the level of interface validation, keeping the original checksums

return jsonObject.getString(key);
}
} else if (APPLICATION_FORM_URLENCODED.toLowerCase().contains(contentType)) {
return getParam(getRequestValue(request));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just use request.getParameter(addressParam)?

if (keyValue.length == 1) {
continue;
}
if (keyValue[0].equals("address")) {
Copy link
Contributor

@halibobo1205 halibobo1205 Aug 9, 2023

Choose a reason for hiding this comment

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

BTW, Why is it limited to address ? getParam seems to be a generic method.

@forfreeday
Copy link
Contributor Author

forfreeday commented Aug 9, 2023

@halibobo1205
getParam doesn't need to be a generic method, it's just that the name of the method looks generic, and the only correct entry for this method is address, while all the others are incorrect.

@halibobo1205
Copy link
Contributor

@halibobo1205 getParam doesn't need to be a generic method, it's just that the name of the method looks generic, and the only correct entry for this method is address, while all the others are incorrect.

@forfreeday Why does getParam need an input?

@forfreeday
Copy link
Contributor Author

forfreeday commented Aug 9, 2023

@halibobo1205
Do you understand what this method does?
getParam is the input parameter of the POST method.
What would happen if you didn't use getParam?

@halibobo1205
Copy link
Contributor

@halibobo1205 Do you understand what this method does? getParam is the input parameter of the POST method. What would happen if you didn't use getParam?

@forfreeday I really don't get it. @tomatoishealthy Please take a look.

Copy link
Contributor

Choose a reason for hiding this comment

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

For historical reasons, this logic does not have a unit test.

Better add comprehensive unit tests for all situations after changing the whole logic of the param extraction to make sure all the new logic works well, such as: good requests for both form&json

@codecov-commenter
Copy link

codecov-commenter commented Aug 9, 2023

Codecov Report

Merging #5367 (1d3036a) into develop (83151aa) will increase coverage by 0.00%.
The diff coverage is 64.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##             develop    #5367   +/-   ##
==========================================
  Coverage      60.95%   60.96%           
- Complexity      9247     9253    +6     
==========================================
  Files            840      840           
  Lines          50064    50081   +17     
  Branches        5578     5584    +6     
==========================================
+ Hits           30517    30530   +13     
  Misses         17146    17146           
- Partials        2401     2405    +4     
Files Changed Coverage Δ
...rc/main/java/org/tron/core/services/http/Util.java 40.65% <64.00%> (+2.11%) ⬆️

... and 8 files with indirect coverage changes

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

String content = (String) result.get("Error");
Assert.assertNull(content);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Copy link
Contributor

Choose a reason for hiding this comment

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

Assert.fail(); is a good choice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not expecting an exception to be thrown; the end result of this use case is a normal return of the

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it mean that even if an UnsupportedEncodingException is thrown, it's still normal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using a statement when pointing out a problem is a good way to respond; if you use a rhetorical question, I can't be sure of your intentions and it doesn't help to accurately describe the problem.

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 point of this use case is not to test for the UnsupportedEncodingException, but rather to determine the value of null.

Copy link
Contributor

Choose a reason for hiding this comment

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

Assert.fail() is added where it is not possible to reach in the expectation, once it is reached, indicates that the test has failed, the result is not as expected.

String content = (String) result.get("Error");
Assert.assertNull(content);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Copy link
Contributor

Choose a reason for hiding this comment

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

Please test all cases of checkGetParam.

Copy link
Contributor Author

@forfreeday forfreeday Aug 14, 2023

Choose a reason for hiding this comment

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

checkGetParam is business-independent and does not need to be validated.

addressStr = jsonObject.getString(addressParam);
}
}
String addressStr = checkGetParam(request, addressParam);
Copy link
Contributor

Choose a reason for hiding this comment

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

getParam or checkAndGetParam ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use checkGetParam as the method name, because there are some checking judgments

}

@After
public void deleteDatabase() {
Copy link
Contributor

Choose a reason for hiding this comment

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

BaseTest has already done the release.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it can be dispensed with here

Copy link
Contributor

@halibobo1205 halibobo1205 left a comment

Choose a reason for hiding this comment

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

LGTM

@halibobo1205 halibobo1205 merged commit f18864c into tronprotocol:develop Aug 18, 2023
@halibobo1205 halibobo1205 changed the title feat(Servlet): optimize the http request interface fix(http): fix java.lang.IllegalStateException: STREAMED Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

java.lang.IllegalStateException: STREAMED: (GetRewardServlet.java:36)
4 participants