curl.c 11 KB

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