arturobernalg commented on code in PR #881:
URL: 
https://github.com/apache/httpcomponents-client/pull/881#discussion_r3894196451


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/ContentCompressionAsyncExec.java:
##########
@@ -188,6 +309,167 @@ public void failed(final Exception ex) {
         });
     }
 
+    private static List<String> createDictionaryAcceptTokens(
+            final CompressionDictionaryStore compressionDictionaryStore) {
+        final List<String> tokens = new ArrayList<>();
+        if (compressionDictionaryStore != null && Brotli4jRuntime.available()) 
{
+            tokens.add(ContentCoding.DCB.token());
+        }
+        if (compressionDictionaryStore != null && ZstdRuntime.available()) {
+            tokens.add(ContentCoding.DCZ.token());
+        }
+        return tokens;
+    }
+
+    private List<String> selectDictionaryAcceptTokens(
+            final CompressionDictionary dictionary) {
+        if (dictionary == null
+                || !dictionaryAcceptTokens.contains(ContentCoding.DCZ.token())
+                || isDczDictionaryCompatible(dictionary)) {
+            return dictionaryAcceptTokens;
+        }
+        final List<String> tokens = new ArrayList<>(dictionaryAcceptTokens);
+        tokens.remove(ContentCoding.DCZ.token());
+        return tokens;
+    }
+
+    private static boolean isDczDictionaryCompatible(
+            final CompressionDictionary dictionary) {
+        return dictionary.getContentLength() >= 8
+                && !dictionary.contentStartsWith(
+                        (byte) 0x37, (byte) 0xa4, (byte) 0x30, (byte) 0xec);
+    }
+
+    private CompressionDictionary prepareDictionaryNegotiation(
+            final HttpRequest request,
+            final boolean enabled,
+            final CompressionDictionary candidate,
+            final List<String> requestDictionaryAcceptTokens) {
+        if (!enabled) {
+            return null;
+        }
+
+        // A caller-provided Accept-Encoding is honoured verbatim: the client 
neither rewrites it
+        // nor negotiates a dictionary on top of an explicit choice.
+        if (request.containsHeader(HttpHeaders.ACCEPT_ENCODING)) {
+            return null;
+        }
+
+        if (candidate == null || requestDictionaryAcceptTokens.isEmpty()) {
+            request.addHeader(MessageSupport.headerOfTokens(
+                    HttpHeaders.ACCEPT_ENCODING, acceptTokens));
+            return null;
+        }
+
+        final List<String> tokens = new ArrayList<>(
+                acceptTokens.size() + requestDictionaryAcceptTokens.size());
+        tokens.addAll(acceptTokens);
+        tokens.addAll(requestDictionaryAcceptTokens);
+        request.addHeader(MessageSupport.headerOfTokens(
+                HttpHeaders.ACCEPT_ENCODING, tokens));
+
+        addDictionaryHeaders(request, candidate);
+        return candidate;
+    }
+
+    private static void addDictionaryHeaders(
+            final HttpRequest request,
+            final CompressionDictionary dictionary) {
+        request.addHeader(
+                CompressionDictionaryHeaderSupport.AVAILABLE_DICTIONARY,
+                CompressionDictionaryHeaderSupport.formatAvailableDictionary(
+                        dictionary.getSha256()));
+        if (!dictionary.getId().isEmpty()) {
+            request.addHeader(
+                    CompressionDictionaryHeaderSupport.DICTIONARY_ID,
+                    CompressionDictionaryHeaderSupport.formatDictionaryId(
+                            dictionary.getId()));
+        }
+    }
+
+    private CompressionDictionary findDictionary(
+            final HttpRequest request,
+            final URI requestUri,
+            final CookieStore privacyPartition) {
+        if (compressionDictionaryStore == null
+                || compressionDictionaryMatcher == null
+                || privacyPartition == null
+                || requestUri == null
+                || !URIScheme.HTTPS.same(requestUri.getScheme())
+                || 
request.containsHeader(CompressionDictionaryHeaderSupport.AVAILABLE_DICTIONARY)
+                || 
request.containsHeader(CompressionDictionaryHeaderSupport.DICTIONARY_ID)) {
+            return null;
+        }
+
+        return compressionDictionaryMatcher.match(
+                requestUri,
+                null,
+                compressionDictionaryStore.getByOrigin(privacyPartition, 
requestUri));
+    }
+
+    private UseAsDictionary parseUseAsDictionary(
+            final HttpResponse response,
+            final URI requestUri,
+            final CookieStore privacyPartition) {
+        if (compressionDictionaryStore == null
+                || privacyPartition == null
+                || requestUri == null
+                || !URIScheme.HTTPS.same(requestUri.getScheme())) {
+            return null;
+        }
+
+        if 
(!response.containsHeader(CompressionDictionaryHeaderSupport.USE_AS_DICTIONARY))
 {

Review Comment:
   @ok2c  please do another pass



-- 
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]

Reply via email to