about summary refs log tree commit diff
path: root/include/libhttpc.h
blob: 6160d215e6789724cc907507d5092e466c8a90d9 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#ifndef __LIBHTTPC_H__
#define __LIBHTTPC_H__

#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>


/**
 * Just typedef to make libhttpc_malloc definition more readable
 * @see LibHTTPC_malloc
 */
typedef void *LibHTTPC_Malloc(size_t);
/**
 * Just typedef to make libhttpc_realloc definition more readable
 * @see libhttpc_realloc
 */
typedef void *LibHTTPC_Realloc(void *, size_t);
/**
 * Just typedef to make libhttpc_free definition more readable
 * @see libhttpc_realloc
 */
typedef void LibHTTPC_Free(void *);

/**
 * Enum that contains all headers that are supported by the HTTP/1.1
 */
enum LibHTTPC_HeaderType
{
    /* General headers */
    LibHTTPC_Header_CACHE_CONTROL,
    LibHTTPC_Header_CONNECTION,
    LibHTTPC_Header_DATE,
    LibHTTPC_Header_PRAGMA,
    LibHTTPC_Header_TRAILER,
    LibHTTPC_Header_TRANSFER_ENCODING,
    LibHTTPC_Header_UPGRADE,
    LibHTTPC_Header_VIA,
    LibHTTPC_Header_WARNING,
    /* Request headers */
    LibHTTPC_Header_ACCEPT,
    LibHTTPC_Header_ACCEPT_CHARSET,
    LibHTTPC_Header_ACCEPT_ENCODING,
    LibHTTPC_Header_ACCEPT_LANGUAGE,
    LibHTTPC_Header_AUTHORIZATION,
    LibHTTPC_Header_EXPECT,
    LibHTTPC_Header_FROM,
    LibHTTPC_Header_HOST,
    LibHTTPC_Header_IF_MATCH,
    LibHTTPC_Header_IF_MODIFIED_SINCE,
    LibHTTPC_Header_IF_NONE_MATCH,
    LibHTTPC_Header_IF_RANGE,
    LibHTTPC_Header_IF_UNMODIFIED_SINCE,
    LibHTTPC_Header_MAX_FORWARDS,
    LibHTTPC_Header_PROXY_AUTHORIZATION,
    LibHTTPC_Header_RANGE,
    LibHTTPC_Header_REFERER,
    LibHTTPC_Header_TE,
    LibHTTPC_Header_USER_AGENT,
    /* Response headers */
    LibHTTPC_Header_ACCEPT_RANGES,
    LibHTTPC_Header_AGE,
    LibHTTPC_Header_ETAG,
    LibHTTPC_Header_LOCATION,
    LibHTTPC_Header_PROXY_AUTHENTICATE,
    LibHTTPC_Header_RETRY_AFTER,
    LibHTTPC_Header_SERVER,
    LibHTTPC_Header_VARY,
    LibHTTPC_Header_WWW_AUTHENTICATE,
    /* Entity headers */
    LibHTTPC_Header_ALLOW,
    LibHTTPC_Header_CONTENT_ENCODING,
    LibHTTPC_Header_CONTENT_LANGUAGE,
    LibHTTPC_Header_CONTENT_LENGTH,
    LibHTTPC_Header_CONTENT_LOCATION,
    LibHTTPC_Header_CONTENT_MD5,
    LibHTTPC_Header_CONTENT_RANGE,
    LibHTTPC_Header_CONTENT_TYPE,
    LibHTTPC_Header_EXPIRES,
    LibHTTPC_Header_LAST_MODIFIED,

    LibHTTPC_Header_EXTENSION_HEADER,
};

/**
 * Enum that contains all methods that are supported by the HTTP/1.1
 */
