286#include "lwip/ip_addr.h"
287#include "lwip/prot/iana.h"
318#ifndef HTTP_REQ_MAX_HDRS
319#define HTTP_REQ_MAX_HDRS (16)
402#define HTTP_METHODS_GET_HEAD \
403 ((1U << HTTP_METHOD_GET) | (1U << HTTP_METHOD_HEAD))
421typedef err_t priv_cnt_f(
struct http *
http);
423#include "http_priv.h"
456 http->cx_priv.priv = priv;
457 http->cx_priv.fini = fini;
458 http->cx_priv.cnt = cnt;
482 return http->cx_priv.priv;
600#ifndef HTTP_DEFAULT_IDLE_TMO_S
601#define HTTP_DEFAULT_IDLE_TMO_S (5)
616#ifndef HTTP_DEFAULT_SEND_TMO_S
617#define HTTP_DEFAULT_SEND_TMO_S (5)
633#ifndef HTTP_DEFAULT_LISTEN_BACKLOG
634#define HTTP_DEFAULT_LISTEN_BACKLOG (10)
662#define STRLEN_LTRL(s) (sizeof(s) - 1)
779 struct srv_data *srv_data;
1161static inline ip_addr_t *
1166 return http_local_ip(
http->pcb);
1183static inline uint16_t
1188 return http_local_port(
http->pcb);
1230static inline ip_addr_t *
1235 return http_remote_ip(
http->pcb);
1251static inline uint16_t
1256 return http_remote_port(
http->pcb);
1300 return http->srv_data->srv_priv;
1329static inline struct req *
1431static inline const char *
1480#define http_req_hdr_ltrl(req, name, val_len) \
1481 http_req_hdr((req), (name), STRLEN_LTRL(name), (val_len))
1523 const char *val,
size_t val_len);
1565 const char *substring,
size_t substring_len);
1648 const char *substring)
1688#define http_req_hdr_eq_ltrl(req, name, val) \
1689 http_req_hdr_eq((req), (name), STRLEN_LTRL(name), (val), \
1729#define http_req_hdr_contains_ltrl(req, name, substring) \
1730 http_req_hdr_contains((req), (name), STRLEN_LTRL(name), (substring), \
1731 STRLEN_LTRL(substring))
1780 const char *val,
size_t val_len,
void *priv);
1903 size_t name_len,
size_t *val_len);
1956static inline const uint8_t *
2012#define http_req_cookie_ltrl(req, name, val_len) \
2013 http_req_cookie((req), (name), STRLEN_LTRL(name), (val_len))
2162static inline const uint8_t *
2168 *len =
req->path.len;
2169 return ((uint8_t*)
req->p->payload) +
req->path.off;
2215static inline const uint8_t *
2221 *len =
req->query.len;
2222 if (
req->query.len == 0)
2224 return ((uint8_t*)
req->p->payload) +
req->query.off;
2289 const uint8_t *name,
size_t name_len,
2728static inline struct resp *
2777 if (status < 100 || status >= 1000)
2779 resp->status = status;
2826static inline uint16_t
2830 return resp->status;
2847#ifndef RESP_HDR_SMALL_BUF_SZ
2848#define RESP_HDR_SMALL_BUF_SZ (512)
2868#ifndef RESP_HDR_LARGE_BUF_SZ
2869#define RESP_HDR_LARGE_BUF_SZ (TCP_MSS)
2895#define RESP_HDR_SMALL_POOL_SZ (MAX_CONCURRENT_CX_HINT)
2898#ifndef RESP_HDR_SMALL_POOL_SZ
2899#define RESP_HDR_SMALL_POOL_SZ (HTTP_N * SRV_N)
2916#ifndef RESP_HDR_LARGE_POOL_SZ
2917#define RESP_HDR_LARGE_POOL_SZ (RESP_HDR_SMALL_POOL_SZ / 2)
2959 const char *val,
size_t val_len);
3045#define http_resp_set_hdr_ltrl(resp, name, val) \
3046 http_resp_set_hdr((resp), (name), STRLEN_LTRL(name), (val), \
3169#define http_resp_set_type_ltrl(resp, type) \
3170 http_resp_set_type((resp), (type), STRLEN_LTRL(type))
3484err_t http_resp_send_buf_chain(
struct http *
http,
const uint8_t *buf,
size_t len,
3485 bool durable,
bool last);
3545 http->resp.status = status;
3546 return http_hndlr_err(
http, NULL);
3648 hndlr_f hndlr, uint8_t methods,
void *priv);
4195 const char *
const in, ssize_t inlen,
bool plus);
4233err_t
hex_decode(
const char *
const buf, ssize_t len, uint64_t *n);
#define CHECK_OBJ_NOTNULL(ptr, type_magic)
Assert that a pointer is not NULL, and points to an object that is valid for its type.
Definition: assertion.h:362
#define AN(x)
Assert that a value is not zero, or not NULL
Definition: assertion.h:141
#define PICOW_HTTP_ASSERT(c)
Assert that a condition is true.
Definition: assertion.h:91
const char * http_req_hdr(struct req *req, const char *name, size_t name_len, size_t *val_len)
Return the value of a request header.
static struct req * http_req(struct http *http)
Get the current request object.
Definition: http.h:1330
static const char * http_req_hdr_str(struct req *req, const char *name, size_t *val_len)
Return the value of a request header with a nul-terminated name.
Definition: http.h:1432
http_method_t
Request method.
Definition: http.h:379
bool http_req_hdr_eq(struct req *req, const char *name, size_t name_len, const char *val, size_t val_len)
Return true if a request header value is equal to a string.
static bool http_req_hdr_eq_str(struct req *req, const char *name, const char *val)
Return true if a request header value is equal to a nul-terminated string.
Definition: http.h:1603
err_t http_req_body_ptr(struct http *http, const uint8_t **buf, size_t *len, size_t off)
Get a pointer to request body contents with zero-copy.
err_t http_req_body(struct http *http, uint8_t buf[], size_t *len, size_t off)
Copy the request body to a buffer.
bool http_req_hdr_contains(struct req *req, const char *name, size_t name_len, const char *substring, size_t substring_len)
Return true if a request header contains a string.
err_t http_req_hdr_iter(struct req *req, name_val_iter_f iter_cb, void *priv)
Iterate over request headers.
err_t http_req_query_iter(const uint8_t *query, size_t len, name_val_iter_f iter_cb, void *priv)
Iterate over query string parameter names and values.
err_t http_req_chunk(struct http *http, uint8_t *buf, size_t *len)
Copy the next chunk in a request body sent with chunked encdoding.
static const uint8_t * http_req_cookie_str(struct req *req, const char *name, size_t *val_len)
Return the value of a cookie with a nul-terminated name.
Definition: http.h:1957
static bool http_req_hdr_contains_str(struct req *req, const char *name, const char *substring)
Return true if a request header contains a nul-terminated string.
Definition: http.h:1647
const uint8_t * http_req_cookie(struct req *req, const char *name, size_t name_len, size_t *val_len)
Return the value of a cookie.
err_t url_decode(char *const out, size_t *outlen, const char *const in, ssize_t inlen, bool plus)
Decode a "percent-encoded" string.
static enum http_method_t http_req_method(struct req *req)
Return the request method.
Definition: http.h:2115
const uint8_t * http_req_query_val(const uint8_t *query, size_t query_len, const uint8_t *name, size_t name_len, size_t *val_len)
Return the value of a query parameter.
err_t http_req_cookie_iter(struct req *req, name_val_iter_f iter_cb, void *priv)
Iterate over cooke name/value pairs.
static const uint8_t * http_req_query(struct req *req, size_t *len)
Return the request query string.
Definition: http.h:2216
static const uint8_t * http_req_path(struct req *req, size_t *len)
Return the path from the first request line.
Definition: http.h:2163
err_t(* name_val_iter_f)(const char *name, size_t name_len, const char *val, size_t val_len, void *priv)
Function type for name/value iterators.
Definition: http.h:1779
static size_t http_req_body_len(struct req *req)
Return the length of the request body.
Definition: http.h:2407
static struct resp * http_resp(struct http *http)
Get the current response object.
Definition: http.h:2729
err_t http_resp_send_chunk(struct http *http, const uint8_t *buf, size_t len, bool durable)
Send a chunk in a chunked-encoded response body.
err_t(* hndlr_f)(struct http *http, void *priv)
Function type for custom response handlers.
Definition: http.h:367
#define http_resp_set_hdr_ltrl(resp, name, val)
Set a response header to a literal string.
Definition: http.h:3045
static err_t http_resp_set_hdr_str(struct resp *resp, const char *name, const char *val)
Set a response header to a nul-terminated string.
Definition: http.h:3001
static err_t http_resp_set_type_str(struct resp *resp, const char *type)
Set the Content-Type response header to a nul-terminated string.
Definition: http.h:3129
err_t register_hndlr_methods(struct server_cfg *cfg, const char *path, hndlr_f hndlr, uint8_t methods, void *priv)
Register a response handler for request methods and a path.
err_t http_resp_set_len(struct resp *resp, size_t len)
Set the Content-Length response header.
err_t http_resp_send_hdr(struct http *http)
Send the response header.
static uint16_t http_resp_status(struct resp *resp)
Get the response status code.
Definition: http.h:2827
static err_t http_resp_set_type(struct resp *resp, const char *type, size_t type_len)
Set the Content-Type response header.
Definition: http.h:3086
err_t http_resp_send_buf(struct http *http, const uint8_t *buf, size_t len, bool durable)
Send the contents of a buffer as the response body.
static err_t http_resp_set_xfer_chunked(struct resp *resp)
Set the Transfer-Encdoing response header to "chunked".
Definition: http.h:3246
static err_t http_resp_set_status(struct resp *resp, uint16_t status)
Set the response status code.
Definition: http.h:2774
static err_t http_resp_err(struct http *http, uint16_t status)
Send an error response.
Definition: http.h:3540
err_t register_error_hndlr(struct server_cfg *cfg, hndlr_f hndlr, void *priv, priv_fini_f fini)
Register a custom error response handler.
err_t register_default_hndlr(struct server_cfg *cfg, hndlr_f hndlr, void *priv, priv_fini_f fini)
Register a default response handler.
err_t http_resp_set_hdr(struct resp *resp, const char *name, size_t name_len, const char *val, size_t val_len)
Set a response header.
static err_t register_hndlr(struct server_cfg *cfg, const char *path, hndlr_f hndlr, enum http_method_t method, void *priv)
Register a response handler for a request method and path.
Definition: http.h:3803
http_status_t
enum for HTTP response status codes
Definition: http.h:493
void http_srv_set_priv(struct server *server, void *priv, priv_fini_f *fini)
Set a server-wide private object.
static void http_cx_set_priv(struct http *http, void *priv, priv_fini_f *fini, priv_cnt_f *cnt)
Set a connection-scoped private object.
Definition: http.h:453
static struct server_cfg http_default_cfg(void)
Get the default HTTP server configuration.
Definition: http.h:894
ip_addr_t * http_srv_ip(struct server *server)
Return the server listener IP address.
static void * http_cx_priv(struct http *http)
Return the connection-scoped private object, if any.
Definition: http.h:479
static uint16_t http_cx_remote_port(struct http *http)
Return the remote port of the current HTTP connection.
Definition: http.h:1252
static uint16_t http_cx_local_port(struct http *http)
Return the local port of the current HTTP connection.
Definition: http.h:1184
static ip_addr_t * http_cx_remote_ip(struct http *http)
Return the remote IP address of the current HTTP connection.
Definition: http.h:1231
static ip_addr_t * http_cx_local_ip(struct http *http)
Return the local IP address of the current HTTP connection.
Definition: http.h:1162
err_t http_srv_fini(struct server *server)
Stop an HTTP server.
static void * http_srv_priv(struct http *http)
Return the server-wide private object, if any.
Definition: http.h:1296
void priv_fini_f(void *p)
Function type for private data finalizers.
Definition: http.h:420
uint16_t http_srv_port(struct server *server)
Return the server listener port number.
err_t http_cfg(const char *name, struct server_cfg *cfg)
Get the default confiuration for a server by name.
err_t http_srv_init(struct server **server, struct server_cfg *cfg)
Start an HTTP server.
err_t format_hex(char *s, size_t *len, uint32_t n, bool upper)
Format a hexadecimal number.
#define STRLEN_LTRL(s)
Length of a literal string.
Definition: http.h:662
err_t format_decimal(char *s, size_t *len, int32_t n)
Format a decimal number.
err_t hex_decode(const char *const buf, ssize_t len, uint64_t *n)
Decode a hexadecimal string.
@ HTTP_METHOD_POST
Definition: http.h:382
@ HTTP_METHOD_HEAD
Definition: http.h:381
@ __HTTP_METHOD_MAX
Definition: http.h:388
@ HTTP_METHOD_CONNECT
Definition: http.h:385
@ HTTP_METHOD_DELETE
Definition: http.h:384
@ HTTP_METHOD_GET
Definition: http.h:380
@ HTTP_METHOD_TRACE
Definition: http.h:387
@ HTTP_METHOD_OPTIONS
Definition: http.h:386
@ HTTP_METHOD_PUT
Definition: http.h:383
@ HTTP_STATUS_URI_TOO_LONG
Definition: http.h:557
@ HTTP_STATUS_IM_A_TEAPOT
Definition: http.h:565
@ HTTP_STATUS_EXPECTATION_FAILED
Definition: http.h:563
@ HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE
Definition: http.h:559
@ HTTP_STATUS_NOT_ACCEPTABLE
Definition: http.h:541
@ HTTP_STATUS_INTERNAL_SERVER_ERROR
Definition: http.h:575
@ HTTP_STATUS_REQUEST_TIMEOUT
Definition: http.h:545
@ HTTP_STATUS_MISDIRECTED_REQUEST
Definition: http.h:567
@ HTTP_STATUS_PROXY_AUTH_REQUIRED
Definition: http.h:543
@ HTTP_STATUS_FORBIDDEN
Definition: http.h:535
@ HTTP_STATUS_CONTENT_TOO_LARGE
Definition: http.h:555
@ HTTP_STATUS_BAD_REQUEST
Definition: http.h:529
@ HTTP_STATUS_RANGE_NOT_SATISFIABLE
Definition: http.h:561
@ HTTP_STATUS_FOUND
Definition: http.h:517
@ HTTP_STATUS_UPGRADE_REQUIRED
Definition: http.h:571
@ HTTP_STATUS_METHOD_NOT_ALLOWED
Definition: http.h:539
@ HTTP_STATUS_REQ_HDR_FIELDS_TOO_LARGE
Definition: http.h:573
@ HTTP_STATUS_GONE
Definition: http.h:549
@ HTTP_STATUS_NOT_MODIFIED
Definition: http.h:521
@ HTTP_STATUS_CONFLICT
Definition: http.h:547
@ HTTP_STATUS_TEMPORARY_REDIRECT
Definition: http.h:525
@ HTTP_STATUS_RESET_CONTENT
Definition: http.h:509
@ HTTP_STATUS_UNPROCESSABLE_CONTENT
Definition: http.h:569
@ HTTP_STATUS_ACCEPTED
Definition: http.h:503
@ HTTP_STATUS_MOVED_PERMANENTLY
Definition: http.h:515
@ HTTP_STATUS_NON_AUTHORITATIVE_INFO
Definition: http.h:505
@ HTTP_STATUS_PARTIAL_CONTENT
Definition: http.h:511
@ HTTP_STATUS_CREATED
Definition: http.h:501
@ HTTP_STATUS_USE_PROXY
Definition: http.h:523
@ HTTP_STATUS_SERVICE_UNAVAILABLE
Definition: http.h:581
@ HTTP_STATUS_LENGTH_REQUIRED
Definition: http.h:551
@ HTTP_STATUS_SWITCHING_PROTOCOLS
Definition: http.h:497
@ HTTP_STATUS_PERMANENT_REDIRECT
Definition: http.h:527
@ HTTP_STATUS_PRECONDITION_FAILED
Definition: http.h:553
@ HTTP_STATUS_BAD_GATEWAY
Definition: http.h:579
@ HTTP_STATUS_NOT_IMPLEMENTED
Definition: http.h:577
@ HTTP_STATUS_MULTIPLE_CHOICES
Definition: http.h:513
@ HTTP_STATUS_NO_CONTENT
Definition: http.h:507
@ HTTP_STATUS_OK
Definition: http.h:499
@ HTTP_STATUS_CONTINUE
Definition: http.h:495
@ HTTP_STATUS_UNAUTHORIZED
Definition: http.h:531
@ HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED
Definition: http.h:585
@ HTTP_STATUS_SEE_OTHER
Definition: http.h:519
@ HTTP_STATUS_GATEWAY_TIMEOUT
Definition: http.h:583
@ HTTP_STATUS_NOT_FOUND
Definition: http.h:537
@ HTTP_STATUS_PAYMENT_REQUIRED
Definition: http.h:533
Current HTTP connection, request and response.
NTP configuration.
Definition: ntp.h:174
HTTP server configuration.
Definition: http.h:774
unsigned idle_tmo_s
Definition: http.h:781
unsigned send_tmo_s
Definition: http.h:783
bool tls
Definition: http.h:791
uint8_t listen_backlog
Definition: http.h:787
const ip_addr_t * ipaddr
Definition: http.h:778
enum lwip_ip_addr_type ip_type
Definition: http.h:789
uint16_t port
Definition: http.h:785