Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add gzip compression option #674

Merged
merged 2 commits into from
Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions acra-http/src/main/java/org/acra/annotation/AcraHttpSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@

import android.support.annotation.NonNull;
import android.support.annotation.RawRes;

import org.acra.ACRAConstants;
import org.acra.config.BaseHttpConfigurationBuilder;
import org.acra.security.KeyStoreFactory;
import org.acra.security.NoKeyStoreFactory;
import org.acra.sender.HttpSender;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* Http sender configuration
Expand Down Expand Up @@ -136,4 +130,12 @@
* @since 5.0.0
*/
@NonNull String certificateType() default ACRAConstants.DEFAULT_CERTIFICATE_TYPE;

/**
* if the server request should be compressed using gzip
*
* @return if compression should be active
* @since 5.2.0
*/
boolean compress() default false;
}
13 changes: 10 additions & 3 deletions acra-http/src/main/java/org/acra/http/BaseHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import static org.acra.ACRA.LOG_TAG;

Expand All @@ -60,6 +61,7 @@ public abstract class BaseHttpRequest<T> implements HttpRequest<T> {
private final int connectionTimeOut;
private final int socketTimeOut;
private final Map<String, String> headers;
private final HttpSenderConfiguration senderConfiguration;

public BaseHttpRequest(@NonNull CoreConfiguration config, @NonNull Context context, @NonNull Method method,
@Nullable String login, @Nullable String password, int connectionTimeOut, int socketTimeOut, @Nullable Map<String, String> headers) {
Expand All @@ -71,6 +73,7 @@ public BaseHttpRequest(@NonNull CoreConfiguration config, @NonNull Context conte
this.connectionTimeOut = connectionTimeOut;
this.socketTimeOut = socketTimeOut;
this.headers = headers;
senderConfiguration = ConfigUtils.getPluginConfiguration(config, HttpSenderConfiguration.class);
}


Expand Down Expand Up @@ -104,7 +107,7 @@ public void send(@NonNull URL url, @NonNull T content) throws IOException {
handleResponse(urlConnection.getResponseCode(), urlConnection.getResponseMessage());
urlConnection.disconnect();
} catch (SocketTimeoutException e) {
if (ConfigUtils.getPluginConfiguration(config, HttpSenderConfiguration.class).dropReportsOnTimeout()) {
if (senderConfiguration.dropReportsOnTimeout()) {
Log.w(ACRA.LOG_TAG, "Dropped report due to timeout");
} else {
throw e;
Expand Down Expand Up @@ -155,6 +158,10 @@ protected void configureHeaders(@NonNull HttpURLConnection connection, @Nullable
connection.setRequestProperty("Authorization", "Basic " + encoded);
}

if (senderConfiguration.compress()) {
connection.setRequestProperty("Content-Encoding", "gzip");
}

if (customHeaders != null) {
for (final Map.Entry<String, String> header : customHeaders.entrySet()) {
connection.setRequestProperty(header.getKey(), header.getValue());
Expand All @@ -171,14 +178,14 @@ protected void writeContent(@NonNull HttpURLConnection connection, @NonNull Meth
// write output - see http://developer.android.com/reference/java/net/HttpURLConnection.html
connection.setRequestMethod(method.name());
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(contentAsBytes.length);

// Disable ConnectionPooling because otherwise OkHttp ConnectionPool will try to start a Thread on #connect
System.setProperty("http.keepAlive", "false");

connection.connect();

final OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
final OutputStream outputStream = senderConfiguration.compress() ? new GZIPOutputStream(connection.getOutputStream(), ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES)
: new BufferedOutputStream(connection.getOutputStream());
try {
outputStream.write(contentAsBytes);
outputStream.flush();
Expand Down