uhttp.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. /* Send len bytes, split into pieces the transport accepts: one TLS record
  321. * carries at most 1024 bytes and SYS_TCP_SEND at most one peer MSS. */
  322. static int xport_send(struct uhttp *h, const void *buf, unsigned int len)
  323. {
  324. const unsigned char *p = (const unsigned char *)buf;
  325. unsigned int chunk;
  326. if (h->url.secure) {
  327. chunk = 1024;
  328. } else {
  329. int mss = u_syscall0(SYS_TCP_PEER_MSS);
  330. chunk = mss > 0 ? (unsigned int)mss : 536;
  331. }
  332. while (len) {
  333. unsigned int n = len < chunk ? len : chunk;
  334. int rv;
  335. if (h->url.secure)
  336. rv = tls_send(p, n);
  337. else
  338. rv = (int)u_syscall2(SYS_TCP_SEND, (int)p, (int)n);
  339. if (rv != 0)
  340. return rv;
  341. p += n;
  342. len -= n;
  343. }
  344. return 0;
  345. }
  346. static int xport_recv(struct uhttp *h, void *buf, unsigned int max,
  347. unsigned int timeout_ms)
  348. {
  349. if (h->url.secure)
  350. return tls_recv(buf, max, timeout_ms);
  351. return (int)u_syscall3(SYS_TCP_RECV, (int)buf, (int)max,
  352. (int)timeout_ms);
  353. }
  354. static void transport_close(struct uhttp *h)
  355. {
  356. if (!h->connected)
  357. return;
  358. if (h->url.secure)
  359. tls_close();
  360. u_syscall0(SYS_TCP_CLOSE);
  361. h->connected = 0;
  362. }
  363. /* Drop the connection without a TLS close_notify: used when a redirect
  364. * leaves this origin, so the hop pays no extra round trip. */
  365. static void transport_abandon(struct uhttp *h)
  366. {
  367. if (!h->connected)
  368. return;
  369. if (h->url.secure)
  370. tls_abandon();
  371. u_syscall0(SYS_TCP_CLOSE);
  372. h->connected = 0;
  373. }
  374. static int reader_getc(struct uhttp *h, unsigned int timeout_ms);
  375. /* Discard a small, length-delimited redirect body so the next request can
  376. * reuse the connection. 1 when the connection is still usable. */
  377. static int drain_small_body(struct uhttp *h)
  378. {
  379. unsigned int left;
  380. if (h->resp.chunked || !h->resp.have_length ||
  381. h->resp.content_length > 4096)
  382. return 0;
  383. left = h->resp.content_length;
  384. while (left) {
  385. if (reader_getc(h, UHTTP_IDLE_TIMEOUT_MS) < 0)
  386. return 0;
  387. left--;
  388. }
  389. /* Bytes past the body would belong to nothing we asked for. */
  390. return h->pos >= h->len;
  391. }
  392. /* Connect to h->url (TCP + TLS). Keeps the NIC acquired. */
  393. static int http_connect(struct uhttp *h)
  394. {
  395. unsigned char ip[4], hop[4], mac[6];
  396. int rv;
  397. if (u_parse_ip(h->url.host, ip) != 0) {
  398. if (u_syscall2(SYS_DNS_RESOLVE, (int)h->url.host, (int)ip) != 0)
  399. return UHTTP_ERR_DNS;
  400. }
  401. u_syscall2(SYS_NET_NEXT_HOP, (int)ip, (int)hop);
  402. rv = u_syscall2(SYS_NET_ARP, (int)hop, (int)mac);
  403. if (rv != 0)
  404. return UHTTP_ERR_ARP;
  405. rv = u_syscall3(SYS_TCP_CONNECT, (int)mac, (int)ip, (int)h->url.port);
  406. if (rv == -1)
  407. return UHTTP_ERR_TIMEOUT;
  408. if (rv == -2)
  409. return UHTTP_ERR_REFUSED;
  410. if (rv == -4)
  411. return UHTTP_ERR_BUSY;
  412. if (rv != 0)
  413. return UHTTP_ERR_CONNECT;
  414. h->connected = 1;
  415. if (h->url.secure) {
  416. if (tls_connect(h->url.host) != 0) {
  417. transport_close(h);
  418. return UHTTP_ERR_TLS;
  419. }
  420. }
  421. return 0;
  422. }
  423. /* ---- response reader ---- */
  424. static int reader_fill(struct uhttp *h, unsigned int timeout_ms)
  425. {
  426. unsigned int now, deadline, rel;
  427. int n;
  428. if (h->eof)
  429. return 0;
  430. now = u_ticks_ms();
  431. deadline = now + timeout_ms;
  432. if ((int)(h->total_deadline - deadline) < 0)
  433. deadline = h->total_deadline;
  434. rel = (int)(deadline - now) > 0 ? deadline - now : 1;
  435. n = xport_recv(h, h->buf, sizeof(h->buf), rel);
  436. if (n > 0) {
  437. h->len = (unsigned int)n;
  438. h->pos = 0;
  439. return 1;
  440. }
  441. if (n == 0) {
  442. h->eof = 1;
  443. return 0;
  444. }
  445. h->error = (n == -1) ? UHTTP_ERR_TIMEOUT : UHTTP_ERR_RESET;
  446. return 0;
  447. }
  448. static int reader_getc(struct uhttp *h, unsigned int timeout_ms)
  449. {
  450. if (h->pos >= h->len) {
  451. if (!reader_fill(h, timeout_ms))
  452. return -1;
  453. }
  454. return h->buf[h->pos++];
  455. }
  456. static int reader_readline(struct uhttp *h, char *out, unsigned int max,
  457. unsigned int timeout_ms)
  458. {
  459. unsigned int n = 0;
  460. int c;
  461. for (;;) {
  462. c = reader_getc(h, timeout_ms);
  463. if (c < 0) {
  464. out[n] = '\0';
  465. return (n == 0) ? -1 : (int)n;
  466. }
  467. if (c == '\n') {
  468. out[n] = '\0';
  469. return (int)n;
  470. }
  471. if (c == '\r')
  472. continue;
  473. if (n + 1 < max)
  474. out[n++] = (char)c;
  475. }
  476. }
  477. /* ---- request / response ---- */
  478. static unsigned int req_app(char *buf, unsigned int pos, unsigned int cap,
  479. const char *s)
  480. {
  481. while (*s && pos + 1 < cap)
  482. buf[pos++] = *s++;
  483. buf[pos] = '\0';
  484. return pos;
  485. }
  486. static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int cap,
  487. unsigned int v)
  488. {
  489. char b[12];
  490. int i = 0;
  491. if (v == 0) {
  492. if (pos + 1 < cap)
  493. buf[pos++] = '0';
  494. buf[pos] = '\0';
  495. return pos;
  496. }
  497. while (v) {
  498. b[i++] = (char)('0' + (v % 10));
  499. v /= 10;
  500. }
  501. while (i) {
  502. i--;
  503. if (pos + 1 < cap)
  504. buf[pos++] = b[i];
  505. }
  506. buf[pos] = '\0';
  507. return pos;
  508. }
  509. /* Read the status line and headers of the current response. Returns 0 and
  510. * sets redirect/location when the response is a redirect with a target. */
  511. static int read_response_head(struct uhttp *h, char *location, int *have_loc,
  512. unsigned int timeout_ms)
  513. {
  514. char line[UHTTP_LINE_MAX];
  515. unsigned int status = 0;
  516. int rv;
  517. h->resp.status = 0;
  518. h->resp.chunked = 0;
  519. h->resp.have_length = 0;
  520. h->resp.content_length = 0;
  521. h->resp.gzip = 0;
  522. h->resp.content_type[0] = '\0';
  523. h->resp.content_encoding[0] = '\0';
  524. h->peer_close = 0;
  525. h->chunk_state = 0;
  526. h->chunk_left = 0;
  527. h->body_read = 0;
  528. h->len = h->pos = 0;
  529. h->eof = 0;
  530. h->error = 0;
  531. *have_loc = 0;
  532. location[0] = '\0';
  533. rv = reader_readline(h, line, sizeof(line), timeout_ms);
  534. if (rv < 0)
  535. return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
  536. UHTTP_ERR_TIMEOUT;
  537. status = parse_status(line);
  538. h->resp.status = (int)status;
  539. if (h->hooks && h->hooks->status)
  540. h->hooks->status(h->hooks->ctx, (int)status, line);
  541. /* HTTP/1.0 has no persistent connections unless the response
  542. * explicitly opts in. Ignoring this kept a half-closed socket for
  543. * the next request, which then waited for a response that could not
  544. * arrive: the next batch of images appeared to hang. */
  545. if (line[0] == 'H' && line[1] == 'T' && line[2] == 'T' &&
  546. line[3] == 'P' && line[4] == '/' && line[5] == '1' &&
  547. line[6] == '.' && line[7] == '0')
  548. h->peer_close = 1;
  549. for (;;) {
  550. rv = reader_readline(h, line, sizeof(line), timeout_ms);
  551. if (rv < 0)
  552. return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
  553. UHTTP_ERR_TIMEOUT;
  554. if (rv == 0)
  555. break;
  556. if (h->hooks && h->hooks->header)
  557. h->hooks->header(h->hooks->ctx, line);
  558. if (hdr_match(line, "content-length"))
  559. h->resp.have_length =
  560. parse_dec_after_colon(line, &h->resp.content_length);
  561. else if (hdr_match(line, "transfer-encoding") &&
  562. contains_ci(line, "chunked"))
  563. h->resp.chunked = 1;
  564. else if (hdr_match(line, "content-encoding")) {
  565. copy_after_colon(line, h->resp.content_encoding,
  566. sizeof(h->resp.content_encoding));
  567. if (contains_ci(h->resp.content_encoding, "gzip"))
  568. h->resp.gzip = 1;
  569. }
  570. else if (hdr_match(line, "content-type")) {
  571. copy_after_colon(line, h->resp.content_type,
  572. sizeof(h->resp.content_type));
  573. } else if (hdr_match(line, "location")) {
  574. copy_after_colon(line, location, UHTTP_LOC_MAX);
  575. *have_loc = location[0] != '\0';
  576. } else if (hdr_match(line, "connection")) {
  577. if (contains_ci(line, "close"))
  578. h->peer_close = 1;
  579. else if (contains_ci(line, "keep-alive"))
  580. h->peer_close = 0;
  581. } else if (h->jar && hdr_match(line, "set-cookie")) {
  582. char *v = line + 11; /* past "set-cookie:" */
  583. while (*v == ' ' || *v == '\t')
  584. v++;
  585. (void)uhttp_jar_set(h->jar, h->url.host, h->url.path, v);
  586. }
  587. }
  588. /* 1xx/204/304 responses never carry a body; without this a kept-alive
  589. * 204 would be read until the idle timeout. */
  590. if ((status >= 100 && status < 200) || status == 204 || status == 304) {
  591. h->resp.chunked = 0;
  592. h->resp.have_length = 1;
  593. h->resp.content_length = 0;
  594. }
  595. return 0;
  596. }
  597. static int send_request(struct uhttp *h, unsigned int flags)
  598. {
  599. static char req[UHTTP_REQ_MAX];
  600. unsigned int n = 0;
  601. int rv;
  602. n = req_app(req, 0, sizeof(req), h->method ? h->method : "GET");
  603. n = req_app(req, n, sizeof(req), " ");
  604. n = req_app(req, n, sizeof(req), h->url.path);
  605. n = req_app(req, n, sizeof(req), " HTTP/1.1\r\nHost: ");
  606. n = req_app(req, n, sizeof(req), h->url.host);
  607. if (h->url.port != (h->url.secure ? 443u : 80u)) {
  608. n = req_app(req, n, sizeof(req), ":");
  609. n = req_app_dec(req, n, sizeof(req), h->url.port);
  610. }
  611. n = req_app(req, n, sizeof(req), "\r\nUser-Agent: ");
  612. n = req_app(req, n, sizeof(req), h->user_agent);
  613. n = req_app(req, n, sizeof(req), "\r\nAccept: ");
  614. n = req_app(req, n, sizeof(req), h->accept ? h->accept : "*/*");
  615. n = req_app(req, n, sizeof(req), "\r\n");
  616. if (h->jar) {
  617. static char cookie[2048];
  618. if (uhttp_jar_header(h->jar, h->url.host, h->url.path,
  619. h->url.secure, cookie, sizeof(cookie))) {
  620. n = req_app(req, n, sizeof(req), "Cookie: ");
  621. n = req_app(req, n, sizeof(req), cookie);
  622. n = req_app(req, n, sizeof(req), "\r\n");
  623. }
  624. }
  625. if (h->extra_headers)
  626. n = req_app(req, n, sizeof(req), h->extra_headers);
  627. if (flags & UHTTP_GZIP)
  628. n = req_app(req, n, sizeof(req), "Accept-Encoding: gzip\r\n");
  629. if (h->body_len) {
  630. n = req_app(req, n, sizeof(req), "Content-Type: ");
  631. n = req_app(req, n, sizeof(req), h->content_type ?
  632. h->content_type : "application/octet-stream");
  633. n = req_app(req, n, sizeof(req), "\r\nContent-Length: ");
  634. n = req_app_dec(req, n, sizeof(req), h->body_len);
  635. n = req_app(req, n, sizeof(req), "\r\n");
  636. }
  637. n = req_app(req, n, sizeof(req),
  638. (h->keepalive && !h->peer_close) ?
  639. "Connection: keep-alive\r\n\r\n" :
  640. "Connection: close\r\n\r\n");
  641. /* req_app stops one byte short of the end: a full buffer means the
  642. * request was truncated. */
  643. if (n + 1 >= sizeof(req))
  644. return UHTTP_ERR_TOOLONG;
  645. rv = xport_send(h, req, n);
  646. if (rv != 0)
  647. return UHTTP_ERR_SEND;
  648. if (h->body_len) {
  649. rv = xport_send(h, h->body, h->body_len);
  650. if (rv != 0)
  651. return UHTTP_ERR_SEND;
  652. }
  653. return 0;
  654. }
  655. void uhttp_init(struct uhttp *h)
  656. {
  657. unsigned int i;
  658. for (i = 0; i < sizeof(*h); i++)
  659. ((unsigned char *)h)[i] = 0;
  660. h->user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  661. }
  662. void uhttp_set_method(struct uhttp *h, const char *method, const char *body,
  663. unsigned int body_len, const char *content_type)
  664. {
  665. h->method = method;
  666. h->body = body;
  667. h->body_len = body_len;
  668. h->content_type = content_type;
  669. }
  670. static int host_same(const char *a, const char *b)
  671. {
  672. while (*a && *b && *a == *b) {
  673. a++;
  674. b++;
  675. }
  676. return *a == *b;
  677. }
  678. int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
  679. const struct uhttp_hooks *hooks)
  680. {
  681. char location[UHTTP_LOC_MAX];
  682. char old_host[UHTTP_HOST_MAX];
  683. unsigned int old_port = 0;
  684. int old_secure = 0;
  685. unsigned int hops = 0;
  686. int reuse = 0;
  687. int rv;
  688. h->hooks = hooks;
  689. if (!h->user_agent)
  690. h->user_agent = "nebos-uhttp/0.1";
  691. if (h->keepalive && h->connected) {
  692. s_copy(old_host, h->url.host, sizeof(old_host));
  693. old_port = h->url.port;
  694. old_secure = h->url.secure;
  695. reuse = 1;
  696. }
  697. h->len = h->pos = 0;
  698. h->eof = 0;
  699. h->error = 0;
  700. h->peer_close = 0;
  701. if (uhttp_parse_url(url, &h->url) != 0)
  702. return UHTTP_ERR_URL;
  703. url_build(&h->url, h->resp.final_url, sizeof(h->resp.final_url));
  704. if (reuse && host_same(old_host, h->url.host) &&
  705. old_port == h->url.port && old_secure == h->url.secure) {
  706. /* Keep the existing TCP/TLS connection. */
  707. } else {
  708. reuse = 0;
  709. if (h->connected)
  710. transport_close(h);
  711. {
  712. struct sys_netinfo ni;
  713. if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1)
  714. return UHTTP_ERR_NIC;
  715. if (u_syscall0(SYS_NET_CONFIGURED) != 1)
  716. return UHTTP_ERR_IP;
  717. }
  718. }
  719. h->total_deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS;
  720. for (;;) {
  721. int have_loc;
  722. int used_reuse = reuse;
  723. reuse = 0;
  724. for (;;) {
  725. if (!used_reuse) {
  726. rv = http_connect(h);
  727. if (rv != 0)
  728. goto fail;
  729. if (h->hooks && h->hooks->connected)
  730. h->hooks->connected(h->hooks->ctx,
  731. h->url.secure);
  732. }
  733. rv = send_request(h, flags);
  734. if (rv == 0)
  735. rv = read_response_head(h, location,
  736. &have_loc,
  737. used_reuse ? 3000 :
  738. UHTTP_IDLE_TIMEOUT_MS);
  739. if (rv == 0)
  740. break;
  741. if (!used_reuse)
  742. goto fail;
  743. /* A kept-alive socket may have been closed by the
  744. * server; reconnect and retry once. */
  745. used_reuse = 0;
  746. transport_close(h);
  747. }
  748. if (h->resp.status >= 300 && h->resp.status < 400 && have_loc) {
  749. if (h->hooks && h->hooks->redirect)
  750. h->hooks->redirect(h->hooks->ctx, location);
  751. if (hops >= UHTTP_MAX_REDIRECTS) {
  752. rv = UHTTP_ERR_TOOMANY;
  753. goto fail;
  754. }
  755. {
  756. struct uhttp_url next = h->url;
  757. if (url_apply_location(&next, location) != 0) {
  758. transport_abandon(h);
  759. rv = UHTTP_ERR_REDIRECT;
  760. goto fail;
  761. }
  762. /* A same-origin redirect (/ -> /en/) keeps the
  763. * TCP/TLS connection; any other one drops it
  764. * without the close handshake. */
  765. if (h->keepalive && !h->peer_close &&
  766. host_same(next.host, h->url.host) &&
  767. next.port == h->url.port &&
  768. next.secure == h->url.secure &&
  769. drain_small_body(h))
  770. reuse = 1;
  771. else
  772. transport_abandon(h);
  773. h->url = next;
  774. }
  775. /* A redirected POST becomes a GET unless the
  776. * server explicitly asks to repeat the request. */
  777. if (h->resp.status != 307 && h->resp.status != 308) {
  778. h->method = 0;
  779. h->body = 0;
  780. h->body_len = 0;
  781. h->content_type = 0;
  782. }
  783. url_build(&h->url, h->resp.final_url,
  784. sizeof(h->resp.final_url));
  785. hops++;
  786. continue;
  787. }
  788. break;
  789. }
  790. return 0;
  791. fail:
  792. h->keepalive = 0;
  793. uhttp_close(h);
  794. return rv;
  795. }
  796. /* Copy up to max decoded body bytes; >0 data, 0 EOF, <0 error. */
  797. int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
  798. unsigned int timeout_ms)
  799. {
  800. unsigned char *out = (unsigned char *)vbuf;
  801. unsigned int n = 0;
  802. int c;
  803. if (h->error)
  804. return h->error;
  805. if (h->eof && h->chunk_state != 1 &&
  806. !(h->resp.have_length && h->body_read < h->resp.content_length))
  807. return 0;
  808. if (max == 0)
  809. return 0;
  810. if (!h->resp.chunked && h->resp.have_length &&
  811. h->body_read >= h->resp.content_length) {
  812. h->eof = 1;
  813. return 0;
  814. }
  815. while (n < max) {
  816. if (h->resp.chunked) {
  817. if (h->chunk_state == 0) {
  818. char line[64];
  819. unsigned int sz = 0, k = 0;
  820. int hl = reader_readline(h, line, sizeof(line),
  821. timeout_ms);
  822. if (hl < 0) {
  823. if (h->eof)
  824. h->peer_close = 1;
  825. if (n)
  826. break;
  827. return h->error ? h->error : 0;
  828. }
  829. while (line[k]) {
  830. char ch = line[k];
  831. unsigned int d;
  832. if (ch >= '0' && ch <= '9')
  833. d = (unsigned int)(ch - '0');
  834. else if (ch >= 'a' && ch <= 'f')
  835. d = (unsigned int)(ch - 'a' + 10);
  836. else if (ch >= 'A' && ch <= 'F')
  837. d = (unsigned int)(ch - 'A' + 10);
  838. else
  839. break;
  840. sz = sz * 16 + d;
  841. k++;
  842. }
  843. if (k == 0) {
  844. h->error = UHTTP_ERR_RESET;
  845. return n ? (int)n : h->error;
  846. }
  847. if (sz == 0) {
  848. h->chunk_state = 3;
  849. } else {
  850. h->chunk_left = sz;
  851. h->chunk_state = 1;
  852. }
  853. } else if (h->chunk_state == 1) {
  854. c = reader_getc(h, timeout_ms);
  855. if (c < 0) {
  856. if (h->eof)
  857. h->peer_close = 1;
  858. if (n)
  859. break;
  860. return h->error ? h->error : 0;
  861. }
  862. out[n++] = (unsigned char)c;
  863. h->body_read++;
  864. if (--h->chunk_left == 0)
  865. h->chunk_state = 2;
  866. } else if (h->chunk_state == 2) {
  867. (void)reader_getc(h, timeout_ms); /* CR */
  868. (void)reader_getc(h, timeout_ms); /* LF */
  869. h->chunk_state = 0;
  870. } else {
  871. char line[64];
  872. int hl = reader_readline(h, line, sizeof(line),
  873. timeout_ms);
  874. if (hl <= 0) {
  875. h->eof = 1;
  876. h->peer_close = 1;
  877. break;
  878. }
  879. }
  880. } else {
  881. if (h->resp.have_length &&
  882. h->body_read >= h->resp.content_length) {
  883. h->eof = 1;
  884. break;
  885. }
  886. c = reader_getc(h, timeout_ms);
  887. if (c < 0) {
  888. if (h->eof)
  889. h->peer_close = 1;
  890. if (n)
  891. break;
  892. return h->error ? h->error : 0;
  893. }
  894. out[n++] = (unsigned char)c;
  895. h->body_read++;
  896. }
  897. }
  898. return (int)n;
  899. }
  900. void uhttp_close(struct uhttp *h)
  901. {
  902. if (h->keepalive && h->connected && !h->peer_close && !h->error)
  903. return;
  904. transport_close(h);
  905. }
  906. const char *uhttp_error_string(int err)
  907. {
  908. switch (err) {
  909. case 0: return "ok";
  910. case UHTTP_ERR_URL: return "bad URL";
  911. case UHTTP_ERR_NIC: return "no NIC found";
  912. case UHTTP_ERR_IP: return "no IP address (run dhcp or netip)";
  913. case UHTTP_ERR_BUSY: return "network busy";
  914. case UHTTP_ERR_DNS: return "DNS lookup failed";
  915. case UHTTP_ERR_ARP: return "no ARP reply";
  916. case UHTTP_ERR_CONNECT: return "connection failed";
  917. case UHTTP_ERR_REFUSED: return "connection refused";
  918. case UHTTP_ERR_TLS: return "TLS handshake failed";
  919. case UHTTP_ERR_SEND: return "request send failed";
  920. case UHTTP_ERR_TIMEOUT: return "timeout";
  921. case UHTTP_ERR_RESET: return "connection reset";
  922. case UHTTP_ERR_REDIRECT: return "bad redirect target";
  923. case UHTTP_ERR_TOOMANY: return "too many redirects";
  924. case UHTTP_ERR_TOOLONG: return "request too long";
  925. default: return "network error";
  926. }
  927. }