From 30f346995a15ef605543eaa75c4537a51bccd0be Mon Sep 17 00:00:00 2001 From: f43nd1r Date: Sat, 21 Oct 2023 23:22:46 +0200 Subject: [PATCH] Make chunking configurable --- .../acra/config/HttpSenderConfiguration.kt | 8 +++++++ .../java/org/acra/http/BaseHttpRequest.kt | 22 +++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/acra-http/src/main/java/org/acra/config/HttpSenderConfiguration.kt b/acra-http/src/main/java/org/acra/config/HttpSenderConfiguration.kt index f8e9a66d83..0ecec8fc62 100644 --- a/acra-http/src/main/java/org/acra/config/HttpSenderConfiguration.kt +++ b/acra-http/src/main/java/org/acra/config/HttpSenderConfiguration.kt @@ -127,6 +127,14 @@ class HttpSenderConfiguration( */ val compress: Boolean = false, + /** + * if the request should be sent in chunks. + * Set to true when using cronet. + * + * @since 5.11.3 + */ + val chunked: Boolean = false, + /** * TLS versions supported by the server. * diff --git a/acra-http/src/main/java/org/acra/http/BaseHttpRequest.kt b/acra-http/src/main/java/org/acra/http/BaseHttpRequest.kt index 58e8543561..6d7b7e1e98 100644 --- a/acra-http/src/main/java/org/acra/http/BaseHttpRequest.kt +++ b/acra-http/src/main/java/org/acra/http/BaseHttpRequest.kt @@ -48,9 +48,11 @@ import javax.net.ssl.TrustManagerFactory * @since 03.03.2017 */ @Suppress("MemberVisibilityCanBePrivate") -abstract class BaseHttpRequest(private val config: CoreConfiguration, private val context: Context, private val method: HttpSender.Method, - private val login: String?, private val password: String?, private val connectionTimeOut: Int, private val socketTimeOut: Int, - private val headers: Map?) : HttpRequest { +abstract class BaseHttpRequest( + private val config: CoreConfiguration, private val context: Context, private val method: HttpSender.Method, + private val login: String?, private val password: String?, private val connectionTimeOut: Int, private val socketTimeOut: Int, + private val headers: Map? +) : HttpRequest { private val senderConfiguration: HttpSenderConfiguration = config.getPluginConfiguration() /** @@ -134,7 +136,9 @@ abstract class BaseHttpRequest(private val config: CoreConfiguration, private // write output - see http://developer.android.com/reference/java/net/HttpURLConnection.html connection.requestMethod = method.name connection.doOutput = true - connection.setChunkedStreamingMode(ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES) + if (senderConfiguration.chunked) { + connection.setChunkedStreamingMode(ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES) + } // Disable ConnectionPooling because otherwise OkHttp ConnectionPool will try to start a Thread on #connect System.setProperty("http.keepAlive", "false") @@ -151,19 +155,19 @@ abstract class BaseHttpRequest(private val config: CoreConfiguration, private @Throws(IOException::class) protected fun handleResponse(responseCode: Int, responseMessage: String) { - debug { "Request response : $responseCode : $responseMessage" } + debug { "Request response : $responseCode : $responseMessage" } if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_MULT_CHOICE) { // All is good - info { "Request received by server" } + info { "Request received by server" } } else if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT || responseCode >= HttpURLConnection.HTTP_INTERNAL_ERROR) { //timeout or server error. Repeat the request later. - warn { "Could not send ACRA Post responseCode=$responseCode message=$responseMessage" } + warn { "Could not send ACRA Post responseCode=$responseCode message=$responseMessage" } throw IOException("Host returned error code $responseCode") } else if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) { // Client error. The request must not be repeated. Discard it. - warn { "$responseCode: Client error - request will be discarded" } + warn { "$responseCode: Client error - request will be discarded" } } else { - warn { "Could not send ACRA Post - request will be discarded. responseCode=$responseCode message=$responseMessage" } + warn { "Could not send ACRA Post - request will be discarded. responseCode=$responseCode message=$responseMessage" } } }