uhttp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /* uhttp: see uhttp.h. Extracted from the original ucmd_curl.c so curl and
  2. * the browser share one HTTP/TLS path. */
  3. #include "uhttp.h"
  4. #include "tls.h"
  5. #include <string.h>
  6. /* ---- tiny string helpers ---- */
  7. static unsigned int s_len(const char *s)
  8. {
  9. unsigned int n = 0;
  10. while (s && s[n])
  11. n++;
  12. return n;
  13. }
  14. static void s_copy(char *d, const char *s, unsigned int cap)
  15. {
  16. unsigned int i = 0;
  17. if (!cap)
  18. return;
  19. while (s && s[i] && i + 1 < cap) {
  20. d[i] = s[i];
  21. i++;
  22. }
  23. d[i] = '\0';
  24. }
  25. static int starts_with(const char *s, const char *prefix)
  26. {
  27. unsigned int i = 0;
  28. while (prefix[i]) {
  29. if (s[i] != prefix[i])
  30. return 0;
  31. i++;
  32. }
  33. return 1;
  34. }
  35. static int contains_ci(const char *hay, const char *needle)
  36. {
  37. unsigned int i, j;
  38. for (i = 0; hay[i]; i++) {
  39. for (j = 0; needle[j]; j++) {
  40. char a = hay[i + j];
  41. if (a >= 'A' && a <= 'Z')
  42. a = (char)(a - 'A' + 'a');
  43. if (a != needle[j])
  44. break;
  45. }
  46. if (!needle[j])
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. /* ---- URL parsing / resolution ---- */
  52. int uhttp_parse_url(const char *s, struct uhttp_url *u)
  53. {
  54. const char *p = s, *h;
  55. unsigned int i;
  56. int secure = 0;
  57. if (!s || !u)
  58. return UHTTP_ERR_URL;
  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. for (i = 0; s[i] && i < 8; i++) {
  66. if (s[i] == ':' && s[i + 1] == '/' && s[i + 2] == '/')
  67. return UHTTP_ERR_URL;
  68. if (s[i] == '/')
  69. break;
  70. }
  71. }
  72. u->secure = secure;
  73. h = p;
  74. while (*p && *p != '/' && *p != ':')
  75. p++;
  76. if (p == h || (unsigned int)(p - h) >= UHTTP_HOST_MAX)
  77. return UHTTP_ERR_URL;
  78. for (i = 0; i < (unsigned int)(p - h); i++)
  79. u->host[i] = h[i];
  80. u->host[i] = '\0';
  81. u->port = secure ? 443 : 80;
  82. if (*p == ':') {
  83. unsigned int port = 0;
  84. p++;
  85. if (*p < '0' || *p > '9')
  86. return UHTTP_ERR_URL;
  87. while (*p >= '0' && *p <= '9') {
  88. port = port * 10 + (unsigned int)(*p - '0');
  89. if (port > 65535)
  90. return UHTTP_ERR_URL;
  91. p++;
  92. }
  93. if (port == 0 || (*p != '/' && *p != '\0'))
  94. return UHTTP_ERR_URL;
  95. u->port = port;
  96. }
  97. i = 0;
  98. if (*p == '/') {
  99. static const char hex[] = "0123456789ABCDEF";
  100. unsigned int j = 0;
  101. /* Percent-encode what cannot go on a request line (spaces,
  102. * controls, quotes, non-ASCII), like a browser does; scripts
  103. * build URLs such as "...&scope=openid email profile". */
  104. while (p[j] && p[j] != '#') {
  105. unsigned char c = (unsigned char)p[j++];
  106. int esc = c <= ' ' || c >= 0x7F || c == '"' ||
  107. c == '<' || c == '>';
  108. if (i + (esc ? 3u : 1u) >= UHTTP_PATH_MAX)
  109. return UHTTP_ERR_URL;
  110. if (esc) {
  111. u->path[i++] = '%';
  112. u->path[i++] = hex[c >> 4];
  113. u->path[i++] = hex[c & 15];
  114. } else {
  115. u->path[i++] = (char)c;
  116. }
  117. }
  118. }
  119. u->path[i] = '\0';
  120. if (i == 0) {
  121. u->path[0] = '/';
  122. u->path[1] = '\0';
  123. }
  124. return 0;
  125. }
  126. static void url_build(const struct uhttp_url *u, char *out, unsigned int cap)
  127. {
  128. unsigned int n = 0;
  129. #define APP(s) do { const char *ap = (s); \
  130. while (*ap && n + 1 < cap) out[n++] = *ap++; } while (0)
  131. APP(u->secure ? "https://" : "http://");
  132. APP(u->host);
  133. if (u->port != (u->secure ? 443u : 80u)) {
  134. char tmp[8];
  135. unsigned int v = u->port, k = 0, j;
  136. if (v == 0) {
  137. tmp[k++] = '0';
  138. } else {
  139. while (v && k < sizeof(tmp)) {
  140. tmp[k++] = (char)('0' + v % 10);
  141. v /= 10;
  142. }
  143. }
  144. if (n + 1 < cap)
  145. out[n++] = ':';
  146. for (j = 0; j < k && n + 1 < cap; j++)
  147. out[n++] = tmp[k - 1 - j];
  148. }
  149. APP(u->path);
  150. #undef APP
  151. out[n] = '\0';
  152. }
  153. static int path_normalize_part(char *path);
  154. /* Normalize an absolute URL path in place: collapse "//", "." and "..".
  155. * Only the path: a query or fragment is kept verbatim (it may carry a URL,
  156. * e.g. ?redirect_uri=https://host/cb). 0 ok, -1 too long. */
  157. /* Path buffers are static (4 KB each): these helpers are leaves and ring-3
  158. * processes are single-threaded, so they need not live on the 128 KB stack. */
  159. static int path_normalize(char *path)
  160. {
  161. static char rest[UHTTP_PATH_MAX];
  162. unsigned int q = 0, i, n;
  163. int rv;
  164. while (path[q] && path[q] != '?' && path[q] != '#')
  165. q++;
  166. if (!path[q])
  167. return path_normalize_part(path);
  168. for (i = 0; path[q + i]; i++)
  169. rest[i] = path[q + i];
  170. rest[i] = '\0';
  171. path[q] = '\0';
  172. rv = path_normalize_part(path);
  173. n = 0;
  174. while (path[n])
  175. n++;
  176. if (rv != 0 || n + i + 1 > UHTTP_PATH_MAX)
  177. return -1;
  178. for (q = 0; q <= i; q++)
  179. path[n + q] = rest[q];
  180. return 0;
  181. }
  182. static int path_normalize_part(char *path)
  183. {
  184. static char tmp[UHTTP_PATH_MAX];
  185. unsigned int i, o = 1, len = 0, start, seglen, j;
  186. int trailing;
  187. while (path[len])
  188. len++;
  189. if (len == 0 || path[0] != '/')
  190. return 0;
  191. trailing = (len > 1 && path[len - 1] == '/');
  192. tmp[0] = '/';
  193. i = 1;
  194. while (path[i]) {
  195. while (path[i] == '/')
  196. i++;
  197. start = i;
  198. while (path[i] && path[i] != '/')
  199. i++;
  200. seglen = i - start;
  201. if (seglen == 0)
  202. break;
  203. if (seglen == 1 && path[start] == '.')
  204. continue;
  205. if (seglen == 2 && path[start] == '.' && path[start + 1] == '.') {
  206. while (o > 1 && tmp[o - 1] != '/')
  207. o--;
  208. if (o > 1)
  209. o--;
  210. continue;
  211. }
  212. if (o + seglen + 2 > sizeof(tmp))
  213. return -1;
  214. if (o > 1)
  215. tmp[o++] = '/';
  216. for (j = 0; j < seglen; j++)
  217. tmp[o++] = path[start + j];
  218. }
  219. if (trailing && o > 1 && tmp[o - 1] != '/') {
  220. if (o + 2 > sizeof(tmp))
  221. return -1;
  222. tmp[o++] = '/';
  223. }
  224. tmp[o] = '\0';
  225. for (i = 0; i <= o; i++)
  226. path[i] = tmp[i];
  227. return 0;
  228. }
  229. /* Apply a Location header to the current URL. */
  230. static int url_apply_location(struct uhttp_url *u, const char *loc)
  231. {
  232. static char joined[UHTTP_PATH_MAX];
  233. char scheme[8];
  234. unsigned int i, dirlen = 1, n = 0;
  235. if (starts_with(loc, "http://") || starts_with(loc, "https://"))
  236. return uhttp_parse_url(loc, u);
  237. if (loc[0] == '/' && loc[1] == '/') {
  238. /* scheme-relative */
  239. s_copy(scheme, u->secure ? "https:" : "http:", sizeof(scheme));
  240. if (s_len(scheme) + s_len(loc) + 1 > UHTTP_LOC_MAX)
  241. return -1;
  242. {
  243. static char full[UHTTP_LOC_MAX];
  244. unsigned int k = 0;
  245. while (scheme[k]) {
  246. full[k] = scheme[k];
  247. k++;
  248. }
  249. for (i = 0; loc[i] && k + 1 < sizeof(full); i++)
  250. full[k++] = loc[i];
  251. full[k] = '\0';
  252. return uhttp_parse_url(full, u);
  253. }
  254. }
  255. if (loc[0] == '/') {
  256. for (i = 0; loc[i]; i++) {
  257. if (i + 1 >= UHTTP_PATH_MAX)
  258. return -1;
  259. u->path[i] = loc[i];
  260. }
  261. u->path[i] = '\0';
  262. return path_normalize(u->path);
  263. }
  264. for (i = 0; u->path[i]; i++)
  265. if (u->path[i] == '/')
  266. dirlen = i + 1;
  267. while (loc[n])
  268. n++;
  269. if (dirlen + n + 1 > UHTTP_PATH_MAX)
  270. return -1;
  271. for (i = 0; i < dirlen; i++)
  272. joined[i] = u->path[i];
  273. for (i = 0; i < n; i++)
  274. joined[dirlen + i] = loc[i];
  275. joined[dirlen + n] = '\0';
  276. for (i = 0; i <= dirlen + n; i++)
  277. u->path[i] = joined[i];
  278. return path_normalize(u->path);
  279. }
  280. int uhttp_resolve(const char *base, const char *href, char *out,
  281. unsigned int cap)
  282. {
  283. static struct uhttp_url u;
  284. if (!base || !href || !out || cap == 0)
  285. return UHTTP_ERR_URL;
  286. if (uhttp_parse_url(base, &u) != 0)
  287. return UHTTP_ERR_URL;
  288. if (href[0] == '#') {
  289. s_copy(out, base, cap);
  290. return 0;
  291. }
  292. if (url_apply_location(&u, href) != 0)
  293. return UHTTP_ERR_URL;
  294. url_build(&u, out, cap);
  295. return 0;
  296. }
  297. /* ---- HTTP header helpers ---- */
  298. static int hdr_match(const char *line, const char *name)
  299. {
  300. unsigned int i;
  301. for (i = 0; name[i]; i++) {
  302. char a = line[i];
  303. if (a >= 'A' && a <= 'Z')
  304. a = (char)(a - 'A' + 'a');
  305. if (a != name[i])
  306. return 0;
  307. }
  308. return line[i] == ':';
  309. }
  310. static int parse_dec_after_colon(const char *line, unsigned int *val)
  311. {
  312. unsigned int i = 0, v = 0;
  313. int digits = 0;
  314. while (line[i] && line[i] != ':')
  315. i++;
  316. if (line[i] != ':')
  317. return 0;
  318. i++;
  319. while (line[i] == ' ' || line[i] == '\t')
  320. i++;
  321. while (line[i] >= '0' && line[i] <= '9') {
  322. v = v * 10 + (unsigned int)(line[i] - '0');
  323. digits = 1;
  324. i++;
  325. }
  326. if (!digits)
  327. return 0;
  328. *val = v;
  329. return 1;
  330. }
  331. static void copy_after_colon(const char *line, char *out, unsigned int max)
  332. {
  333. unsigned int i = 0, j = 0;
  334. while (line[i] && line[i] != ':')
  335. i++;
  336. if (line[i] == ':')
  337. i++;
  338. while (line[i] == ' ' || line[i] == '\t')
  339. i++;
  340. while (line[i] && j + 1 < max)
  341. out[j++] = line[i++];
  342. out[j] = '\0';
  343. }
  344. static unsigned int parse_status(const char *line)
  345. {
  346. unsigned int i = 0, v = 0, digits = 0;
  347. while (line[i] && line[i] != ' ')
  348. i++;
  349. if (line[i] == ' ')
  350. i++;
  351. while (line[i] >= '0' && line[i] <= '9') {
  352. v = v * 10 + (unsigned int)(line[i] - '0');
  353. digits++;
  354. i++;
  355. }
  356. return digits == 3 ? v : 0;
  357. }
  358. /* ---- transport ---- */
  359. /* Send len bytes, split into pieces the transport accepts: one TLS record
  360. * carries at most 1024 bytes and SYS_TCP_SEND at most one peer MSS. */
  361. static int xport_send(struct uhttp *h, const void *buf, unsigned int len)
  362. {
  363. const unsigned char *p = (const unsigned char *)buf;
  364. unsigned int chunk;
  365. if (h->url.secure) {
  366. chunk = 1024;
  367. } else {
  368. int mss = u_syscall0(SYS_TCP_PEER_MSS);
  369. chunk = mss > 0 ? (unsigned int)mss : 536;
  370. }
  371. while (len) {
  372. unsigned int n = len < chunk ? len : chunk;
  373. int rv;
  374. if (h->url.secure)
  375. rv = tls_send(p, n);
  376. else
  377. rv = (int)u_syscall2(SYS_TCP_SEND, (int)p, (int)n);
  378. if (rv != 0)
  379. return rv;
  380. p += n;
  381. len -= n;
  382. }
  383. return 0;
  384. }
  385. static int xport_recv(struct uhttp *h, void *buf, unsigned int max,
  386. unsigned int timeout_ms)
  387. {
  388. if (h->url.secure)
  389. return tls_recv(buf, max, timeout_ms);
  390. return (int)u_syscall3(SYS_TCP_RECV, (int)buf, (int)max,
  391. (int)timeout_ms);
  392. }
  393. static void transport_close(struct uhttp *h)
  394. {
  395. if (!h->connected)
  396. return;
  397. if (h->url.secure)
  398. tls_close();
  399. u_syscall0(SYS_TCP_CLOSE);
  400. h->connected = 0;
  401. }
  402. /* Drop the connection without a TLS close_notify: used when a redirect
  403. * leaves this origin, so the hop pays no extra round trip. */
  404. static void transport_abandon(struct uhttp *h)
  405. {
  406. if (!h->connected)
  407. return;
  408. if (h->url.secure)
  409. tls_abandon();
  410. u_syscall0(SYS_TCP_CLOSE);
  411. h->connected = 0;
  412. }
  413. static int reader_getc(struct uhttp *h, unsigned int timeout_ms);
  414. /* Discard a small, length-delimited redirect body so the next request can
  415. * reuse the connection. 1 when the connection is still usable. */
  416. static int drain_small_body(struct uhttp *h)
  417. {
  418. unsigned int left;
  419. if (h->resp.chunked || !h->resp.have_length ||
  420. h->resp.content_length > 4096)
  421. return 0;
  422. left = h->resp.content_length;
  423. while (left) {
  424. if (reader_getc(h, UHTTP_IDLE_TIMEOUT_MS) < 0)
  425. return 0;
  426. left--;
  427. }
  428. /* Bytes past the body would belong to nothing we asked for. */
  429. return h->pos >= h->len;
  430. }
  431. /* Connect to h->url (TCP + TLS). Keeps the NIC acquired. */
  432. static int http_connect(struct uhttp *h)
  433. {
  434. unsigned char ip[4], hop[4], mac[6];
  435. int rv;
  436. if (u_parse_ip(h->url.host, ip) != 0) {
  437. if (u_syscall2(SYS_DNS_RESOLVE, (int)h->url.host, (int)ip) != 0)
  438. return UHTTP_ERR_DNS;
  439. }
  440. u_syscall2(SYS_NET_NEXT_HOP, (int)ip, (int)hop);
  441. rv = u_syscall2(SYS_NET_ARP, (int)hop, (int)mac);
  442. if (rv != 0)
  443. return UHTTP_ERR_ARP;
  444. rv = u_syscall3(SYS_TCP_CONNECT, (int)mac, (int)ip, (int)h->url.port);
  445. if (rv == -1)
  446. return UHTTP_ERR_TIMEOUT;
  447. if (rv == -2)
  448. return UHTTP_ERR_REFUSED;
  449. if (rv == -4)
  450. return UHTTP_ERR_BUSY;
  451. if (rv != 0)
  452. return UHTTP_ERR_CONNECT;
  453. h->connected = 1;
  454. if (h->url.secure) {
  455. if (tls_connect(h->url.host) != 0) {
  456. transport_close(h);
  457. return UHTTP_ERR_TLS;
  458. }
  459. }
  460. return 0;
  461. }
  462. /* ---- response reader ---- */
  463. static int reader_fill(struct uhttp *h, unsigned int timeout_ms)
  464. {
  465. unsigned int now, deadline, rel;
  466. int n;
  467. if (h->eof)
  468. return 0;
  469. now = u_ticks_ms();
  470. deadline = now + timeout_ms;
  471. if ((int)(h->total_deadline - deadline) < 0)
  472. deadline = h->total_deadline;
  473. rel = (int)(deadline - now) > 0 ? deadline - now : 1;
  474. n = xport_recv(h, h->buf, sizeof(h->buf), rel);
  475. if (n > 0) {
  476. h->len = (unsigned int)n;
  477. h->pos = 0;
  478. return 1;
  479. }
  480. if (n == 0) {
  481. h->eof = 1;
  482. return 0;
  483. }
  484. h->error = (n == -1) ? UHTTP_ERR_TIMEOUT : UHTTP_ERR_RESET;
  485. return 0;
  486. }
  487. static int reader_getc(struct uhttp *h, unsigned int timeout_ms)
  488. {
  489. if (h->pos >= h->len) {
  490. if (!reader_fill(h, timeout_ms))
  491. return -1;
  492. }
  493. return h->buf[h->pos++];
  494. }
  495. static int reader_readline(struct uhttp *h, char *out, unsigned int max,
  496. unsigned int timeout_ms)
  497. {
  498. unsigned int n = 0;
  499. int c;
  500. for (;;) {
  501. c = reader_getc(h, timeout_ms);
  502. if (c < 0) {
  503. out[n] = '\0';
  504. return (n == 0) ? -1 : (int)n;
  505. }
  506. if (c == '\n') {
  507. out[n] = '\0';
  508. return (int)n;
  509. }
  510. if (c == '\r')
  511. continue;
  512. if (n + 1 < max)
  513. out[n++] = (char)c;
  514. }
  515. }
  516. /* ---- request / response ---- */
  517. static unsigned int req_app(char *buf, unsigned int pos, unsigned int cap,
  518. const char *s)
  519. {
  520. while (*s && pos + 1 < cap)
  521. buf[pos++] = *s++;
  522. buf[pos] = '\0';
  523. return pos;
  524. }
  525. static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int cap,
  526. unsigned int v)
  527. {
  528. char b[12];
  529. int i = 0;
  530. if (v == 0) {
  531. if (pos + 1 < cap)
  532. buf[pos++] = '0';
  533. buf[pos] = '\0';
  534. return pos;
  535. }
  536. while (v) {
  537. b[i++] = (char)('0' + (v % 10));
  538. v /= 10;
  539. }
  540. while (i) {
  541. i--;
  542. if (pos + 1 < cap)
  543. buf[pos++] = b[i];
  544. }
  545. buf[pos] = '\0';
  546. return pos;
  547. }
  548. /* Read the status line and headers of the current response. Returns 0 and
  549. * sets redirect/location when the response is a redirect with a target. */
  550. static int read_response_head(struct uhttp *h, char *location, int *have_loc,
  551. unsigned int timeout_ms)
  552. {
  553. char line[UHTTP_LINE_MAX];
  554. unsigned int status = 0;
  555. int rv;
  556. h->resp.status = 0;
  557. h->resp.chunked = 0;
  558. h->resp.have_length = 0;
  559. h->resp.content_length = 0;
  560. h->resp.gzip = 0;
  561. h->resp.content_type[0] = '\0';
  562. h->resp.content_encoding[0] = '\0';
  563. h->peer_close = 0;
  564. h->chunk_state = 0;
  565. h->chunk_left = 0;
  566. h->body_read = 0;
  567. h->len = h->pos = 0;
  568. h->eof = 0;
  569. h->error = 0;
  570. *have_loc = 0;
  571. location[0] = '\0';
  572. rv = reader_readline(h, line, sizeof(line), timeout_ms);
  573. if (rv < 0)
  574. return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
  575. UHTTP_ERR_TIMEOUT;
  576. status = parse_status(line);
  577. h->resp.status = (int)status;
  578. if (h->hooks && h->hooks->status)
  579. h->hooks->status(h->hooks->ctx, (int)status, line);
  580. /* HTTP/1.0 has no persistent connections unless the response
  581. * explicitly opts in. Ignoring this kept a half-closed socket for
  582. * the next request, which then waited for a response that could not
  583. * arrive: the next batch of images appeared to hang. */
  584. if (line[0] == 'H' && line[1] == 'T' && line[2] == 'T' &&
  585. line[3] == 'P' && line[4] == '/' && line[5] == '1' &&
  586. line[6] == '.' && line[7] == '0')
  587. h->peer_close = 1;
  588. for (;;) {
  589. rv = reader_readline(h, line, sizeof(line), timeout_ms);
  590. if (rv < 0)
  591. return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
  592. UHTTP_ERR_TIMEOUT;
  593. if (rv == 0)
  594. break;
  595. if (h->hooks && h->hooks->header)
  596. h->hooks->header(h->hooks->ctx, line);
  597. if (hdr_match(line, "content-length"))
  598. h->resp.have_length =
  599. parse_dec_after_colon(line, &h->resp.content_length);
  600. else if (hdr_match(line, "transfer-encoding") &&
  601. contains_ci(line, "chunked"))
  602. h->resp.chunked = 1;
  603. else if (hdr_match(line, "content-encoding")) {
  604. copy_after_colon(line, h->resp.content_encoding,
  605. sizeof(h->resp.content_encoding));
  606. if (contains_ci(h->resp.content_encoding, "gzip"))
  607. h->resp.gzip = 1;
  608. }
  609. else if (hdr_match(line, "content-type")) {
  610. copy_after_colon(line, h->resp.content_type,
  611. sizeof(h->resp.content_type));
  612. } else if (hdr_match(line, "location")) {
  613. copy_after_colon(line, location, UHTTP_LOC_MAX);
  614. *have_loc = location[0] != '\0';
  615. } else if (hdr_match(line, "connection")) {
  616. if (contains_ci(line, "close"))
  617. h->peer_close = 1;
  618. else if (contains_ci(line, "keep-alive"))
  619. h->peer_close = 0;
  620. } else if (h->jar && hdr_match(line, "set-cookie")) {
  621. char *v = line + 11; /* past "set-cookie:" */
  622. while (*v == ' ' || *v == '\t')
  623. v++;
  624. (void)uhttp_jar_set(h->jar, h->url.host, h->url.path, v);
  625. }
  626. }
  627. /* 1xx/204/304 responses never carry a body; without this a kept-alive
  628. * 204 would be read until the idle timeout. */
  629. if ((status >= 100 && status < 200) || status == 204 || status == 304) {
  630. h->resp.chunked = 0;
  631. h->resp.have_length = 1;
  632. h->resp.content_length = 0;
  633. }
  634. return 0;
  635. }
  636. static int send_request(struct uhttp *h, unsigned int flags)
  637. {
  638. static char req[UHTTP_REQ_MAX];
  639. unsigned int n = 0;
  640. int rv;
  641. n = req_app(req, 0, sizeof(req), h->method ? h->method : "GET");
  642. n = req_app(req, n, sizeof(req), " ");
  643. n = req_app(req, n, sizeof(req), h->url.path);
  644. n = req_app(req, n, sizeof(req), " HTTP/1.1\r\nHost: ");
  645. n = req_app(req, n, sizeof(req), h->url.host);
  646. if (h->url.port != (h->url.secure ? 443u : 80u)) {
  647. n = req_app(req, n, sizeof(req), ":");
  648. n = req_app_dec(req, n, sizeof(req), h->url.port);
  649. }
  650. n = req_app(req, n, sizeof(req), "\r\nUser-Agent: ");
  651. n = req_app(req, n, sizeof(req), h->user_agent);
  652. n = req_app(req, n, sizeof(req), "\r\nAccept: ");
  653. n = req_app(req, n, sizeof(req), h->accept ? h->accept : "*/*");
  654. n = req_app(req, n, sizeof(req), "\r\n");
  655. if (h->jar) {
  656. static char cookie[2048];
  657. if (uhttp_jar_header(h->jar, h->url.host, h->url.path,
  658. h->url.secure, cookie, sizeof(cookie))) {
  659. n = req_app(req, n, sizeof(req), "Cookie: ");
  660. n = req_app(req, n, sizeof(req), cookie);
  661. n = req_app(req, n, sizeof(req), "\r\n");
  662. }
  663. }
  664. if (h->extra_headers)
  665. n = req_app(req, n, sizeof(req), h->extra_headers);
  666. if (flags & UHTTP_GZIP)
  667. n = req_app(req, n, sizeof(req), "Accept-Encoding: gzip\r\n");
  668. if (h->body_len) {
  669. n = req_app(req, n, sizeof(req), "Content-Type: ");
  670. n = req_app(req, n, sizeof(req), h->content_type ?
  671. h->content_type : "application/octet-stream");
  672. n = req_app(req, n, sizeof(req), "\r\nContent-Length: ");
  673. n = req_app_dec(req, n, sizeof(req), h->body_len);
  674. n = req_app(req, n, sizeof(req), "\r\n");
  675. }
  676. n = req_app(req, n, sizeof(req),
  677. (h->keepalive && !h->peer_close) ?
  678. "Connection: keep-alive\r\n\r\n" :
  679. "Connection: close\r\n\r\n");
  680. /* req_app stops one byte short of the end: a full buffer means the
  681. * request was truncated. */
  682. if (n + 1 >= sizeof(req))
  683. return UHTTP_ERR_TOOLONG;
  684. rv = xport_send(h, req, n);
  685. if (rv != 0)
  686. return UHTTP_ERR_SEND;
  687. if (h->body_len) {
  688. rv = xport_send(h, h->body, h->body_len);
  689. if (rv != 0)
  690. return UHTTP_ERR_SEND;
  691. }
  692. return 0;
  693. }
  694. void uhttp_init(struct uhttp *h)
  695. {
  696. unsigned int i;
  697. for (i = 0; i < sizeof(*h); i++)
  698. ((unsigned char *)h)[i] = 0;
  699. h->user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  700. }
  701. void uhttp_set_method(struct uhttp *h, const char *method, const char *body,
  702. unsigned int body_len, const char *content_type)
  703. {
  704. h->method = method;
  705. h->body = body;
  706. h->body_len = body_len;
  707. h->content_type = content_type;
  708. }
  709. static int host_same(const char *a, const char *b)
  710. {
  711. while (*a && *b && *a == *b) {
  712. a++;
  713. b++;
  714. }
  715. return *a == *b;
  716. }
  717. int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
  718. const struct uhttp_hooks *hooks)
  719. {
  720. char location[UHTTP_LOC_MAX];
  721. char old_host[UHTTP_HOST_MAX];
  722. unsigned int old_port = 0;
  723. int old_secure = 0;
  724. unsigned int hops = 0;
  725. int reuse = 0;
  726. int rv;
  727. h->hooks = hooks;
  728. if (!h->user_agent)
  729. h->user_agent = "nebos-uhttp/0.1";
  730. if (h->keepalive && h->connected) {
  731. s_copy(old_host, h->url.host, sizeof(old_host));
  732. old_port = h->url.port;
  733. old_secure = h->url.secure;
  734. reuse = 1;
  735. }
  736. h->len = h->pos = 0;
  737. h->eof = 0;
  738. h->error = 0;
  739. h->peer_close = 0;
  740. if (uhttp_parse_url(url, &h->url) != 0)
  741. return UHTTP_ERR_URL;
  742. url_build(&h->url, h->resp.final_url, sizeof(h->resp.final_url));
  743. if (reuse && host_same(old_host, h->url.host) &&
  744. old_port == h->url.port && old_secure == h->url.secure) {
  745. /* Keep the existing TCP/TLS connection. */
  746. } else {
  747. reuse = 0;
  748. if (h->connected)
  749. transport_close(h);
  750. {
  751. struct sys_netinfo ni;
  752. if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1)
  753. return UHTTP_ERR_NIC;
  754. if (u_syscall0(SYS_NET_CONFIGURED) != 1)
  755. return UHTTP_ERR_IP;
  756. }
  757. }
  758. h->total_deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS;
  759. for (;;) {
  760. int have_loc;
  761. int used_reuse = reuse;
  762. reuse = 0;
  763. for (;;) {
  764. if (!used_reuse) {
  765. rv = http_connect(h);
  766. if (rv != 0)
  767. goto fail;
  768. if (h->hooks && h->hooks->connected)
  769. h->hooks->connected(h->hooks->ctx,
  770. h->url.secure);
  771. }
  772. rv = send_request(h, flags);
  773. if (rv == 0)
  774. rv = read_response_head(h, location,
  775. &have_loc,
  776. used_reuse ? 3000 :
  777. UHTTP_IDLE_TIMEOUT_MS);
  778. if (rv == 0)
  779. break;
  780. if (!used_reuse)
  781. goto fail;
  782. /* A kept-alive socket may have been closed by the
  783. * server; reconnect and retry once. */
  784. used_reuse = 0;
  785. transport_close(h);
  786. }
  787. if (h->resp.status >= 300 && h->resp.status < 400 && have_loc) {
  788. if (h->hooks && h->hooks->redirect)
  789. h->hooks->redirect(h->hooks->ctx, location);
  790. if (hops >= UHTTP_MAX_REDIRECTS) {
  791. rv = UHTTP_ERR_TOOMANY;
  792. goto fail;
  793. }
  794. {
  795. struct uhttp_url next = h->url;
  796. if (url_apply_location(&next, location) != 0) {
  797. transport_abandon(h);
  798. rv = UHTTP_ERR_REDIRECT;
  799. goto fail;
  800. }
  801. /* A same-origin redirect (/ -> /en/) keeps the
  802. * TCP/TLS connection; any other one drops it
  803. * without the close handshake. */
  804. if (h->keepalive && !h->peer_close &&
  805. host_same(next.host, h->url.host) &&
  806. next.port == h->url.port &&
  807. next.secure == h->url.secure &&
  808. drain_small_body(h))
  809. reuse = 1;
  810. else
  811. transport_abandon(h);
  812. h->url = next;
  813. }
  814. /* A redirected POST becomes a GET unless the
  815. * server explicitly asks to repeat the request. */
  816. if (h->resp.status != 307 && h->resp.status != 308) {
  817. h->method = 0;
  818. h->body = 0;
  819. h->body_len = 0;
  820. h->content_type = 0;
  821. }
  822. url_build(&h->url, h->resp.final_url,
  823. sizeof(h->resp.final_url));
  824. hops++;
  825. continue;
  826. }
  827. break;
  828. }
  829. return 0;
  830. fail:
  831. h->keepalive = 0;
  832. uhttp_close(h);
  833. return rv;
  834. }
  835. /* Copy up to max decoded body bytes; >0 data, 0 EOF, <0 error. */
  836. int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
  837. unsigned int timeout_ms)
  838. {
  839. unsigned char *out = (unsigned char *)vbuf;
  840. unsigned int n = 0;
  841. if (h->error)
  842. return h->error;
  843. if (h->eof && h->chunk_state != 1 &&
  844. !(h->resp.have_length && h->body_read < h->resp.content_length))
  845. return 0;
  846. if (max == 0)
  847. return 0;
  848. if (!h->resp.chunked && h->resp.have_length &&
  849. h->body_read >= h->resp.content_length) {
  850. h->eof = 1;
  851. return 0;
  852. }
  853. while (n < max) {
  854. if (h->resp.chunked) {
  855. if (h->chunk_state == 0) {
  856. char line[64];
  857. unsigned int sz = 0, k = 0;
  858. int hl = reader_readline(h, line, sizeof(line),
  859. timeout_ms);
  860. if (hl < 0) {
  861. if (h->eof)
  862. h->peer_close = 1;
  863. if (n)
  864. break;
  865. return h->error ? h->error : 0;
  866. }
  867. while (line[k]) {
  868. char ch = line[k];
  869. unsigned int d;
  870. if (ch >= '0' && ch <= '9')
  871. d = (unsigned int)(ch - '0');
  872. else if (ch >= 'a' && ch <= 'f')
  873. d = (unsigned int)(ch - 'a' + 10);
  874. else if (ch >= 'A' && ch <= 'F')
  875. d = (unsigned int)(ch - 'A' + 10);
  876. else
  877. break;
  878. sz = sz * 16 + d;
  879. k++;
  880. }
  881. if (k == 0) {
  882. h->error = UHTTP_ERR_RESET;
  883. return n ? (int)n : h->error;
  884. }
  885. if (sz == 0) {
  886. h->chunk_state = 3;
  887. } else {
  888. h->chunk_left = sz;
  889. h->chunk_state = 1;
  890. }
  891. } else if (h->chunk_state == 1) {
  892. unsigned int avail, want;
  893. if (h->pos >= h->len) {
  894. if (!reader_fill(h, timeout_ms)) {
  895. if (h->eof)
  896. h->peer_close = 1;
  897. if (n)
  898. break;
  899. return h->error ? h->error : 0;
  900. }
  901. }
  902. avail = h->len - h->pos;
  903. want = max - n;
  904. if (want > h->chunk_left)
  905. want = h->chunk_left;
  906. if (want > avail)
  907. want = avail;
  908. if (!want)
  909. continue;
  910. memcpy(out + n, h->buf + h->pos, want);
  911. h->pos += want;
  912. n += want;
  913. h->body_read += want;
  914. h->chunk_left -= want;
  915. if (h->chunk_left == 0)
  916. h->chunk_state = 2;
  917. } else if (h->chunk_state == 2) {
  918. (void)reader_getc(h, timeout_ms); /* CR */
  919. (void)reader_getc(h, timeout_ms); /* LF */
  920. h->chunk_state = 0;
  921. } else {
  922. char line[64];
  923. int hl = reader_readline(h, line, sizeof(line),
  924. timeout_ms);
  925. if (hl <= 0) {
  926. h->eof = 1;
  927. h->peer_close = 1;
  928. break;
  929. }
  930. }
  931. } else {
  932. unsigned int avail, want;
  933. if (h->resp.have_length &&
  934. h->body_read >= h->resp.content_length) {
  935. h->eof = 1;
  936. break;
  937. }
  938. if (h->pos >= h->len) {
  939. if (!reader_fill(h, timeout_ms)) {
  940. if (h->eof)
  941. h->peer_close = 1;
  942. if (n)
  943. break;
  944. return h->error ? h->error : 0;
  945. }
  946. }
  947. avail = h->len - h->pos;
  948. want = max - n;
  949. if (h->resp.have_length) {
  950. unsigned int left = h->resp.content_length -
  951. h->body_read;
  952. if (want > left)
  953. want = left;
  954. }
  955. if (want > avail)
  956. want = avail;
  957. if (!want)
  958. continue;
  959. memcpy(out + n, h->buf + h->pos, want);
  960. h->pos += want;
  961. n += want;
  962. h->body_read += want;
  963. }
  964. }
  965. return (int)n;
  966. }
  967. void uhttp_close(struct uhttp *h)
  968. {
  969. if (h->keepalive && h->connected && !h->peer_close && !h->error)
  970. return;
  971. transport_close(h);
  972. }
  973. const char *uhttp_error_string(int err)
  974. {
  975. switch (err) {
  976. case 0: return "ok";
  977. case UHTTP_ERR_URL: return "bad URL";
  978. case UHTTP_ERR_NIC: return "no NIC found";
  979. case UHTTP_ERR_IP: return "no IP address (run dhcp or netip)";
  980. case UHTTP_ERR_BUSY: return "network busy";
  981. case UHTTP_ERR_DNS: return "DNS lookup failed";
  982. case UHTTP_ERR_ARP: return "no ARP reply";
  983. case UHTTP_ERR_CONNECT: return "connection failed";
  984. case UHTTP_ERR_REFUSED: return "connection refused";
  985. case UHTTP_ERR_TLS: return "TLS handshake failed";
  986. case UHTTP_ERR_SEND: return "request send failed";
  987. case UHTTP_ERR_TIMEOUT: return "timeout";
  988. case UHTTP_ERR_RESET: return "connection reset";
  989. case UHTTP_ERR_REDIRECT: return "bad redirect target";
  990. case UHTTP_ERR_TOOMANY: return "too many redirects";
  991. case UHTTP_ERR_TOOLONG: return "request too long";
  992. default: return "network error";
  993. }
  994. }