| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /* uhttp: small ring-3 HTTP/1.1 GET client shared by curl and the browser.
- *
- * The kernel owns the NIC and the TCP stack; this module drives them through
- * the SYS_NET_* / SYS_TCP_* calls, optionally wraps the connection in TLS
- * (tls.c), follows redirects and de-chunks the response. The caller supplies
- * the connection object, so there is no allocation.
- *
- * https uses the phase-A tls.c client: encrypted but WITHOUT certificate
- * verification. The caller can show a warning (curl does).
- */
- #ifndef UHTTP_H
- #define UHTTP_H
- #include "ucmd_lib.h"
- #include "uhttp_cookie.h"
- #define UHTTP_HOST_MAX 128
- #define UHTTP_PATH_MAX 1024 /* path + query (OIDC URLs are long) */
- #define UHTTP_LOC_MAX 1280
- #define UHTTP_LINE_MAX 1536 /* Location / Set-Cookie header lines */
- #define UHTTP_READER_MAX 16384
- #define UHTTP_REQ_MAX 4096 /* request line + headers */
- #define UHTTP_MAX_REDIRECTS 8
- #define UHTTP_IDLE_TIMEOUT_MS 10000
- /* Backstop only: the idle timeout above catches a stalled peer, so slow but
- * steady links (a 1.5 MB news page on emulated hardware, a big image over a
- * poor connection) must not be cut off after a minute. */
- #define UHTTP_TOTAL_TIMEOUT_MS 300000
- /* uhttp_open flags. */
- #define UHTTP_GZIP 1 /* send Accept-Encoding: gzip (caller decodes) */
- /* Error codes (uhttp_open/uhttp_read return 0 on success). */
- #define UHTTP_ERR_URL (-1)
- #define UHTTP_ERR_NIC (-2)
- #define UHTTP_ERR_IP (-3)
- #define UHTTP_ERR_BUSY (-4)
- #define UHTTP_ERR_DNS (-5)
- #define UHTTP_ERR_ARP (-6)
- #define UHTTP_ERR_CONNECT (-7)
- #define UHTTP_ERR_REFUSED (-8)
- #define UHTTP_ERR_TLS (-9)
- #define UHTTP_ERR_SEND (-10)
- #define UHTTP_ERR_TIMEOUT (-11)
- #define UHTTP_ERR_RESET (-12)
- #define UHTTP_ERR_REDIRECT (-13)
- #define UHTTP_ERR_TOOMANY (-14)
- #define UHTTP_ERR_TOOLONG (-15)
- struct uhttp_url {
- char host[UHTTP_HOST_MAX];
- char path[UHTTP_PATH_MAX];
- unsigned int port;
- int secure;
- };
- struct uhttp_response {
- int status;
- int chunked;
- int have_length;
- unsigned int content_length;
- int gzip; /* Content-Encoding: gzip */
- char content_type[64];
- char content_encoding[16];
- char final_url[UHTTP_HOST_MAX + UHTTP_PATH_MAX + 16];
- };
- /* Optional callbacks; curl uses them for its existing verbose output, the
- * browser passes NULL. */
- struct uhttp_hooks {
- void (*connected)(void *ctx, int secure);
- void (*status)(void *ctx, int code, const char *line);
- void (*header)(void *ctx, const char *line);
- void (*redirect)(void *ctx, const char *location);
- void *ctx;
- };
- struct uhttp {
- struct uhttp_url url;
- struct uhttp_response resp;
- const struct uhttp_hooks *hooks;
- const char *user_agent;
- const char *method; /* NULL means GET */
- const char *body; /* request body for POST */
- unsigned int body_len;
- const char *content_type;
- /* Optional cookie jar: every Set-Cookie (redirects included) is stored
- * and matching cookies are sent with each request and redirect hop.
- * The jar outlives the connection, so reconnects keep the session. */
- struct uhttp_jar *jar;
- const char *accept; /* Accept header; NULL means star/star */
- const char *extra_headers; /* verbatim "Name: value\r\n" lines */
- unsigned char buf[UHTTP_READER_MAX];
- unsigned int len, pos;
- unsigned int total_deadline;
- int eof;
- int error; /* UHTTP_ERR_* once a read failed */
- int connected;
- int keepalive; /* reuse the TCP/TLS connection */
- int peer_close; /* response asked to close it */
- /* chunked transfer state */
- int chunk_state; /* 0 size line, 1 data, 2 crlf, 3 trailers */
- unsigned int chunk_left;
- unsigned int body_read;
- };
- void uhttp_init(struct uhttp *h);
- /* Select the request method and body. Call after uhttp_init() and before
- * uhttp_open(); NULL method restores GET. */
- void uhttp_set_method(struct uhttp *h, const char *method, const char *body,
- unsigned int body_len, const char *content_type);
- int uhttp_parse_url(const char *s, struct uhttp_url *u);
- int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
- const struct uhttp_hooks *hooks);
- int uhttp_read(struct uhttp *h, void *buf, unsigned int max,
- unsigned int timeout_ms);
- void uhttp_close(struct uhttp *h);
- const char *uhttp_error_string(int err);
- /* Resolve an href against a base URL (absolute, absolute-path, relative and
- * scheme-relative forms). 0 on success. */
- int uhttp_resolve(const char *base, const char *href, char *out,
- unsigned int cap);
- #endif
|