ucmd_curl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /* curl: ring-3 command. Fetch one http:// or https:// URL with a minimal
  2. * HTTP GET and dump the status line, the headers and the body to the terminal.
  3. *
  4. * https uses the tls.c client (TLS 1.3, ChaCha20-Poly1305, X25519) WITHOUT
  5. * certificate verification (phase A): the handshake is encrypted, but a
  6. * man-in-the-middle is not detected; the leaf fingerprint is printed for a
  7. * manual check. Redirects (3xx + Location) are followed up to
  8. * MAX_REDIRECTS hops: absolute URLs, absolute paths and relative paths
  9. * (resolved against the current path, "."/".." collapsed). No gzip; a
  10. * chunked response is decoded. The TCP client (tcp.c) and the DNS resolver
  11. * stay in the kernel; the nic is owned for the run (SYS_NET_ACQUIRE) and
  12. * TCP goes through the SYS_TCP_* calls. */
  13. #include "ucmd_lib.h"
  14. #include "tls.h"
  15. #define URL_HOST_MAX 128
  16. #define URL_PATH_MAX 128
  17. #define REQ_MAX 512
  18. #define LINE_MAX_C 320
  19. #define READER_MAX 1024
  20. #define LOCATION_MAX 256
  21. #define MAX_REDIRECTS 5
  22. #define IDLE_TIMEOUT_MS 10000
  23. #define TOTAL_TIMEOUT_MS 60000
  24. struct url {
  25. char host[URL_HOST_MAX];
  26. char path[URL_PATH_MAX];
  27. unsigned int port;
  28. int secure;
  29. };
  30. static struct url u;
  31. /* ---- transport: plain TCP or TLS ---- */
  32. static int xport_send(const void *buf, unsigned int len)
  33. {
  34. if (u.secure)
  35. return tls_send(buf, len);
  36. return (int)u_syscall2(SYS_TCP_SEND, (int)buf, (int)len);
  37. }
  38. static int xport_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  39. {
  40. if (u.secure)
  41. return tls_recv(buf, max, timeout_ms);
  42. return (int)u_syscall3(SYS_TCP_RECV, (int)buf, (int)max,
  43. (int)timeout_ms);
  44. }
  45. static int starts_with(const char *s, const char *prefix)
  46. {
  47. unsigned int i = 0;
  48. while (prefix[i]) {
  49. if (s[i] != prefix[i])
  50. return 0;
  51. i++;
  52. }
  53. return 1;
  54. }
  55. /* ---- URL parsing ---- */
  56. /* Parse http:// or https:// host[:port][/path]; 0 on success, -1 error. */
  57. static int url_parse(const char *s, struct url *u)
  58. {
  59. const char *p = s, *h;
  60. unsigned int i;
  61. int secure = 0;
  62. if (starts_with(s, "https://")) {
  63. secure = 1;
  64. p += 8;
  65. } else if (starts_with(s, "http://")) {
  66. p += 7;
  67. } else {
  68. /* An unsupported scheme like ftp:// is easy to spot: a "://"
  69. * within the first few characters. */
  70. for (i = 0; s[i] && i < 8; i++) {
  71. if (s[i] == ':' && s[i + 1] == '/' && s[i + 2] == '/')
  72. return -1;
  73. if (s[i] == '/')
  74. break;
  75. }
  76. }
  77. u->secure = secure;
  78. h = p;
  79. while (*p && *p != '/' && *p != ':')
  80. p++;
  81. if (p == h || (unsigned int)(p - h) >= URL_HOST_MAX)
  82. return -1;
  83. for (i = 0; i < (unsigned int)(p - h); i++)
  84. u->host[i] = h[i];
  85. u->host[i] = '\0';
  86. u->port = secure ? 443 : 80;
  87. if (*p == ':') {
  88. unsigned int port = 0;
  89. p++;
  90. if (*p < '0' || *p > '9')
  91. return -1;
  92. while (*p >= '0' && *p <= '9') {
  93. port = port * 10 + (unsigned int)(*p - '0');
  94. if (port > 65535)
  95. return -1;
  96. p++;
  97. }
  98. if (port == 0 || (*p != '/' && *p != '\0'))
  99. return -1;
  100. u->port = port;
  101. }
  102. if (*p == '/') {
  103. i = 0;
  104. while (p[i]) {
  105. if (i + 1 >= URL_PATH_MAX)
  106. return -1;
  107. u->path[i] = p[i];
  108. i++;
  109. }
  110. u->path[i] = '\0';
  111. } else {
  112. u->path[0] = '/';
  113. u->path[1] = '\0';
  114. }
  115. return 0;
  116. }
  117. /* ---- redirect target handling ---- */
  118. /* Normalize an absolute URL path in place: collapse "//", "." and ".." and
  119. * keep the leading '/' plus a trailing one. 0 ok, -1 too long. */
  120. static int path_normalize(char *path)
  121. {
  122. char tmp[URL_PATH_MAX];
  123. unsigned int i, o = 1, len = 0, start, seglen, j;
  124. int trailing;
  125. while (path[len])
  126. len++;
  127. if (len == 0 || path[0] != '/')
  128. return 0;
  129. trailing = (len > 1 && path[len - 1] == '/');
  130. tmp[0] = '/';
  131. i = 1;
  132. while (path[i]) {
  133. while (path[i] == '/')
  134. i++;
  135. start = i;
  136. while (path[i] && path[i] != '/')
  137. i++;
  138. seglen = i - start;
  139. if (seglen == 0)
  140. break;
  141. if (seglen == 1 && path[start] == '.')
  142. continue;
  143. if (seglen == 2 && path[start] == '.' &&
  144. path[start + 1] == '.') {
  145. while (o > 1 && tmp[o - 1] != '/')
  146. o--;
  147. if (o > 1)
  148. o--;
  149. continue;
  150. }
  151. if (o + seglen + 2 > sizeof(tmp))
  152. return -1;
  153. if (o > 1)
  154. tmp[o++] = '/';
  155. for (j = 0; j < seglen; j++)
  156. tmp[o++] = path[start + j];
  157. }
  158. if (trailing && o > 1 && tmp[o - 1] != '/') {
  159. if (o + 2 > sizeof(tmp))
  160. return -1;
  161. tmp[o++] = '/';
  162. }
  163. tmp[o] = '\0';
  164. for (i = 0; i <= o; i++)
  165. path[i] = tmp[i];
  166. return 0;
  167. }
  168. /* Apply a Location header to the current URL. Supports absolute URLs,
  169. * absolute paths and relative paths (joined to the current path's
  170. * directory). 0 ok, -1 unparsable/too long. */
  171. static int url_apply_location(struct url *u, const char *loc)
  172. {
  173. char joined[URL_PATH_MAX];
  174. unsigned int i, dirlen = 1, n = 0;
  175. if (starts_with(loc, "http://") || starts_with(loc, "https://"))
  176. return url_parse(loc, u);
  177. if (loc[0] == '/') {
  178. if (loc[1] == '/')
  179. return -1; /* scheme-relative: unsupported */
  180. for (i = 0; loc[i]; i++) {
  181. if (i + 1 >= URL_PATH_MAX)
  182. return -1;
  183. u->path[i] = loc[i];
  184. }
  185. u->path[i] = '\0';
  186. return path_normalize(u->path);
  187. }
  188. /* Relative: keep the directory part of the current path. */
  189. for (i = 0; u->path[i]; i++) {
  190. if (u->path[i] == '/')
  191. dirlen = i + 1;
  192. }
  193. while (loc[n])
  194. n++;
  195. if (dirlen + n + 1 > URL_PATH_MAX)
  196. return -1;
  197. for (i = 0; i < dirlen; i++)
  198. joined[i] = u->path[i];
  199. for (i = 0; i < n; i++)
  200. joined[dirlen + i] = loc[i];
  201. joined[dirlen + n] = '\0';
  202. for (i = 0; i <= dirlen + n; i++)
  203. u->path[i] = joined[i];
  204. return path_normalize(u->path);
  205. }
  206. /* ---- request building ---- */
  207. static unsigned int req_app(char *buf, unsigned int pos, const char *s)
  208. {
  209. while (*s && pos < REQ_MAX - 1)
  210. buf[pos++] = *s++;
  211. return pos;
  212. }
  213. static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int v)
  214. {
  215. char b[12];
  216. int i = 0;
  217. if (v == 0) {
  218. if (pos < REQ_MAX - 1)
  219. buf[pos++] = '0';
  220. return pos;
  221. }
  222. while (v) {
  223. b[i++] = (char)('0' + (v % 10));
  224. v /= 10;
  225. }
  226. while (i) {
  227. i--;
  228. if (pos < REQ_MAX - 1)
  229. buf[pos++] = b[i];
  230. }
  231. return pos;
  232. }
  233. /* ---- HTTP response reader (a refillable line/byte stream) ---- */
  234. struct reader {
  235. unsigned char buf[READER_MAX];
  236. unsigned int len, pos;
  237. unsigned int total_deadline;
  238. int eof;
  239. int error; /* 0, -1 timeout, -2 reset */
  240. };
  241. /* Returns 1 when new data (or EOF) is available, 0 on timeout/reset. */
  242. static int reader_fill(struct reader *r)
  243. {
  244. unsigned int now, idle, deadline, rel;
  245. int n;
  246. if (r->eof)
  247. return 0;
  248. now = u_ticks_ms();
  249. idle = now + IDLE_TIMEOUT_MS;
  250. deadline = idle < r->total_deadline ? idle : r->total_deadline;
  251. rel = (int)(deadline - now) > 0 ? deadline - now : 1;
  252. n = xport_recv(r->buf, sizeof(r->buf), rel);
  253. if (n > 0) {
  254. r->len = (unsigned int)n;
  255. r->pos = 0;
  256. return 1;
  257. }
  258. if (n == 0) {
  259. r->eof = 1;
  260. return 0;
  261. }
  262. r->error = n;
  263. return 0;
  264. }
  265. static int reader_getc(struct reader *r)
  266. {
  267. if (r->pos >= r->len) {
  268. if (!reader_fill(r))
  269. return -1;
  270. }
  271. return r->buf[r->pos++];
  272. }
  273. /* Read up to (not including) the next '\n'; -1 on stream error/EOF. */
  274. static int reader_readline(struct reader *r, char *out, unsigned int max)
  275. {
  276. unsigned int n = 0;
  277. int c;
  278. for (;;) {
  279. c = reader_getc(r);
  280. if (c < 0) {
  281. out[n] = '\0';
  282. return (n == 0) ? -1 : (int)n;
  283. }
  284. if (c == '\n') {
  285. out[n] = '\0';
  286. return (int)n;
  287. }
  288. if (c == '\r')
  289. continue;
  290. if (n + 1 < max)
  291. out[n++] = (char)c;
  292. }
  293. }
  294. /* ---- HTTP pieces ---- */
  295. /* Case-insensitive match of a header name; line must continue with ':'. */
  296. static int hdr_match(const char *line, const char *name)
  297. {
  298. unsigned int i;
  299. for (i = 0; name[i]; i++) {
  300. char a = line[i];
  301. if (a >= 'A' && a <= 'Z')
  302. a = (char)(a - 'A' + 'a');
  303. if (a != name[i])
  304. return 0;
  305. }
  306. return line[i] == ':';
  307. }
  308. static int parse_dec_after_colon(const char *line, unsigned int *val)
  309. {
  310. unsigned int i = 0, v = 0;
  311. int digits = 0;
  312. while (line[i] && line[i] != ':')
  313. i++;
  314. if (line[i] != ':')
  315. return 0;
  316. i++;
  317. while (line[i] == ' ' || line[i] == '\t')
  318. i++;
  319. while (line[i] >= '0' && line[i] <= '9') {
  320. v = v * 10 + (unsigned int)(line[i] - '0');
  321. digits = 1;
  322. i++;
  323. }
  324. if (!digits)
  325. return 0;
  326. *val = v;
  327. return 1;
  328. }
  329. static void copy_after_colon(const char *line, char *out, unsigned int max)
  330. {
  331. unsigned int i = 0, j = 0;
  332. while (line[i] && line[i] != ':')
  333. i++;
  334. if (line[i] == ':')
  335. i++;
  336. while (line[i] == ' ' || line[i] == '\t')
  337. i++;
  338. while (line[i] && j + 1 < max)
  339. out[j++] = line[i++];
  340. out[j] = '\0';
  341. }
  342. static int contains_ci(const char *hay, const char *needle)
  343. {
  344. unsigned int i, j;
  345. for (i = 0; hay[i]; i++) {
  346. for (j = 0; needle[j]; j++) {
  347. char a = hay[i + j];
  348. if (a >= 'A' && a <= 'Z')
  349. a = (char)(a - 'A' + 'a');
  350. if (a != needle[j])
  351. break;
  352. }
  353. if (!needle[j])
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. static unsigned int parse_status(const char *line)
  359. {
  360. unsigned int i = 0, v = 0, digits = 0;
  361. while (line[i] && line[i] != ' ')
  362. i++;
  363. if (line[i] == ' ')
  364. i++;
  365. while (line[i] >= '0' && line[i] <= '9') {
  366. v = v * 10 + (unsigned int)(line[i] - '0');
  367. digits++;
  368. i++;
  369. }
  370. return digits == 3 ? v : 0;
  371. }
  372. /* Dump the body, filtering '\r'. With have_limit, stop after limit bytes. */
  373. static unsigned int dump_body(struct reader *r, int have_limit,
  374. unsigned int limit)
  375. {
  376. unsigned int n = 0;
  377. int c;
  378. for (;;) {
  379. if (have_limit && n >= limit)
  380. break;
  381. c = reader_getc(r);
  382. if (c < 0)
  383. break;
  384. if (c == '\r')
  385. continue;
  386. u_putc((char)c);
  387. n++;
  388. }
  389. return n;
  390. }
  391. /* Decode a chunked body (hex size line, data, CRLF; size 0 ends it). */
  392. static unsigned int dump_chunked(struct reader *r)
  393. {
  394. unsigned int total = 0;
  395. char line[64];
  396. for (;;) {
  397. unsigned int sz = 0, k = 0, i;
  398. int hl = reader_readline(r, line, sizeof(line));
  399. if (hl <= 0)
  400. return total;
  401. while (line[k]) {
  402. char ch = line[k];
  403. unsigned int d;
  404. if (ch >= '0' && ch <= '9')
  405. d = (unsigned int)(ch - '0');
  406. else if (ch >= 'a' && ch <= 'f')
  407. d = (unsigned int)(ch - 'a' + 10);
  408. else if (ch >= 'A' && ch <= 'F')
  409. d = (unsigned int)(ch - 'A' + 10);
  410. else
  411. break;
  412. sz = sz * 16 + d;
  413. k++;
  414. }
  415. if (k == 0) {
  416. u_puts("curl: bad chunk size\n");
  417. return total;
  418. }
  419. if (sz == 0) {
  420. /* trailer lines up to the empty line */
  421. while (reader_readline(r, line, sizeof(line)) > 0)
  422. ;
  423. return total;
  424. }
  425. for (i = 0; i < sz; i++) {
  426. int c = reader_getc(r);
  427. if (c < 0)
  428. return total;
  429. u_putc((char)c);
  430. total++;
  431. }
  432. reader_readline(r, line, sizeof(line)); /* the CRLF */
  433. }
  434. }
  435. void _start(const char *arg)
  436. {
  437. static char req[REQ_MAX];
  438. static struct reader r;
  439. struct sys_netinfo ni;
  440. char line[LINE_MAX_C];
  441. char location[LOCATION_MAX];
  442. unsigned int status = 0, body = 0, content_len = 0;
  443. unsigned int hops = 0;
  444. int have_loc, have_len, chunked, rv, connected = 0, follow = 1;
  445. if (!arg || !arg[0]) {
  446. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  447. u_exit(1);
  448. }
  449. rv = url_parse(arg, &u);
  450. if (rv != 0) {
  451. u_puts("curl: bad URL\n");
  452. u_exit(1);
  453. }
  454. if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1) {
  455. u_puts("curl: no NIC found\n");
  456. u_exit(1);
  457. }
  458. if (u_syscall0(SYS_NET_CONFIGURED) != 1) {
  459. u_puts("curl: no IP address (run dhcp or netip)\n");
  460. u_exit(1);
  461. }
  462. if (u_syscall0(SYS_NET_ACQUIRE) != 0) {
  463. u_puts("curl: NIC busy\n");
  464. u_exit(1);
  465. }
  466. for (;;) {
  467. unsigned char ip[4], hop[4], mac[6];
  468. unsigned int n;
  469. have_loc = have_len = chunked = 0;
  470. content_len = 0;
  471. u_puts(u.secure ? "curl: GET https://" : "curl: GET http://");
  472. u_puts(u.host);
  473. if (u.port != (u.secure ? 443u : 80u)) {
  474. u_puts(":");
  475. u_putdec(u.port);
  476. }
  477. u_puts(u.path);
  478. u_putc('\n');
  479. if (u_parse_ip(u.host, ip) != 0) {
  480. if (u_syscall2(SYS_DNS_RESOLVE, (int)u.host, (int)ip) != 0)
  481. goto out;
  482. }
  483. u_syscall2(SYS_NET_NEXT_HOP, (int)ip, (int)hop);
  484. rv = u_syscall2(SYS_NET_ARP, (int)hop, (int)mac);
  485. if (rv != 0) {
  486. u_puts("curl: no ARP reply from ");
  487. u_put_ip(hop);
  488. u_putc('\n');
  489. goto out;
  490. }
  491. rv = u_syscall3(SYS_TCP_CONNECT, (int)mac, (int)ip, (int)u.port);
  492. if (rv == -1) {
  493. u_puts("curl: connect timeout\n");
  494. goto out;
  495. }
  496. if (rv == -2) {
  497. u_puts("curl: connection refused\n");
  498. goto out;
  499. }
  500. if (rv != 0) {
  501. u_puts("curl: connect failed\n");
  502. goto out;
  503. }
  504. connected = 1;
  505. if (u.secure) {
  506. u_puts("curl: connected, TLS handshake\n");
  507. if (tls_connect(u.host) != 0)
  508. goto out;
  509. u_puts("curl: WARNING: certificate NOT verified "
  510. "(trust-all mode)\n");
  511. } else {
  512. u_puts("curl: connected, sending request\n");
  513. }
  514. n = req_app(req, 0, "GET ");
  515. n = req_app(req, n, u.path);
  516. n = req_app(req, n, u.secure ? " HTTP/1.1\r\nHost: "
  517. : " HTTP/1.0\r\nHost: ");
  518. n = req_app(req, n, u.host);
  519. if (u.port != (u.secure ? 443u : 80u)) {
  520. n = req_app(req, n, ":");
  521. n = req_app_dec(req, n, u.port);
  522. }
  523. n = req_app(req, n, "\r\nUser-Agent: nebos-curl/0.1\r\n"
  524. "Accept: */*\r\nConnection: close\r\n\r\n");
  525. if ((int)n > u_syscall0(SYS_TCP_PEER_MSS)) {
  526. u_puts("curl: request too long\n");
  527. goto out;
  528. }
  529. rv = xport_send(req, n);
  530. if (rv != 0) {
  531. u_puts("curl: request send failed\n");
  532. goto out;
  533. }
  534. r.len = 0;
  535. r.pos = 0;
  536. r.eof = 0;
  537. r.error = 0;
  538. r.total_deadline = u_ticks_ms() + TOTAL_TIMEOUT_MS;
  539. rv = reader_readline(&r, line, sizeof(line));
  540. if (rv < 0) {
  541. u_puts("curl: no response (");
  542. u_puts(r.error == -2 ? "reset" : "timeout");
  543. u_puts(")\n");
  544. goto out;
  545. }
  546. status = parse_status(line);
  547. u_puts("curl: ");
  548. u_puts(line);
  549. u_putc('\n');
  550. for (;;) {
  551. rv = reader_readline(&r, line, sizeof(line));
  552. if (rv < 0) {
  553. u_puts("curl: connection lost in headers\n");
  554. goto out;
  555. }
  556. if (rv == 0)
  557. break; /* end of headers */
  558. u_puts(line);
  559. u_putc('\n');
  560. if (hdr_match(line, "content-length"))
  561. have_len = parse_dec_after_colon(line,
  562. &content_len);
  563. else if (hdr_match(line, "transfer-encoding") &&
  564. contains_ci(line, "chunked"))
  565. chunked = 1;
  566. else if (hdr_match(line, "location")) {
  567. copy_after_colon(line, location,
  568. sizeof(location));
  569. have_loc = 1;
  570. }
  571. }
  572. if (status < 300 || status >= 400 || !have_loc)
  573. break; /* final response: dump the body */
  574. if (hops >= MAX_REDIRECTS) {
  575. u_puts("curl: too many redirects\n");
  576. follow = 0;
  577. break;
  578. }
  579. u_puts("curl: redirect -> ");
  580. u_puts(location);
  581. u_putc('\n');
  582. tls_close();
  583. u_syscall0(SYS_TCP_CLOSE);
  584. connected = 0;
  585. if (url_apply_location(&u, location) != 0) {
  586. u_puts("curl: bad redirect target\n");
  587. follow = 0;
  588. break;
  589. }
  590. hops++;
  591. }
  592. if (follow && connected) {
  593. u_putc('\n');
  594. if (chunked)
  595. body = dump_chunked(&r);
  596. else
  597. body = dump_body(&r, have_len, content_len);
  598. u_putc('\n');
  599. if (r.error == -2)
  600. u_puts("curl: connection reset\n");
  601. else if (r.error == -1)
  602. u_puts("curl: timeout while reading body\n");
  603. u_puts("curl: ");
  604. u_putdec(body);
  605. u_puts(" bytes received, status ");
  606. u_putdec(status);
  607. u_putc('\n');
  608. }
  609. out:
  610. if (connected) {
  611. tls_close();
  612. u_syscall0(SYS_TCP_CLOSE);
  613. }
  614. u_syscall0(SYS_NET_RELEASE);
  615. u_exit(0);
  616. }