forked from cozis/xHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
46 lines (39 loc) · 868 Bytes
/
example.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
// Build with:
// $ gcc example.c xhttp.c -o example
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include "xhttp.h"
static xh_handle handle;
static void callback(xh_request *req, xh_response *res, void *userp)
{
(void) req;
(void) userp;
res->status = 200;
if(!strcmp(req->URL.str, "/file"))
res->file = "example.c";
else
res->body.str = "Hello, world!";
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;
}