FreeRDP
Loading...
Searching...
No Matches
core/gateway/http.c
1
20#include <freerdp/config.h>
21
22#include <errno.h>
23#include <stdint.h>
24
25#include <winpr/crt.h>
26#include <winpr/print.h>
27#include <winpr/stream.h>
28#include <winpr/string.h>
29#include <winpr/rpc.h>
30#include <winpr/sysinfo.h>
31
32#include <freerdp/log.h>
33#include <freerdp/crypto/crypto.h>
34
35/* websocket need sha1 for Sec-Websocket-Accept */
36#include <winpr/crypto.h>
37
38#ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
39#include <valgrind/memcheck.h>
40#endif
41
42#include "http.h"
43#include "../tcp.h"
44#include "../utils.h"
45
46#define TAG FREERDP_TAG("core.gateway.http")
47
48#define RESPONSE_SIZE_LIMIT (64ULL * 1024ULL * 1024ULL)
49
50#define WEBSOCKET_MAGIC_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
51
52struct s_http_context
53{
54 char* Method;
55 char* URI;
56 char* Connection;
57 char* Pragma;
58 BOOL websocketUpgrade;
59 char* SecWebsocketKey;
60 wListDictionary* cookies;
61 wHashTable* headers;
62};
63
64struct s_http_request
65{
66 char* Method;
67 char* URI;
68 char* AuthScheme;
69 char* AuthParam;
70 char* Authorization;
71 size_t ContentLength;
72 TRANSFER_ENCODING TransferEncoding;
73 wHashTable* headers;
74};
75
76struct s_http_response
77{
78 size_t count;
79 char** lines;
80
81 UINT16 StatusCode;
82 char* ReasonPhrase;
83
84 size_t ContentLength;
85 char* ContentType;
86 TRANSFER_ENCODING TransferEncoding;
87 char* SecWebsocketVersion;
88 char* SecWebsocketAccept;
89
90 size_t BodyLength;
91 char* BodyContent;
92
93 wHashTable* Authenticates;
94 wHashTable* SetCookie;
95 wStream* data;
96};
97
98static wHashTable* HashTable_New_String(void);
99
100static const char* string_strnstr(const char* str1, const char* str2, size_t slen)
101{
102 char c = 0;
103 char sc = 0;
104 size_t len = 0;
105
106 if ((c = *str2++) != '\0')
107 {
108 len = strnlen(str2, slen + 1);
109
110 do
111 {
112 do
113 {
114 if (slen-- < 1 || (sc = *str1++) == '\0')
115 return NULL;
116 } while (sc != c);
117
118 if (len > slen)
119 return NULL;
120 } while (strncmp(str1, str2, len) != 0);
121
122 str1--;
123 }
124
125 return str1;
126}
127
128static BOOL strings_equals_nocase(const void* obj1, const void* obj2)
129{
130 if (!obj1 || !obj2)
131 return FALSE;
132
133 return _stricmp(obj1, obj2) == 0;
134}
135
136HttpContext* http_context_new(void)
137{
138 HttpContext* context = (HttpContext*)calloc(1, sizeof(HttpContext));
139 if (!context)
140 return NULL;
141
142 context->headers = HashTable_New_String();
143 if (!context->headers)
144 goto fail;
145
146 context->cookies = ListDictionary_New(FALSE);
147 if (!context->cookies)
148 goto fail;
149
150 {
151 wObject* key = ListDictionary_KeyObject(context->cookies);
152 wObject* value = ListDictionary_ValueObject(context->cookies);
153 if (!key || !value)
154 goto fail;
155
156 key->fnObjectFree = winpr_ObjectStringFree;
157 key->fnObjectNew = winpr_ObjectStringClone;
158 value->fnObjectFree = winpr_ObjectStringFree;
159 value->fnObjectNew = winpr_ObjectStringClone;
160 }
161
162 return context;
163
164fail:
165 WINPR_PRAGMA_DIAG_PUSH
166 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
167 http_context_free(context);
168 WINPR_PRAGMA_DIAG_POP
169 return NULL;
170}
171
172BOOL http_context_set_method(HttpContext* context, const char* Method)
173{
174 if (!context || !Method)
175 return FALSE;
176
177 free(context->Method);
178 context->Method = _strdup(Method);
179
180 if (!context->Method)
181 return FALSE;
182
183 return TRUE;
184}
185
186BOOL http_request_set_content_type(HttpRequest* request, const char* ContentType)
187{
188 if (!request || !ContentType)
189 return FALSE;
190
191 return http_request_set_header(request, "Content-Type", "%s", ContentType);
192}
193
194const char* http_context_get_uri(HttpContext* context)
195{
196 if (!context)
197 return NULL;
198
199 return context->URI;
200}
201
202BOOL http_context_set_uri(HttpContext* context, const char* URI)
203{
204 if (!context || !URI)
205 return FALSE;
206
207 free(context->URI);
208 context->URI = _strdup(URI);
209
210 if (!context->URI)
211 return FALSE;
212
213 return TRUE;
214}
215
216BOOL http_context_set_user_agent(HttpContext* context, const char* UserAgent)
217{
218 if (!context || !UserAgent)
219 return FALSE;
220
221 return http_context_set_header(context, "User-Agent", "%s", UserAgent);
222}
223
224BOOL http_context_set_x_ms_user_agent(HttpContext* context, const char* X_MS_UserAgent)
225{
226 if (!context || !X_MS_UserAgent)
227 return FALSE;
228
229 return http_context_set_header(context, "X-MS-User-Agent", "%s", X_MS_UserAgent);
230}
231
232BOOL http_context_set_host(HttpContext* context, const char* Host)
233{
234 if (!context || !Host)
235 return FALSE;
236
237 return http_context_set_header(context, "Host", "%s", Host);
238}
239
240BOOL http_context_set_accept(HttpContext* context, const char* Accept)
241{
242 if (!context || !Accept)
243 return FALSE;
244
245 return http_context_set_header(context, "Accept", "%s", Accept);
246}
247
248BOOL http_context_set_cache_control(HttpContext* context, const char* CacheControl)
249{
250 if (!context || !CacheControl)
251 return FALSE;
252
253 return http_context_set_header(context, "Cache-Control", "%s", CacheControl);
254}
255
256BOOL http_context_set_connection(HttpContext* context, const char* Connection)
257{
258 if (!context || !Connection)
259 return FALSE;
260
261 free(context->Connection);
262 context->Connection = _strdup(Connection);
263
264 if (!context->Connection)
265 return FALSE;
266
267 return TRUE;
268}
269
270WINPR_ATTR_FORMAT_ARG(2, 0)
271static BOOL list_append(HttpContext* context, WINPR_FORMAT_ARG const char* str, va_list ap)
272{
273 BOOL rc = FALSE;
274 va_list vat;
275 char* Pragma = NULL;
276 size_t PragmaSize = 0;
277
278 va_copy(vat, ap);
279 const int size = winpr_vasprintf(&Pragma, &PragmaSize, str, ap);
280 va_end(vat);
281
282 if (size <= 0)
283 goto fail;
284
285 {
286 char* sstr = NULL;
287 size_t slen = 0;
288 if (context->Pragma)
289 {
290 winpr_asprintf(&sstr, &slen, "%s, %s", context->Pragma, Pragma);
291 free(Pragma);
292 }
293 else
294 sstr = Pragma;
295 Pragma = NULL;
296
297 free(context->Pragma);
298 context->Pragma = sstr;
299 }
300
301 rc = TRUE;
302
303fail:
304 va_end(ap);
305 return rc;
306}
307
308WINPR_ATTR_FORMAT_ARG(2, 3)
309BOOL http_context_set_pragma(HttpContext* context, WINPR_FORMAT_ARG const char* Pragma, ...)
310{
311 if (!context || !Pragma)
312 return FALSE;
313
314 free(context->Pragma);
315 context->Pragma = NULL;
316
317 va_list ap = { 0 };
318 va_start(ap, Pragma);
319 return list_append(context, Pragma, ap);
320}
321
322WINPR_ATTR_FORMAT_ARG(2, 3)
323BOOL http_context_append_pragma(HttpContext* context, const char* Pragma, ...)
324{
325 if (!context || !Pragma)
326 return FALSE;
327
328 va_list ap = { 0 };
329 va_start(ap, Pragma);
330 return list_append(context, Pragma, ap);
331}
332
333static char* guid2str(const GUID* guid, char* buffer, size_t len)
334{
335 if (!guid)
336 return NULL;
337 RPC_CSTR strguid = NULL;
338
339 RPC_STATUS rpcStatus = UuidToStringA(guid, &strguid);
340
341 if (rpcStatus != RPC_S_OK)
342 return NULL;
343
344 (void)sprintf_s(buffer, len, "{%s}", strguid);
345 RpcStringFreeA(&strguid);
346 return buffer;
347}
348
349BOOL http_context_set_rdg_connection_id(HttpContext* context, const GUID* RdgConnectionId)
350{
351 if (!context || !RdgConnectionId)
352 return FALSE;
353
354 char buffer[64] = { 0 };
355 return http_context_set_header(context, "RDG-Connection-Id", "%s",
356 guid2str(RdgConnectionId, buffer, sizeof(buffer)));
357}
358
359BOOL http_context_set_rdg_correlation_id(HttpContext* context, const GUID* RdgCorrelationId)
360{
361 if (!context || !RdgCorrelationId)
362 return FALSE;
363
364 char buffer[64] = { 0 };
365 return http_context_set_header(context, "RDG-Correlation-Id", "%s",
366 guid2str(RdgCorrelationId, buffer, sizeof(buffer)));
367}
368
369BOOL http_context_enable_websocket_upgrade(HttpContext* context, BOOL enable)
370{
371 if (!context)
372 return FALSE;
373
374 if (enable)
375 {
376 GUID key = { 0 };
377 if (RPC_S_OK != UuidCreate(&key))
378 return FALSE;
379
380 free(context->SecWebsocketKey);
381 context->SecWebsocketKey = crypto_base64_encode((BYTE*)&key, sizeof(key));
382 if (!context->SecWebsocketKey)
383 return FALSE;
384 }
385
386 context->websocketUpgrade = enable;
387 return TRUE;
388}
389
390BOOL http_context_is_websocket_upgrade_enabled(HttpContext* context)
391{
392 return context->websocketUpgrade;
393}
394
395BOOL http_context_set_rdg_auth_scheme(HttpContext* context, const char* RdgAuthScheme)
396{
397 if (!context || !RdgAuthScheme)
398 return FALSE;
399
400 return http_context_set_header(context, "RDG-Auth-Scheme", "%s", RdgAuthScheme);
401}
402
403BOOL http_context_set_cookie(HttpContext* context, const char* CookieName, const char* CookieValue)
404{
405 if (!context || !CookieName || !CookieValue)
406 return FALSE;
407 if (ListDictionary_Contains(context->cookies, CookieName))
408 {
409 if (!ListDictionary_SetItemValue(context->cookies, CookieName, CookieValue))
410 return FALSE;
411 }
412 else
413 {
414 if (!ListDictionary_Add(context->cookies, CookieName, CookieValue))
415 return FALSE;
416 }
417 return TRUE;
418}
419
420void http_context_free(HttpContext* context)
421{
422 if (context)
423 {
424 free(context->SecWebsocketKey);
425 free(context->URI);
426 free(context->Method);
427 free(context->Connection);
428 free(context->Pragma);
429 HashTable_Free(context->headers);
430 ListDictionary_Free(context->cookies);
431 free(context);
432 }
433}
434
435BOOL http_request_set_method(HttpRequest* request, const char* Method)
436{
437 if (!request || !Method)
438 return FALSE;
439
440 free(request->Method);
441 request->Method = _strdup(Method);
442
443 if (!request->Method)
444 return FALSE;
445
446 return TRUE;
447}
448
449BOOL http_request_set_uri(HttpRequest* request, const char* URI)
450{
451 if (!request || !URI)
452 return FALSE;
453
454 free(request->URI);
455 request->URI = _strdup(URI);
456
457 if (!request->URI)
458 return FALSE;
459
460 return TRUE;
461}
462
463BOOL http_request_set_auth_scheme(HttpRequest* request, const char* AuthScheme)
464{
465 if (!request || !AuthScheme)
466 return FALSE;
467
468 free(request->AuthScheme);
469 request->AuthScheme = _strdup(AuthScheme);
470
471 if (!request->AuthScheme)
472 return FALSE;
473
474 return TRUE;
475}
476
477BOOL http_request_set_auth_param(HttpRequest* request, const char* AuthParam)
478{
479 if (!request || !AuthParam)
480 return FALSE;
481
482 free(request->AuthParam);
483 request->AuthParam = _strdup(AuthParam);
484
485 if (!request->AuthParam)
486 return FALSE;
487
488 return TRUE;
489}
490
491BOOL http_request_set_transfer_encoding(HttpRequest* request, TRANSFER_ENCODING TransferEncoding)
492{
493 if (!request || TransferEncoding == TransferEncodingUnknown)
494 return FALSE;
495
496 request->TransferEncoding = TransferEncoding;
497
498 return TRUE;
499}
500
501WINPR_ATTR_FORMAT_ARG(2, 3)
502static BOOL http_encode_print(wStream* s, WINPR_FORMAT_ARG const char* fmt, ...)
503{
504 char* str = NULL;
505 va_list ap = { 0 };
506 int length = 0;
507 int used = 0;
508
509 if (!s || !fmt)
510 return FALSE;
511
512 va_start(ap, fmt);
513 length = vsnprintf(NULL, 0, fmt, ap) + 1;
514 va_end(ap);
515
516 if (!Stream_EnsureRemainingCapacity(s, (size_t)length))
517 return FALSE;
518
519 str = (char*)Stream_Pointer(s);
520 va_start(ap, fmt);
521 used = vsnprintf(str, (size_t)length, fmt, ap);
522 va_end(ap);
523
524 /* Strip the trailing '\0' from the string. */
525 if ((used + 1) != length)
526 return FALSE;
527
528 Stream_Seek(s, (size_t)used);
529 return TRUE;
530}
531
532static BOOL http_encode_body_line(wStream* s, const char* param, const char* value)
533{
534 if (!s || !param || !value)
535 return FALSE;
536
537 return http_encode_print(s, "%s: %s\r\n", param, value);
538}
539
540static BOOL http_encode_content_length_line(wStream* s, size_t ContentLength)
541{
542 return http_encode_print(s, "Content-Length: %" PRIuz "\r\n", ContentLength);
543}
544
545static BOOL http_encode_header_line(wStream* s, const char* Method, const char* URI)
546{
547 if (!s || !Method || !URI)
548 return FALSE;
549
550 return http_encode_print(s, "%s %s HTTP/1.1\r\n", Method, URI);
551}
552
553static BOOL http_encode_authorization_line(wStream* s, const char* AuthScheme,
554 const char* AuthParam)
555{
556 if (!s || !AuthScheme || !AuthParam)
557 return FALSE;
558
559 return http_encode_print(s, "Authorization: %s %s\r\n", AuthScheme, AuthParam);
560}
561
562static BOOL http_encode_cookie_line(wStream* s, wListDictionary* cookies)
563{
564 ULONG_PTR* keys = NULL;
565 BOOL status = TRUE;
566
567 if (!s && !cookies)
568 return FALSE;
569
570 ListDictionary_Lock(cookies);
571 const size_t count = ListDictionary_GetKeys(cookies, &keys);
572
573 if (count == 0)
574 goto unlock;
575
576 status = http_encode_print(s, "Cookie: ");
577 if (!status)
578 goto unlock;
579
580 for (size_t x = 0; status && x < count; x++)
581 {
582 char* cur = (char*)ListDictionary_GetItemValue(cookies, (void*)keys[x]);
583 if (!cur)
584 {
585 status = FALSE;
586 continue;
587 }
588 if (x > 0)
589 {
590 status = http_encode_print(s, "; ");
591 if (!status)
592 continue;
593 }
594 status = http_encode_print(s, "%s=%s", (char*)keys[x], cur);
595 }
596
597 status = http_encode_print(s, "\r\n");
598unlock:
599 free(keys);
600 ListDictionary_Unlock(cookies);
601 return status;
602}
603
604static BOOL write_headers(const void* pkey, void* pvalue, void* arg)
605{
606 const char* key = pkey;
607 const char* value = pvalue;
608 wStream* s = arg;
609
610 WINPR_ASSERT(key);
611 WINPR_ASSERT(value);
612 WINPR_ASSERT(s);
613
614 return http_encode_body_line(s, key, value);
615}
616
617wStream* http_request_write(HttpContext* context, HttpRequest* request)
618{
619 wStream* s = NULL;
620
621 if (!context || !request)
622 return NULL;
623
624 s = Stream_New(NULL, 1024);
625
626 if (!s)
627 return NULL;
628
629 if (!http_encode_header_line(s, request->Method, request->URI) ||
630
631 !http_encode_body_line(s, "Pragma", context->Pragma))
632 goto fail;
633
634 if (!context->websocketUpgrade)
635 {
636 if (!http_encode_body_line(s, "Connection", context->Connection))
637 goto fail;
638 }
639 else
640 {
641 if (!http_encode_body_line(s, "Connection", "Upgrade") ||
642 !http_encode_body_line(s, "Upgrade", "websocket") ||
643 !http_encode_body_line(s, "Sec-Websocket-Version", "13") ||
644 !http_encode_body_line(s, "Sec-Websocket-Key", context->SecWebsocketKey))
645 goto fail;
646 }
647
648 if (request->TransferEncoding != TransferEncodingIdentity)
649 {
650 if (request->TransferEncoding == TransferEncodingChunked)
651 {
652 if (!http_encode_body_line(s, "Transfer-Encoding", "chunked"))
653 goto fail;
654 }
655 else
656 goto fail;
657 }
658 else
659 {
660 if (!http_encode_content_length_line(s, request->ContentLength))
661 goto fail;
662 }
663
664 if (!utils_str_is_empty(request->Authorization))
665 {
666 if (!http_encode_body_line(s, "Authorization", request->Authorization))
667 goto fail;
668 }
669 else if (!utils_str_is_empty(request->AuthScheme) && !utils_str_is_empty(request->AuthParam))
670 {
671 if (!http_encode_authorization_line(s, request->AuthScheme, request->AuthParam))
672 goto fail;
673 }
674
675 if (!HashTable_Foreach(context->headers, write_headers, s))
676 goto fail;
677
678 if (!HashTable_Foreach(request->headers, write_headers, s))
679 goto fail;
680
681 if (!http_encode_cookie_line(s, context->cookies))
682 goto fail;
683
684 if (!http_encode_print(s, "\r\n"))
685 goto fail;
686
687 Stream_SealLength(s);
688 return s;
689fail:
690 Stream_Free(s, TRUE);
691 return NULL;
692}
693
694HttpRequest* http_request_new(void)
695{
696 HttpRequest* request = (HttpRequest*)calloc(1, sizeof(HttpRequest));
697 if (!request)
698 return NULL;
699
700 request->headers = HashTable_New_String();
701 if (!request->headers)
702 goto fail;
703 request->TransferEncoding = TransferEncodingIdentity;
704 return request;
705fail:
706 http_request_free(request);
707 return NULL;
708}
709
710void http_request_free(HttpRequest* request)
711{
712 if (!request)
713 return;
714
715 free(request->AuthParam);
716 free(request->AuthScheme);
717 free(request->Authorization);
718 free(request->Method);
719 free(request->URI);
720 HashTable_Free(request->headers);
721 free(request);
722}
723
724static BOOL http_response_parse_header_status_line(HttpResponse* response, const char* status_line)
725{
726 BOOL rc = FALSE;
727 char* separator = NULL;
728 char* status_code = NULL;
729
730 if (!response)
731 goto fail;
732
733 if (status_line)
734 separator = strchr(status_line, ' ');
735
736 if (!separator)
737 goto fail;
738
739 status_code = separator + 1;
740 separator = strchr(status_code, ' ');
741
742 if (!separator)
743 goto fail;
744
745 {
746 const char* reason_phrase = separator + 1;
747 *separator = '\0';
748 errno = 0;
749 {
750 long val = strtol(status_code, NULL, 0);
751
752 if ((errno != 0) || (val < 0) || (val > INT16_MAX))
753 goto fail;
754
755 response->StatusCode = (UINT16)val;
756 }
757 free(response->ReasonPhrase);
758 response->ReasonPhrase = _strdup(reason_phrase);
759 }
760
761 if (!response->ReasonPhrase)
762 goto fail;
763
764 *separator = ' ';
765 rc = TRUE;
766fail:
767
768 if (!rc)
769 WLog_ERR(TAG, "http_response_parse_header_status_line failed [%s]", status_line);
770
771 return rc;
772}
773
774static BOOL http_response_parse_header_field(HttpResponse* response, const char* name,
775 const char* value)
776{
777 WINPR_ASSERT(response);
778
779 if (!name || !value)
780 return FALSE;
781
782 if (_stricmp(name, "Content-Length") == 0)
783 {
784 unsigned long long val = 0;
785 errno = 0;
786 val = _strtoui64(value, NULL, 0);
787
788 if ((errno != 0) || (val > INT32_MAX))
789 return FALSE;
790
791 response->ContentLength = WINPR_ASSERTING_INT_CAST(size_t, val);
792 return TRUE;
793 }
794
795 if (_stricmp(name, "Content-Type") == 0)
796 {
797 free(response->ContentType);
798 response->ContentType = _strdup(value);
799
800 return response->ContentType != NULL;
801 }
802
803 if (_stricmp(name, "Transfer-Encoding") == 0)
804 {
805 if (_stricmp(value, "identity") == 0)
806 response->TransferEncoding = TransferEncodingIdentity;
807 else if (_stricmp(value, "chunked") == 0)
808 response->TransferEncoding = TransferEncodingChunked;
809 else
810 response->TransferEncoding = TransferEncodingUnknown;
811
812 return TRUE;
813 }
814
815 if (_stricmp(name, "Sec-WebSocket-Version") == 0)
816 {
817 free(response->SecWebsocketVersion);
818 response->SecWebsocketVersion = _strdup(value);
819
820 return response->SecWebsocketVersion != NULL;
821 }
822
823 if (_stricmp(name, "Sec-WebSocket-Accept") == 0)
824 {
825 free(response->SecWebsocketAccept);
826 response->SecWebsocketAccept = _strdup(value);
827
828 return response->SecWebsocketAccept != NULL;
829 }
830
831 if (_stricmp(name, "WWW-Authenticate") == 0)
832 {
833 const char* authScheme = value;
834 const char* authValue = "";
835 char* separator = strchr(value, ' ');
836
837 if (separator)
838 {
839 /* WWW-Authenticate: Basic realm=""
840 * WWW-Authenticate: NTLM base64token
841 * WWW-Authenticate: Digest realm="testrealm@host.com", qop="auth, auth-int",
842 * nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
843 * opaque="5ccc069c403ebaf9f0171e9517f40e41"
844 */
845 *separator = '\0';
846 authValue = separator + 1;
847 }
848
849 return HashTable_Insert(response->Authenticates, authScheme, authValue);
850 }
851
852 if (_stricmp(name, "Set-Cookie") == 0)
853 {
854 char* separator = strchr(value, '=');
855
856 if (!separator)
857 return FALSE;
858
859 /* Set-Cookie: name=value
860 * Set-Cookie: name=value; Attribute=value
861 * Set-Cookie: name="value with spaces"; Attribute=value
862 */
863 *separator = '\0';
864 const char* CookieName = value;
865 char* CookieValue = separator + 1;
866
867 if (*CookieValue == '"')
868 {
869 char* p = CookieValue;
870 while (*p != '"' && *p != '\0')
871 {
872 p++;
873 if (*p == '\\')
874 p++;
875 }
876 *p = '\0';
877 }
878 else
879 {
880 char* p = CookieValue;
881 while (*p != ';' && *p != '\0' && *p != ' ')
882 {
883 p++;
884 }
885 *p = '\0';
886 }
887 return HashTable_Insert(response->SetCookie, CookieName, CookieValue);
888 }
889
890 /* Ignore unknown lines */
891 return TRUE;
892}
893
894static BOOL http_response_parse_header(HttpResponse* response)
895{
896 BOOL rc = FALSE;
897 char c = 0;
898 char* line = NULL;
899 char* name = NULL;
900 char* colon_pos = NULL;
901 char* end_of_header = NULL;
902 char end_of_header_char = 0;
903
904 if (!response)
905 goto fail;
906
907 if (!response->lines)
908 goto fail;
909
910 if (!http_response_parse_header_status_line(response, response->lines[0]))
911 goto fail;
912
913 for (size_t count = 1; count < response->count; count++)
914 {
915 line = response->lines[count];
916
926 if (line)
927 colon_pos = strchr(line, ':');
928 else
929 colon_pos = NULL;
930
931 if ((colon_pos == NULL) || (colon_pos == line))
932 return FALSE;
933
934 /* retrieve the position just after header name */
935 for (end_of_header = colon_pos; end_of_header != line; end_of_header--)
936 {
937 c = end_of_header[-1];
938
939 if (c != ' ' && c != '\t' && c != ':')
940 break;
941 }
942
943 if (end_of_header == line)
944 goto fail;
945
946 end_of_header_char = *end_of_header;
947 *end_of_header = '\0';
948 name = line;
949
950 /* eat space and tabs before header value */
951 char* value = colon_pos + 1;
952 for (; *value; value++)
953 {
954 if ((*value != ' ') && (*value != '\t'))
955 break;
956 }
957
958 const int res = http_response_parse_header_field(response, name, value);
959 *end_of_header = end_of_header_char;
960 if (!res)
961 goto fail;
962 }
963
964 rc = TRUE;
965fail:
966
967 if (!rc)
968 WLog_ERR(TAG, "parsing failed");
969
970 return rc;
971}
972
973static void http_response_print(wLog* log, DWORD level, const HttpResponse* response,
974 const char* file, size_t line, const char* fkt)
975{
976 char buffer[64] = { 0 };
977
978 WINPR_ASSERT(log);
979 WINPR_ASSERT(response);
980
981 if (!WLog_IsLevelActive(log, level))
982 return;
983
984 const long status = http_response_get_status_code(response);
985 WLog_PrintTextMessage(log, level, line, file, fkt, "HTTP status: %s",
986 freerdp_http_status_string_format(status, buffer, ARRAYSIZE(buffer)));
987
988 if (WLog_IsLevelActive(log, WLOG_DEBUG))
989 {
990 for (size_t i = 0; i < response->count; i++)
991 WLog_PrintTextMessage(log, WLOG_DEBUG, line, file, fkt, "[%" PRIuz "] %s", i,
992 response->lines[i]);
993 }
994
995 if (response->ReasonPhrase)
996 WLog_PrintTextMessage(log, level, line, file, fkt, "[reason] %s", response->ReasonPhrase);
997
998 if (WLog_IsLevelActive(log, WLOG_TRACE))
999 {
1000 WLog_PrintTextMessage(log, WLOG_TRACE, line, file, fkt, "[body][%" PRIuz "] %s",
1001 response->BodyLength, response->BodyContent);
1002 }
1003}
1004
1005static BOOL http_use_content_length(const char* cur)
1006{
1007 size_t pos = 0;
1008
1009 if (!cur)
1010 return FALSE;
1011
1012 if (_strnicmp(cur, "application/rpc", 15) == 0)
1013 pos = 15;
1014 else if (_strnicmp(cur, "text/plain", 10) == 0)
1015 pos = 10;
1016 else if (_strnicmp(cur, "text/html", 9) == 0)
1017 pos = 9;
1018 else if (_strnicmp(cur, "application/json", 16) == 0)
1019 pos = 16;
1020
1021 if (pos > 0)
1022 {
1023 char end = cur[pos];
1024
1025 switch (end)
1026 {
1027 case ' ':
1028 case ';':
1029 case '\0':
1030 case '\r':
1031 case '\n':
1032 return TRUE;
1033
1034 default:
1035 return FALSE;
1036 }
1037 }
1038
1039 return FALSE;
1040}
1041
1042static int print_bio_error(const char* str, size_t len, void* bp)
1043{
1044 wLog* log = bp;
1045
1046 WINPR_UNUSED(bp);
1047 WLog_Print(log, WLOG_ERROR, "%s", str);
1048 if (len > INT32_MAX)
1049 return -1;
1050 return (int)len;
1051}
1052
1053int http_chuncked_read(BIO* bio, BYTE* pBuffer, size_t size,
1054 http_encoding_chunked_context* encodingContext)
1055{
1056 int status = 0;
1057 int effectiveDataLen = 0;
1058 WINPR_ASSERT(bio);
1059 WINPR_ASSERT(pBuffer);
1060 WINPR_ASSERT(encodingContext != NULL);
1061 WINPR_ASSERT(size <= INT32_MAX);
1062 while (TRUE)
1063 {
1064 switch (encodingContext->state)
1065 {
1066 case ChunkStateData:
1067 {
1068 const size_t rd =
1069 (size > encodingContext->nextOffset ? encodingContext->nextOffset : size);
1070 if (rd > INT32_MAX)
1071 return -1;
1072
1073 ERR_clear_error();
1074 status = BIO_read(bio, pBuffer, (int)rd);
1075 if (status <= 0)
1076 return (effectiveDataLen > 0 ? effectiveDataLen : status);
1077
1078 encodingContext->nextOffset -= WINPR_ASSERTING_INT_CAST(uint32_t, status);
1079 if (encodingContext->nextOffset == 0)
1080 {
1081 encodingContext->state = ChunkStateFooter;
1082 encodingContext->headerFooterPos = 0;
1083 }
1084 effectiveDataLen += status;
1085
1086 if ((size_t)status == size)
1087 return effectiveDataLen;
1088
1089 pBuffer += status;
1090 size -= (size_t)status;
1091 }
1092 break;
1093 case ChunkStateFooter:
1094 {
1095 char _dummy[2] = { 0 };
1096 WINPR_ASSERT(encodingContext->nextOffset == 0);
1097 WINPR_ASSERT(encodingContext->headerFooterPos < 2);
1098 ERR_clear_error();
1099 status = BIO_read(bio, _dummy, (int)(2 - encodingContext->headerFooterPos));
1100 if (status >= 0)
1101 {
1102 encodingContext->headerFooterPos += (size_t)status;
1103 if (encodingContext->headerFooterPos == 2)
1104 {
1105 encodingContext->state = ChunkStateLenghHeader;
1106 encodingContext->headerFooterPos = 0;
1107 }
1108 }
1109 else
1110 return (effectiveDataLen > 0 ? effectiveDataLen : status);
1111 }
1112 break;
1113 case ChunkStateLenghHeader:
1114 {
1115 BOOL _haveNewLine = FALSE;
1116 char* dst = &encodingContext->lenBuffer[encodingContext->headerFooterPos];
1117 WINPR_ASSERT(encodingContext->nextOffset == 0);
1118 while (encodingContext->headerFooterPos < 10 && !_haveNewLine)
1119 {
1120 ERR_clear_error();
1121 status = BIO_read(bio, dst, 1);
1122 if (status >= 0)
1123 {
1124 if (*dst == '\n')
1125 _haveNewLine = TRUE;
1126 encodingContext->headerFooterPos += (size_t)status;
1127 dst += status;
1128 }
1129 else
1130 return (effectiveDataLen > 0 ? effectiveDataLen : status);
1131 }
1132 *dst = '\0';
1133 /* strtoul is tricky, error are reported via errno, we also need
1134 * to ensure the result does not overflow */
1135 errno = 0;
1136 size_t tmp = strtoul(encodingContext->lenBuffer, NULL, 16);
1137 if ((errno != 0) || (tmp > SIZE_MAX))
1138 {
1139 /* denote end of stream if something bad happens */
1140 encodingContext->nextOffset = 0;
1141 encodingContext->state = ChunkStateEnd;
1142 return -1;
1143 }
1144 encodingContext->nextOffset = tmp;
1145 encodingContext->state = ChunkStateData;
1146
1147 if (encodingContext->nextOffset == 0)
1148 { /* end of stream */
1149 WLog_DBG(TAG, "chunked encoding end of stream received");
1150 encodingContext->headerFooterPos = 0;
1151 encodingContext->state = ChunkStateEnd;
1152 return (effectiveDataLen > 0 ? effectiveDataLen : 0);
1153 }
1154 }
1155 break;
1156 default:
1157 /* invalid state / ChunkStateEnd */
1158 return -1;
1159 }
1160 }
1161}
1162
1163#define sleep_or_timeout(tls, startMS, timeoutMS) \
1164 sleep_or_timeout_((tls), (startMS), (timeoutMS), __FILE__, __func__, __LINE__)
1165static BOOL sleep_or_timeout_(rdpTls* tls, UINT64 startMS, UINT32 timeoutMS, const char* file,
1166 const char* fkt, size_t line)
1167{
1168 WINPR_ASSERT(tls);
1169
1170 USleep(100);
1171 const UINT64 nowMS = GetTickCount64();
1172 if (nowMS - startMS > timeoutMS)
1173 {
1174 DWORD level = WLOG_ERROR;
1175 wLog* log = WLog_Get(TAG);
1176 if (WLog_IsLevelActive(log, level))
1177 WLog_PrintTextMessage(log, level, line, file, fkt, "timeout [%" PRIu32 "ms] exceeded",
1178 timeoutMS);
1179 return TRUE;
1180 }
1181 if (!BIO_should_retry(tls->bio))
1182 {
1183 DWORD level = WLOG_ERROR;
1184 wLog* log = WLog_Get(TAG);
1185 if (WLog_IsLevelActive(log, level))
1186 {
1187 WLog_PrintTextMessage(log, level, line, file, fkt, "Retries exceeded");
1188 ERR_print_errors_cb(print_bio_error, log);
1189 }
1190 return TRUE;
1191 }
1192 if (freerdp_shall_disconnect_context(tls->context))
1193 return TRUE;
1194
1195 return FALSE;
1196}
1197
1198static SSIZE_T http_response_recv_line(rdpTls* tls, HttpResponse* response)
1199{
1200 WINPR_ASSERT(tls);
1201 WINPR_ASSERT(response);
1202
1203 SSIZE_T payloadOffset = -1;
1204 const UINT32 timeoutMS =
1205 freerdp_settings_get_uint32(tls->context->settings, FreeRDP_TcpConnectTimeout);
1206 const UINT64 startMS = GetTickCount64();
1207 while (payloadOffset <= 0)
1208 {
1209 size_t bodyLength = 0;
1210 size_t position = 0;
1211 int status = -1;
1212 size_t s = 0;
1213 char* end = NULL;
1214 /* Read until we encounter \r\n\r\n */
1215 ERR_clear_error();
1216
1217 status = BIO_read(tls->bio, Stream_Pointer(response->data), 1);
1218 if (status <= 0)
1219 {
1220 if (sleep_or_timeout(tls, startMS, timeoutMS))
1221 goto out_error;
1222 continue;
1223 }
1224
1225#ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
1226 VALGRIND_MAKE_MEM_DEFINED(Stream_Pointer(response->data), status);
1227#endif
1228 Stream_Seek(response->data, (size_t)status);
1229
1230 if (!Stream_EnsureRemainingCapacity(response->data, 1024))
1231 goto out_error;
1232
1233 position = Stream_GetPosition(response->data);
1234
1235 if (position < 4)
1236 continue;
1237 else if (position > RESPONSE_SIZE_LIMIT)
1238 {
1239 WLog_ERR(TAG, "Request header too large! (%" PRIdz " bytes) Aborting!", bodyLength);
1240 goto out_error;
1241 }
1242
1243 /* Always check at most the lase 8 bytes for occurrence of the desired
1244 * sequence of \r\n\r\n */
1245 s = (position > 8) ? 8 : position;
1246 end = (char*)Stream_Pointer(response->data) - s;
1247
1248 if (string_strnstr(end, "\r\n\r\n", s) != NULL)
1249 payloadOffset = WINPR_ASSERTING_INT_CAST(SSIZE_T, Stream_GetPosition(response->data));
1250 }
1251
1252out_error:
1253 return payloadOffset;
1254}
1255
1256static BOOL http_response_recv_body(rdpTls* tls, HttpResponse* response, BOOL readContentLength,
1257 size_t payloadOffset, size_t bodyLength)
1258{
1259 BOOL rc = FALSE;
1260
1261 WINPR_ASSERT(tls);
1262 WINPR_ASSERT(response);
1263
1264 const UINT64 startMS = GetTickCount64();
1265 const UINT32 timeoutMS =
1266 freerdp_settings_get_uint32(tls->context->settings, FreeRDP_TcpConnectTimeout);
1267
1268 if ((response->TransferEncoding == TransferEncodingChunked) && readContentLength)
1269 {
1271 ctx.state = ChunkStateLenghHeader;
1272 ctx.nextOffset = 0;
1273 ctx.headerFooterPos = 0;
1274 int full_len = 0;
1275 do
1276 {
1277 if (!Stream_EnsureRemainingCapacity(response->data, 2048))
1278 goto out_error;
1279
1280 int status = http_chuncked_read(tls->bio, Stream_Pointer(response->data),
1281 Stream_GetRemainingCapacity(response->data), &ctx);
1282 if (status <= 0)
1283 {
1284 if (sleep_or_timeout(tls, startMS, timeoutMS))
1285 goto out_error;
1286 }
1287 else
1288 {
1289 Stream_Seek(response->data, (size_t)status);
1290 full_len += status;
1291 }
1292 } while (ctx.state != ChunkStateEnd);
1293 response->BodyLength = WINPR_ASSERTING_INT_CAST(uint32_t, full_len);
1294 if (response->BodyLength > 0)
1295 response->BodyContent = &(Stream_BufferAs(response->data, char))[payloadOffset];
1296 }
1297 else
1298 {
1299 while (response->BodyLength < bodyLength)
1300 {
1301 int status = 0;
1302
1303 if (!Stream_EnsureRemainingCapacity(response->data, bodyLength - response->BodyLength))
1304 goto out_error;
1305
1306 ERR_clear_error();
1307 size_t diff = bodyLength - response->BodyLength;
1308 if (diff > INT32_MAX)
1309 diff = INT32_MAX;
1310 status = BIO_read(tls->bio, Stream_Pointer(response->data), (int)diff);
1311
1312 if (status <= 0)
1313 {
1314 if (sleep_or_timeout(tls, startMS, timeoutMS))
1315 goto out_error;
1316 continue;
1317 }
1318
1319 Stream_Seek(response->data, (size_t)status);
1320 response->BodyLength += (unsigned long)status;
1321
1322 if (response->BodyLength > RESPONSE_SIZE_LIMIT)
1323 {
1324 WLog_ERR(TAG, "Request body too large! (%" PRIdz " bytes) Aborting!",
1325 response->BodyLength);
1326 goto out_error;
1327 }
1328 }
1329
1330 if (response->BodyLength > 0)
1331 response->BodyContent = &(Stream_BufferAs(response->data, char))[payloadOffset];
1332
1333 if (bodyLength != response->BodyLength)
1334 {
1335 WLog_WARN(TAG, "%s unexpected body length: actual: %" PRIuz ", expected: %" PRIuz,
1336 response->ContentType, response->BodyLength, bodyLength);
1337
1338 if (bodyLength > 0)
1339 response->BodyLength = MIN(bodyLength, response->BodyLength);
1340 }
1341
1342 /* '\0' terminate the http body */
1343 if (!Stream_EnsureRemainingCapacity(response->data, sizeof(UINT16)))
1344 goto out_error;
1345 Stream_Write_UINT16(response->data, 0);
1346 }
1347
1348 rc = TRUE;
1349out_error:
1350 return rc;
1351}
1352
1353static void clear_lines(HttpResponse* response)
1354{
1355 WINPR_ASSERT(response);
1356
1357 for (size_t x = 0; x < response->count; x++)
1358 {
1359 WINPR_ASSERT(response->lines);
1360 char* line = response->lines[x];
1361 free(line);
1362 }
1363
1364 free((void*)response->lines);
1365 response->lines = NULL;
1366 response->count = 0;
1367}
1368
1369HttpResponse* http_response_recv(rdpTls* tls, BOOL readContentLength)
1370{
1371 size_t bodyLength = 0;
1372 HttpResponse* response = http_response_new();
1373
1374 if (!response)
1375 return NULL;
1376
1377 response->ContentLength = 0;
1378
1379 const SSIZE_T payloadOffset = http_response_recv_line(tls, response);
1380 if (payloadOffset < 0)
1381 goto out_error;
1382
1383 if (payloadOffset)
1384 {
1385 size_t count = 0;
1386 char* buffer = Stream_BufferAs(response->data, char);
1387 const char* line = Stream_BufferAs(response->data, char);
1388 char* context = NULL;
1389
1390 while ((line = string_strnstr(line, "\r\n",
1391 WINPR_ASSERTING_INT_CAST(size_t, payloadOffset) -
1392 WINPR_ASSERTING_INT_CAST(size_t, (line - buffer)) - 2UL)))
1393 {
1394 line += 2;
1395 count++;
1396 }
1397
1398 clear_lines(response);
1399 response->count = count;
1400
1401 if (count)
1402 {
1403 response->lines = (char**)calloc(response->count, sizeof(char*));
1404
1405 if (!response->lines)
1406 goto out_error;
1407 }
1408
1409 buffer[payloadOffset - 1] = '\0';
1410 buffer[payloadOffset - 2] = '\0';
1411 count = 0;
1412 line = strtok_s(buffer, "\r\n", &context);
1413
1414 while (line && (response->count > count))
1415 {
1416 response->lines[count] = _strdup(line);
1417 if (!response->lines[count])
1418 goto out_error;
1419 line = strtok_s(NULL, "\r\n", &context);
1420 count++;
1421 }
1422
1423 if (!http_response_parse_header(response))
1424 goto out_error;
1425
1426 response->BodyLength =
1427 Stream_GetPosition(response->data) - WINPR_ASSERTING_INT_CAST(size_t, payloadOffset);
1428
1429 WINPR_ASSERT(response->BodyLength == 0);
1430 bodyLength = response->BodyLength; /* expected body length */
1431
1432 if (readContentLength && (response->ContentLength > 0))
1433 {
1434 const char* cur = response->ContentType;
1435
1436 while (cur != NULL)
1437 {
1438 if (http_use_content_length(cur))
1439 {
1440 if (response->ContentLength < RESPONSE_SIZE_LIMIT)
1441 bodyLength = response->ContentLength;
1442
1443 break;
1444 }
1445 else
1446 readContentLength = FALSE; /* prevent chunked read */
1447
1448 cur = strchr(cur, ';');
1449 }
1450 }
1451
1452 if (bodyLength > RESPONSE_SIZE_LIMIT)
1453 {
1454 WLog_ERR(TAG, "Expected request body too large! (%" PRIdz " bytes) Aborting!",
1455 bodyLength);
1456 goto out_error;
1457 }
1458
1459 /* Fetch remaining body! */
1460 if (!http_response_recv_body(tls, response, readContentLength,
1461 WINPR_ASSERTING_INT_CAST(size_t, payloadOffset), bodyLength))
1462 goto out_error;
1463 }
1464 Stream_SealLength(response->data);
1465
1466 /* Ensure '\0' terminated string */
1467 if (!Stream_EnsureRemainingCapacity(response->data, 2))
1468 goto out_error;
1469 Stream_Write_UINT16(response->data, 0);
1470
1471 return response;
1472out_error:
1473 WLog_ERR(TAG, "No response");
1474 http_response_free(response);
1475 return NULL;
1476}
1477
1478const char* http_response_get_body(const HttpResponse* response)
1479{
1480 if (!response)
1481 return NULL;
1482
1483 return response->BodyContent;
1484}
1485
1486wHashTable* HashTable_New_String(void)
1487{
1488 wHashTable* table = HashTable_New(FALSE);
1489 if (!table)
1490 return NULL;
1491
1492 if (!HashTable_SetupForStringData(table, TRUE))
1493 {
1494 HashTable_Free(table);
1495 return NULL;
1496 }
1497 HashTable_KeyObject(table)->fnObjectEquals = strings_equals_nocase;
1498 HashTable_ValueObject(table)->fnObjectEquals = strings_equals_nocase;
1499 return table;
1500}
1501
1502HttpResponse* http_response_new(void)
1503{
1504 HttpResponse* response = (HttpResponse*)calloc(1, sizeof(HttpResponse));
1505
1506 if (!response)
1507 return NULL;
1508
1509 response->Authenticates = HashTable_New_String();
1510
1511 if (!response->Authenticates)
1512 goto fail;
1513
1514 response->SetCookie = HashTable_New_String();
1515
1516 if (!response->SetCookie)
1517 goto fail;
1518
1519 response->data = Stream_New(NULL, 2048);
1520
1521 if (!response->data)
1522 goto fail;
1523
1524 response->TransferEncoding = TransferEncodingIdentity;
1525 return response;
1526fail:
1527 WINPR_PRAGMA_DIAG_PUSH
1528 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1529 http_response_free(response);
1530 WINPR_PRAGMA_DIAG_POP
1531 return NULL;
1532}
1533
1534void http_response_free(HttpResponse* response)
1535{
1536 if (!response)
1537 return;
1538
1539 clear_lines(response);
1540 free(response->ReasonPhrase);
1541 free(response->ContentType);
1542 free(response->SecWebsocketAccept);
1543 free(response->SecWebsocketVersion);
1544 HashTable_Free(response->Authenticates);
1545 HashTable_Free(response->SetCookie);
1546 Stream_Free(response->data, TRUE);
1547 free(response);
1548}
1549
1550const char* http_request_get_uri(HttpRequest* request)
1551{
1552 if (!request)
1553 return NULL;
1554
1555 return request->URI;
1556}
1557
1558SSIZE_T http_request_get_content_length(HttpRequest* request)
1559{
1560 if (!request)
1561 return -1;
1562
1563 return (SSIZE_T)request->ContentLength;
1564}
1565
1566BOOL http_request_set_content_length(HttpRequest* request, size_t length)
1567{
1568 if (!request)
1569 return FALSE;
1570
1571 request->ContentLength = length;
1572 return TRUE;
1573}
1574
1575UINT16 http_response_get_status_code(const HttpResponse* response)
1576{
1577 WINPR_ASSERT(response);
1578
1579 return response->StatusCode;
1580}
1581
1582size_t http_response_get_body_length(const HttpResponse* response)
1583{
1584 WINPR_ASSERT(response);
1585
1586 return response->BodyLength;
1587}
1588
1589const char* http_response_get_auth_token(const HttpResponse* response, const char* method)
1590{
1591 if (!response || !method)
1592 return NULL;
1593
1594 return HashTable_GetItemValue(response->Authenticates, method);
1595}
1596
1597const char* http_response_get_setcookie(const HttpResponse* response, const char* cookie)
1598{
1599 if (!response || !cookie)
1600 return NULL;
1601
1602 return HashTable_GetItemValue(response->SetCookie, cookie);
1603}
1604
1605TRANSFER_ENCODING http_response_get_transfer_encoding(const HttpResponse* response)
1606{
1607 if (!response)
1608 return TransferEncodingUnknown;
1609
1610 return response->TransferEncoding;
1611}
1612
1613BOOL http_response_is_websocket(const HttpContext* http, const HttpResponse* response)
1614{
1615 BOOL isWebsocket = FALSE;
1616 WINPR_DIGEST_CTX* sha1 = NULL;
1617 char* base64accept = NULL;
1618 BYTE sha1_digest[WINPR_SHA1_DIGEST_LENGTH];
1619
1620 if (!http || !response)
1621 return FALSE;
1622
1623 if (!http->websocketUpgrade || response->StatusCode != HTTP_STATUS_SWITCH_PROTOCOLS)
1624 return FALSE;
1625
1626 if (response->SecWebsocketVersion && _stricmp(response->SecWebsocketVersion, "13") != 0)
1627 return FALSE;
1628
1629 if (!response->SecWebsocketAccept)
1630 return FALSE;
1631
1632 /* now check if Sec-Websocket-Accept is correct */
1633
1634 sha1 = winpr_Digest_New();
1635 if (!sha1)
1636 goto out;
1637
1638 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
1639 goto out;
1640
1641 if (!winpr_Digest_Update(sha1, (BYTE*)http->SecWebsocketKey, strlen(http->SecWebsocketKey)))
1642 goto out;
1643 if (!winpr_Digest_Update(sha1, (const BYTE*)WEBSOCKET_MAGIC_GUID, strlen(WEBSOCKET_MAGIC_GUID)))
1644 goto out;
1645
1646 if (!winpr_Digest_Final(sha1, sha1_digest, sizeof(sha1_digest)))
1647 goto out;
1648
1649 base64accept = crypto_base64_encode(sha1_digest, WINPR_SHA1_DIGEST_LENGTH);
1650 if (!base64accept)
1651 goto out;
1652
1653 if (_stricmp(response->SecWebsocketAccept, base64accept) != 0)
1654 {
1655 WLog_WARN(TAG, "Webserver gave Websocket Upgrade response but sanity check failed");
1656 goto out;
1657 }
1658 isWebsocket = TRUE;
1659out:
1660 winpr_Digest_Free(sha1);
1661 free(base64accept);
1662 return isWebsocket;
1663}
1664
1665void http_response_log_error_status_(wLog* log, DWORD level, const HttpResponse* response,
1666 const char* file, size_t line, const char* fkt)
1667{
1668 WINPR_ASSERT(log);
1669 WINPR_ASSERT(response);
1670
1671 if (!WLog_IsLevelActive(log, level))
1672 return;
1673
1674 char buffer[64] = { 0 };
1675 const UINT16 status = http_response_get_status_code(response);
1676 WLog_PrintTextMessage(log, level, line, file, fkt, "Unexpected HTTP status: %s",
1677 freerdp_http_status_string_format(status, buffer, ARRAYSIZE(buffer)));
1678 http_response_print(log, level, response, file, line, fkt);
1679}
1680
1681static BOOL extract_cookie(const void* pkey, void* pvalue, void* arg)
1682{
1683 const char* key = pkey;
1684 const char* value = pvalue;
1685 HttpContext* context = arg;
1686
1687 WINPR_ASSERT(arg);
1688 WINPR_ASSERT(key);
1689 WINPR_ASSERT(value);
1690
1691 return http_context_set_cookie(context, key, value);
1692}
1693
1694BOOL http_response_extract_cookies(const HttpResponse* response, HttpContext* context)
1695{
1696 WINPR_ASSERT(response);
1697 WINPR_ASSERT(context);
1698
1699 return HashTable_Foreach(response->SetCookie, extract_cookie, context);
1700}
1701
1702FREERDP_LOCAL BOOL http_context_set_header(HttpContext* context, const char* key, const char* value,
1703 ...)
1704{
1705 WINPR_ASSERT(context);
1706 va_list ap;
1707 va_start(ap, value);
1708 const BOOL rc = http_context_set_header_va(context, key, value, ap);
1709 va_end(ap);
1710 return rc;
1711}
1712
1713BOOL http_request_set_header(HttpRequest* request, const char* key, const char* value, ...)
1714{
1715 WINPR_ASSERT(request);
1716 char* v = NULL;
1717 size_t vlen = 0;
1718 va_list ap;
1719 va_start(ap, value);
1720 winpr_vasprintf(&v, &vlen, value, ap);
1721 va_end(ap);
1722 const BOOL rc = HashTable_Insert(request->headers, key, v);
1723 free(v);
1724 return rc;
1725}
1726
1727BOOL http_context_set_header_va(HttpContext* context, const char* key, const char* value,
1728 va_list ap)
1729{
1730 char* v = NULL;
1731 size_t vlen = 0;
1732 winpr_vasprintf(&v, &vlen, value, ap);
1733 const BOOL rc = HashTable_Insert(context->headers, key, v);
1734 free(v);
1735 return rc;
1736}
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
This struct contains function pointer to initialize/free objects.
Definition collections.h:57