Fix UnsupportedCharsetException in add, closes #2057

This commit is contained in:
Dominik Schürmann
2017-02-17 08:54:33 +01:00
parent 2c8996bbcd
commit 626cd2a40a

View File

@@ -1,6 +1,5 @@
/* /*
* Copyright (C) 2016 Dominik Schürmann <dominik@dominikschuermann.de> * Copyright (C) 2016-2017 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2011-2014 Thialfihar <thi@thialfihar.org>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -214,13 +213,7 @@ public class ParcelableHkpKeyserver extends Keyserver implements Parcelable {
.execute(); .execute();
// contains body both in case of success or failure // contains body both in case of success or failure
String responseBody; String responseBody = getResponseBodyAsUtf8(response);
byte[] responseBytes = response.body().bytes();
try {
responseBody = new String(responseBytes, response.body().contentType().charset(UTF_8));
} catch (UnsupportedCharsetException e) {
responseBody = new String(responseBytes, UTF_8);
}
if (response.isSuccessful()) { if (response.isSuccessful()) {
return responseBody; return responseBody;
@@ -234,12 +227,21 @@ public class ParcelableHkpKeyserver extends Keyserver implements Parcelable {
} catch (TlsCertificatePinning.TlsCertificatePinningException e) { } catch (TlsCertificatePinning.TlsCertificatePinningException e) {
Log.e(Constants.TAG, "Exception in pinning certs", e); Log.e(Constants.TAG, "Exception in pinning certs", e);
throw new Keyserver.QueryFailedException("Exception in pinning certs"); throw new Keyserver.QueryFailedException("Exception in pinning certs");
} catch (UnsupportedCharsetException e) {
Log.e(Constants.TAG, "UnsupportedCharsetException", e);
throw new Keyserver.QueryFailedException("Unsupported charset");
} }
} }
private String getResponseBodyAsUtf8(Response response) throws IOException {
String responseBody;
byte[] responseBytes = response.body().bytes();
try {
responseBody = new String(responseBytes, response.body().contentType().charset(UTF_8));
} catch (UnsupportedCharsetException e) {
responseBody = new String(responseBytes, UTF_8);
}
return responseBody;
}
/** /**
* Results are sorted by creation date of key! * Results are sorted by creation date of key!
*/ */
@@ -261,6 +263,8 @@ public class ParcelableHkpKeyserver extends Keyserver implements Parcelable {
.addQueryParameter("search", query) .addQueryParameter("search", query)
.build(); .build();
Log.d(Constants.TAG, "Keyserver search: " + url + " using Proxy: " + proxy.getProxy());
data = query(url, proxy); data = query(url, proxy);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
Log.e(Constants.TAG, "Unsupported keyserver URI", e); Log.e(Constants.TAG, "Unsupported keyserver URI", e);
@@ -431,9 +435,11 @@ public class ParcelableHkpKeyserver extends Keyserver implements Parcelable {
.newCall(request) .newCall(request)
.execute(); .execute();
String responseBody = getResponseBodyAsUtf8(response);
Log.d(Constants.TAG, "Adding key with URL: " + url Log.d(Constants.TAG, "Adding key with URL: " + url
+ ", response code: " + response.code() + ", response code: " + response.code()
+ ", body: " + response.body().string()); + ", body: " + responseBody);
if (response.code() != 200) { if (response.code() != 200) {
throw new Keyserver.AddKeyException(); throw new Keyserver.AddKeyException();