uhttp.c 25 KB

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