6 /* Call-back type for non-interrupting receving from socket. See recv(2). */
7 typedef ssize_t (*http_recv_callback_t
) (int socket
, void *buffer
,
8 size_t length
, int flags
);
9 /* Application must set this pointer. */
10 extern http_recv_callback_t http_recv_callback
;
12 /* Call-back type for non-interrupting sending to socket. See send(2). */
13 typedef ssize_t (*http_send_callback_t
) (int socket
, const void *buffer
,
14 size_t length
, int flags
);
15 /* Application must set this pointer. */
16 extern http_send_callback_t http_send_callback
;
19 HTTP_ERROR_SERVER
= -1,
20 HTTP_ERROR_SUCCESS
= 0,
25 HTTP_METHOD_UNKNOWN
= 0,
33 struct http_header
*next
;
39 struct http_header
*headers
; /* NULL terminated linked list */
44 struct http_response
{
47 struct http_header
*headers
; /* NULL terminated linked list */
52 /* Free HTTP header and set it to NULL */
53 void http_header_free(struct http_header
**header
);
55 /* Free HTTP headers and set it to NULL */
56 void http_headers_free(struct http_header
**headers
);
58 /* Free HTTP request and set it to NULL */
59 void http_request_free(struct http_request
**request
);
61 /* Free HTTP response and set it to NULL */
62 void http_response_free(struct http_response
**response
);
64 /* Read a HTTP request from connected socket.
65 * @http_request is heap-allocated received HTTP request,
66 * or NULL in case of error.
67 * @return http_error code. */
68 http_error
http_read_request(int socket
, struct http_request
**request
);
70 /* Write a HTTP response to connected socket. Auto-add Content-Length header.
71 * @return 0 in case of success. */
72 int http_write_response(int socket
, const struct http_response
*response
);
74 /* Send a 200 Ok response with a cookie */
75 int http_send_response_200_cookie(int client_socket
,
76 const char *cokie_name
, const char *cookie_value
,
77 const char *cookie_domain
, const char *cookie_path
,
78 const void *body
, size_t body_length
, const char *type
);
80 /* Send a 200 Ok response */
81 int http_send_response_200(int client_socket
,
82 const void *body
, size_t body_length
, const char *type
);
84 /* Send a 302 Found response setting a cookie */
85 int http_send_response_302_cookie(int client_socket
, const char *cokie_name
,
86 const char *cookie_value
, const char *cookie_domain
,
87 const char *cookie_path
, const char *location
);
89 /* Send a 302 Found response with totp authentication scheme header */
90 int http_send_response_302_totp(int client_socket
,
91 const char *code
, const char *text
, const char *location
);
93 /* Send a 400 Bad Request response.
94 * Use non-NULL @reason to override status message. */
95 int http_send_response_400(int client_socket
, const char *reason
);
97 /* Send a 401 Unauthorized response with Basic authentication scheme header */
98 int http_send_response_401_basic(int client_socket
);
100 /* Send a 401 Unauthorized response with OTP authentication scheme header for
102 int http_send_response_401_otp(int client_socket
,
103 const char *method
, const char *code
, const char *text
);
105 /* Send a 403 Forbidden response */
106 int http_send_response_403(int client_socket
);
108 /* Send a 500 Internal Server Error response.
109 * Use non-NULL @reason to override status message. */
110 int http_send_response_500(int client_socket
, const char *reason
);
112 /* Send a 503 Service Temporarily Unavailable response */
113 int http_send_response_503(int client_socket
,
114 const void *body
, size_t body_length
, const char *type
);
116 /* Returns true if request carries WWW-Authenticate header */
117 int http_client_authenticates(const struct http_request
*request
);
119 /* Return HTTP_ERROR_SUCCESS if request carries valid Basic credentials.
120 * NULL @username or @password equales to empty string. */
121 http_error
http_authenticate_basic(const struct http_request
*request
,
122 const char *username
, const char *password
);
124 /* Return HTTP_ERROR_SUCCESS if request carries valid OTP credentials.
125 * NULL @username or @password or @otp equal to empty string. */
126 http_error
http_authenticate_otp(const struct http_request
*request
,
127 const char *username
, const char *password
, const char *otp
);
129 /* Return cookie value by name or NULL if does not present. */
130 const char *http_find_cookie(const struct http_request
*request
,
133 /* Return Host header value or NULL if does not present. Returned string is
134 * statically allocated. */
135 const char *http_find_host(const struct http_request
*request
);