]> git.kaiwu.me - quickjs.git/commitdiff
Simplify and clarify URL quoting js_std_urlGet
authorCharlie Gordon <github@chqrlie.org>
Mon, 12 Feb 2024 17:34:52 +0000 (18:34 +0100)
committerCharlie Gordon <github@chqrlie.org>
Mon, 12 Feb 2024 17:34:52 +0000 (18:34 +0100)
quickjs-libc.c

index 2e779504f60e8af6182dfce6d51a68b3416b9db5..01c9db42c8c6a75685243cc32297f377ea1a7fab 100644 (file)
@@ -1335,7 +1335,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
     DynBuf header_buf_s, *header_buf = &header_buf_s;
     char *buf;
     size_t i, len;
-    int c, status;
+    int status;
     JSValue response = JS_UNDEFINED, ret_obj;
     JSValueConst options_obj;
     FILE *f;
@@ -1363,17 +1363,20 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
 
     js_std_dbuf_init(ctx, &cmd_buf);
     dbuf_printf(&cmd_buf, "%s '", URL_GET_PROGRAM);
-    len = strlen(url);
-    for(i = 0; i < len; i++) {
-        switch (c = url[i]) {
+    for(i = 0; url[i] != '\0'; i++) {
+        unsigned char c = url[i];
+        switch (c) {
         case '\'':
+            /* shell single quoted string does not support \' */
             dbuf_putstr(&cmd_buf, "'\\''");
             break;
         case '[': case ']': case '{': case '}': case '\\':
+            /* prevent interpretation by curl as range or set specification */
             dbuf_putc(&cmd_buf, '\\');
             /* FALLTHROUGH */
         default:
             dbuf_putc(&cmd_buf, c);
+            break;
         }
     }
     JS_FreeCString(ctx, url);