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


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/ContentCompressionAsyncExec.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.impl.async;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.function.UnaryOperator;
+
+import org.apache.hc.client5.http.async.AsyncExecCallback;
+import org.apache.hc.client5.http.async.AsyncExecChain;
+import org.apache.hc.client5.http.async.AsyncExecChainHandler;
+import org.apache.hc.client5.http.async.methods.InflatingAsyncDataConsumer;
+import org.apache.hc.client5.http.entity.compress.ContentCoding;
+import org.apache.hc.client5.http.protocol.HttpClientContext;
+import org.apache.hc.core5.annotation.Contract;
+import org.apache.hc.core5.annotation.Internal;
+import org.apache.hc.core5.annotation.ThreadingBehavior;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HeaderElement;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.config.Lookup;
+import org.apache.hc.core5.http.config.RegistryBuilder;
+import org.apache.hc.core5.http.message.BasicHeaderValueParser;
+import org.apache.hc.core5.http.message.MessageSupport;
+import org.apache.hc.core5.http.message.ParserCursor;
+import org.apache.hc.core5.http.nio.AsyncDataConsumer;
+import org.apache.hc.core5.http.nio.AsyncEntityProducer;
+import org.apache.hc.core5.util.Args;
+
+@Contract(threading = ThreadingBehavior.STATELESS)
+@Internal
+public final class ContentCompressionAsyncExec implements 
AsyncExecChainHandler {
+
+    private final Lookup<UnaryOperator<AsyncDataConsumer>> decoders;
+    private final Header acceptEncoding;
+    private final boolean ignoreUnknown;
+
+    public ContentCompressionAsyncExec(
+            final LinkedHashMap<String, UnaryOperator<AsyncDataConsumer>> 
decoderMap,
+            final boolean ignoreUnknown) {
+
+        Args.notEmpty(decoderMap, "Decoder map");
+
+        this.acceptEncoding = MessageSupport.headerOfTokens(
+                HttpHeaders.ACCEPT_ENCODING, new 
ArrayList<>(decoderMap.keySet()));
+
+        final RegistryBuilder<UnaryOperator<AsyncDataConsumer>> rb = 
RegistryBuilder.create();
+        decoderMap.forEach(rb::register);
+        this.decoders = rb.build();
+        this.ignoreUnknown = ignoreUnknown;
+    }
+
+    /**
+     * default = DEFLATE only
+     */
+    public ContentCompressionAsyncExec() {
+        final LinkedHashMap<String, UnaryOperator<AsyncDataConsumer>> map = 
new LinkedHashMap<>();
+        map.put(ContentCoding.DEFLATE.token(),
+                d -> new InflatingAsyncDataConsumer(d, null));
+        this.acceptEncoding = MessageSupport.headerOfTokens(
+                HttpHeaders.ACCEPT_ENCODING, 
Collections.singletonList(ContentCoding.DEFLATE.token()));
+        this.decoders = 
RegistryBuilder.<UnaryOperator<AsyncDataConsumer>>create()
+                .register(ContentCoding.DEFLATE.token(), 
map.get(ContentCoding.DEFLATE.token()))
+                .build();
+        this.ignoreUnknown = true;
+    }
+
+
+    @Override
+    public void execute(final HttpRequest request,
+                        final AsyncEntityProducer producer,
+                        final AsyncExecChain.Scope scope,
+                        final AsyncExecChain chain,
+                        final AsyncExecCallback cb) throws IOException, 
HttpException {
+
+        final HttpClientContext ctx = scope != null ? scope.clientContext : 
HttpClientContext.create();
+        final boolean enabled = 
ctx.getRequestConfigOrDefault().isContentCompressionEnabled();
+
+        if (enabled && !request.containsHeader(HttpHeaders.ACCEPT_ENCODING)) {
+            request.addHeader(acceptEncoding);
+        }
+
+        chain.proceed(request, producer, scope, new AsyncExecCallback() {
+
+            @Override
+            public AsyncDataConsumer handleResponse(final HttpResponse rsp, 
final EntityDetails details)
+                    throws HttpException, IOException {
+
+                if (!enabled) {
+                    return cb.handleResponse(rsp, details);
+                }
+
+                /* header lookup – unchanged from previous version */
+                String raw = details != null ? details.getContentEncoding() : 
null;
+                if (raw == null) {
+                    final Header h = 
rsp.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
+                    if (h != null) raw = h.getValue();
+                }
+
+                AsyncDataConsumer downstream = cb.handleResponse(rsp, details);

Review Comment:
   On more time  @ok2c 



-- 
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: dev-unsubscr...@hc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org
For additional commands, e-mail: dev-h...@hc.apache.org

Reply via email to