6 /* Call-back type for non-interrupting receiving from socket. See recv(2).
7 * @context is opaque pointer passed to the call-back. */
8 typedef ssize_t (*http_recv_callback_t
) (void *context
, void *buffer
,
9 size_t length
, int flags
);
10 /* Application must set these pointers. */
11 extern http_recv_callback_t http_recv_callback
;
12 extern void *http_recv_context
;
14 /* Call-back type for non-interrupting sending to socket. See send(2).
15 * @context is opaque pointer passed to the call-back. */
16 typedef ssize_t (*http_send_callback_t
) (void *context
, const void *buffer
,
17 size_t length
, int flags
);
18 /* Application must set these pointers. */
19 extern http_send_callback_t http_send_callback
;
20 extern void *http_send_context
;
23 HTTP_ERROR_SERVER
= -1,
24 HTTP_ERROR_SUCCESS
= 0,
29 HTTP_METHOD_UNKNOWN
= 0,
37 struct http_header
*next
;
43 struct http_header
*headers
; /* NULL terminated linked list */
48 struct http_response
{
51 struct http_header
*headers
; /* NULL terminated linked list */
56 /* Free HTTP header and set it to NULL */
57 void http_header_free(struct http_header
**header
);
59 /* Free HTTP headers and set it to NULL */
60 void http_headers_free(struct http_header
**headers
);
62 /* Free HTTP request and set it to NULL */
63 void http_request_free(struct http_request
**request
);
65 /* Free HTTP response and set it to NULL */
66 void http_response_free(struct http_response
**response
);
68 /* Read a HTTP request from connected socket.
69 * @http_request is heap-allocated received HTTP request,
70 * or NULL in case of error.
71 * @return http_error code. */
72 http_error
http_read_request(int socket
, struct http_request
**request
);
74 /* Write a HTTP response to connected socket. Auto-add Content-Length header.
75 * @return 0 in case of success. */
76 int http_write_response(int socket
, const struct http_response
*response
);
78 /* Send a 200 Ok response with a cookie */
79 int http_send_response_200_cookie(int client_socket
,
80 const char *cokie_name
, const char *cookie_value
,
81 const char *cookie_domain
, const char *cookie_path
,
82 const void *body
, size_t body_length
, const char *type
);
84 /* Send a 200 Ok response */
85 int http_send_response_200(int client_socket
,
86 const void *body
, size_t body_length
, const char *type
);
88 /* Send a 302 Found response setting a cookie */
89 int http_send_response_302_cookie(int client_socket
, const char *cokie_name
,
90 const char *cookie_value
, const char *cookie_domain
,
91 const char *cookie_path
, const char *location
);
93 /* Send a 302 Found response with totp authentication scheme header */
94 int http_send_response_302_totp(int client_socket
,
95 const char *code
, const char *text
, const char *location
);
97 /* Send a 400 Bad Request response.
98 * Use non-NULL @reason to override status message. */
99 int http_send_response_400(int client_socket
, const char *reason
);
101 /* Send a 401 Unauthorized response with Basic authentication scheme header */
102 int http_send_response_401_basic(int client_socket
);
104 /* Send a 401 Unauthorized response with OTP authentication scheme header for
106 int http_send_response_401_otp(int client_socket
,
107 const char *method
, const char *code
, const char *text
);
109 /* Send a 403 Forbidden response */
110 int http_send_response_403(int client_socket
);
112 /* Send a 500 Internal Server Error response.
113 * Use non-NULL @reason to override status message. */
114 int http_send_response_500(int client_socket
, const char *reason
);
116 /* Send a 503 Service Temporarily Unavailable response */
117 int http_send_response_503(int client_socket
,
118 const void *body
, size_t body_length
, const char *type
);
120 /* Returns true if request carries WWW-Authenticate header */
121 int http_client_authenticates(const struct http_request
*request
);
123 /* Return HTTP_ERROR_SUCCESS if request carries valid Basic credentials.
124 * NULL @username or @password equales to empty string. */
125 http_error
http_authenticate_basic(const struct http_request
*request
,
126 const char *username
, const char *password
);
128 /* Return HTTP_ERROR_SUCCESS if request carries valid OTP credentials.
129 * NULL @username or @password or @otp equal to empty string. */
130 http_error
http_authenticate_otp(const struct http_request
*request
,
131 const char *username
, const char *password
, const char *otp
);
133 /* Return cookie value by name or NULL if does not present. */
134 const char *http_find_cookie(const struct http_request
*request
,
137 /* Return Host header value or NULL if does not present. Returned string is
138 * statically allocated. */
139 const char *http_find_host(const struct http_request
*request
);