serkanturgut commented on a change in pull request #153: HTTPCLIENT-1992: 
Impossible to access trailer-headers 
URL: 
https://github.com/apache/httpcomponents-client/pull/153#discussion_r293127437
 
 

 ##########
 File path: 
httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ChunkAwareResponseEntityProxy.java
 ##########
 @@ -0,0 +1,71 @@
+/*
+ * ====================================================================
+ * 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.classic;
+
+import org.apache.hc.client5.http.classic.ExecRuntime;
+import org.apache.hc.core5.function.Supplier;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.impl.io.ChunkedInputStream;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+class ChunkAwareResponseEntityProxy extends ResponseEntityProxy {
+
+    private ChunkedInputStream internalChunkedInputStream;
+
+    public static void enhanceChunkedResponseSupport(final ClassicHttpResponse 
response, final ExecRuntime execRuntime) throws IOException {
+        final HttpEntity entity = response.getEntity();
+        if (!(entity.getContent() instanceof ChunkedInputStream)) {
+            throw new IllegalStateException("Underlying stream is not 
ChunkedInputStream, unable to reach footers.");
+        }
+        final ChunkedInputStream content = (ChunkedInputStream) 
entity.getContent();
+        if (entity != null && entity.isStreaming() && execRuntime != null) {
+            response.setEntity(new ChunkAwareResponseEntityProxy(entity, 
execRuntime, content));
+        }
+    }
+
+    ChunkAwareResponseEntityProxy(final HttpEntity entity, final ExecRuntime 
execRuntime, final ChunkedInputStream chunkedInputStream) {
+        super(entity, execRuntime);
+        this.internalChunkedInputStream = chunkedInputStream;
+    }
+
+    @Override
+    public Supplier<List<? extends Header>> getTrailers() {
 
 Review comment:
   getTrailers() return a supplier which reaches to 
ChunkedInputStream.getFooters(). So getTrailers() in 
ChunkAwareResponseEntityProxy is aware of the underlying stream is 
ChunkedInputStream. 
   
   If we embed getTrailers() into ResponseEntityProxy, we have to check whether 
the underlying stream is ChunkedInputStream (ChunkAwareResponseEntityProxy is 
doing this check during initialization) if it is not a IllegalStateException 
should be thrown there to notify caller. However this approach contradicts with 
throw-early-catch-late practice. Given that getTrailers() (or Supplier.get()) 
typically will be called at the end of HTTP communication by the user of 
client, It would be a unpleasant surprise to caller to see an exception 
   
   Another reason is that I wanted to leave ResponseEntityProxy stream agnostic 
as it is currently. Placing new getTrailers() implementation in 
ResponseEntityProxy brings certain responsibility to ResponseEntityProxy to 
recognize the type of underlying stream to some extent. 
   
   If you think this is still worth it, I could give it a try to eliminate it. 
I am open to suggestions, please let me know what you think.
   
   Thank you

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to