elsloo closed pull request #1981: TR: Added 'Accept-Encoding: gzip' headers to
outgoing HTTP requests
URL: https://github.com/apache/incubator-trafficcontrol/pull/1981
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java
b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java
index 65d77f77e..772c00c39 100644
---
a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java
+++
b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/Fetcher.java
@@ -27,6 +27,7 @@
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
+import java.util.zip.GZIPInputStream;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
@@ -45,6 +46,7 @@
protected static final String POST_STR = "POST";
protected static final String UTF8_STR = "UTF-8";
protected static final int DEFAULT_TIMEOUT = 10000;
+ private static final String GZIP_ENCODING_STRING = "gzip";
protected int timeout = DEFAULT_TIMEOUT; // override if you want
something different
protected final Map<String, String> requestProps = new HashMap<String,
String>();
@@ -102,6 +104,7 @@ public boolean verify(final String arg0, final SSLSession
arg1) {
http.setInstanceFollowRedirects(false);
http.setRequestMethod(method);
http.setAllowUserInteraction(true);
+ http.addRequestProperty("Accept-Encoding",
GZIP_ENCODING_STRING);
for (final String key : requestProps.keySet()) {
http.addRequestProperty(key, requestProps.get(key));
@@ -143,14 +146,7 @@ private String fetchIfModifiedSince(final String url,
final String data, final S
}
final StringBuilder sb = new StringBuilder();
-
- try (final BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()))) {
- String input;
-
- while ((input = in.readLine()) != null) {
- sb.append(input);
- }
- }
+ createStringBuilderFromResponse(sb, connection);
return sb.toString();
} finally {
@@ -173,13 +169,7 @@ public int getIfModifiedSince(final String url, final long
lastFetchTime, final
return status;
}
- try (final BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()))) {
- String input;
-
- while ((input = in.readLine()) != null) {
- stringBuilder.append(input);
- }
- }
+ createStringBuilderFromResponse(stringBuilder,
connection);
return status;
} finally {
@@ -210,4 +200,23 @@ public int hashCode() {
result = 31 * result + (requestProps != null ?
requestProps.hashCode() : 0);
return result;
}
+
+ public void createStringBuilderFromResponse (final StringBuilder sb,
final HttpURLConnection connection) throws IOException {
+ if
(GZIP_ENCODING_STRING.equals(connection.getContentEncoding())) {
+ final GZIPInputStream zippedInputStream = new
GZIPInputStream(connection.getInputStream());
+ final BufferedReader r = new BufferedReader(new
InputStreamReader(zippedInputStream));
+ String input;
+ while((input = r.readLine()) != null) {
+ sb.append(input);
+ }
+ } else {
+ try (final BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()))) {
+ String input;
+
+ while ((input = in.readLine()) != null) {
+ sb.append(input);
+ }
+ }
+ }
+ }
}
diff --git
a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java
b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java
index ed1de75a4..3072b6564 100644
---
a/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java
+++
b/traffic_router/core/src/main/java/com/comcast/cdn/traffic_control/traffic_router/core/util/PeriodicResourceUpdater.java
@@ -15,12 +15,14 @@
package com.comcast.cdn.traffic_control.traffic_router.core.util;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
@@ -29,6 +31,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
+import java.util.zip.GZIPInputStream;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
@@ -59,6 +62,8 @@
protected ScheduledExecutorService executorService =
Executors.newSingleThreadScheduledExecutor();
protected long pollingInterval;
+ private static final String GZIP_ENCODING_STRING = "gzip";
+
protected ScheduledFuture<?> scheduledService;
public PeriodicResourceUpdater(final AbstractUpdatable listener, final
ResourceUrl urls, final String location, final int interval, final boolean
pauseTilLoaded) {
@@ -133,6 +138,7 @@ public synchronized boolean updateDatabase() {
if (!hasBeenLoaded || needsUpdating(existingDB)) {
final Request request =
getRequest(urls.nextUrl());
if (request != null) {
+
request.getHeaders().add("Accept-Encoding", GZIP_ENCODING_STRING);
asyncHttpClient.executeRequest(request,
new UpdateHandler(request)); // AsyncHandlers are NOT thread safe; one instance
per request
return true;
}
@@ -256,7 +262,21 @@ public Integer onCompleted(final Response response) throws
IOException {
return code;
}
- updateDatabase(response.getResponseBody());
+ final String responseBody;
+ if
(GZIP_ENCODING_STRING.equals(response.getHeader("Content-Encoding"))) {
+ final StringBuilder stringBuilder = new
StringBuilder();
+ final GZIPInputStream zippedInputStream = new
GZIPInputStream(response.getResponseBodyAsStream());
+ final BufferedReader r = new BufferedReader(new
InputStreamReader(zippedInputStream));
+ String line;
+ while((line = r.readLine()) != null) {
+ stringBuilder.append(line);
+ }
+ responseBody = stringBuilder.toString();
+ } else {
+ responseBody = response.getResponseBody();
+ }
+
+ updateDatabase(responseBody);
return code;
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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