uhttp.c 25 KB

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