enum LibHTTPC_Method
{
    LibHTTPC_Method_OPTIONS,
    LibHTTPC_Method_GET,
    LibHTTPC_Method_HEAD,
    LibHTTPC_Method_POST,
    LibHTTPC_Method_PUT,
    LibHTTPC_Method_DELETE,
    LibHTTPC_Method_TRACE,
    LibHTTPC_Method_CONNECT,
    LibHTTPC_Method_EXTENSION_METHOD,
};

/**
 * Enum that contains all statuses that are supported by the HTTP/1.1
 */
enum LibHTTPC_Status
{
    /* 1xx */
    LibHTTPC_Status_CONTINUE               = 100,
    LibHTTPC_Status_SWITCHING_PROTOCOLS    = 101,
    /* 2xx */
    LibHTTPC_Status_OK                     = 200,
    LibHTTPC_Status_CREATED                = 201,
    LibHTTPC_Status_ACCEPTED               = 202,
    LibHTTPC_Status_NONAUTHORITATIVE_INFO  = 203,
    LibHTTPC_Status_NO_CONTENT             = 204,
    LibHTTPC_Status_RESET_CONTENT          = 205,
    LibHTTPC_Status_PARTIAL_CONTENT        = 206,
    /* 3xx */
    LibHTTPC_Status_MULTIPLE_CHOICES       = 300,
    LibHTTPC_Status_MOVED_PERMANENTLY      = 301,
    LibHTTPC_Status_FOUND                  = 302,
    LibHTTPC_Status_SEE_OTHER              = 303,
    LibHTTPC_Status_NOT_MODIFIED           = 304,
    LibHTTPC_Status_USE_PROXY              = 305,
    /* 306 is unused in HTTP/1.1 */
    LibHTTPC_Status_TEMPORARY_REDIRECT     = 307,
    /* 4xx */
    LibHTTPC_Status_BAD_REQUEST            = 400,
    LibHTTPC_Status_UNAUTHORIZED           = 401,
    LibHTTPC_Status_PAYMENT_REQUIRED       = 402,
    LibHTTPC_Status_FORBIDDEN              = 403,
    LibHTTPC_Status_NOT_FOUND              = 404,
    LibHTTPC_Status_METHOD_NOT_ALLOWED     = 405,
    LibHTTPC_Status_NOT_ACCEPTABLE         = 406,
    LibHTTPC_Status_PROXY_AUTH_REQUIRED    = 407,
    LibHTTPC_Status_REQUEST_TIMEOUT        = 408,
    LibHTTPC_Status_CONFLICT               = 409,
    LibHTTPC_Status_GONE                   = 410,
    LibHTTPC_Status_LENGTH_REQUIRED        = 411,
    LibHTTPC_Status_PRECONDITION_FAILED    = 412,
    LibHTTPC_Status_ENTITY_TOO_LARGE       = 413,
    LibHTTPC_Status_URI_TOO_LONG           = 414,
    LibHTTPC_Status_UNSUPPORTED_MEDIA_TYPE = 415,
    LibHTTPC_Status_RANGE_NOT_SATISFIABLE  = 416,
    LibHTTPC_Status_EXPECTATION_FAILED     = 417,
    /* 5xx */
    LibHTTPC_Status_INTERNAL_SERVER_ERROR  = 500,
    LibHTTPC_Status_NOT_IMPLEMENTED        = 501,
    LibHTTPC_Status_BAD_GATEWAY            = 502,
    LibHTTPC_Status_SERVICE_UNAVAILABLE    = 503,
    LibHTTPC_Status_GATEWAY_TIMEOUT        = 504,
    LibHTTPC_Status_HTTP_VER_NOT_SUPPORTED = 505,
};

struct LibHTTPC_Header
{
    const char *name;
    const char *value;
};

/**
 * Struct that contains parsed request.
 * @see LibHTTPC_loadRequest
 */
