quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

commit 113f3607604d532f83e7a532030e107a10eb30fb
parent 7e252316edd93ec69f970a4b7dfb553f54c2ee10
Author: Florian Dold <florian@dold.me>
Date:   Fri, 11 Nov 2022 20:53:06 +0100

wip

Diffstat:
Mquickjs-libc.c | 46++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+), 0 deletions(-)

diff --git a/quickjs-libc.c b/quickjs-libc.c @@ -2085,6 +2085,38 @@ int expect_property_str_bool(JSContext *ctx, JSValueConst this_val, const char * return bool_val; } +static int gather_headers(JSContext *ctx, JSValueConst js_headers, struct curl_slist **headers) +{ + JSValue length_prop; + uint32_t length; + + length_prop = JS_GetPropertyStr(ctx, js_headers, "length"); + if (JS_IsException(length_prop)) { + return -1; + } + if (0 != JS_ToUint32(ctx, &length, length_prop)) { + return -1; + } + JS_FreeValue(ctx, length_prop); + + for (uint32_t i = 0; i < length; i++) { + JSValue item = JS_GetPropertyUint32(ctx, js_headers, i); + if (JS_IsException(item)) { + goto exception; + } + const char *cstr = JS_ToCString(ctx, item); + if (!cstr) { + JS_FreeValue(ctx, item); + goto exception; + } + *headers = curl_slist_append (*headers, cstr); + JS_FreeCString(ctx, cstr); + } + return 0; +exception: + return -1; +} + /** * fetchHttp(url, { method, headers, body }) */ @@ -2167,6 +2199,20 @@ static JSValue js_os_fetchHttp(JSContext *ctx, JSValueConst this_val, goto exception; } + if (JS_VALUE_GET_TAG(options) == JS_TAG_OBJECT) { + JSValue header_item = JS_GetPropertyStr(ctx, options, "headers"); + if (JS_IsException(header_item)) { + goto exception; + } + if (JS_VALUE_GET_TAG(header_item) == JS_TAG_OBJECT) { + if (0 != gather_headers(ctx, header_item , &headers)) { + JS_FreeValue(ctx, header_item); + goto exception; + } + } + JS_FreeValue(ctx, header_item); + } + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); res = curl_easy_perform(curl);