ucmd_curl.c 9.2 KB

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