[core] move http status to string to utils

freerdp_http_status_string and freerdp_http_status_string_format are now
exposed as public API functions to work with http status codes
This commit is contained in:
akallabeth 2023-07-21 14:12:53 +02:00 committed by akallabeth
parent 9eb80ffb19
commit 14d0ec9335
8 changed files with 151 additions and 158 deletions

View File

@ -22,7 +22,54 @@
#include <freerdp/api.h>
typedef enum
{
HTTP_STATUS_CONTINUE = 100,
HTTP_STATUS_SWITCH_PROTOCOLS = 101,
HTTP_STATUS_OK = 200,
HTTP_STATUS_CREATED = 201,
HTTP_STATUS_ACCEPTED = 202,
HTTP_STATUS_PARTIAL = 203,
HTTP_STATUS_NO_CONTENT = 204,
HTTP_STATUS_RESET_CONTENT = 205,
HTTP_STATUS_PARTIAL_CONTENT = 206,
HTTP_STATUS_WEBDAV_MULTI_STATUS = 207,
HTTP_STATUS_AMBIGUOUS = 300,
HTTP_STATUS_MOVED = 301,
HTTP_STATUS_REDIRECT = 302,
HTTP_STATUS_REDIRECT_METHOD = 303,
HTTP_STATUS_NOT_MODIFIED = 304,
HTTP_STATUS_USE_PROXY = 305,
HTTP_STATUS_REDIRECT_KEEP_VERB = 307,
HTTP_STATUS_BAD_REQUEST = 400,
HTTP_STATUS_DENIED = 401,
HTTP_STATUS_PAYMENT_REQ = 402,
HTTP_STATUS_FORBIDDEN = 403,
HTTP_STATUS_NOT_FOUND = 404,
HTTP_STATUS_BAD_METHOD = 405,
HTTP_STATUS_NONE_ACCEPTABLE = 406,
HTTP_STATUS_PROXY_AUTH_REQ = 407,
HTTP_STATUS_REQUEST_TIMEOUT = 408,
HTTP_STATUS_CONFLICT = 409,
HTTP_STATUS_GONE = 410,
HTTP_STATUS_LENGTH_REQUIRED = 411,
HTTP_STATUS_PRECOND_FAILED = 412,
HTTP_STATUS_REQUEST_TOO_LARGE = 413,
HTTP_STATUS_URI_TOO_LONG = 414,
HTTP_STATUS_UNSUPPORTED_MEDIA = 415,
HTTP_STATUS_RETRY_WITH = 449,
HTTP_STATUS_SERVER_ERROR = 500,
HTTP_STATUS_NOT_SUPPORTED = 501,
HTTP_STATUS_BAD_GATEWAY = 502,
HTTP_STATUS_SERVICE_UNAVAIL = 503,
HTTP_STATUS_GATEWAY_TIMEOUT = 504,
HTTP_STATUS_VERSION_NOT_SUP = 505
} FREERDP_HTTP_STATUS;
FREERDP_API BOOL freerdp_http_request(const char* url, const char* body, long* status_code,
BYTE** response, size_t* response_length);
FREERDP_API const char* freerdp_http_status_string(long status);
FREERDP_API char* freerdp_http_status_string_format(long status, char* buffer, size_t size);
#endif /* FREERDP_UTILS_HTTP_H */

View File

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

View File