struct LibHTTPC_Request
{
    char                   *buf;                /**< Pointer to char *request */
    char                   *method;             /**< Request method */
    char                   *uri;                /**< Request URI */
    char                   *version;            /**< HTTP version */
    struct LibHTTPC_Header *headers;            /**< Header array */
    char                   *body;               /**< Pointer to body */
    size_t                  header_count;       /**< Length of header array */
    int                     selfalloc;          /**< Marker if request was allocated by the library */
    int                     header_selfalloc;   /**< Marker if header array was allocated by the library */
};

/**
 * Struct that contains response
 * @see LibHTTPC_dumpResponse
 */
struct LibHTTPC_Response
{
    char                   *buf;                        /**< Pointer to char *response */
    const char             *version;                    /**< HTTP version */
    enum LibHTTPC_Status    status;                     /**< Status code */
    const char             *phrase;                     /**< Phrase describing status code */
    struct LibHTTPC_Header *headers;                    /**< Header array */
    const char             *body;                       /**< Pointer to body */
    size_t                  header_count;               /**< Length of header array */
};

/**
 * Malloc used by the library
 */
extern LibHTTPC_Malloc *LibHTTPC_malloc;
/**
 * Realloc used by the library
 */
extern LibHTTPC_Realloc *LibHTTPC_realloc;
/**
 * Free used by the library
 */
extern LibHTTPC_Free *LibHTTPC_free;

/**
 * Setup LibHTTPC_malloc and LibHTTPC_realloc
 * @param malloc Malloc implementation
 * @param realloc Realloc implementation
 */
void LibHTTPC(LibHTTPC_Malloc *malloc, LibHTTPC_Realloc *realloc, LibHTTPC_Free *free);

/**
 * Parse header name
 */
enum LibHTTPC_HeaderType LibHTTPC_loadHeader(const char *header);
/**
 * Parse method name
 */
enum LibHTTPC_Method LibHTTPC_loadMethod(const char *method);

/**
 * Get name of the header by enum LibHTTPC_Header
 */
const char *LibHTTPC_dumpHeader(enum LibHTTPC_HeaderType header);
/**
 * Get name of the header by enum LibHTTPC_Method
 */
const char *LibHTTPC_dumpMethod(enum LibHTTPC_Method method);
/**
 * Get name of the header by enum LibHTTPC_Status
 */
const char *LibHTTPC_dumpStatus(enum LibHTTPC_Status status);

/**
 * Load request from string. Be careful as this function will destroy buf after
 * run
 * @param[out] request_buf Pointer to buffer where to save request.
 *
 *                         request_buf.header_count should be either set to size of header_names and header_values or to
 *                         -1.
 *
 *                         If (request_buf.header_count == -1), then library will allocate header array itself and set
 *                         request_buf.header_count to amount of headers.
 *
 *                         If (request_buf == NULL), then library will allocate buffer itself using LibHTTPC_malloc and
 *                         LibHTTPC_realloc. It will also set request_buf.header_count to -1.
 *
 *                         If function wants to use malloc or realloc, but they aren't set, it will return NULL.
 * @param[in]  buf         Buffer that contains request to parse
 * @see LibHTTPC
 * @see LibHTTPC_Request_
 */
struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_buf, char *buf);
/**
 * Construct C-String with resopnse that can be sent to client from
 * LibHTTPC_Response struct
 * @param[in]  response Pointer to response
 * @param[out] buf      Pointer to buffer where to save formatted response
 * @param      buf_len  Size of buf
 */
char *LibHTTPC_dumpResponse(struct LibHTTPC_Response *response, char *buf, size_t buf_len);

/**
 * Destructor for LibHTTPC_Request
 * @param request Request to free
 */
void LibHTTPC_Request_(struct LibHTTPC_Request *request);

/**
 * Not implemented yet
 */
struct LibHTTPC_Request *LibHTTPC_readRequest(
    int sockfd,
    struct LibHTTPC_Request *request_buf,
    char *buf, size_t buf_len
);

/**
 * Not implemented yet
 */
int LibHTTPC_writeResponse(int sockfd, struct LibHTTPC_Response *response);

#endif /* __LIBHTTPC_H__ */