ucmd_curl.c 12 KB

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