test: server: Pass http_connection to HTTP funcation
[libisds.git] / test / simline / http.h
blob67994645d286dc599b0f96f5120f5b229259bdba
1 #ifndef __ISDS_HTTP_H
2 #define __ISDS_HTTP_H
4 #include <sys/types.h>
6 /* Forward declarations */
7 struct http_connection;
9 /* Call-back type for non-interrupting receiving from socket. See recv(2).
10 * @buffer is data to send by the call-back
11 * @lentgh is number of bytes to send
12 * @connection carries socket and callback_data. */
13 typedef ssize_t (*http_recv_callback_t) (
14 const struct http_connection *connection, void *buffer, size_t length);
16 /* Call-back type for non-interrupting sending to socket. See send(2).
17 * @buffer is memory to store received data by the call-back
18 * @lentgh is size of the @buffer in bytes
19 * @connection carries socket and callback_data. */
20 typedef ssize_t (*http_send_callback_t) (
21 const struct http_connection *connection, const void *buffer,
22 size_t length);
24 struct http_connection {
25 int socket; /* Accepted TCP client socket */
26 http_recv_callback_t recv_callback; /* Non-interrupting reading
27 from socket */
28 http_send_callback_t send_callback; /* Non-interrupting writing
29 to socket */
30 void *callback_data; /* Pointer to pass to callbacks */
33 typedef enum {
34 HTTP_ERROR_SERVER = -1,
35 HTTP_ERROR_SUCCESS = 0,
36 HTTP_ERROR_CLIENT = 1
37 } http_error;
39 typedef enum {
40 HTTP_METHOD_UNKNOWN = 0,
41 HTTP_METHOD_GET,
42 HTTP_METHOD_POST
43 } http_method;
45 struct http_header {
46 char *name;
47 char *value;
48 struct http_header *next;
51 struct http_request {
52 http_method method;
53 char *uri;
54 struct http_header *headers; /* NULL terminated linked list */
55 void *body;
56 size_t body_length;
59 struct http_response {
60 unsigned int status;
61 char *reason;
62 struct http_header *headers; /* NULL terminated linked list */
63 void *body;
64 size_t body_length;
67 /* Free HTTP header and set it to NULL */
68 void http_header_free(struct http_header **header);
70 /* Free HTTP headers and set it to NULL */
71 void http_headers_free(struct http_header **headers);
73 /* Free HTTP request and set it to NULL */
74 void http_request_free(struct http_request **request);
76 /* Free HTTP response and set it to NULL */
77 void http_response_free(struct http_response **response);
79 /* Read a HTTP request from connection.
80 * @http_request is heap-allocated received HTTP request,
81 * or NULL in case of error.
82 * @return http_error code. */
83 http_error http_read_request(const struct http_connection *connection,
84 struct http_request **request);
86 /* Write a HTTP response to connection. Auto-add Content-Length header.
87 * @return 0 in case of success. */
88 int http_write_response(const struct http_connection *connection,
89 const struct http_response *response);
91 /* Send a 200 Ok response with a cookie */
92 int http_send_response_200_cookie(const struct http_connection *connection,
93 const char *cokie_name, const char *cookie_value,
94 const char *cookie_domain, const char *cookie_path,
95 const void *body, size_t body_length, const char *type);
97 /* Send a 200 Ok response */
98 int http_send_response_200(const struct http_connection *connection,
99 const void *body, size_t body_length, const char *type);
101 /* Send a 302 Found response setting a cookie */
102 int http_send_response_302_cookie(const struct http_connection *connection,
103 const char *cokie_name, const char *cookie_value,
104 const char *cookie_domain, const char *cookie_path,
105 const char *location);
107 /* Send a 302 Found response with totp authentication scheme header */
108 int http_send_response_302_totp(const struct http_connection *connection,
109 const char *code, const char *text, const char *location);
111 /* Send a 400 Bad Request response.
112 * Use non-NULL @reason to override status message. */
113 int http_send_response_400(const struct http_connection *connection,
114 const char *reason);
116 /* Send a 401 Unauthorized response with Basic authentication scheme header */
117 int http_send_response_401_basic(const struct http_connection *connection);
119 /* Send a 401 Unauthorized response with OTP authentication scheme header for
120 * given @method. */
121 int http_send_response_401_otp(const struct http_connection *connection,
122 const char *method, const char *code, const char *text);
124 /* Send a 403 Forbidden response */
125 int http_send_response_403(const struct http_connection *connection);
127 /* Send a 500 Internal Server Error response.
128 * Use non-NULL @reason to override status message. */
129 int http_send_response_500(const struct http_connection *connection,
130 const char *reason);
132 /* Send a 503 Service Temporarily Unavailable response */
133 int http_send_response_503(const struct http_connection *connection,
134 const void *body, size_t body_length, const char *type);
136 /* Returns true if request carries WWW-Authenticate header */
137 int http_client_authenticates(const struct http_request *request);
139 /* Return HTTP_ERROR_SUCCESS if request carries valid Basic credentials.
140 * NULL @username or @password equales to empty string. */
141 http_error http_authenticate_basic(const struct http_request *request,
142 const char *username, const char *password);
144 /* Return HTTP_ERROR_SUCCESS if request carries valid OTP credentials.
145 * NULL @username or @password or @otp equal to empty string. */
146 http_error http_authenticate_otp(const struct http_request *request,
147 const char *username, const char *password, const char *otp);
149 /* Return cookie value by name or NULL if does not present. */
150 const char *http_find_cookie(const struct http_request *request,
151 const char *name);
153 /* Return Host header value or NULL if does not present. Returned string is
154 * statically allocated. */
155 const char *http_find_host(const struct http_request *request);
157 #endif