@ -1532,99 +1532,3 @@ out:
free(base64accept);
return isWebsocket;
}
const char* http_status_string(long status)
{
switch (status)
{
case HTTP_STATUS_CONTINUE:
return "HTTP_STATUS_CONTINUE";
case HTTP_STATUS_SWITCH_PROTOCOLS:
return "HTTP_STATUS_SWITCH_PROTOCOLS";
case HTTP_STATUS_OK:
return "HTTP_STATUS_OK";
case HTTP_STATUS_CREATED:
return "HTTP_STATUS_CREATED";
case HTTP_STATUS_ACCEPTED:
return "HTTP_STATUS_ACCEPTED";
case HTTP_STATUS_PARTIAL:
return "HTTP_STATUS_PARTIAL";
case HTTP_STATUS_NO_CONTENT:
return "HTTP_STATUS_NO_CONTENT";
case HTTP_STATUS_RESET_CONTENT:
return "HTTP_STATUS_RESET_CONTENT";
case HTTP_STATUS_PARTIAL_CONTENT:
return "HTTP_STATUS_PARTIAL_CONTENT";
case HTTP_STATUS_WEBDAV_MULTI_STATUS:
return "HTTP_STATUS_WEBDAV_MULTI_STATUS";
case HTTP_STATUS_AMBIGUOUS:
return "HTTP_STATUS_AMBIGUOUS";
case HTTP_STATUS_MOVED:
return "HTTP_STATUS_MOVED";
case HTTP_STATUS_REDIRECT:
return "HTTP_STATUS_REDIRECT";
case HTTP_STATUS_REDIRECT_METHOD:
return "HTTP_STATUS_REDIRECT_METHOD";
case HTTP_STATUS_NOT_MODIFIED:
return "HTTP_STATUS_NOT_MODIFIED";
case HTTP_STATUS_USE_PROXY:
return "HTTP_STATUS_USE_PROXY";
case HTTP_STATUS_REDIRECT_KEEP_VERB:
return "HTTP_STATUS_REDIRECT_KEEP_VERB";
case HTTP_STATUS_BAD_REQUEST:
return "HTTP_STATUS_BAD_REQUEST";
case HTTP_STATUS_DENIED:
return "HTTP_STATUS_DENIED";
case HTTP_STATUS_PAYMENT_REQ:
return "HTTP_STATUS_PAYMENT_REQ";
case HTTP_STATUS_FORBIDDEN:
return "HTTP_STATUS_FORBIDDEN";
case HTTP_STATUS_NOT_FOUND:
return "HTTP_STATUS_NOT_FOUND";
case HTTP_STATUS_BAD_METHOD:
return "HTTP_STATUS_BAD_METHOD";
case HTTP_STATUS_NONE_ACCEPTABLE:
return "HTTP_STATUS_NONE_ACCEPTABLE";
case HTTP_STATUS_PROXY_AUTH_REQ:
return "HTTP_STATUS_PROXY_AUTH_REQ";
case HTTP_STATUS_REQUEST_TIMEOUT:
return "HTTP_STATUS_REQUEST_TIMEOUT";
case HTTP_STATUS_CONFLICT:
return "HTTP_STATUS_CONFLICT";
case HTTP_STATUS_GONE:
return "HTTP_STATUS_GONE";
case HTTP_STATUS_LENGTH_REQUIRED:
return "HTTP_STATUS_LENGTH_REQUIRED";
case HTTP_STATUS_PRECOND_FAILED:
return "HTTP_STATUS_PRECOND_FAILED";
case HTTP_STATUS_REQUEST_TOO_LARGE:
return "HTTP_STATUS_REQUEST_TOO_LARGE";
case HTTP_STATUS_URI_TOO_LONG:
return "HTTP_STATUS_URI_TOO_LONG";
case HTTP_STATUS_UNSUPPORTED_MEDIA:
return "HTTP_STATUS_UNSUPPORTED_MEDIA";
case HTTP_STATUS_RETRY_WITH:
return "HTTP_STATUS_RETRY_WITH";
case HTTP_STATUS_SERVER_ERROR:
return "HTTP_STATUS_SERVER_ERROR";
case HTTP_STATUS_NOT_SUPPORTED:
return "HTTP_STATUS_NOT_SUPPORTED";
case HTTP_STATUS_BAD_GATEWAY:
return "HTTP_STATUS_BAD_GATEWAY";
case HTTP_STATUS_SERVICE_UNAVAIL:
return "HTTP_STATUS_SERVICE_UNAVAIL";
case HTTP_STATUS_GATEWAY_TIMEOUT:
return "HTTP_STATUS_GATEWAY_TIMEOUT";
case HTTP_STATUS_VERSION_NOT_SUP:
return "HTTP_STATUS_VERSION_NOT_SUP";
default:
return "HTTP_STATUS_UNKNOWN";
}
}
char* http_status_string_format(long status, char* buffer, size_t size)
{
const char* code = http_status_string(status);
_snprintf(buffer, size, "%s [%ld]", code, status);
return buffer;
}

View File

