Skip to content

Comments

EntityUtils clean ups and internal refactoring#161

Merged
garydgregory merged 5 commits intoapache:masterfrom
garydgregory:EnityUtils_clean_ups
Nov 18, 2019
Merged

EntityUtils clean ups and internal refactoring#161
garydgregory merged 5 commits intoapache:masterfrom
garydgregory:EnityUtils_clean_ups

Conversation

@garydgregory
Copy link
Member

EntityUtils clean ups and internal refactoring. If this is OK, I'll do something similar for 4.5.x if needed.

@michael-o
Copy link
Member

I will have a look at this tomorrow.

}

private static int getCheckedContentLength(final HttpEntity entity) {
final int contentLength = (int) Args.checkContentLength(entity);
Copy link
Member

Choose a reason for hiding this comment

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

@garydgregory I am not a very big fan of shared code at all cost. I would very much prefer to keep Args.checkContentLength and similar argument checks at the very beginning of each method, and not hidden somewhere deep inside a static method. Otherwise looks good to me.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, they should be on public methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ok2c I moved the checks to the start of methods.

@michael-o I did not add public methods to keep the API surface minimal.

Copy link
Member

Choose a reason for hiding this comment

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

@garydgregory What I actually mean was that Args#checkContentLength should be at the beginning of the method. getCheckedContentLength method could be wherever it needs to be. Please decouple Args#checkContentLength and getCheckedContentLength. They are unrelated.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ok2c Sorry for the misunderstanding, I just committed changes for this request.

}

private static int getCheckedContentLength(final HttpEntity entity) {
final int contentLength = (int) Args.checkContentLength(entity);
Copy link
Member

Choose a reason for hiding this comment

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

I agree, they should be on public methods.

}
final ByteArrayBuffer buffer = new ByteArrayBuffer(i);
final byte[] tmp = new byte[4096];
final ByteArrayBuffer buffer = new ByteArrayBuffer(getCheckedContentLength(entity));
Copy link
Member

Choose a reason for hiding this comment

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

What if Content-Length is 500_000_000? Will you really create a buffer with that size?

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-o
That's what the currently release code does; please see https://github.com/apache/httpcomponents-core/blob/rel/v4.4.12/httpcore/src/main/java/org/apache/http/util/EntityUtils.java#L135

May we address this separately please?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we should. It requires at least warning/documentation.

Copy link
Member

Choose a reason for hiding this comment

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

@michael-o Anyone in their mind should never be using EntityUtils#toString in the first place. I do not think there is anything to be done here other than saying that one ought not use this method in production.

Copy link
Member

Choose a reason for hiding this comment

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

@ok2c That would be perfectly fine. Nothing can be improved here codewise.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would it then be appropriate to have a method that takes a max result size argument?

Copy link
Member

Choose a reason for hiding this comment

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

As an overload, likely. How do you intend the fail if content length is greater or chunked transfer has reached that limit?

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-o You get what you asked for, a String of length N. The call site can decide what to do if that length is less than the content length or some other length specifier.

Copy link
Member

Choose a reason for hiding this comment

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

@garydgregory I propose to commit this change-set and raise another PR for the new method.

Copy link
Member Author

@garydgregory garydgregory Nov 18, 2019

Choose a reason for hiding this comment

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

@ok2c +1

*/
public final class EntityUtils {

private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder why are still have this. RFC 7230 and friends to not define any default encoding any more.

final Charset charset) throws IOException {
final Charset actualCharset = charset == null ? DEFAULT_CHARSET : charset;
final CharArrayBuffer buf = new CharArrayBuffer(
contentLength > 0 ? (int) contentLength : DEFAULT_CHAR_BUFFER_SIZE);
Copy link
Member

Choose a reason for hiding this comment

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

Same here for the length...

public static byte[] toByteArray(final HttpEntity entity) throws IOException {
Args.notNull(entity, "Entity");
int contentLength = (int) Args.checkContentLength(entity);
contentLength = toContentLength(contentLength);
Copy link
Member

Choose a reason for hiding this comment

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

@garydgregory Do you mind folding these two one in order to make contentLength final? final int contentLength = toContentLength((int) Args.checkContentLength(entity)) and then everything is going to be fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ok2c Done!

@garydgregory garydgregory merged commit a50d790 into apache:master Nov 18, 2019
asfgit pushed a commit that referenced this pull request Nov 19, 2019
* Refactor and reuse magic numbers into constants.
* Refactor some common code into private methods.
@garydgregory garydgregory deleted the EnityUtils_clean_ups branch November 19, 2019 14:22
garydgregory added a commit that referenced this pull request Nov 21, 2019
* EntityUtils clean ups and internal refactoring (#161)

* - Refactor and reuse magic numbers into constants.
- Javadoc minor edits.

* Refactor some common code into private methods.

* Move calls to getCheckedContentLength(entity) to the start of methods
per Oleg's PR review.

* Refactor per Oleg's review of PR #161.

* In-line result of Args.checkContentLength().

* Add and use max result length parameter version of APIs in EntityUtils.

All previous methods now delegate to the new methods and use a default
length constant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants