ucmd_curl.c 10 KB

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