uhttp.c 21 KB

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