ucmd_curl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* curl: ring-3 command. Fetch one http:// or https:// URL with the shared
  2. * uhttp client and dump the status line, headers and body to the terminal.
  3. *
  4. * It also has a headless `__fetch` mode used by the browser: read
  5. * /share/bfreq, fetch it, and write the body plus /share/bfmeta for the GUI
  6. * process. That keeps DNS/TLS/body waits out of the browser's UI loop.
  7. *
  8. * The TLS and HTTP code lives in uhttp.c (shared with the browser); this file
  9. * only keeps the CLI, the helper mode and the verbose output hooks. */
  10. #include <stdlib.h>
  11. #include "ucmd_lib.h"
  12. #include "uhttp.h"
  13. #define LINE_MAX_C 320
  14. static void on_connected(void *ctx, int secure)
  15. {
  16. (void)ctx;
  17. if (secure) {
  18. u_puts("curl: connected, TLS handshake\n");
  19. } else {
  20. u_puts("curl: connected, sending request\n");
  21. }
  22. }
  23. static void on_status(void *ctx, int code, const char *line)
  24. {
  25. (void)ctx;
  26. (void)code;
  27. u_puts("curl: ");
  28. u_puts(line);
  29. u_putc('\n');
  30. }
  31. static void on_header(void *ctx, const char *line)
  32. {
  33. (void)ctx;
  34. u_puts(line);
  35. u_putc('\n');
  36. }
  37. static void on_redirect(void *ctx, const char *location)
  38. {
  39. (void)ctx;
  40. u_puts("curl: redirect -> ");
  41. u_puts(location);
  42. u_putc('\n');
  43. }
  44. static const struct uhttp_hooks hooks = {
  45. .connected = on_connected,
  46. .status = on_status,
  47. .header = on_header,
  48. .redirect = on_redirect,
  49. };
  50. /* ---- headless fetch helper for the browser ---------------------------- */
  51. #define FETCH_REQ "/share/bfreq"
  52. #define FETCH_BODY "/share/bfbody"
  53. #define FETCH_META "/share/bfmeta"
  54. #define FETCH_DONE "/share/bfdone"
  55. #define FETCH_MAX (2u * 1024u * 1024u)
  56. static unsigned int c_len(const char *s)
  57. {
  58. unsigned int n = 0;
  59. while (s && s[n])
  60. n++;
  61. return n;
  62. }
  63. static void app_ch(char *d, unsigned int *n, char c, unsigned int cap)
  64. {
  65. if (*n + 1 < cap)
  66. d[(*n)++] = c;
  67. d[*n] = '\0';
  68. }
  69. static void app_str(char *d, unsigned int *n, const char *s, unsigned int cap)
  70. {
  71. while (s && *s)
  72. app_ch(d, n, *s++, cap);
  73. }
  74. static void app_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap)
  75. {
  76. char tmp[12];
  77. unsigned int i = 0, j;
  78. do {
  79. tmp[i++] = (char)('0' + v % 10);
  80. v /= 10;
  81. } while (v && i < sizeof(tmp));
  82. for (j = 0; j < i; j++)
  83. app_ch(d, n, tmp[i - 1 - j], cap);
  84. }
  85. static void fetch_fail(unsigned int status, int err, const char *ctype,
  86. const char *enc, const char *final)
  87. {
  88. char meta[768];
  89. unsigned int n = 0;
  90. app_dec(meta, &n, status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  91. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  92. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  93. app_str(meta, &n, enc ? enc : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  94. app_str(meta, &n, ctype ? ctype : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  95. app_str(meta, &n, final ? final : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  96. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  97. u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
  98. }
  99. static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
  100. int err, unsigned int body_len, unsigned int body_trunc)
  101. {
  102. char meta[1024];
  103. unsigned int n = 0;
  104. if (with_seq) {
  105. app_dec(meta, &n, seq, sizeof(meta));
  106. app_ch(meta, &n, '\n', sizeof(meta));
  107. }
  108. app_dec(meta, &n, (unsigned int)h->resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  109. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  110. app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  111. app_str(meta, &n, h->resp.content_encoding, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  112. app_str(meta, &n, h->resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  113. app_str(meta, &n, h->resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  114. app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  115. u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
  116. }
  117. /* Fetch one URL through a possibly reused keep-alive connection. */
  118. static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
  119. unsigned char *buf, unsigned int *outlen,
  120. unsigned int *outtrunc, int *outerr)
  121. {
  122. unsigned int total = 0;
  123. int rv, trunc = 0, err = 0;
  124. *outlen = 0;
  125. *outtrunc = 0;
  126. *outerr = 0;
  127. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  128. if (rv == UHTTP_ERR_BUSY) {
  129. int tries;
  130. for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
  131. u_sleep_ms(20);
  132. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  133. }
  134. }
  135. if (rv != 0) {
  136. *outerr = rv;
  137. return;
  138. }
  139. for (;;) {
  140. rv = uhttp_read(h, buf + total, cap - total,
  141. UHTTP_IDLE_TIMEOUT_MS);
  142. if (rv > 0) {
  143. total += (unsigned int)rv;
  144. if (total >= cap) {
  145. trunc = 1;
  146. break;
  147. }
  148. continue;
  149. }
  150. if (rv < 0)
  151. err = rv;
  152. break;
  153. }
  154. *outlen = total;
  155. *outtrunc = (unsigned int)trunc;
  156. *outerr = err;
  157. }
  158. static void fetch_once(void)
  159. {
  160. static struct uhttp h;
  161. char req[768];
  162. unsigned char *buf;
  163. unsigned int cap = 0;
  164. unsigned int body_len = 0, body_trunc = 0;
  165. unsigned int i;
  166. int rn, err = 0;
  167. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_REQ, (int)req,
  168. (int)(sizeof(req) - 1));
  169. if (rn <= 0)
  170. u_exit(1);
  171. req[rn] = '\0';
  172. i = 0;
  173. while (req[i] >= '0' && req[i] <= '9') {
  174. cap = cap * 10 + (unsigned int)(req[i] - '0');
  175. i++;
  176. }
  177. while (req[i] == ' ')
  178. i++;
  179. while (rn > 0 && (req[rn - 1] == '\n' || req[rn - 1] == '\r'))
  180. req[--rn] = '\0';
  181. if (i >= (unsigned int)rn || !req[i]) {
  182. fetch_fail(0, -1, "", "", "");
  183. u_exit(1);
  184. }
  185. if (cap == 0 || cap > FETCH_MAX)
  186. cap = FETCH_MAX;
  187. buf = (unsigned char *)malloc(FETCH_MAX);
  188. if (!buf) {
  189. fetch_fail(0, -1, "", "", "");
  190. u_exit(1);
  191. }
  192. uhttp_init(&h);
  193. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  194. helper_fetch(&h, req + i, cap, buf, &body_len, &body_trunc, &err);
  195. if (body_len)
  196. u_syscall3(SYS_WRITE_FILE, (int)FETCH_BODY, (int)buf,
  197. (int)body_len);
  198. write_meta(0, 0, &h, err, body_len, body_trunc);
  199. uhttp_close(&h);
  200. free(buf);
  201. u_exit(err ? 1 : 0);
  202. }
  203. /* Long-lived helper: the browser writes a numbered request, we fetch it
  204. * (reusing the same TCP/TLS connection whenever the host is unchanged) and
  205. * publish body + meta + a small done marker. Idle instances exit and
  206. * release the NIC after ten seconds. */
  207. static void fetch_serve(void)
  208. {
  209. static struct uhttp h;
  210. char req[768];
  211. unsigned char *buf;
  212. unsigned int last_seq = 0, idle = 0;
  213. buf = (unsigned char *)malloc(FETCH_MAX);
  214. if (!buf)
  215. u_exit(1);
  216. uhttp_init(&h);
  217. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  218. h.keepalive = 1;
  219. for (;;) {
  220. unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
  221. char done[16];
  222. unsigned int dn = 0;
  223. unsigned int i = 0;
  224. char *url;
  225. int rn, err = 0;
  226. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_REQ, (int)req,
  227. (int)(sizeof(req) - 1));
  228. if (rn <= 0) {
  229. url = 0;
  230. } else {
  231. req[rn] = '\0';
  232. while (req[i] >= '0' && req[i] <= '9') {
  233. seq = seq * 10 + (unsigned int)(req[i] - '0');
  234. i++;
  235. }
  236. while (req[i] == ' ')
  237. i++;
  238. while (req[i] >= '0' && req[i] <= '9') {
  239. cap = cap * 10 + (unsigned int)(req[i] - '0');
  240. i++;
  241. }
  242. while (req[i] == ' ')
  243. i++;
  244. while (rn > 0 && (req[rn - 1] == '\n' ||
  245. req[rn - 1] == '\r'))
  246. req[--rn] = '\0';
  247. url = (i < (unsigned int)rn) ? req + i : 0;
  248. }
  249. if (!url || !url[0] || seq == last_seq) {
  250. idle++;
  251. u_sleep_ms(50);
  252. if (idle >= 200)
  253. break;
  254. continue;
  255. }
  256. idle = 0;
  257. last_seq = seq;
  258. if (cap == 0 || cap > FETCH_MAX)
  259. cap = FETCH_MAX;
  260. helper_fetch(&h, url, cap, buf, &body_len, &body_trunc, &err);
  261. if (body_len)
  262. u_syscall3(SYS_WRITE_FILE, (int)FETCH_BODY, (int)buf,
  263. (int)body_len);
  264. write_meta(seq, 1, &h, err, body_len, body_trunc);
  265. app_dec(done, &dn, seq, sizeof(done));
  266. u_syscall3(SYS_WRITE_FILE, (int)FETCH_DONE, (int)done, (int)dn);
  267. if (err || body_trunc || h.peer_close) {
  268. h.keepalive = 0;
  269. uhttp_close(&h);
  270. h.keepalive = 1;
  271. }
  272. }
  273. h.keepalive = 0;
  274. uhttp_close(&h);
  275. free(buf);
  276. u_exit(0);
  277. }
  278. void _start(const char *arg)
  279. {
  280. static struct uhttp h;
  281. unsigned char buf[512];
  282. unsigned int total = 0;
  283. int rv;
  284. if (arg && c_len(arg) == 7 && arg[0] == '_' && arg[1] == '_') {
  285. if (arg[2] == 'f')
  286. fetch_once();
  287. else if (arg[2] == 's')
  288. fetch_serve();
  289. }
  290. if (!arg || !arg[0]) {
  291. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  292. u_exit(1);
  293. }
  294. uhttp_init(&h);
  295. h.user_agent = "nebos-curl/0.1";
  296. u_puts("curl: GET ");
  297. u_puts(arg);
  298. u_putc('\n');
  299. rv = uhttp_open(&h, arg, 0, &hooks);
  300. if (rv != 0) {
  301. u_puts("curl: ");
  302. u_puts(uhttp_error_string(rv));
  303. u_putc('\n');
  304. uhttp_close(&h);
  305. u_exit(1);
  306. }
  307. if (h.url.secure)
  308. u_puts("curl: WARNING: certificate NOT verified "
  309. "(trust-all mode)\n");
  310. u_putc('\n');
  311. for (;;) {
  312. rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS);
  313. if (rv > 0) {
  314. int i;
  315. for (i = 0; i < rv; i++)
  316. u_putc((char)buf[i]);
  317. total += (unsigned int)rv;
  318. continue;
  319. }
  320. if (rv < 0) {
  321. u_puts("\ncurl: ");
  322. u_puts(uhttp_error_string(rv));
  323. u_puts(" while reading body\n");
  324. }
  325. break;
  326. }
  327. u_putc('\n');
  328. u_puts("curl: ");
  329. u_putdec(total);
  330. u_puts(" bytes received, status ");
  331. u_putdec((unsigned int)h.resp.status);
  332. u_putc('\n');
  333. uhttp_close(&h);
  334. u_exit(0);
  335. }