forked from cozis/xHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample2.c
64 lines (51 loc) · 1.46 KB
/
example2.c
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
// Build with:
// $ gcc example2.c xhttp.c -o example2
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include "xhttp.h"
static xh_handle handle;
static char buffer[1024];
static void callback(xh_request *req, xh_response *res, void *userp)
{
(void) req;
(void) userp;
int post;
char username[32];
if(!xh_urlcmp(req->URL.str, "/users/:s",
sizeof(username), username)) {
snprintf(buffer, sizeof(buffer), "Hello, %s!\n", username);
res->status = 200;
res->body.str = buffer;
} else if(!xh_urlcmp(req->URL.str, "/users/:s/posts/:d",
sizeof(username), username, &post)) {
snprintf(buffer, sizeof(buffer), "Hello, %s! You asked for post no. %d!\n", username, post);
res->status = 200;
res->body.str = buffer;
} else {
res->status = 404;
res->body.str = "It seems like what you're looking for isn't here! :S";
}
xh_header_add(res, "Content-Type", "text/plain");
}
static void handle_sigterm(int signum)
{
(void) signum;
xh_quit(handle);
}
int main()
{
signal(SIGTERM, handle_sigterm);
signal(SIGQUIT, handle_sigterm);
signal(SIGINT, handle_sigterm);
const char *error = xhttp(NULL, 8080, callback,
NULL, &handle, NULL);
if(error != NULL)
{
fprintf(stderr, "ERROR: %s\n", error);
return 1;
}
fprintf(stderr, "OK\n");
return 0;
}