MCPcopy Create free account
hub / github.com/buke/quickjs-go / js_std_urlGet

Function js_std_urlGet

deps/quickjs/quickjs-libc.c:1737–1891  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1735}
1736
1737static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
1738 int argc, JSValueConst *argv)
1739{
1740 const char *url;
1741 DynBuf cmd_buf;
1742 DynBuf data_buf_s, *data_buf = &data_buf_s;
1743 DynBuf header_buf_s, *header_buf = &header_buf_s;
1744 char *buf;
1745 size_t i, len;
1746 int status;
1747 JSValue response = JS_UNDEFINED, ret_obj;
1748 JSValueConst options_obj;
1749 FILE *f;
1750 bool binary_flag, full_flag;
1751
1752 url = JS_ToCString(ctx, argv[0]);
1753 if (!url)
1754 return JS_EXCEPTION;
1755
1756 binary_flag = false;
1757 full_flag = false;
1758
1759 if (argc >= 2) {
1760 options_obj = argv[1];
1761
1762 if (get_bool_option(ctx, &binary_flag, options_obj, "binary"))
1763 goto fail_obj;
1764
1765 if (get_bool_option(ctx, &full_flag, options_obj, "full")) {
1766 fail_obj:
1767 JS_FreeCString(ctx, url);
1768 return JS_EXCEPTION;
1769 }
1770 }
1771
1772 js_std_dbuf_init(ctx, &cmd_buf);
1773 dbuf_printf(&cmd_buf, "%s '", URL_GET_PROGRAM);
1774 for(i = 0; url[i] != '\0'; i++) {
1775 unsigned char c = url[i];
1776 switch (c) {
1777 case '\'':
1778 /* shell single quoted string does not support \' */
1779 dbuf_putstr(&cmd_buf, "'\\''");
1780 break;
1781 case '[': case ']': case '{': case '}': case '\\':
1782 /* prevent interpretation by curl as range or set specification */
1783 dbuf_putc(&cmd_buf, '\\');
1784 /* FALLTHROUGH */
1785 default:
1786 dbuf_putc(&cmd_buf, c);
1787 break;
1788 }
1789 }
1790 JS_FreeCString(ctx, url);
1791 dbuf_putstr(&cmd_buf, "'");
1792 dbuf_putc(&cmd_buf, '\0');
1793 if (dbuf_error(&cmd_buf)) {
1794 dbuf_free(&cmd_buf);

Callers

nothing calls this directly

Calls 14

JS_ToCStringFunction · 0.85
get_bool_optionFunction · 0.85
js_std_dbuf_initFunction · 0.85
dbuf_printfFunction · 0.85
dbuf_putstrFunction · 0.85
dbuf_putcFunction · 0.85
dbuf_errorFunction · 0.85
dbuf_freeFunction · 0.85
http_get_header_lineFunction · 0.85
http_get_statusFunction · 0.85
dbuf_putFunction · 0.85
JS_IsExceptionFunction · 0.85

Tested by

no test coverage detected