ucmd_curl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_MAX (2u * 1024u * 1024u)
  54. static unsigned int c_len(const char *s)
  55. {
  56. unsigned int n = 0;
  57. while (s && s[n])
  58. n++;
  59. return n;
  60. }
  61. static void app_ch(char *d, unsigned int *n, char c, unsigned int cap)
  62. {
  63. if (*n + 1 < cap)
  64. d[(*n)++] = c;
  65. d[*n] = '\0';
  66. }
  67. static void app_str(char *d, unsigned int *n, const char *s, unsigned int cap)
  68. {
  69. while (s && *s)
  70. app_ch(d, n, *s++, cap);
  71. }
  72. static void app_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap)
  73. {
  74. char tmp[12];
  75. unsigned int i = 0, j;
  76. do {
  77. tmp[i++] = (char)('0' + v % 10);
  78. v /= 10;
  79. } while (v && i < sizeof(tmp));
  80. for (j = 0; j < i; j++)
  81. app_ch(d, n, tmp[i - 1 - j], cap);
  82. }
  83. static void fetch_fail(unsigned int status, int err, const char *ctype,
  84. const char *enc, const char *final)
  85. {
  86. char meta[768];
  87. unsigned int n = 0;
  88. app_dec(meta, &n, status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  89. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  90. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  91. app_str(meta, &n, enc ? enc : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  92. app_str(meta, &n, ctype ? ctype : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  93. app_str(meta, &n, final ? final : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  94. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  95. u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
  96. }
  97. static void fetch_once(void)
  98. {
  99. static struct uhttp h;
  100. char req[512];
  101. char meta[768];
  102. unsigned char *buf, *body;
  103. unsigned int cap = 0, total = 0, n = 0;
  104. unsigned int body_len = 0, body_trunc = 0;
  105. unsigned int i, trunc = 0;
  106. int rn, rv, err = 0;
  107. const char *enc_out;
  108. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_REQ, (int)req,
  109. (int)(sizeof(req) - 1));
  110. if (rn <= 0)
  111. u_exit(1);
  112. req[rn] = '\0';
  113. i = 0;
  114. while (req[i] >= '0' && req[i] <= '9') {
  115. cap = cap * 10 + (unsigned int)(req[i] - '0');
  116. i++;
  117. }
  118. while (req[i] == ' ')
  119. i++;
  120. while (rn > 0 && (req[rn - 1] == '\n' || req[rn - 1] == '\r'))
  121. req[--rn] = '\0';
  122. if (i >= (unsigned int)rn || !req[i]) {
  123. fetch_fail(0, -1, "", "", "");
  124. u_exit(1);
  125. }
  126. if (cap == 0 || cap > FETCH_MAX)
  127. cap = FETCH_MAX;
  128. buf = (unsigned char *)u_mem_alloc(FETCH_MAX);
  129. if (!buf) {
  130. fetch_fail(0, -1, "", "", "");
  131. u_exit(1);
  132. }
  133. uhttp_init(&h);
  134. h.user_agent = "nebos-browser-fetch/0.1";
  135. rv = uhttp_open(&h, req + i, 0, 0);
  136. /* The browser cancels a page load by killing our predecessor. It can
  137. * still own the single NIC receiver for a tick or two, so a fresh
  138. * helper waits briefly instead of failing the new page outright. */
  139. if (rv == UHTTP_ERR_BUSY) {
  140. int tries;
  141. for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
  142. u_sleep_ms(20);
  143. rv = uhttp_open(&h, req + i, 0, 0);
  144. }
  145. }
  146. if (rv != 0) {
  147. err = rv;
  148. fetch_fail(0, err, h.resp.content_type,
  149. h.resp.content_encoding, h.resp.final_url);
  150. uhttp_close(&h);
  151. u_mem_free();
  152. u_exit(1);
  153. }
  154. for (;;) {
  155. rv = uhttp_read(&h, buf + total, cap - total,
  156. UHTTP_IDLE_TIMEOUT_MS);
  157. if (rv > 0) {
  158. total += (unsigned int)rv;
  159. if (total >= cap) {
  160. trunc = 1;
  161. break;
  162. }
  163. continue;
  164. }
  165. if (rv < 0) {
  166. err = rv;
  167. }
  168. break;
  169. }
  170. body = buf;
  171. body_len = total;
  172. body_trunc = trunc;
  173. enc_out = h.resp.content_encoding;
  174. if (body_len)
  175. u_syscall3(SYS_WRITE_FILE, (int)FETCH_BODY, (int)body,
  176. (int)body_len);
  177. app_dec(meta, &n, (unsigned int)h.resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  178. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  179. app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  180. app_str(meta, &n, enc_out, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  181. app_str(meta, &n, h.resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  182. app_str(meta, &n, h.resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  183. app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  184. u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
  185. uhttp_close(&h);
  186. u_mem_free();
  187. u_exit(err ? 1 : 0);
  188. }
  189. void _start(const char *arg)
  190. {
  191. static struct uhttp h;
  192. unsigned char buf[512];
  193. unsigned int total = 0;
  194. int rv;
  195. if (arg && c_len(arg) == 7 && arg[0] == '_' &&
  196. arg[1] == '_' && arg[2] == 'f' && arg[3] == 'e' &&
  197. arg[4] == 't' && arg[5] == 'c' && arg[6] == 'h') {
  198. fetch_once();
  199. }
  200. if (!arg || !arg[0]) {
  201. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  202. u_exit(1);
  203. }
  204. uhttp_init(&h);
  205. h.user_agent = "nebos-curl/0.1";
  206. u_puts("curl: GET ");
  207. u_puts(arg);
  208. u_putc('\n');
  209. rv = uhttp_open(&h, arg, 0, &hooks);
  210. if (rv != 0) {
  211. u_puts("curl: ");
  212. u_puts(uhttp_error_string(rv));
  213. u_putc('\n');
  214. uhttp_close(&h);
  215. u_exit(1);
  216. }
  217. if (h.url.secure)
  218. u_puts("curl: WARNING: certificate NOT verified "
  219. "(trust-all mode)\n");
  220. u_putc('\n');
  221. for (;;) {
  222. rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS);
  223. if (rv > 0) {
  224. int i;
  225. for (i = 0; i < rv; i++)
  226. u_putc((char)buf[i]);
  227. total += (unsigned int)rv;
  228. continue;
  229. }
  230. if (rv < 0) {
  231. u_puts("\ncurl: ");
  232. u_puts(uhttp_error_string(rv));
  233. u_puts(" while reading body\n");
  234. }
  235. break;
  236. }
  237. u_putc('\n');
  238. u_puts("curl: ");
  239. u_putdec(total);
  240. u_puts(" bytes received, status ");
  241. u_putdec((unsigned int)h.resp.status);
  242. u_putc('\n');
  243. uhttp_close(&h);
  244. u_exit(0);
  245. }