ok2c commented on code in PR #651: URL: https://github.com/apache/httpcomponents-client/pull/651#discussion_r2156402395
########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java: ########## @@ -81,36 +80,34 @@ public final class ContentCompressionExec implements ExecChainHandler { private final Lookup<InputStreamFactory> decoderRegistry; private final boolean ignoreUnknown; + private static final Map<String, InputStreamFactory> DECODERS = ContentDecoderRegistry.getRegistry(); + public ContentCompressionExec( final List<String> acceptEncoding, final Lookup<InputStreamFactory> decoderRegistry, final boolean ignoreUnknown) { - final boolean brotliSupported = decoderRegistry == null && BrotliDecompressingEntity.isAvailable(); - if (acceptEncoding != null) { - this.acceptEncoding = MessageSupport.headerOfTokens(HttpHeaders.ACCEPT_ENCODING, acceptEncoding); - } else { - final List<String> encodings = new ArrayList<>(4); - encodings.add("gzip"); - encodings.add("x-gzip"); - encodings.add("deflate"); - if (brotliSupported) { - encodings.add("br"); - } - this.acceptEncoding = MessageSupport.headerOfTokens(HttpHeaders.ACCEPT_ENCODING, encodings); - } + final List<String> encodingsHeader = acceptEncoding != null + ? acceptEncoding + : buildDefaultAcceptEncoding(); + + this.acceptEncoding = MessageSupport + .headerOfTokens(HttpHeaders.ACCEPT_ENCODING, encodingsHeader); + if (decoderRegistry != null) { this.decoderRegistry = decoderRegistry; } else { - final RegistryBuilder<InputStreamFactory> builder = RegistryBuilder.<InputStreamFactory>create() - .register("gzip", GZIPInputStreamFactory.getInstance()) - .register("x-gzip", GZIPInputStreamFactory.getInstance()) - .register("deflate", DeflateInputStreamFactory.getInstance()); - if (brotliSupported) { - builder.register("br", BrotliInputStreamFactory.getInstance()); - } + final Map<String, InputStreamFactory> decoders = Review Comment: @arturobernalg The content of `encodingsHeader` list should be generated based on this map. This would also make `#buildDefaultAcceptEncoding` unnecessary. ########## httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/ContentCoding.java: ########## @@ -0,0 +1,122 @@ +/* + * ==================================================================== + * 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.entity.compress; + +import java.util.Locale; + +/** + * Enumeration of the canonical IANA content-coding tokens supported by HttpClient for + * HTTP request and response bodies. + * <p> + * Each constant corresponds to the standard token used in the {@code Content-Encoding} + * and {@code Accept-Encoding} headers. Some codings (e.g. Brotli, Zstandard, XZ/LZMA) + * may require additional helper libraries at runtime. + * + * @since 5.6 + */ +public enum ContentCoding { + + /** + * GZIP compression format. + */ + GZIP("gzip"), + /** + * "deflate" compression format (zlib or raw). + */ + DEFLATE("deflate"), + /** + * Legacy alias for GZIP. + */ + X_GZIP("x-gzip"), + + // Optional codecs requiring Commons-Compress or native helpers + /** + * Brotli compression format. + */ + BROTLI("br"), + /** + * Zstandard compression format. + */ + ZSTD("zstd"), + /** + * XZ compression format. + */ + XZ("xz"), + /** + * LZMA compression format. + */ + LZMA("lzma"), + /** + * Framed LZ4 compression format. + */ + LZ4_FRAMED("lz4-framed"), + /** + * Block LZ4 compression format. + */ + LZ4_BLOCK("lz4-block"), + /** + * BZIP2 compression format. + */ + BZIP2("bzip2"), + /** + * Pack200 compression format. + */ + PACK200("pack200"), + /** + * Deflate64 compression format. + */ + DEFLATE64("deflate64"); + + private final String token; + + ContentCoding(final String token) { + this.token = token; + } + + /** + * Returns the standard IANA token string for this content-coding. + * + * @return the lowercase token used in HTTP headers + */ + public String token() { + return token; + } + + /** + * Lookup the enum by token (case-insensitive). + */ + public static ContentCoding fromToken(final String token) { + final String copyToken = token.toLowerCase(Locale.ROOT); + for (final ContentCoding coding : values()) { Review Comment: @arturobernalg This method can be optimized by creating a static map ########## httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/ContentDecoderRegistry.java: ########## @@ -0,0 +1,141 @@ +/* + * ==================================================================== + * 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.entity.compress; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Locale; +import java.util.Map; + +import org.apache.hc.client5.http.entity.BrotliDecompressingEntity; +import org.apache.hc.client5.http.entity.BrotliInputStreamFactory; +import org.apache.hc.client5.http.entity.DeflateInputStreamFactory; +import org.apache.hc.client5.http.entity.GZIPInputStreamFactory; +import org.apache.hc.client5.http.entity.InputStreamFactory; +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.annotation.ThreadingBehavior; + +/** + * Immutable run-time catalogue of {@link InputStreamFactory} instances + * capable of <em>decoding</em> HTTP entity bodies. + * + * <p>The map is populated once during class initialisation:</p> + * <ol> + * <li>Built-ins: {@code gzip} and {@code deflate} are always present.</li> + * <li>If Commons-Compress is on the class-path we register a configurable + * list of codecs (br, zstd, xz, …) via + * {@link CommonsCompressDecoderFactory} – guarded by a cheap + * presence check.</li> + * <li>If Commons was absent or could not supply <code>br</code>, + * we fall back to the pure native singleton + * {@link BrotliInputStreamFactory} (when the <code>org.brotli</code> + * decoder JAR is available).</li> + * </ol> + * + * <p>The resulting {@code Map} is wrapped in + * {@link Collections#unmodifiableMap(Map)} and published through + * {@link #getRegistry()} for safe, lock-free concurrent reads.</p> + * + * @since 5.6 + */ +@Internal +@Contract(threading = ThreadingBehavior.STATELESS) +public final class ContentDecoderRegistry { + + private static final String CCSF = + "org.apache.commons.compress.compressors.CompressorStreamFactory"; + + + private static final Map<String, InputStreamFactory> REGISTRY = buildRegistry(); + + + /** + * Returns the unmodifiable codec map (key = canonical token, value = factory). + */ + public static Map<String, InputStreamFactory> getRegistry() { + return REGISTRY; + } + + + private static Map<String, InputStreamFactory> buildRegistry() { Review Comment: @arturobernalg Why not using `ContentCoding` as a key instead of plain `String`? -- 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