[core,gateway] unify http response logging

This commit is contained in:
Armin Novak 2023-07-28 13:11:37 +02:00 committed by akallabeth
parent 0bdb62e9b5
commit cd7cb514a8
6 changed files with 43 additions and 40 deletions

View File

@ -423,9 +423,7 @@ BOOL arm_resolve_endpoint(rdpContext* context, DWORD timeout)
}
else
{
char buffer[64] = { 0 };
WLog_ERR(TAG, "Unexpected HTTP status: %s",
freerdp_http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
http_response_log_error_status(WLog_Get(TAG), WLOG_ERROR, response);
goto arm_error;
}

View File

@ -955,17 +955,15 @@ fail:
return rc;
}
BOOL http_response_print(HttpResponse* response)
static void http_response_print(wLog* log, DWORD level, HttpResponse* response)
{
size_t i;
WINPR_ASSERT(log);
WINPR_ASSERT(response);
if (!response)
return FALSE;
for (i = 0; i < response->count; i++)
WLog_ERR(TAG, "%s", response->lines[i]);
return TRUE;
if (!WLog_IsLevelActive(log, level))
return;
for (size_t i = 0; i < response->count; i++)
WLog_Print(log, level, "[%" PRIuz "] %s", i, response->lines[i]);
}
static BOOL http_use_content_length(const char* cur)
@ -1437,16 +1435,14 @@ BOOL http_request_set_content_length(HttpRequest* request, size_t length)
long http_response_get_status_code(HttpResponse* response)
{
if (!response)
return -1;
WINPR_ASSERT(response);
return response->StatusCode;
}
SSIZE_T http_response_get_body_length(HttpResponse* response)
size_t http_response_get_body_length(HttpResponse* response)
{
if (!response)
return -1;
WINPR_ASSERT(response);
return (SSIZE_T)response->BodyLength;
}
@ -1532,3 +1528,18 @@ out:
free(base64accept);
return isWebsocket;
}
void http_response_log_error_status(wLog* log, DWORD level, HttpResponse* response)
{
WINPR_ASSERT(log);
WINPR_ASSERT(response);
if (!WLog_IsLevelActive(log, level))
return;
char buffer[64] = { 0 };
const long status = http_response_get_status_code(response);
WLog_Print(log, level, "Unexpected HTTP status: %s",
freerdp_http_status_string_format(status, buffer, ARRAYSIZE(buffer)));
http_response_print(log, level, response);
}

View File

@ -101,17 +101,18 @@ typedef struct s_http_response HttpResponse;
FREERDP_LOCAL HttpResponse* http_response_new(void);
FREERDP_LOCAL void http_response_free(HttpResponse* response);
FREERDP_LOCAL BOOL http_response_print(HttpResponse* response);
FREERDP_LOCAL HttpResponse* http_response_recv(rdpTls* tls, BOOL readContentLength);
FREERDP_LOCAL long http_response_get_status_code(HttpResponse* response);
FREERDP_LOCAL SSIZE_T http_response_get_body_length(HttpResponse* response);
FREERDP_LOCAL size_t http_response_get_body_length(HttpResponse* response);
FREERDP_LOCAL const BYTE* http_response_get_body(HttpResponse* response);
FREERDP_LOCAL const char* http_response_get_auth_token(HttpResponse* response, const char* method);
FREERDP_LOCAL const char* http_response_get_setcookie(HttpResponse* response, const char* cookie);
FREERDP_LOCAL TRANSFER_ENCODING http_response_get_transfer_encoding(HttpResponse* response);
FREERDP_LOCAL BOOL http_response_is_websocket(HttpContext* http, HttpResponse* response);
FREERDP_LOCAL void http_response_log_error_status(wLog* log, DWORD level, HttpResponse* response);
/* chunked read helper */
FREERDP_LOCAL int http_chuncked_read(BIO* bio, BYTE* pBuffer, size_t size,
http_encoding_chunked_context* encodingContext);

View File

@ -720,8 +720,7 @@ static BOOL rdg_recv_auth_token(rdpCredsspAuth* auth, HttpResponse* response)
case HTTP_STATUS_OK:
break;
default:
WLog_WARN(TAG, "Unexpected HTTP status: %s",
freerdp_http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
http_response_log_error_status(WLog_Get(TAG), WLOG_WARN, response);
return FALSE;
}
@ -1303,11 +1302,6 @@ static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char*
{
char buffer[64] = { 0 };
HttpResponse* response = NULL;
long statusCode;
SSIZE_T bodyLength;
long StatusCode;
TRANSFER_ENCODING encoding;
BOOL isWebsocket;
if (!rdg_tls_connect(rdg, tls, peerAddress, timeout))
return FALSE;
@ -1333,7 +1327,7 @@ static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char*
return FALSE;
}
StatusCode = http_response_get_status_code(response);
const long StatusCode = http_response_get_status_code(response);
switch (StatusCode)
{
@ -1345,7 +1339,10 @@ static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char*
http_response_free(response);
return FALSE;
}
case HTTP_STATUS_OK:
break;
default:
http_response_log_error_status(WLog_Get(TAG), WLOG_WARN, response);
break;
}
@ -1394,10 +1391,10 @@ static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char*
}
}
statusCode = http_response_get_status_code(response);
bodyLength = http_response_get_body_length(response);
encoding = http_response_get_transfer_encoding(response);
isWebsocket = http_response_is_websocket(rdg->http, response);
const long statusCode = http_response_get_status_code(response);
const size_t bodyLength = http_response_get_body_length(response);
const TRANSFER_ENCODING encoding = http_response_get_transfer_encoding(response);
const BOOL isWebsocket = http_response_is_websocket(rdg->http, response);
http_response_free(response);
WLog_DBG(TAG, "%s authorization result: %s", method,
freerdp_http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
@ -1442,8 +1439,7 @@ static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char*
}
return TRUE;
default:
WLog_WARN(TAG, "Unexpected HTTP status %s",
freerdp_http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
http_response_log_error_status(WLog_Get(TAG), WLOG_WARN, response);
return FALSE;
}

View File

@ -602,11 +602,7 @@ static SSIZE_T rpc_client_default_out_channel_recv(rdpRpc* rpc)
if (statusCode != HTTP_STATUS_OK)
{
char buffer[64] = { 0 };
WLog_ERR(TAG, "error! Status Code: %s",
freerdp_http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
http_response_print(response);
http_response_log_error_status(WLog_Get(TAG), WLOG_ERROR, response);
if (statusCode == HTTP_STATUS_DENIED)
{

View File

@ -179,8 +179,7 @@ static BOOL wst_recv_auth_token(rdpCredsspAuth* auth, HttpResponse* response)
case HTTP_STATUS_OK:
break;
default:
WLog_WARN(TAG, "Unexpected HTTP status: %s",
freerdp_http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
http_response_log_error_status(WLog_Get(TAG), WLOG_WARN, response);
return FALSE;
}
@ -481,7 +480,9 @@ BOOL wst_connect(rdpWst* wst, DWORD timeout)
case HTTP_STATUS_DENIED:
success = wst_handle_denied(wst, &response, &StatusCode);
break;
default:
http_response_log_error_status(WLog_Get(TAG), WLOG_WARN, response);
break;
}