20 #include <freerdp/config.h>
21 #include <freerdp/utils/http.h>
23 #include <winpr/assert.h>
24 #include <winpr/string.h>
26 #include <openssl/bio.h>
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
30 #include <freerdp/log.h>
31 #define TAG FREERDP_TAG("utils.http")
33 static const char get_header_fmt[] =
"GET %s HTTP/1.1\r\n"
37 static const char post_header_fmt[] =
"POST %s HTTP/1.1\r\n"
39 "Content-Type: application/x-www-form-urlencoded\r\n"
40 "Content-Length: %lu\r\n"
43 #define log_errors(log, msg) log_errors_(log, msg, __FILE__, __func__, __LINE__)
44 static void log_errors_(wLog* log,
const char* msg,
const char* file,
const char* fkt,
size_t line)
46 const DWORD level = WLOG_ERROR;
49 if (!WLog_IsLevelActive(log, level))
52 BOOL error_logged = FALSE;
53 while ((ec = ERR_get_error()))
56 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
"%s: %s", msg,
57 ERR_error_string(ec, NULL));
60 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
61 "%s (no details available)", msg);
64 static int get_line(BIO* bio,
char* buffer,
size_t size)
66 #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
73 int rc = BIO_read(bio, &buffer[pos], 1);
76 char cur = buffer[pos];
78 if ((cur ==
'\n') || (pos >= size - 1))
87 return BIO_get_line(bio, buffer, (
int)size);
91 BOOL freerdp_http_request(
const char* url,
const char* body,
long* status_code, BYTE** response,
92 size_t* response_length)
95 char* hostname = NULL;
96 const char* path = NULL;
100 char buffer[1024] = { 0 };
102 SSL_CTX* ssl_ctx = NULL;
105 WINPR_ASSERT(status_code);
106 WINPR_ASSERT(response);
107 WINPR_ASSERT(response_length);
109 wLog* log = WLog_Get(TAG);
114 if (!url || strnlen(url, 8) < 8 || strncmp(url,
"https://", 8) != 0 ||
115 !(path = strchr(url + 8,
'/')))
117 WLog_Print(log, WLOG_ERROR,
"invalid url provided");
121 const size_t len = path - (url + 8);
122 hostname = strndup(&url[8], len);
130 if (winpr_asprintf(&headers, &size, post_header_fmt, path, hostname, blen) < 0)
138 if (winpr_asprintf(&headers, &size, get_header_fmt, path, hostname) < 0)
145 ssl_ctx = SSL_CTX_new(TLS_client_method());
149 log_errors(log,
"could not set up ssl context");
153 if (!SSL_CTX_set_default_verify_paths(ssl_ctx))
155 log_errors(log,
"could not set ssl context verify paths");
159 SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
161 bio = BIO_new_ssl_connect(ssl_ctx);
164 log_errors(log,
"could not set up connection");
168 if (BIO_set_conn_port(bio,
"https") <= 0)
170 log_errors(log,
"could not set port");
174 if (!BIO_set_conn_hostname(bio, hostname))
176 log_errors(log,
"could not set hostname");
180 BIO_get_ssl(bio, &ssl);
183 log_errors(log,
"could not get ssl");
187 if (!SSL_set_tlsext_host_name(ssl, hostname))
189 log_errors(log,
"could not set sni hostname");
193 WLog_Print(log, WLOG_DEBUG,
"headers:\n%s", headers);
195 const size_t hlen = strnlen(headers, size);
196 if (hlen > INT32_MAX)
199 if (BIO_write(bio, headers, (
int)hlen) < 0)
201 log_errors(log,
"could not write headers");
207 WLog_Print(log, WLOG_DEBUG,
"body:\n%s", body);
211 WLog_Print(log, WLOG_ERROR,
"body too long!");
216 if (BIO_write(bio, body, (
int)blen) < 0)
218 log_errors(log,
"could not write body");
223 status = get_line(bio, buffer,
sizeof(buffer));
226 log_errors(log,
"could not read response");
231 if (sscanf(buffer,
"HTTP/1.1 %li %*[^\r\n]\r\n", status_code) < 1)
233 WLog_Print(log, WLOG_ERROR,
"invalid HTTP status line");
239 status = get_line(bio, buffer,
sizeof(buffer));
242 log_errors(log,
"could not read response");
247 char* name = strtok_s(buffer,
":", &val);
248 if (name && (_stricmp(name,
"content-length") == 0))
251 *response_length = strtoul(val, NULL, 10);
254 char ebuffer[256] = { 0 };
255 WLog_Print(log, WLOG_ERROR,
"could not parse content length (%s): %s [%d]", val,
256 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
260 }
while (strcmp(buffer,
"\r\n") != 0);
262 if (*response_length > 0)
264 if (*response_length > INT_MAX)
266 WLog_Print(log, WLOG_ERROR,
"response too long!");
270 *response = calloc(1, *response_length + 1);
275 size_t left = *response_length;
278 const int rd = (left < INT32_MAX) ? (
int)left : INT32_MAX;
279 status = BIO_read(bio, p, rd);
282 log_errors(log,
"could not read response");
286 if ((
size_t)status > left)
288 left -= (size_t)status;
299 *response_length = 0;
304 SSL_CTX_free(ssl_ctx);
308 const char* freerdp_http_status_string(
long status)
312 case HTTP_STATUS_CONTINUE:
313 return "HTTP_STATUS_CONTINUE";
314 case HTTP_STATUS_SWITCH_PROTOCOLS:
315 return "HTTP_STATUS_SWITCH_PROTOCOLS";
317 return "HTTP_STATUS_OK";
318 case HTTP_STATUS_CREATED:
319 return "HTTP_STATUS_CREATED";
320 case HTTP_STATUS_ACCEPTED:
321 return "HTTP_STATUS_ACCEPTED";
322 case HTTP_STATUS_PARTIAL:
323 return "HTTP_STATUS_PARTIAL";
324 case HTTP_STATUS_NO_CONTENT:
325 return "HTTP_STATUS_NO_CONTENT";
326 case HTTP_STATUS_RESET_CONTENT:
327 return "HTTP_STATUS_RESET_CONTENT";
328 case HTTP_STATUS_PARTIAL_CONTENT:
329 return "HTTP_STATUS_PARTIAL_CONTENT";
330 case HTTP_STATUS_WEBDAV_MULTI_STATUS:
331 return "HTTP_STATUS_WEBDAV_MULTI_STATUS";
332 case HTTP_STATUS_AMBIGUOUS:
333 return "HTTP_STATUS_AMBIGUOUS";
334 case HTTP_STATUS_MOVED:
335 return "HTTP_STATUS_MOVED";
336 case HTTP_STATUS_REDIRECT:
337 return "HTTP_STATUS_REDIRECT";
338 case HTTP_STATUS_REDIRECT_METHOD:
339 return "HTTP_STATUS_REDIRECT_METHOD";
340 case HTTP_STATUS_NOT_MODIFIED:
341 return "HTTP_STATUS_NOT_MODIFIED";
342 case HTTP_STATUS_USE_PROXY:
343 return "HTTP_STATUS_USE_PROXY";
344 case HTTP_STATUS_REDIRECT_KEEP_VERB:
345 return "HTTP_STATUS_REDIRECT_KEEP_VERB";
346 case HTTP_STATUS_BAD_REQUEST:
347 return "HTTP_STATUS_BAD_REQUEST";
348 case HTTP_STATUS_DENIED:
349 return "HTTP_STATUS_DENIED";
350 case HTTP_STATUS_PAYMENT_REQ:
351 return "HTTP_STATUS_PAYMENT_REQ";
352 case HTTP_STATUS_FORBIDDEN:
353 return "HTTP_STATUS_FORBIDDEN";
354 case HTTP_STATUS_NOT_FOUND:
355 return "HTTP_STATUS_NOT_FOUND";
356 case HTTP_STATUS_BAD_METHOD:
357 return "HTTP_STATUS_BAD_METHOD";
358 case HTTP_STATUS_NONE_ACCEPTABLE:
359 return "HTTP_STATUS_NONE_ACCEPTABLE";
360 case HTTP_STATUS_PROXY_AUTH_REQ:
361 return "HTTP_STATUS_PROXY_AUTH_REQ";
362 case HTTP_STATUS_REQUEST_TIMEOUT:
363 return "HTTP_STATUS_REQUEST_TIMEOUT";
364 case HTTP_STATUS_CONFLICT:
365 return "HTTP_STATUS_CONFLICT";
366 case HTTP_STATUS_GONE:
367 return "HTTP_STATUS_GONE";
368 case HTTP_STATUS_LENGTH_REQUIRED:
369 return "HTTP_STATUS_LENGTH_REQUIRED";
370 case HTTP_STATUS_PRECOND_FAILED:
371 return "HTTP_STATUS_PRECOND_FAILED";
372 case HTTP_STATUS_REQUEST_TOO_LARGE:
373 return "HTTP_STATUS_REQUEST_TOO_LARGE";
374 case HTTP_STATUS_URI_TOO_LONG:
375 return "HTTP_STATUS_URI_TOO_LONG";
376 case HTTP_STATUS_UNSUPPORTED_MEDIA:
377 return "HTTP_STATUS_UNSUPPORTED_MEDIA";
378 case HTTP_STATUS_RETRY_WITH:
379 return "HTTP_STATUS_RETRY_WITH";
380 case HTTP_STATUS_SERVER_ERROR:
381 return "HTTP_STATUS_SERVER_ERROR";
382 case HTTP_STATUS_NOT_SUPPORTED:
383 return "HTTP_STATUS_NOT_SUPPORTED";
384 case HTTP_STATUS_BAD_GATEWAY:
385 return "HTTP_STATUS_BAD_GATEWAY";
386 case HTTP_STATUS_SERVICE_UNAVAIL:
387 return "HTTP_STATUS_SERVICE_UNAVAIL";
388 case HTTP_STATUS_GATEWAY_TIMEOUT:
389 return "HTTP_STATUS_GATEWAY_TIMEOUT";
390 case HTTP_STATUS_VERSION_NOT_SUP:
391 return "HTTP_STATUS_VERSION_NOT_SUP";
393 return "HTTP_STATUS_UNKNOWN";
397 const char* freerdp_http_status_string_format(
long status,
char* buffer,
size_t size)
399 const char* code = freerdp_http_status_string(status);
400 (void)_snprintf(buffer, size,
"%s [%ld]", code, status);