ok2c commented on code in PR #846:
URL:
https://github.com/apache/httpcomponents-client/pull/846#discussion_r3588936082
##########
httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java:
##########
@@ -138,13 +173,62 @@ public String generateKey(final URI requestUri) {
*/
public String generateKey(final HttpHost host, final HttpRequest request) {
final String s = CacheSupport.requestUriRaw(host, request);
+ final String hash = Method.QUERY.isSame(request.getMethod()) ?
getBodyHash(request) : null;
try {
- return generateKey(new URI(s));
+ return generateKey(new URI(s), hash);
} catch (final URISyntaxException ex) {
return s;
}
}
+ protected byte[] getRequestBodyBytes(final HttpRequest request) {
+ if (bodyBytesExtractor != null) {
+ final byte[] bytes = bodyBytesExtractor.apply(request);
+ if (bytes != null) {
+ return bytes;
+ }
+ }
+ if (request instanceof SimpleHttpRequest) {
+ return ((SimpleHttpRequest) request).getBodyBytes();
+ }
+ if (request instanceof ClassicHttpRequest) {
+ final HttpEntity entity = ((ClassicHttpRequest)
request).getEntity();
+ if (entity != null) {
+ try {
+ if (entity.isRepeatable()) {
+ final byte[] bytes = EntityUtils.toByteArray(entity);
+ ((ClassicHttpRequest) request).setEntity(new
ByteArrayEntity(bytes, ContentType.parse(entity.getContentType())));
+ return bytes;
+ }
+ } catch (final Exception ignore) {
+ }
+ }
+ }
+ return null;
Review Comment:
@desiderantes I apologize it took me to long to get back to you.
Your approach is not unreasonable but it can still lead to cache entries
with partial / broken keys, if the content body they are enclosing cannot be
extracted. The key generator _must_ always produce a valid key. This method
_must_ not return null.
What we need to do instead is to ensure those requests that cannot be
correctly represented as `SimpleHttpRequest` get routed along the execution
chain bypassing the caching layer.
Please as the first step review my pull request #850
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]