diff options
author | Torsten Grote <t@grobox.de> | 2020-08-27 16:42:03 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-08-27 16:42:03 -0300 |
commit | 53d99e46e6b34d4437f46266cb797a65c0736803 (patch) | |
tree | 23f21d022abea280132e3440b3825a71483c7bd2 /cashier/src/main/java/net/taler/cashier/HttpHelper.kt | |
parent | ed3f86481a71517e7bf6ffa46dc8d160b508ec38 (diff) | |
download | taler-android-53d99e46e6b34d4437f46266cb797a65c0736803.tar.gz taler-android-53d99e46e6b34d4437f46266cb797a65c0736803.tar.bz2 taler-android-53d99e46e6b34d4437f46266cb797a65c0736803.zip |
[cashier] don't crash on unexpected network input
Diffstat (limited to 'cashier/src/main/java/net/taler/cashier/HttpHelper.kt')
-rw-r--r-- | cashier/src/main/java/net/taler/cashier/HttpHelper.kt | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/cashier/src/main/java/net/taler/cashier/HttpHelper.kt b/cashier/src/main/java/net/taler/cashier/HttpHelper.kt index 003c2f6..fd48b2d 100644 --- a/cashier/src/main/java/net/taler/cashier/HttpHelper.kt +++ b/cashier/src/main/java/net/taler/cashier/HttpHelper.kt @@ -18,12 +18,15 @@ package net.taler.cashier import android.util.Log import androidx.annotation.WorkerThread +import net.taler.cashier.config.Config +import okhttp3.Authenticator import okhttp3.Credentials -import okhttp3.MediaType +import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.OkHttpClient import okhttp3.Request -import okhttp3.RequestBody +import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response +import okhttp3.Route import org.json.JSONException import org.json.JSONObject @@ -47,23 +50,23 @@ object HttpHelper { Log.e(TAG, "Error retrieving $url", e) return HttpJsonResult.Error(0) } - return if (response.code() == 200 && response.body() != null) { - val jsonObject = JSONObject(response.body()!!.string()) + return if (response.code == 200 && response.body != null) { + val jsonObject = JSONObject(response.body!!.string()) HttpJsonResult.Success(jsonObject) } else { - Log.e(TAG, "Received status ${response.code()} from $url expected 200") - HttpJsonResult.Error(response.code(), getErrorBody(response)) + Log.e(TAG, "Received status ${response.code} from $url expected 200") + HttpJsonResult.Error(response.code, getErrorBody(response)) } } - private val MEDIA_TYPE_JSON = MediaType.parse("$MIME_TYPE_JSON; charset=utf-8") + private val MEDIA_TYPE_JSON = "$MIME_TYPE_JSON; charset=utf-8".toMediaTypeOrNull() @WorkerThread fun makeJsonPostRequest(url: String, body: JSONObject, config: Config): HttpJsonResult { val request = Request.Builder() .addHeader("Accept", MIME_TYPE_JSON) .url(url) - .post(RequestBody.create(MEDIA_TYPE_JSON, body.toString())) + .post(body.toString().toRequestBody(MEDIA_TYPE_JSON)) .build() val response = try { getHttpClient(config.username, config.password) @@ -73,31 +76,33 @@ object HttpHelper { Log.e(TAG, "Error retrieving $url", e) return HttpJsonResult.Error(0) } - return if (response.code() == 200 && response.body() != null) { - val jsonObject = JSONObject(response.body()!!.string()) + return if (response.code == 200 && response.body != null) { + val jsonObject = JSONObject(response.body!!.string()) HttpJsonResult.Success(jsonObject) } else { - Log.e(TAG, "Received status ${response.code()} from $url expected 200") - HttpJsonResult.Error(response.code(), getErrorBody(response)) + Log.e(TAG, "Received status ${response.code} from $url expected 200") + HttpJsonResult.Error(response.code, getErrorBody(response)) } } private fun getHttpClient(username: String, password: String) = - OkHttpClient.Builder().authenticator { _, response -> - val credential = Credentials.basic(username, password) - if (credential == response.request().header("Authorization")) { - // If we already failed with these credentials, don't retry - return@authenticator null + OkHttpClient.Builder().authenticator(object : Authenticator { + override fun authenticate(route: Route?, response: Response): Request? { + val credential = Credentials.basic(username, password) + if (credential == response.request.header("Authorization")) { + // If we already failed with these credentials, don't retry + return null + } + return response + .request + .newBuilder() + .header("Authorization", credential) + .build() } - response - .request() - .newBuilder() - .header("Authorization", credential) - .build() - }.build() + }).build() private fun getErrorBody(response: Response): String? { - val body = response.body()?.string() ?: return null + val body = response.body?.string() ?: return null Log.e(TAG, "Response body: $body") return try { val json = JSONObject(body) |