2023-07-03 00:16:01 +08:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Simple HTTP client request utility
|
|
|
|
*
|
|
|
|
* Copyright 2023 Isaac Klein <fifthdegree@protonmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <freerdp/config.h>
|
|
|
|
#include <freerdp/utils/http.h>
|
|
|
|
|
|
|
|
#include <winpr/assert.h>
|
|
|
|
#include <winpr/string.h>
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
#include <freerdp/log.h>
|
|
|
|
#define TAG FREERDP_TAG("utils.http")
|
|
|
|
|
|
|
|
static const char get_header_fmt[] = "GET %s HTTP/1.1\r\n"
|
|
|
|
"Host: %s\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
|
|
|
static const char post_header_fmt[] = "POST %s HTTP/1.1\r\n"
|
|
|
|
"Host: %s\r\n"
|
|
|
|
"Content-Type: application/x-www-form-urlencoded\r\n"
|
|
|
|
"Content-Length: %lu\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
2023-07-27 15:02:03 +08:00
|
|
|
#define log_errors(log, msg) log_errors_(log, msg, __FILE__, __func__, __LINE__)
|
2023-07-21 19:44:49 +08:00
|
|
|
static void log_errors_(wLog* log, const char* msg, const char* file, const char* fkt, size_t line)
|
2023-07-03 00:16:01 +08:00
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
const DWORD level = WLOG_ERROR;
|
2023-07-03 00:16:01 +08:00
|
|
|
unsigned long ec = 0;
|
2023-07-21 19:44:49 +08:00
|
|
|
|
|
|
|
if (!WLog_IsLevelActive(log, level))
|
|
|
|
return;
|
|
|
|
|
|
|
|
BOOL error_logged = FALSE;
|
2023-07-03 00:16:01 +08:00
|
|
|
while ((ec = ERR_get_error()))
|
2023-07-21 19:44:49 +08:00
|
|
|
{
|
|
|
|
error_logged = TRUE;
|
|
|
|
WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt, "%s: %s", msg,
|
|
|
|
ERR_error_string(ec, NULL));
|
|
|
|
}
|
|
|
|
if (!error_logged)
|
|
|
|
WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
|
|
|
|
"%s (no details available)", msg);
|
2023-07-03 00:16:01 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 16:20:38 +08:00
|
|
|
static int get_line(BIO* bio, char* buffer, size_t size)
|
|
|
|
{
|
|
|
|
#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
|
|
|
|
if (size <= 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int rc = BIO_read(bio, &buffer[pos], 1);
|
|
|
|
if (rc <= 0)
|
|
|
|
return rc;
|
|
|
|
char cur = buffer[pos];
|
|
|
|
pos += rc;
|
|
|
|
if ((cur == '\n') || (pos >= size - 1))
|
|
|
|
{
|
|
|
|
buffer[pos] = '\0';
|
|
|
|
return (int)pos;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
#else
|
2024-10-14 21:50:38 +08:00
|
|
|
if (size > INT32_MAX)
|
|
|
|
return -1;
|
|
|
|
return BIO_get_line(bio, buffer, (int)size);
|
2023-07-20 16:20:38 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-07-03 00:16:01 +08:00
|
|
|
BOOL freerdp_http_request(const char* url, const char* body, long* status_code, BYTE** response,
|
|
|
|
size_t* response_length)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
char* hostname = NULL;
|
|
|
|
const char* path = NULL;
|
|
|
|
char* headers = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
int status = 0;
|
|
|
|
char buffer[1024] = { 0 };
|
|
|
|
BIO* bio = NULL;
|
|
|
|
SSL_CTX* ssl_ctx = NULL;
|
2023-07-20 20:54:22 +08:00
|
|
|
SSL* ssl = NULL;
|
2023-07-03 00:16:01 +08:00
|
|
|
|
|
|
|
WINPR_ASSERT(status_code);
|
|
|
|
WINPR_ASSERT(response);
|
|
|
|
WINPR_ASSERT(response_length);
|
|
|
|
|
2023-07-21 19:44:49 +08:00
|
|
|
wLog* log = WLog_Get(TAG);
|
|
|
|
WINPR_ASSERT(log);
|
|
|
|
|
2023-07-03 00:16:01 +08:00
|
|
|
*response = NULL;
|
|
|
|
|
2023-07-20 14:44:24 +08:00
|
|
|
if (!url || strnlen(url, 8) < 8 || strncmp(url, "https://", 8) != 0 ||
|
2023-07-03 00:16:01 +08:00
|
|
|
!(path = strchr(url + 8, '/')))
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
WLog_Print(log, WLOG_ERROR, "invalid url provided");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-07-20 14:44:24 +08:00
|
|
|
const size_t len = path - (url + 8);
|
|
|
|
hostname = strndup(&url[8], len);
|
2023-07-03 00:16:01 +08:00
|
|
|
if (!hostname)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-07-24 13:53:42 +08:00
|
|
|
size_t blen = 0;
|
2023-07-03 00:16:01 +08:00
|
|
|
if (body)
|
|
|
|
{
|
2023-07-24 13:53:42 +08:00
|
|
|
blen = strlen(body);
|
|
|
|
if (winpr_asprintf(&headers, &size, post_header_fmt, path, hostname, blen) < 0)
|
2024-04-07 16:42:26 +08:00
|
|
|
{
|
|
|
|
free(hostname);
|
2023-07-03 00:16:01 +08:00
|
|
|
return FALSE;
|
2024-04-07 16:42:26 +08:00
|
|
|
}
|
2023-07-03 00:16:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (winpr_asprintf(&headers, &size, get_header_fmt, path, hostname) < 0)
|
2024-04-07 16:42:26 +08:00
|
|
|
{
|
|
|
|
free(hostname);
|
2023-07-03 00:16:01 +08:00
|
|
|
return FALSE;
|
2024-04-07 16:42:26 +08:00
|
|
|
}
|
2023-07-03 00:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl_ctx = SSL_CTX_new(TLS_client_method());
|
|
|
|
|
|
|
|
if (!ssl_ctx)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not set up ssl context");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SSL_CTX_set_default_verify_paths(ssl_ctx))
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not set ssl context verify paths");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
|
|
|
|
|
|
|
|
bio = BIO_new_ssl_connect(ssl_ctx);
|
|
|
|
if (!bio)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not set up connection");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BIO_set_conn_port(bio, "https") <= 0)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not set port");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BIO_set_conn_hostname(bio, hostname))
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not set hostname");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-07-20 20:54:22 +08:00
|
|
|
BIO_get_ssl(bio, &ssl);
|
|
|
|
if (!ssl)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not get ssl");
|
2023-07-20 20:54:22 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SSL_set_tlsext_host_name(ssl, hostname))
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not set sni hostname");
|
2023-07-20 20:54:22 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-07-21 19:44:49 +08:00
|
|
|
WLog_Print(log, WLOG_DEBUG, "headers:\n%s", headers);
|
2023-07-03 00:16:01 +08:00
|
|
|
ERR_clear_error();
|
2024-10-14 21:50:38 +08:00
|
|
|
const size_t hlen = strnlen(headers, size);
|
|
|
|
if (hlen > INT32_MAX)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (BIO_write(bio, headers, (int)hlen) < 0)
|
2023-07-03 00:16:01 +08:00
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not write headers");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
WLog_Print(log, WLOG_DEBUG, "body:\n%s", body);
|
2023-07-03 00:16:01 +08:00
|
|
|
|
2023-07-24 13:53:42 +08:00
|
|
|
if (blen > INT_MAX)
|
2023-07-03 00:16:01 +08:00
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
WLog_Print(log, WLOG_ERROR, "body too long!");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_clear_error();
|
2024-10-14 21:50:38 +08:00
|
|
|
if (BIO_write(bio, body, (int)blen) < 0)
|
2023-07-03 00:16:01 +08:00
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not write body");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 16:20:38 +08:00
|
|
|
status = get_line(bio, buffer, sizeof(buffer));
|
2023-07-03 00:16:01 +08:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not read response");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2024-10-01 04:45:08 +08:00
|
|
|
// NOLINTNEXTLINE(cert-err34-c)
|
2023-07-03 00:16:01 +08:00
|
|
|
if (sscanf(buffer, "HTTP/1.1 %li %*[^\r\n]\r\n", status_code) < 1)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
WLog_Print(log, WLOG_ERROR, "invalid HTTP status line");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-07-20 16:20:38 +08:00
|
|
|
status = get_line(bio, buffer, sizeof(buffer));
|
2023-07-03 00:16:01 +08:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not read response");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* val = NULL;
|
|
|
|
char* name = strtok_s(buffer, ":", &val);
|
|
|
|
if (name && (_stricmp(name, "content-length") == 0))
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
*response_length = strtoul(val, NULL, 10);
|
|
|
|
if (errno)
|
|
|
|
{
|
2024-02-05 20:01:08 +08:00
|
|
|
char ebuffer[256] = { 0 };
|
2023-07-21 19:44:49 +08:00
|
|
|
WLog_Print(log, WLOG_ERROR, "could not parse content length (%s): %s [%d]", val,
|
2024-02-05 20:01:08 +08:00
|
|
|
winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (strcmp(buffer, "\r\n") != 0);
|
|
|
|
|
|
|
|
if (*response_length > 0)
|
|
|
|
{
|
|
|
|
if (*response_length > INT_MAX)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
WLog_Print(log, WLOG_ERROR, "response too long!");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-07-20 14:44:24 +08:00
|
|
|
*response = calloc(1, *response_length + 1);
|
|
|
|
if (!*response)
|
|
|
|
goto out;
|
|
|
|
|
2023-07-03 00:16:01 +08:00
|
|
|
BYTE* p = *response;
|
2024-10-14 21:50:38 +08:00
|
|
|
size_t left = *response_length;
|
2023-07-03 00:16:01 +08:00
|
|
|
while (left > 0)
|
|
|
|
{
|
2024-10-14 21:50:38 +08:00
|
|
|
const int rd = (left < INT32_MAX) ? (int)left : INT32_MAX;
|
|
|
|
status = BIO_read(bio, p, rd);
|
2023-07-03 00:16:01 +08:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
2023-07-21 19:44:49 +08:00
|
|
|
log_errors(log, "could not read response");
|
2023-07-03 00:16:01 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
p += status;
|
2024-10-14 21:50:38 +08:00
|
|
|
if ((size_t)status > left)
|
|
|
|
break;
|
|
|
|
left -= (size_t)status;
|
2023-07-03 00:16:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
free(*response);
|
|
|
|
*response = NULL;
|
|
|
|
*response_length = 0;
|
|
|
|
}
|
|
|
|
free(hostname);
|
|
|
|
free(headers);
|
|
|
|
BIO_free_all(bio);
|
|
|
|
SSL_CTX_free(ssl_ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-07-21 20:12:53 +08:00
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 23:07:16 +08:00
|
|
|
const char* freerdp_http_status_string_format(long status, char* buffer, size_t size)
|
2023-07-21 20:12:53 +08:00
|
|
|
{
|
|
|
|
const char* code = freerdp_http_status_string(status);
|
2024-08-26 21:39:33 +08:00
|
|
|
(void)_snprintf(buffer, size, "%s [%ld]", code, status);
|
2023-07-21 20:12:53 +08:00
|
|
|
return buffer;
|
|
|
|
}
|