1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.classic;
29
30 import java.io.IOException;
31 import java.util.List;
32 import java.util.Locale;
33
34 import org.apache.hc.client5.http.classic.ExecChain;
35 import org.apache.hc.client5.http.classic.ExecChainHandler;
36 import org.apache.hc.client5.http.config.RequestConfig;
37 import org.apache.hc.client5.http.entity.BrotliDecompressingEntity;
38 import org.apache.hc.client5.http.entity.BrotliInputStreamFactory;
39 import org.apache.hc.client5.http.entity.DecompressingEntity;
40 import org.apache.hc.client5.http.entity.DeflateInputStreamFactory;
41 import org.apache.hc.client5.http.entity.GZIPInputStreamFactory;
42 import org.apache.hc.client5.http.entity.InputStreamFactory;
43 import org.apache.hc.client5.http.protocol.HttpClientContext;
44 import org.apache.hc.core5.annotation.Contract;
45 import org.apache.hc.core5.annotation.Internal;
46 import org.apache.hc.core5.annotation.ThreadingBehavior;
47 import org.apache.hc.core5.http.ClassicHttpRequest;
48 import org.apache.hc.core5.http.ClassicHttpResponse;
49 import org.apache.hc.core5.http.Header;
50 import org.apache.hc.core5.http.HeaderElement;
51 import org.apache.hc.core5.http.HttpEntity;
52 import org.apache.hc.core5.http.HttpException;
53 import org.apache.hc.core5.http.HttpHeaders;
54 import org.apache.hc.core5.http.config.Lookup;
55 import org.apache.hc.core5.http.config.RegistryBuilder;
56 import org.apache.hc.core5.http.message.BasicHeaderValueParser;
57 import org.apache.hc.core5.http.message.MessageSupport;
58 import org.apache.hc.core5.http.message.ParserCursor;
59 import org.apache.hc.core5.util.Args;
60
61
62
63
64
65
66
67
68
69
70
71
72 @Contract(threading = ThreadingBehavior.STATELESS)
73 @Internal
74 public final class ContentCompressionExec implements ExecChainHandler {
75
76 private final Header acceptEncoding;
77 private final Lookup<InputStreamFactory> decoderRegistry;
78 private final boolean ignoreUnknown;
79
80
81
82
83 private static final String[] EMPTY_STRING_ARRAY = {};
84
85 public ContentCompressionExec(
86 final List<String> acceptEncoding,
87 final Lookup<InputStreamFactory> decoderRegistry,
88 final boolean ignoreUnknown) {
89
90 final boolean brotliSupported = BrotliDecompressingEntity.isAvailable();
91 final String[] encoding;
92 if (brotliSupported) {
93 encoding = new String[] {"gzip", "x-gzip", "deflate", "br"};
94 } else {
95 encoding = new String[] {"gzip", "x-gzip", "deflate"};
96 }
97 this.acceptEncoding = MessageSupport.format(HttpHeaders.ACCEPT_ENCODING,
98 acceptEncoding != null ? acceptEncoding.toArray(
99 EMPTY_STRING_ARRAY) : encoding);
100
101 if (decoderRegistry != null) {
102 this.decoderRegistry = decoderRegistry;
103 } else {
104 final RegistryBuilder<InputStreamFactory> builder = RegistryBuilder.<InputStreamFactory>create()
105 .register("gzip", GZIPInputStreamFactory.getInstance())
106 .register("x-gzip", GZIPInputStreamFactory.getInstance())
107 .register("deflate", DeflateInputStreamFactory.getInstance());
108 if (brotliSupported) {
109 builder.register("br", BrotliInputStreamFactory.getInstance());
110 }
111 this.decoderRegistry = builder.build();
112 }
113
114
115 this.ignoreUnknown = ignoreUnknown;
116 }
117
118 public ContentCompressionExec(final boolean ignoreUnknown) {
119 this(null, null, ignoreUnknown);
120 }
121
122
123
124
125
126
127
128
129
130
131 public ContentCompressionExec() {
132 this(null, null, true);
133 }
134
135
136 @Override
137 public ClassicHttpResponse execute(
138 final ClassicHttpRequest request,
139 final ExecChain.Scope scope,
140 final ExecChain chain) throws IOException, HttpException {
141 Args.notNull(request, "HTTP request");
142 Args.notNull(scope, "Scope");
143
144 final HttpClientContext clientContext = scope.clientContext;
145 final RequestConfig requestConfig = clientContext.getRequestConfig();
146
147
148 if (!request.containsHeader(HttpHeaders.ACCEPT_ENCODING) && requestConfig.isContentCompressionEnabled()) {
149 request.addHeader(acceptEncoding);
150 }
151
152 final ClassicHttpResponse response = chain.proceed(request, scope);
153
154 final HttpEntity entity = response.getEntity();
155
156
157 if (requestConfig.isContentCompressionEnabled() && entity != null && entity.getContentLength() != 0) {
158 final String contentEncoding = entity.getContentEncoding();
159 if (contentEncoding != null) {
160 final ParserCursor cursor = new ParserCursor(0, contentEncoding.length());
161 final HeaderElement[] codecs = BasicHeaderValueParser.INSTANCE.parseElements(contentEncoding, cursor);
162 for (final HeaderElement codec : codecs) {
163 final String codecname = codec.getName().toLowerCase(Locale.ROOT);
164 final InputStreamFactory decoderFactory = decoderRegistry.lookup(codecname);
165 if (decoderFactory != null) {
166 response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory));
167 response.removeHeaders(HttpHeaders.CONTENT_LENGTH);
168 response.removeHeaders(HttpHeaders.CONTENT_ENCODING);
169 response.removeHeaders(HttpHeaders.CONTENT_MD5);
170 } else {
171 if (!"identity".equals(codecname) && !ignoreUnknown) {
172 throw new HttpException("Unsupported Content-Encoding: " + codec.getName());
173 }
174 }
175 }
176 }
177 }
178 return response;
179 }
180
181 }