ucmd_curl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if (rv != 0) {
  137. err = rv;
  138. fetch_fail(0, err, h.resp.content_type,
  139. h.resp.content_encoding, h.resp.final_url);
  140. uhttp_close(&h);
  141. u_mem_free();
  142. u_exit(1);
  143. }
  144. for (;;) {
  145. rv = uhttp_read(&h, buf + total, cap - total,
  146. UHTTP_IDLE_TIMEOUT_MS);
  147. if (rv > 0) {
  148. total += (unsigned int)rv;
  149. if (total >= cap) {
  150. trunc = 1;
  151. break;
  152. }
  153. continue;
  154. }
  155. if (rv < 0) {
  156. err = rv;
  157. }
  158. break;
  159. }
  160. body = buf;
  161. body_len = total;
  162. body_trunc = trunc;
  163. enc_out = h.resp.content_encoding;
  164. if (body_len)
  165. u_syscall3(SYS_WRITE_FILE, (int)FETCH_BODY, (int)body,
  166. (int)body_len);
  167. app_dec(meta, &n, (unsigned int)h.resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  168. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  169. app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  170. app_str(meta, &n, enc_out, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  171. app_str(meta, &n, h.resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  172. app_str(meta, &n, h.resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  173. app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  174. u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
  175. uhttp_close(&h);
  176. u_mem_free();
  177. u_exit(err ? 1 : 0);
  178. }
  179. void _start(const char *arg)
  180. {
  181. static struct uhttp h;
  182. unsigned char buf[512];
  183. unsigned int total = 0;
  184. int rv;
  185. if (arg && c_len(arg) == 7 && arg[0] == '_' &&
  186. arg[1] == '_' && arg[2] == 'f' && arg[3] == 'e' &&
  187. arg[4] == 't' && arg[5] == 'c' && arg[6] == 'h') {
  188. fetch_once();
  189. }
  190. if (!arg || !arg[0]) {
  191. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  192. u_exit(1);
  193. }
  194. uhttp_init(&h);
  195. h.user_agent = "nebos-curl/0.1";
  196. u_puts("curl: GET ");
  197. u_puts(arg);
  198. u_putc('\n');
  199. rv = uhttp_open(&h, arg, 0, &hooks);
  200. if (rv != 0) {
  201. u_puts("curl: ");
  202. u_puts(uhttp_error_string(rv));
  203. u_putc('\n');
  204. uhttp_close(&h);
  205. u_exit(1);
  206. }
  207. if (h.url.secure)
  208. u_puts("curl: WARNING: certificate NOT verified "
  209. "(trust-all mode)\n");
  210. u_putc('\n');
  211. for (;;) {
  212. rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS);
  213. if (rv > 0) {
  214. int i;
  215. for (i = 0; i < rv; i++)
  216. u_putc((char)buf[i]);
  217. total += (unsigned int)rv;
  218. continue;
  219. }
  220. if (rv < 0) {
  221. u_puts("\ncurl: ");
  222. u_puts(uhttp_error_string(rv));
  223. u_puts(" while reading body\n");
  224. }
  225. break;
  226. }
  227. u_putc('\n');
  228. u_puts("curl: ");
  229. u_putdec(total);
  230. u_puts(" bytes received, status ");
  231. u_putdec((unsigned int)h.resp.status);
  232. u_putc('\n');
  233. uhttp_close(&h);
  234. u_exit(0);
  235. }