-
Notifications
You must be signed in to change notification settings - Fork 0
/
webb.h
170 lines (151 loc) · 4.39 KB
/
webb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef WEBB_H
#define WEBB_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/** @brief An HTTP method. */
typedef enum WebbMethod {
WEBB_CONNECT,
WEBB_DELETE,
WEBB_GET,
WEBB_HEAD,
WEBB_OPTIONS,
WEBB_PATCH,
WEBB_POST,
WEBB_PUT,
WEBB_TRACE,
WEBB_INVALID = 0,
} WebbMethod;
/** @brief A list of HTTP headers. */
typedef struct WebbHeaders {
/** @brief The key/name of the header. */
char *key;
/** @brief The value of the header. */
char *val;
/** @brief The next header in the linked list. */
struct WebbHeaders *next;
} WebbHeaders;
/** @brief A Webb HTTP request. */
typedef struct WebbRequest {
/** @brief The HTTP verb of the request. */
WebbMethod method;
/** @brief The uri component of the request. */
char *uri;
/** @brief The query string of the request (may be NULL). */
char *query;
/** @brief The HTTP request headers. */
WebbHeaders *headers;
/** @brief The HTTP request body (may be NULL). */
char *body;
/** @brief The length of the request body. */
size_t body_len;
} WebbRequest;
/** @brief The type of the response body. */
typedef enum WebbBodyType {
WEBB_BODY_NULL = 0,
WEBB_BODY_ALLOCATED,
WEBB_BODY_STATIC,
WEBB_BODY_FD,
} WebbBodyType;
/** @brief A Webb response body. */
typedef struct WebbBody {
/** @brief The length of the HTTP response body. */
size_t len;
/** @brief The body type. */
WebbBodyType type;
union {
/** @brief The HTTP body buffer. */
char *buf;
/** @brief The HTTP body file descriptor. */
int fd;
} body;
} WebbBody;
/** @brief A Webb HTTP response. */
typedef struct WebbResponse {
/** @brief HTTP status response code (e.g 200 for OK). */
int status;
/** @brief HTTP headers of the response. */
WebbHeaders *headers;
/** @brief HTTP body to send */
WebbBody body;
} WebbResponse;
/**
* @brief A Webb HTTP handler function. Accepts an incoming request and returns a response.
* Note that this function has to be thread-safe.
*
* @param req The HTTP request object.
* @param res The HTTP response object, mutated by the function.
*
* @returns The HTTP status code (e.g 200 for OK), -1 on unexpected errors.
*/
typedef int(WebbHandler)(const WebbRequest *req, WebbResponse *res);
/**
* @brief Starts the Webb http server.
*
* @param port The port to listen to (e.g "8080").
* @param handler The http request/response handler function.
*
* @returns A non-zero error. Note that this function never returns unless an error occurred.
*/
int webb_server_run(const char *port, WebbHandler *handler);
/**
* @brief Get the value of a given header from the request.
*
* @param req The HTTP request.
* @param key The HTTP header name.
*
* @returns The value of the HTTP header if present, otherwise NULL. Value is still owned by req.
*/
const char *webb_get_header(const WebbRequest *req, const char *key);
/**
* @brief Set a given response header.
*
* @param res The HTTP response.
* @param key The header name. Has to be a non-allocated string.
* @param val The header value. Has to be an allocated string (will be freed).
*/
void webb_set_header(WebbResponse *res, char *key, char *val);
/**
* @brief Set the body of the response. Body will be freed.
*
* @param res The HTTP response.
* @param body The body to send. Has to be allocated.
* @param len The length of the body.
*/
void webb_set_body(WebbResponse *res, char *body, size_t len);
/**
* @brief Set the body of the response as a static string. Body will NOT be freed.
*
* @param res The HTTP response.
* @param body The body to send.
* @param len The length of the body.
*/
void webb_set_body_static(WebbResponse *res, char *body, size_t len);
/**
* @brief Set the body of the response as a file descriptor to send. E.g an opened file.
*
* @param res The HTTP response.
* @param fd The file descriptor to read from.
*/
void webb_set_body_fd(WebbResponse *res, int fd, size_t len);
/**
* @brief Convert an HTTP method to it's string representation (e.g HTTP_GET -> "GET").
*
* @param m The HTTP method.
*
* @returns The method string. Should not be freed.
*/
const char *webb_method_str(WebbMethod m);
/**
* @brief Get the status message of a HTTP status code (e.g 200 -> "OK").
*
* @param status The HTTP status code.
*
* @returns The status message, or NULL if status is not a valid HTTP status code. Should not be freed.
*/
const char *webb_status_str(int status);
#ifdef __cplusplus
}
#endif
#endif