-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslughttp.h
39 lines (32 loc) · 1.08 KB
/
slughttp.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
#include <stdio.h>
#include <stdbool.h>
typedef struct ResponseObj *Response;
typedef struct RequestObj *Request;
typedef struct ServerObj* Server;
typedef void(*Handler)(Request, Response);
// A String wrapper is helpful for passing around slices of buffers.
// A macro CString is provided to turn constant char arrays into Strings.
typedef struct String {
char *chars;
size_t len;
} String;
#define CString(X) (String){.chars=X,.len=sizeof(X)-1}
#define resp_write(R, ...) do{\
int _bytes_written = snprintf(resp_buf(R), resp_nleft(R), __VA_ARGS__);\
resp_wrote_n(R, _bytes_written);\
}while(0)
char *resp_buf(Response r);
int resp_nleft(Response r);
void resp_wrote_n(Response r, size_t wrote);
void resp_status(Response r, int n);
String req_path(Request r);
String req_post_arg(Request r, String arg_name);
typedef struct ServerOpts {
bool multiprocess;
bool handle_sigint;
bool enable_logging;
} ServerOpts;
Server new_server(int port, struct ServerOpts opts);
int handle_path(Server s, const char *path, Handler h);
Handler get_handler(Server s, String path);
void serve_forever(Server s);