Skip to content
pi-c Documentation
Login

Documentation

/* 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 {
    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) */
    pic_http_body_fn on_body;
    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];
} pic_http_resp;

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