-
Notifications
You must be signed in to change notification settings - Fork 0
/
httplib.h
129 lines (113 loc) · 4.19 KB
/
httplib.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
#ifndef SIMPLEHTTPLIB_HTTPLIB_H
#define SIMPLEHTTPLIB_HTTPLIB_H
#include <stddef.h>
/**
* Represents a HTTP request method.
*/
typedef enum http_method_enum {
GET, POST, PUT, DELETE, PATCH, HEAD, CONNECT, OPTIONS, TRACE
} http_method;
/**
* Represents a HTTP header. Contains the header field key, the value and a pointer
* to the next header to form a linked list.
* All pointers of this struct must point to allocated memory or 0.
*/
typedef struct http_header_struct {
char * key;
char * value;
struct http_header_struct * next;
} http_header;
/**
* Represents a HTTP request. Contains the request method, the HTTP path, a linked list
* of headers, the query string (optional, without leading '?'), the request body (optional)
* and the content length of the body (0 if there is no body).
* All pointers of this struct must point to allocated memory or 0.
*/
typedef struct http_request_struct {
http_method method;
char * path;
http_header * headers;
char * query;
size_t content_length;
char * body;
} http_request;
/**
* Represents a HTTP response. Contains the HTTP status code, the status message,
* a linked list of headers, the response body (optional) and the content length
* of the body (0 if there is no body).
* All pointers of this struct must point to allocated memory or 0.
*/
typedef struct http_response_struct {
int status_code;
char * status_message;
http_header * headers;
size_t content_length;
char * body;
} http_response;
/**
* Function pointer type for the HTTP request handler function.
*/
typedef void (*http_request_handler) (http_request * req, http_response * res_out);
/**
* Used to start the HTTP server.
* @param port the port on which the server should listen
* @param handler a function pointer to the request handler function
* @return a negative integer, if the server exited with an error
*/
int httplib_start_server(int port, http_request_handler handler);
/**
* Helper function to add a header to a HTTP response.
* @param self a pointer to the response struct
* @param key the header field key
* @param value the header value
* @return 0 if the header was set, a negative value if the header was not valid
*/
int httplib_response_add_header(http_response * self, char * key, char * value);
/**
* Helper function to set the content length and to allocate the memory
* for the body of a response struct
* @param self a pointer to the response struct
* @param length the content length
*/
void httplib_response_set_content_length(http_response * self, size_t length);
/**
* Helper function to set the response status of a response struct
* @param self a pointer to the response struct
* @param code the status code
* @param message the status message
*/
void httplib_response_set_status(http_response * self, int code, char * message);
/**
* Helper function to get a header value from a HTTP.
* Returned value is allocated memory and needs to be freed, if not null.
* @param self a pointer to the request struct
* @param key the header field key
* @return the value of the header or null, if the header does not exist in the request
*/
char * httplib_request_get_header(http_request * self, char * key);
/**
* Helper function to get a query value from a HTTP request.
* Returned value is allocated memory and needs to be freed, if not null.
* @param self a pointer to the request struct
* @param key the query key
* @return the value of the query or null, if the query key does not exist in the request
*/
char * httplib_request_get_query_value(http_request * self, char * key);
/**
* Helper function to check if a header is valid.
* @param key the header field key
* @param value the header value
* @return true if the header is valid, otherwise false
*/
int httplib_is_valid_header(const char * key, const char * value);
/**
* Helper function to decode a percent encoded character.
* @param encoded a pointer to the first of the two characters in the string
* after the percent symbol, which make up the encoded character
* @return the decoded character
*/
char httplib_decode_hex(const char * encoded);
#define SOCKET_ERROR -1
#define BIND_ERROR -2
#define LISTEN_ERROR -3
#endif //SIMPLEHTTPLIB_HTTPLIB_H