/* HTTP/1.1 for one POST per connection. The response parser is a pure
* function over bytes (status line, headers, then a body delimited by chunked
* encoding, Content-Length or the end of the connection), so it is fuzzed on
* its own. */
#ifndef PIC_HTTP_H
#define PIC_HTTP_H
#include <stddef.h>
#include "net.h"
/* Body bytes as they arrive. Nonzero stops the response; feed returns it. */
typedef int (*pic_http_body_fn)(void *ctx, const char *bytes, size_t n);
typedef struct pic_http_resp pic_http_resp;
/* The final response's headers are in (R's status and headers set), before any body byte.
* Nonzero stops the response; feed returns it. */
typedef int (*pic_http_head_fn)(void *ctx, const pic_http_resp *r);
struct pic_http_resp {
int state; /* internal */
int status; /* final status code, once headers are done */
char retry_after[32]; /* Retry-After value, if any */
char request_id[128]; /* request-id header, if any */
char content_type[128]; /* Content-Type, if any */
char etag[128]; /* ETag, if any */
char last_modified[64]; /* Last-Modified, if any */
char peer[8]; /* Pic-Peer, if any (remote control's relay) */
/* The rate-limit headers (every one whose name has "ratelimit" in it,
* the vendors' prefixes alike), one "name: value" line each, the name
* lowercased; what does not fit is dropped whole. */
char ratelimit[1024];
pic_http_body_fn on_body;
pic_http_head_fn on_head; /* NULL unless set after pic_http_resp_init; gets ctx too */
/* One more header the caller names (NULL unless set after pic_http_resp_init; any case),
* its value in wanted (MCP's Mcp-Session-Id). */
const char *want;
char wanted[2048];
/* Set after pic_http_resp_init by a caller that sent Accept-Encoding: gzip and inflates the body
* itself: Content-Encoding gzip is then taken (gzip set) rather than refused. The body bytes
* reach on_body still compressed. */
int gzip_ok, gzip;
void *ctx;
char line[8192];
size_t line_len;
size_t header_bytes;
unsigned long long remaining; /* bytes left of the body or chunk */
int chunked, has_length, body_started;
char err[160];
};
void pic_http_resp_init(pic_http_resp *r, pic_http_body_fn on_body, void *ctx);
/* 0, a callback's nonzero value, or -1 on a malformed response (r->err). */
int pic_http_feed(pic_http_resp *r, const char *bytes, size_t n);
/* 1 once the body is complete (more bytes are an error). */
int pic_http_done(const pic_http_resp *r);
/* At the end of the connection: 0 if the response was complete, else -1. */
int pic_http_end(pic_http_resp *r);
/* POSTs body to url (https://host[:port]/path, or http:// to loopback only)
* with extra header lines ("Name: value"; a Content-Type among them replaces
* the default application/json), streaming the response body to
* r's callback. Header values are never copied into errors. 0 when a full
* response arrived (any status), -1 on a transport or protocol failure with
* err set. pic_net_tls_failed(net) then tells a TLS failure from others. */
int pic_http_post(pic_net *net, const char *url, const char *const *headers, size_t nheaders, const char *body,
size_t body_len, pic_http_resp *r, int idle_seconds, int deadline_seconds, char *err, size_t errcap);
/* GETs url, as pic_http_post does otherwise (no body). */
int pic_http_get(pic_net *net, const char *url, const char *const *headers, size_t nheaders, pic_http_resp *r,
int idle_seconds, int deadline_seconds, char *err, size_t errcap);
/* Any METHOD (PUT, DELETE), with BODY (NULL: none, no Content-Length) as pic_http_post. */
int pic_http_request(pic_net *net, const char *method, const char *url, const char *const *headers, size_t nheaders,
const char *body, size_t body_len, pic_http_resp *r, int idle_seconds, int deadline_seconds,
char *err, size_t errcap);
#endif