Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Android: Fix #579: Crash in HttpClientTask logging #594

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
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
Loading