Skip to content

Commit

Permalink
Android: Fix #579: Crash in HttpClientTask logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hvge committed Jun 5, 2024
1 parent 2054976 commit c1795ec
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,15 @@ private void setThreadStatsTag() {
* @param connection prepared connection object.
* @param requestData (optional) byte array with request data.
*/
private void logRequest(HttpURLConnection connection, byte[] requestData) {
private void logRequest(@Nullable HttpURLConnection connection, @Nullable byte[] requestData) {
if (!PowerAuthLog.isEnabled()) {
return;
}
// Endpoint
final IEndpointDefinition<TResponse> endpoint = httpRequestHelper.getEndpoint();
// URL, method
final String url = connection.getURL().toString();
final boolean hasConnection = connection != null;
final String url = hasConnection ? connection.getURL().toString() : "null";
final String method = endpoint.getHttpMethod();
// Flags
final boolean signature = endpoint.getAuthorizationUriId() != null;
Expand All @@ -287,7 +288,7 @@ private void logRequest(HttpURLConnection connection, byte[] requestData) {
PowerAuthLog.d("HTTP %s request%s: -> %s", method, signedEncrypted, url);
} else {
// Verbose, put headers and body (if not encrypted) into the log.
final Map<String,List<String>> prop = connection.getRequestProperties();
final Map<String,List<String>> prop = hasConnection ? connection.getRequestProperties() : null;
final String propStr = prop == null ? "<empty>" : prop.toString();
if (encrypted) {
PowerAuthLog.d("HTTP %s request%s: %s\n- Headers: %s- Body: <encrypted>", method, signedEncrypted, url, propStr);
Expand All @@ -305,14 +306,15 @@ private void logRequest(HttpURLConnection connection, byte[] requestData) {
* @param responseData (optional) data returned in HTTP request.
* @param error (optional) error produced during the request.
*/
private void logResponse(HttpURLConnection connection, byte[] responseData, Throwable error) {
private void logResponse(@Nullable HttpURLConnection connection, @Nullable byte[] responseData, @Nullable Throwable error) {
if (!PowerAuthLog.isEnabled()) {
return;
}
// Endpoint
final IEndpointDefinition<TResponse> endpoint = httpRequestHelper.getEndpoint();
// URL, method
final String url = connection.getURL().toString();
final boolean hasConnection = connection != null;
final String url = hasConnection ? connection.getURL().toString() : "null";
final String method = endpoint.getHttpMethod();
final String errorMessage;
if (error != null) {
Expand All @@ -329,7 +331,7 @@ private void logResponse(HttpURLConnection connection, byte[] responseData, Thro
// Response code
int responseCode;
try {
responseCode = connection.getResponseCode();
responseCode = hasConnection ? connection.getResponseCode() : 0;
} catch (IOException e) {
responseCode = 0;
}
Expand All @@ -343,7 +345,7 @@ private void logResponse(HttpURLConnection connection, byte[] responseData, Thro
} else {
final boolean encrypted = endpoint.getEncryptorId() != EciesEncryptorId.NONE;
// Response headers
final String responseHeaders = connection.getHeaderFields().toString();
final String responseHeaders = hasConnection ? connection.getHeaderFields().toString() : "{}";
// Response body
final String responseBodyTmp = responseData == null ? "<empty>" : new String(responseData, Charset.defaultCharset());
final String responseBody;
Expand Down

0 comments on commit c1795ec

Please sign in to comment.