@ -23,61 +23,10 @@
#include <winpr/stream.h>
#include <freerdp/api.h>
#include <freerdp/utils/http.h>
#include "../../crypto/tls.h"
typedef enum
{
HTTP_STATUS_CONTINUE = 100,
HTTP_STATUS_SWITCH_PROTOCOLS = 101
,
HTTP_STATUS_OK = 200,
HTTP_STATUS_CREATED = 201,
HTTP_STATUS_ACCEPTED = 202,
HTTP_STATUS_PARTIAL = 203,
HTTP_STATUS_NO_CONTENT = 204,
HTTP_STATUS_RESET_CONTENT = 205,
HTTP_STATUS_PARTIAL_CONTENT = 206,
HTTP_STATUS_WEBDAV_MULTI_STATUS = 207
,
HTTP_STATUS_AMBIGUOUS = 300,
HTTP_STATUS_MOVED = 301,
HTTP_STATUS_REDIRECT = 302,
HTTP_STATUS_REDIRECT_METHOD = 303,
HTTP_STATUS_NOT_MODIFIED = 304,
HTTP_STATUS_USE_PROXY = 305,
HTTP_STATUS_REDIRECT_KEEP_VERB = 307
,
HTTP_STATUS_BAD_REQUEST = 400,
HTTP_STATUS_DENIED = 401,
HTTP_STATUS_PAYMENT_REQ = 402,
HTTP_STATUS_FORBIDDEN = 403,
HTTP_STATUS_NOT_FOUND = 404,
HTTP_STATUS_BAD_METHOD = 405,
HTTP_STATUS_NONE_ACCEPTABLE = 406,
HTTP_STATUS_PROXY_AUTH_REQ = 407,
HTTP_STATUS_REQUEST_TIMEOUT = 408,
HTTP_STATUS_CONFLICT = 409,
HTTP_STATUS_GONE = 410,
HTTP_STATUS_LENGTH_REQUIRED = 411,
HTTP_STATUS_PRECOND_FAILED = 412,
HTTP_STATUS_REQUEST_TOO_LARGE = 413,
HTTP_STATUS_URI_TOO_LONG = 414,
HTTP_STATUS_UNSUPPORTED_MEDIA = 415,
HTTP_STATUS_RETRY_WITH = 449
,
HTTP_STATUS_SERVER_ERROR = 500,
HTTP_STATUS_NOT_SUPPORTED = 501,
HTTP_STATUS_BAD_GATEWAY = 502,
HTTP_STATUS_SERVICE_UNAVAIL = 503,
HTTP_STATUS_GATEWAY_TIMEOUT = 504,
HTTP_STATUS_VERSION_NOT_SUP = 505
} HTTP_STATUS;
typedef enum
{
TransferEncodingUnknown,
@ -163,9 +112,6 @@ FREERDP_LOCAL const char* http_response_get_setcookie(HttpResponse* response, co
FREERDP_LOCAL TRANSFER_ENCODING http_response_get_transfer_encoding(HttpResponse* response);
FREERDP_LOCAL BOOL http_response_is_websocket(HttpContext* http, HttpResponse* response);
FREERDP_LOCAL const char* http_status_string(long status);
FREERDP_LOCAL char* http_status_string_format(long status, char* buffer, size_t size);
/* chunked read helper */
FREERDP_LOCAL int http_chuncked_read(BIO* bio, BYTE* pBuffer, size_t size,
http_encoding_chunked_context* encodingContext);

View File

@ -721,7 +721,7 @@ static BOOL rdg_recv_auth_token(rdpCredsspAuth* auth, HttpResponse* response)
break;
default:
WLog_WARN(TAG, "Unexpected HTTP status: %s",
http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
freerdp_http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
return FALSE;
}
@ -1397,7 +1397,7 @@ static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char*
isWebsocket = http_response_is_websocket(rdg->http, response);
http_response_free(response);
WLog_DBG(TAG, "%s authorization result: %s", method,
http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
freerdp_http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
switch (statusCode)
{
@ -1440,7 +1440,7 @@ static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char*
return TRUE;
default:
WLog_WARN(TAG, "Unexpected HTTP status %s",
http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
freerdp_http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
return FALSE;
}

View File

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

View File

@ -180,7 +180,7 @@ static BOOL wst_recv_auth_token(rdpCredsspAuth* auth, HttpResponse* response)
break;
default:
WLog_WARN(TAG, "Unexpected HTTP status: %s",
http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
freerdp_http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
return FALSE;
}
@ -500,7 +500,7 @@ BOOL wst_connect(rdpWst* wst, DWORD timeout)
{
char buffer[64] = { 0 };
WLog_ERR(TAG, "Unexpected HTTP status: %s",
http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
freerdp_http_status_string_format(StatusCode, buffer, ARRAYSIZE(buffer)));
}
return FALSE;
}

View File

@ -268,3 +268,99 @@ out:
SSL_CTX_free(ssl_ctx);
return ret;
}
const char* freerdp_http_status_string(long status)
{
switch (status)
{
case HTTP_STATUS_CONTINUE:
return "HTTP_STATUS_CONTINUE";
case HTTP_STATUS_SWITCH_PROTOCOLS:
return "HTTP_STATUS_SWITCH_PROTOCOLS";
case HTTP_STATUS_OK:
return "HTTP_STATUS_OK";
case HTTP_STATUS_CREATED:
return "HTTP_STATUS_CREATED";
case HTTP_STATUS_ACCEPTED:
return "HTTP_STATUS_ACCEPTED";
case HTTP_STATUS_PARTIAL:
return "HTTP_STATUS_PARTIAL";
case HTTP_STATUS_NO_CONTENT:
return "HTTP_STATUS_NO_CONTENT";
case HTTP_STATUS_RESET_CONTENT:
return "HTTP_STATUS_RESET_CONTENT";
case HTTP_STATUS_PARTIAL_CONTENT:
return "HTTP_STATUS_PARTIAL_CONTENT";
case HTTP_STATUS_WEBDAV_MULTI_STATUS:
return "HTTP_STATUS_WEBDAV_MULTI_STATUS";
case HTTP_STATUS_AMBIGUOUS:
return "HTTP_STATUS_AMBIGUOUS";
case HTTP_STATUS_MOVED:
return "HTTP_STATUS_MOVED";
case HTTP_STATUS_REDIRECT:
return "HTTP_STATUS_REDIRECT";
case HTTP_STATUS_REDIRECT_METHOD:
return "HTTP_STATUS_REDIRECT_METHOD";
case HTTP_STATUS_NOT_MODIFIED:
return "HTTP_STATUS_NOT_MODIFIED";
case HTTP_STATUS_USE_PROXY:
return "HTTP_STATUS_USE_PROXY";
case HTTP_STATUS_REDIRECT_KEEP_VERB:
return "HTTP_STATUS_REDIRECT_KEEP_VERB";
case HTTP_STATUS_BAD_REQUEST:
return "HTTP_STATUS_BAD_REQUEST";
case HTTP_STATUS_DENIED:
return "HTTP_STATUS_DENIED";
case HTTP_STATUS_PAYMENT_REQ:
return "HTTP_STATUS_PAYMENT_REQ";
case HTTP_STATUS_FORBIDDEN:
return "HTTP_STATUS_FORBIDDEN";
case HTTP_STATUS_NOT_FOUND:
return "HTTP_STATUS_NOT_FOUND";
case HTTP_STATUS_BAD_METHOD:
return "HTTP_STATUS_BAD_METHOD";
case HTTP_STATUS_NONE_ACCEPTABLE:
return "HTTP_STATUS_NONE_ACCEPTABLE";
case HTTP_STATUS_PROXY_AUTH_REQ:
return "HTTP_STATUS_PROXY_AUTH_REQ";
case HTTP_STATUS_REQUEST_TIMEOUT:
return "HTTP_STATUS_REQUEST_TIMEOUT";
case HTTP_STATUS_CONFLICT:
return "HTTP_STATUS_CONFLICT";
case HTTP_STATUS_GONE:
return "HTTP_STATUS_GONE";
case HTTP_STATUS_LENGTH_REQUIRED:
return "HTTP_STATUS_LENGTH_REQUIRED";
case HTTP_STATUS_PRECOND_FAILED:
return "HTTP_STATUS_PRECOND_FAILED";
case HTTP_STATUS_REQUEST_TOO_LARGE:
return "HTTP_STATUS_REQUEST_TOO_LARGE";
case HTTP_STATUS_URI_TOO_LONG:
return "HTTP_STATUS_URI_TOO_LONG";
case HTTP_STATUS_UNSUPPORTED_MEDIA:
return "HTTP_STATUS_UNSUPPORTED_MEDIA";
case HTTP_STATUS_RETRY_WITH:
return "HTTP_STATUS_RETRY_WITH";
case HTTP_STATUS_SERVER_ERROR:
return "HTTP_STATUS_SERVER_ERROR";
case HTTP_STATUS_NOT_SUPPORTED:
return "HTTP_STATUS_NOT_SUPPORTED";
case HTTP_STATUS_BAD_GATEWAY:
return "HTTP_STATUS_BAD_GATEWAY";
case HTTP_STATUS_SERVICE_UNAVAIL:
return "HTTP_STATUS_SERVICE_UNAVAIL";
case HTTP_STATUS_GATEWAY_TIMEOUT:
return "HTTP_STATUS_GATEWAY_TIMEOUT";
case HTTP_STATUS_VERSION_NOT_SUP:
return "HTTP_STATUS_VERSION_NOT_SUP";
default:
return "HTTP_STATUS_UNKNOWN";
}
}
char* freerdp_http_status_string_format(long status, char* buffer, size_t size)
{
const char* code = freerdp_http_status_string(status);
_snprintf(buffer, size, "%s [%ld]", code, status);
return buffer;
}