ucmd_curl.c 11 KB

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