| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101 |
- /* uhttp: see uhttp.h. Extracted from the original ucmd_curl.c so curl and
- * the browser share one HTTP/TLS path. */
- #include "uhttp.h"
- #include "tls.h"
- #include <string.h>
- /* ---- tiny string helpers ---- */
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void s_copy(char *d, const char *s, unsigned int cap)
- {
- unsigned int i = 0;
- if (!cap)
- return;
- while (s && s[i] && i + 1 < cap) {
- d[i] = s[i];
- i++;
- }
- d[i] = '\0';
- }
- static int starts_with(const char *s, const char *prefix)
- {
- unsigned int i = 0;
- while (prefix[i]) {
- if (s[i] != prefix[i])
- return 0;
- i++;
- }
- return 1;
- }
- static int contains_ci(const char *hay, const char *needle)
- {
- unsigned int i, j;
- for (i = 0; hay[i]; i++) {
- for (j = 0; needle[j]; j++) {
- char a = hay[i + j];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (a != needle[j])
- break;
- }
- if (!needle[j])
- return 1;
- }
- return 0;
- }
- /* ---- URL parsing / resolution ---- */
- int uhttp_parse_url(const char *s, struct uhttp_url *u)
- {
- const char *p = s, *h;
- unsigned int i;
- int secure = 0;
- if (!s || !u)
- return UHTTP_ERR_URL;
- if (starts_with(s, "https://")) {
- secure = 1;
- p += 8;
- } else if (starts_with(s, "http://")) {
- p += 7;
- } else {
- for (i = 0; s[i] && i < 8; i++) {
- if (s[i] == ':' && s[i + 1] == '/' && s[i + 2] == '/')
- return UHTTP_ERR_URL;
- if (s[i] == '/')
- break;
- }
- }
- u->secure = secure;
- h = p;
- while (*p && *p != '/' && *p != ':')
- p++;
- if (p == h || (unsigned int)(p - h) >= UHTTP_HOST_MAX)
- return UHTTP_ERR_URL;
- for (i = 0; i < (unsigned int)(p - h); i++)
- u->host[i] = h[i];
- u->host[i] = '\0';
- u->port = secure ? 443 : 80;
- if (*p == ':') {
- unsigned int port = 0;
- p++;
- if (*p < '0' || *p > '9')
- return UHTTP_ERR_URL;
- while (*p >= '0' && *p <= '9') {
- port = port * 10 + (unsigned int)(*p - '0');
- if (port > 65535)
- return UHTTP_ERR_URL;
- p++;
- }
- if (port == 0 || (*p != '/' && *p != '\0'))
- return UHTTP_ERR_URL;
- u->port = port;
- }
- i = 0;
- if (*p == '/') {
- static const char hex[] = "0123456789ABCDEF";
- unsigned int j = 0;
- /* Percent-encode what cannot go on a request line (spaces,
- * controls, quotes, non-ASCII), like a browser does; scripts
- * build URLs such as "...&scope=openid email profile". */
- while (p[j] && p[j] != '#') {
- unsigned char c = (unsigned char)p[j++];
- int esc = c <= ' ' || c >= 0x7F || c == '"' ||
- c == '<' || c == '>';
- if (i + (esc ? 3u : 1u) >= UHTTP_PATH_MAX)
- return UHTTP_ERR_URL;
- if (esc) {
- u->path[i++] = '%';
- u->path[i++] = hex[c >> 4];
- u->path[i++] = hex[c & 15];
- } else {
- u->path[i++] = (char)c;
- }
- }
- }
- u->path[i] = '\0';
- if (i == 0) {
- u->path[0] = '/';
- u->path[1] = '\0';
- }
- return 0;
- }
- static void url_build(const struct uhttp_url *u, char *out, unsigned int cap)
- {
- unsigned int n = 0;
- #define APP(s) do { const char *ap = (s); \
- while (*ap && n + 1 < cap) out[n++] = *ap++; } while (0)
- APP(u->secure ? "https://" : "http://");
- APP(u->host);
- if (u->port != (u->secure ? 443u : 80u)) {
- char tmp[8];
- unsigned int v = u->port, k = 0, j;
- if (v == 0) {
- tmp[k++] = '0';
- } else {
- while (v && k < sizeof(tmp)) {
- tmp[k++] = (char)('0' + v % 10);
- v /= 10;
- }
- }
- if (n + 1 < cap)
- out[n++] = ':';
- for (j = 0; j < k && n + 1 < cap; j++)
- out[n++] = tmp[k - 1 - j];
- }
- APP(u->path);
- #undef APP
- out[n] = '\0';
- }
- static int path_normalize_part(char *path);
- /* Normalize an absolute URL path in place: collapse "//", "." and "..".
- * Only the path: a query or fragment is kept verbatim (it may carry a URL,
- * e.g. ?redirect_uri=https://host/cb). 0 ok, -1 too long. */
- /* Path buffers are static (4 KB each): these helpers are leaves and ring-3
- * processes are single-threaded, so they need not live on the 128 KB stack. */
- static int path_normalize(char *path)
- {
- static char rest[UHTTP_PATH_MAX];
- unsigned int q = 0, i, n;
- int rv;
- while (path[q] && path[q] != '?' && path[q] != '#')
- q++;
- if (!path[q])
- return path_normalize_part(path);
- for (i = 0; path[q + i]; i++)
- rest[i] = path[q + i];
- rest[i] = '\0';
- path[q] = '\0';
- rv = path_normalize_part(path);
- n = 0;
- while (path[n])
- n++;
- if (rv != 0 || n + i + 1 > UHTTP_PATH_MAX)
- return -1;
- for (q = 0; q <= i; q++)
- path[n + q] = rest[q];
- return 0;
- }
- static int path_normalize_part(char *path)
- {
- static char tmp[UHTTP_PATH_MAX];
- unsigned int i, o = 1, len = 0, start, seglen, j;
- int trailing;
- while (path[len])
- len++;
- if (len == 0 || path[0] != '/')
- return 0;
- trailing = (len > 1 && path[len - 1] == '/');
- tmp[0] = '/';
- i = 1;
- while (path[i]) {
- while (path[i] == '/')
- i++;
- start = i;
- while (path[i] && path[i] != '/')
- i++;
- seglen = i - start;
- if (seglen == 0)
- break;
- if (seglen == 1 && path[start] == '.')
- continue;
- if (seglen == 2 && path[start] == '.' && path[start + 1] == '.') {
- while (o > 1 && tmp[o - 1] != '/')
- o--;
- if (o > 1)
- o--;
- continue;
- }
- if (o + seglen + 2 > sizeof(tmp))
- return -1;
- if (o > 1)
- tmp[o++] = '/';
- for (j = 0; j < seglen; j++)
- tmp[o++] = path[start + j];
- }
- if (trailing && o > 1 && tmp[o - 1] != '/') {
- if (o + 2 > sizeof(tmp))
- return -1;
- tmp[o++] = '/';
- }
- tmp[o] = '\0';
- for (i = 0; i <= o; i++)
- path[i] = tmp[i];
- return 0;
- }
- /* Apply a Location header to the current URL. */
- static int url_apply_location(struct uhttp_url *u, const char *loc)
- {
- static char joined[UHTTP_PATH_MAX];
- char scheme[8];
- unsigned int i, dirlen = 1, n = 0;
- if (starts_with(loc, "http://") || starts_with(loc, "https://"))
- return uhttp_parse_url(loc, u);
- if (loc[0] == '/' && loc[1] == '/') {
- /* scheme-relative */
- s_copy(scheme, u->secure ? "https:" : "http:", sizeof(scheme));
- if (s_len(scheme) + s_len(loc) + 1 > UHTTP_LOC_MAX)
- return -1;
- {
- static char full[UHTTP_LOC_MAX];
- unsigned int k = 0;
- while (scheme[k]) {
- full[k] = scheme[k];
- k++;
- }
- for (i = 0; loc[i] && k + 1 < sizeof(full); i++)
- full[k++] = loc[i];
- full[k] = '\0';
- return uhttp_parse_url(full, u);
- }
- }
- if (loc[0] == '/') {
- for (i = 0; loc[i]; i++) {
- if (i + 1 >= UHTTP_PATH_MAX)
- return -1;
- u->path[i] = loc[i];
- }
- u->path[i] = '\0';
- return path_normalize(u->path);
- }
- for (i = 0; u->path[i]; i++)
- if (u->path[i] == '/')
- dirlen = i + 1;
- while (loc[n])
- n++;
- if (dirlen + n + 1 > UHTTP_PATH_MAX)
- return -1;
- for (i = 0; i < dirlen; i++)
- joined[i] = u->path[i];
- for (i = 0; i < n; i++)
- joined[dirlen + i] = loc[i];
- joined[dirlen + n] = '\0';
- for (i = 0; i <= dirlen + n; i++)
- u->path[i] = joined[i];
- return path_normalize(u->path);
- }
- int uhttp_resolve(const char *base, const char *href, char *out,
- unsigned int cap)
- {
- static struct uhttp_url u;
- if (!base || !href || !out || cap == 0)
- return UHTTP_ERR_URL;
- if (uhttp_parse_url(base, &u) != 0)
- return UHTTP_ERR_URL;
- if (href[0] == '#') {
- s_copy(out, base, cap);
- return 0;
- }
- if (url_apply_location(&u, href) != 0)
- return UHTTP_ERR_URL;
- url_build(&u, out, cap);
- return 0;
- }
- /* ---- HTTP header helpers ---- */
- static int hdr_match(const char *line, const char *name)
- {
- unsigned int i;
- for (i = 0; name[i]; i++) {
- char a = line[i];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (a != name[i])
- return 0;
- }
- return line[i] == ':';
- }
- static int parse_dec_after_colon(const char *line, unsigned int *val)
- {
- unsigned int i = 0, v = 0;
- int digits = 0;
- while (line[i] && line[i] != ':')
- i++;
- if (line[i] != ':')
- return 0;
- i++;
- while (line[i] == ' ' || line[i] == '\t')
- i++;
- while (line[i] >= '0' && line[i] <= '9') {
- v = v * 10 + (unsigned int)(line[i] - '0');
- digits = 1;
- i++;
- }
- if (!digits)
- return 0;
- *val = v;
- return 1;
- }
- static void copy_after_colon(const char *line, char *out, unsigned int max)
- {
- unsigned int i = 0, j = 0;
- while (line[i] && line[i] != ':')
- i++;
- if (line[i] == ':')
- i++;
- while (line[i] == ' ' || line[i] == '\t')
- i++;
- while (line[i] && j + 1 < max)
- out[j++] = line[i++];
- out[j] = '\0';
- }
- static unsigned int parse_status(const char *line)
- {
- unsigned int i = 0, v = 0, digits = 0;
- while (line[i] && line[i] != ' ')
- i++;
- if (line[i] == ' ')
- i++;
- while (line[i] >= '0' && line[i] <= '9') {
- v = v * 10 + (unsigned int)(line[i] - '0');
- digits++;
- i++;
- }
- return digits == 3 ? v : 0;
- }
- /* ---- transport ---- */
- /* Send len bytes, split into pieces the transport accepts: one TLS record
- * carries at most 1024 bytes and SYS_TCP_SEND at most one peer MSS. */
- static int xport_send(struct uhttp *h, const void *buf, unsigned int len)
- {
- const unsigned char *p = (const unsigned char *)buf;
- unsigned int chunk;
- if (h->url.secure) {
- chunk = 1024;
- } else {
- int mss = u_syscall0(SYS_TCP_PEER_MSS);
- chunk = mss > 0 ? (unsigned int)mss : 536;
- }
- while (len) {
- unsigned int n = len < chunk ? len : chunk;
- int rv;
- if (h->url.secure)
- rv = tls_send(p, n);
- else
- rv = (int)u_syscall2(SYS_TCP_SEND, (int)p, (int)n);
- if (rv != 0)
- return rv;
- p += n;
- len -= n;
- }
- return 0;
- }
- static int xport_recv(struct uhttp *h, void *buf, unsigned int max,
- unsigned int timeout_ms)
- {
- if (h->url.secure)
- return tls_recv(buf, max, timeout_ms);
- return (int)u_syscall3(SYS_TCP_RECV, (int)buf, (int)max,
- (int)timeout_ms);
- }
- static void transport_close(struct uhttp *h)
- {
- if (!h->connected)
- return;
- if (h->url.secure)
- tls_close();
- u_syscall0(SYS_TCP_CLOSE);
- h->connected = 0;
- }
- /* Drop the connection without a TLS close_notify: used when a redirect
- * leaves this origin, so the hop pays no extra round trip. */
- static void transport_abandon(struct uhttp *h)
- {
- if (!h->connected)
- return;
- if (h->url.secure)
- tls_abandon();
- u_syscall0(SYS_TCP_CLOSE);
- h->connected = 0;
- }
- static int reader_getc(struct uhttp *h, unsigned int timeout_ms);
- /* Discard a small, length-delimited redirect body so the next request can
- * reuse the connection. 1 when the connection is still usable. */
- static int drain_small_body(struct uhttp *h)
- {
- unsigned int left;
- if (h->resp.chunked || !h->resp.have_length ||
- h->resp.content_length > 4096)
- return 0;
- left = h->resp.content_length;
- while (left) {
- if (reader_getc(h, UHTTP_IDLE_TIMEOUT_MS) < 0)
- return 0;
- left--;
- }
- /* Bytes past the body would belong to nothing we asked for. */
- return h->pos >= h->len;
- }
- /* Connect to h->url (TCP + TLS). Keeps the NIC acquired. */
- static int http_connect(struct uhttp *h)
- {
- unsigned char ip[4], hop[4], mac[6];
- int rv;
- if (u_parse_ip(h->url.host, ip) != 0) {
- if (u_syscall2(SYS_DNS_RESOLVE, (int)h->url.host, (int)ip) != 0)
- return UHTTP_ERR_DNS;
- }
- u_syscall2(SYS_NET_NEXT_HOP, (int)ip, (int)hop);
- rv = u_syscall2(SYS_NET_ARP, (int)hop, (int)mac);
- if (rv != 0)
- return UHTTP_ERR_ARP;
- rv = u_syscall3(SYS_TCP_CONNECT, (int)mac, (int)ip, (int)h->url.port);
- if (rv == -1)
- return UHTTP_ERR_TIMEOUT;
- if (rv == -2)
- return UHTTP_ERR_REFUSED;
- if (rv == -4)
- return UHTTP_ERR_BUSY;
- if (rv != 0)
- return UHTTP_ERR_CONNECT;
- h->connected = 1;
- if (h->url.secure) {
- if (tls_connect(h->url.host) != 0) {
- transport_close(h);
- return UHTTP_ERR_TLS;
- }
- }
- return 0;
- }
- /* ---- response reader ---- */
- static int reader_fill(struct uhttp *h, unsigned int timeout_ms)
- {
- unsigned int now, deadline, rel;
- int n;
- if (h->eof)
- return 0;
- now = u_ticks_ms();
- deadline = now + timeout_ms;
- if ((int)(h->total_deadline - deadline) < 0)
- deadline = h->total_deadline;
- rel = (int)(deadline - now) > 0 ? deadline - now : 1;
- n = xport_recv(h, h->buf, sizeof(h->buf), rel);
- if (n > 0) {
- h->len = (unsigned int)n;
- h->pos = 0;
- return 1;
- }
- if (n == 0) {
- h->eof = 1;
- return 0;
- }
- h->error = (n == -1) ? UHTTP_ERR_TIMEOUT : UHTTP_ERR_RESET;
- return 0;
- }
- static int reader_getc(struct uhttp *h, unsigned int timeout_ms)
- {
- if (h->pos >= h->len) {
- if (!reader_fill(h, timeout_ms))
- return -1;
- }
- return h->buf[h->pos++];
- }
- static int reader_readline(struct uhttp *h, char *out, unsigned int max,
- unsigned int timeout_ms)
- {
- unsigned int n = 0;
- int c;
- for (;;) {
- c = reader_getc(h, timeout_ms);
- if (c < 0) {
- out[n] = '\0';
- return (n == 0) ? -1 : (int)n;
- }
- if (c == '\n') {
- out[n] = '\0';
- return (int)n;
- }
- if (c == '\r')
- continue;
- if (n + 1 < max)
- out[n++] = (char)c;
- }
- }
- /* ---- request / response ---- */
- static unsigned int req_app(char *buf, unsigned int pos, unsigned int cap,
- const char *s)
- {
- while (*s && pos + 1 < cap)
- buf[pos++] = *s++;
- buf[pos] = '\0';
- return pos;
- }
- static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int cap,
- unsigned int v)
- {
- char b[12];
- int i = 0;
- if (v == 0) {
- if (pos + 1 < cap)
- buf[pos++] = '0';
- buf[pos] = '\0';
- return pos;
- }
- while (v) {
- b[i++] = (char)('0' + (v % 10));
- v /= 10;
- }
- while (i) {
- i--;
- if (pos + 1 < cap)
- buf[pos++] = b[i];
- }
- buf[pos] = '\0';
- return pos;
- }
- /* Read the status line and headers of the current response. Returns 0 and
- * sets redirect/location when the response is a redirect with a target. */
- static int read_response_head(struct uhttp *h, char *location, int *have_loc,
- unsigned int timeout_ms)
- {
- char line[UHTTP_LINE_MAX];
- unsigned int status = 0;
- int rv;
- h->resp.status = 0;
- h->resp.chunked = 0;
- h->resp.have_length = 0;
- h->resp.content_length = 0;
- h->resp.gzip = 0;
- h->resp.content_type[0] = '\0';
- h->resp.content_encoding[0] = '\0';
- h->peer_close = 0;
- h->chunk_state = 0;
- h->chunk_left = 0;
- h->body_read = 0;
- h->len = h->pos = 0;
- h->eof = 0;
- h->error = 0;
- *have_loc = 0;
- location[0] = '\0';
- rv = reader_readline(h, line, sizeof(line), timeout_ms);
- if (rv < 0)
- return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
- UHTTP_ERR_TIMEOUT;
- status = parse_status(line);
- h->resp.status = (int)status;
- if (h->hooks && h->hooks->status)
- h->hooks->status(h->hooks->ctx, (int)status, line);
- /* HTTP/1.0 has no persistent connections unless the response
- * explicitly opts in. Ignoring this kept a half-closed socket for
- * the next request, which then waited for a response that could not
- * arrive: the next batch of images appeared to hang. */
- if (line[0] == 'H' && line[1] == 'T' && line[2] == 'T' &&
- line[3] == 'P' && line[4] == '/' && line[5] == '1' &&
- line[6] == '.' && line[7] == '0')
- h->peer_close = 1;
- for (;;) {
- rv = reader_readline(h, line, sizeof(line), timeout_ms);
- if (rv < 0)
- return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
- UHTTP_ERR_TIMEOUT;
- if (rv == 0)
- break;
- if (h->hooks && h->hooks->header)
- h->hooks->header(h->hooks->ctx, line);
- if (hdr_match(line, "content-length"))
- h->resp.have_length =
- parse_dec_after_colon(line, &h->resp.content_length);
- else if (hdr_match(line, "transfer-encoding") &&
- contains_ci(line, "chunked"))
- h->resp.chunked = 1;
- else if (hdr_match(line, "content-encoding")) {
- copy_after_colon(line, h->resp.content_encoding,
- sizeof(h->resp.content_encoding));
- if (contains_ci(h->resp.content_encoding, "gzip"))
- h->resp.gzip = 1;
- }
- else if (hdr_match(line, "content-type")) {
- copy_after_colon(line, h->resp.content_type,
- sizeof(h->resp.content_type));
- } else if (hdr_match(line, "location")) {
- copy_after_colon(line, location, UHTTP_LOC_MAX);
- *have_loc = location[0] != '\0';
- } else if (hdr_match(line, "connection")) {
- if (contains_ci(line, "close"))
- h->peer_close = 1;
- else if (contains_ci(line, "keep-alive"))
- h->peer_close = 0;
- } else if (h->jar && hdr_match(line, "set-cookie")) {
- char *v = line + 11; /* past "set-cookie:" */
- while (*v == ' ' || *v == '\t')
- v++;
- (void)uhttp_jar_set(h->jar, h->url.host, h->url.path, v);
- }
- }
- /* 1xx/204/304 responses never carry a body; without this a kept-alive
- * 204 would be read until the idle timeout. */
- if ((status >= 100 && status < 200) || status == 204 || status == 304) {
- h->resp.chunked = 0;
- h->resp.have_length = 1;
- h->resp.content_length = 0;
- }
- return 0;
- }
- static int send_request(struct uhttp *h, unsigned int flags)
- {
- static char req[UHTTP_REQ_MAX];
- unsigned int n = 0;
- int rv;
- n = req_app(req, 0, sizeof(req), h->method ? h->method : "GET");
- n = req_app(req, n, sizeof(req), " ");
- n = req_app(req, n, sizeof(req), h->url.path);
- n = req_app(req, n, sizeof(req), " HTTP/1.1\r\nHost: ");
- n = req_app(req, n, sizeof(req), h->url.host);
- if (h->url.port != (h->url.secure ? 443u : 80u)) {
- n = req_app(req, n, sizeof(req), ":");
- n = req_app_dec(req, n, sizeof(req), h->url.port);
- }
- n = req_app(req, n, sizeof(req), "\r\nUser-Agent: ");
- n = req_app(req, n, sizeof(req), h->user_agent);
- n = req_app(req, n, sizeof(req), "\r\nAccept: ");
- n = req_app(req, n, sizeof(req), h->accept ? h->accept : "*/*");
- n = req_app(req, n, sizeof(req), "\r\n");
- if (h->jar) {
- static char cookie[2048];
- if (uhttp_jar_header(h->jar, h->url.host, h->url.path,
- h->url.secure, cookie, sizeof(cookie))) {
- n = req_app(req, n, sizeof(req), "Cookie: ");
- n = req_app(req, n, sizeof(req), cookie);
- n = req_app(req, n, sizeof(req), "\r\n");
- }
- }
- if (h->extra_headers)
- n = req_app(req, n, sizeof(req), h->extra_headers);
- if (flags & UHTTP_GZIP)
- n = req_app(req, n, sizeof(req), "Accept-Encoding: gzip\r\n");
- if (h->body_len) {
- n = req_app(req, n, sizeof(req), "Content-Type: ");
- n = req_app(req, n, sizeof(req), h->content_type ?
- h->content_type : "application/octet-stream");
- n = req_app(req, n, sizeof(req), "\r\nContent-Length: ");
- n = req_app_dec(req, n, sizeof(req), h->body_len);
- n = req_app(req, n, sizeof(req), "\r\n");
- }
- n = req_app(req, n, sizeof(req),
- (h->keepalive && !h->peer_close) ?
- "Connection: keep-alive\r\n\r\n" :
- "Connection: close\r\n\r\n");
- /* req_app stops one byte short of the end: a full buffer means the
- * request was truncated. */
- if (n + 1 >= sizeof(req))
- return UHTTP_ERR_TOOLONG;
- rv = xport_send(h, req, n);
- if (rv != 0)
- return UHTTP_ERR_SEND;
- if (h->body_len) {
- rv = xport_send(h, h->body, h->body_len);
- if (rv != 0)
- return UHTTP_ERR_SEND;
- }
- return 0;
- }
- void uhttp_init(struct uhttp *h)
- {
- unsigned int i;
- for (i = 0; i < sizeof(*h); i++)
- ((unsigned char *)h)[i] = 0;
- h->user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
- }
- void uhttp_set_method(struct uhttp *h, const char *method, const char *body,
- unsigned int body_len, const char *content_type)
- {
- h->method = method;
- h->body = body;
- h->body_len = body_len;
- h->content_type = content_type;
- }
- static int host_same(const char *a, const char *b)
- {
- while (*a && *b && *a == *b) {
- a++;
- b++;
- }
- return *a == *b;
- }
- int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
- const struct uhttp_hooks *hooks)
- {
- char location[UHTTP_LOC_MAX];
- char old_host[UHTTP_HOST_MAX];
- unsigned int old_port = 0;
- int old_secure = 0;
- unsigned int hops = 0;
- int reuse = 0;
- int rv;
- h->hooks = hooks;
- if (!h->user_agent)
- h->user_agent = "nebos-uhttp/0.1";
- if (h->keepalive && h->connected) {
- s_copy(old_host, h->url.host, sizeof(old_host));
- old_port = h->url.port;
- old_secure = h->url.secure;
- reuse = 1;
- }
- h->len = h->pos = 0;
- h->eof = 0;
- h->error = 0;
- h->peer_close = 0;
- if (uhttp_parse_url(url, &h->url) != 0)
- return UHTTP_ERR_URL;
- url_build(&h->url, h->resp.final_url, sizeof(h->resp.final_url));
- if (reuse && host_same(old_host, h->url.host) &&
- old_port == h->url.port && old_secure == h->url.secure) {
- /* Keep the existing TCP/TLS connection. */
- } else {
- reuse = 0;
- if (h->connected)
- transport_close(h);
- {
- struct sys_netinfo ni;
- if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1)
- return UHTTP_ERR_NIC;
- if (u_syscall0(SYS_NET_CONFIGURED) != 1)
- return UHTTP_ERR_IP;
- }
- }
- h->total_deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS;
- for (;;) {
- int have_loc;
- int used_reuse = reuse;
- reuse = 0;
- for (;;) {
- if (!used_reuse) {
- rv = http_connect(h);
- if (rv != 0)
- goto fail;
- if (h->hooks && h->hooks->connected)
- h->hooks->connected(h->hooks->ctx,
- h->url.secure);
- }
- rv = send_request(h, flags);
- if (rv == 0)
- rv = read_response_head(h, location,
- &have_loc,
- used_reuse ? 3000 :
- UHTTP_IDLE_TIMEOUT_MS);
- if (rv == 0)
- break;
- if (!used_reuse)
- goto fail;
- /* A kept-alive socket may have been closed by the
- * server; reconnect and retry once. */
- used_reuse = 0;
- transport_close(h);
- }
- if (h->resp.status >= 300 && h->resp.status < 400 && have_loc) {
- if (h->hooks && h->hooks->redirect)
- h->hooks->redirect(h->hooks->ctx, location);
- if (hops >= UHTTP_MAX_REDIRECTS) {
- rv = UHTTP_ERR_TOOMANY;
- goto fail;
- }
- {
- struct uhttp_url next = h->url;
- if (url_apply_location(&next, location) != 0) {
- transport_abandon(h);
- rv = UHTTP_ERR_REDIRECT;
- goto fail;
- }
- /* A same-origin redirect (/ -> /en/) keeps the
- * TCP/TLS connection; any other one drops it
- * without the close handshake. */
- if (h->keepalive && !h->peer_close &&
- host_same(next.host, h->url.host) &&
- next.port == h->url.port &&
- next.secure == h->url.secure &&
- drain_small_body(h))
- reuse = 1;
- else
- transport_abandon(h);
- h->url = next;
- }
- /* A redirected POST becomes a GET unless the
- * server explicitly asks to repeat the request. */
- if (h->resp.status != 307 && h->resp.status != 308) {
- h->method = 0;
- h->body = 0;
- h->body_len = 0;
- h->content_type = 0;
- }
- url_build(&h->url, h->resp.final_url,
- sizeof(h->resp.final_url));
- hops++;
- continue;
- }
- break;
- }
- return 0;
- fail:
- h->keepalive = 0;
- uhttp_close(h);
- return rv;
- }
- /* Copy up to max decoded body bytes; >0 data, 0 EOF, <0 error. */
- int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
- unsigned int timeout_ms)
- {
- unsigned char *out = (unsigned char *)vbuf;
- unsigned int n = 0;
- if (h->error)
- return h->error;
- if (h->eof && h->chunk_state != 1 &&
- !(h->resp.have_length && h->body_read < h->resp.content_length))
- return 0;
- if (max == 0)
- return 0;
- if (!h->resp.chunked && h->resp.have_length &&
- h->body_read >= h->resp.content_length) {
- h->eof = 1;
- return 0;
- }
- while (n < max) {
- if (h->resp.chunked) {
- if (h->chunk_state == 0) {
- char line[64];
- unsigned int sz = 0, k = 0;
- int hl = reader_readline(h, line, sizeof(line),
- timeout_ms);
- if (hl < 0) {
- if (h->eof)
- h->peer_close = 1;
- if (n)
- break;
- return h->error ? h->error : 0;
- }
- while (line[k]) {
- char ch = line[k];
- unsigned int d;
- if (ch >= '0' && ch <= '9')
- d = (unsigned int)(ch - '0');
- else if (ch >= 'a' && ch <= 'f')
- d = (unsigned int)(ch - 'a' + 10);
- else if (ch >= 'A' && ch <= 'F')
- d = (unsigned int)(ch - 'A' + 10);
- else
- break;
- sz = sz * 16 + d;
- k++;
- }
- if (k == 0) {
- h->error = UHTTP_ERR_RESET;
- return n ? (int)n : h->error;
- }
- if (sz == 0) {
- h->chunk_state = 3;
- } else {
- h->chunk_left = sz;
- h->chunk_state = 1;
- }
- } else if (h->chunk_state == 1) {
- unsigned int avail, want;
- if (h->pos >= h->len) {
- if (!reader_fill(h, timeout_ms)) {
- if (h->eof)
- h->peer_close = 1;
- if (n)
- break;
- return h->error ? h->error : 0;
- }
- }
- avail = h->len - h->pos;
- want = max - n;
- if (want > h->chunk_left)
- want = h->chunk_left;
- if (want > avail)
- want = avail;
- if (!want)
- continue;
- memcpy(out + n, h->buf + h->pos, want);
- h->pos += want;
- n += want;
- h->body_read += want;
- h->chunk_left -= want;
- if (h->chunk_left == 0)
- h->chunk_state = 2;
- } else if (h->chunk_state == 2) {
- (void)reader_getc(h, timeout_ms); /* CR */
- (void)reader_getc(h, timeout_ms); /* LF */
- h->chunk_state = 0;
- } else {
- char line[64];
- int hl = reader_readline(h, line, sizeof(line),
- timeout_ms);
- if (hl <= 0) {
- h->eof = 1;
- h->peer_close = 1;
- break;
- }
- }
- } else {
- unsigned int avail, want;
- if (h->resp.have_length &&
- h->body_read >= h->resp.content_length) {
- h->eof = 1;
- break;
- }
- if (h->pos >= h->len) {
- if (!reader_fill(h, timeout_ms)) {
- if (h->eof)
- h->peer_close = 1;
- if (n)
- break;
- return h->error ? h->error : 0;
- }
- }
- avail = h->len - h->pos;
- want = max - n;
- if (h->resp.have_length) {
- unsigned int left = h->resp.content_length -
- h->body_read;
- if (want > left)
- want = left;
- }
- if (want > avail)
- want = avail;
- if (!want)
- continue;
- memcpy(out + n, h->buf + h->pos, want);
- h->pos += want;
- n += want;
- h->body_read += want;
- }
- }
- return (int)n;
- }
- void uhttp_close(struct uhttp *h)
- {
- if (h->keepalive && h->connected && !h->peer_close && !h->error)
- return;
- transport_close(h);
- }
- const char *uhttp_error_string(int err)
- {
- switch (err) {
- case 0: return "ok";
- case UHTTP_ERR_URL: return "bad URL";
- case UHTTP_ERR_NIC: return "no NIC found";
- case UHTTP_ERR_IP: return "no IP address (run dhcp or netip)";
- case UHTTP_ERR_BUSY: return "network busy";
- case UHTTP_ERR_DNS: return "DNS lookup failed";
- case UHTTP_ERR_ARP: return "no ARP reply";
- case UHTTP_ERR_CONNECT: return "connection failed";
- case UHTTP_ERR_REFUSED: return "connection refused";
- case UHTTP_ERR_TLS: return "TLS handshake failed";
- case UHTTP_ERR_SEND: return "request send failed";
- case UHTTP_ERR_TIMEOUT: return "timeout";
- case UHTTP_ERR_RESET: return "connection reset";
- case UHTTP_ERR_REDIRECT: return "bad redirect target";
- case UHTTP_ERR_TOOMANY: return "too many redirects";
- case UHTTP_ERR_TOOLONG: return "request too long";
- default: return "network error";
- }
- }
|