EntityUtils clean ups and internal refactoring#161
EntityUtils clean ups and internal refactoring#161garydgregory merged 5 commits intoapache:masterfrom
Conversation
|
I will have a look at this tomorrow. |
| } | ||
|
|
||
| private static int getCheckedContentLength(final HttpEntity entity) { | ||
| final int contentLength = (int) Args.checkContentLength(entity); |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
I agree, they should be on public methods.
There was a problem hiding this comment.
@ok2c I moved the checks to the start of methods.
@michael-o I did not add public methods to keep the API surface minimal.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
@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); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
What if Content-Length is 500_000_000? Will you really create a buffer with that size?
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
Yes, we should. It requires at least warning/documentation.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
@ok2c That would be perfectly fine. Nothing can be improved here codewise.
There was a problem hiding this comment.
Would it then be appropriate to have a method that takes a max result size argument?
There was a problem hiding this comment.
As an overload, likely. How do you intend the fail if content length is greater or chunked transfer has reached that limit?
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
@garydgregory I propose to commit this change-set and raise another PR for the new method.
| */ | ||
| public final class EntityUtils { | ||
|
|
||
| private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1; |
There was a problem hiding this comment.
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); |
| public static byte[] toByteArray(final HttpEntity entity) throws IOException { | ||
| Args.notNull(entity, "Entity"); | ||
| int contentLength = (int) Args.checkContentLength(entity); | ||
| contentLength = toContentLength(contentLength); |
There was a problem hiding this comment.
@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.
* Refactor and reuse magic numbers into constants. * Refactor some common code into private methods.
* 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.
EntityUtils clean ups and internal refactoring. If this is OK, I'll do something similar for 4.5.x if needed.