-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathexample_router_srv.c
More file actions
122 lines (112 loc) · 3.91 KB
/
Copy pathexample_router_srv.c
File metadata and controls
122 lines (112 loc) · 3.91 KB
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
/* _
* ___ __ _ __ _ _ _(_)
* / __|/ _` |/ _` | | | | |
* \__ \ (_| | (_| | |_| | |
* |___/\__,_|\__, |\__,_|_|
* |___/
*
* Cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (C) 2016-2019 Silvio Clecio <silvioprog@gmail.com>
*
* Sagui library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Sagui library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sagui library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Tests:
*
* # return "Home"
* curl http://localhost:<PORT>/home
* # return "Download"
* curl http://localhost:<PORT>/download
* # return "file: <FILENAME>"
* curl http://localhost:<PORT>/download/<FILENAME>
* # return "About"
* curl http://localhost:<PORT>/about
* # return "404"
* curl http://localhost:<PORT>/other
*/
#include <stdio.h>
#include <stdlib.h>
#include <sagui.h>
/* NOTE: Error checking has been omitted to make it clear. */
struct holder {
struct sg_httpreq *req;
struct sg_httpres *res;
};
static int route_download_file_cb(void *cls, const char *name,
const char *val) {
sprintf(cls, "%s: %s", name, val);
return 0;
}
static void route_home_cb(__SG_UNUSED void *cls, struct sg_route *route) {
struct holder *holder = sg_route_user_data(route);
sg_httpres_send(
holder->res,
"<html><head><title>Home</title></head><body>Home</body></html>",
"text/html", 200);
}
static void route_download_cb(__SG_UNUSED void *cls, struct sg_route *route) {
struct holder *holder = sg_route_user_data(route);
struct sg_str *page = sg_str_new();
char file[256];
memset(file, 0, sizeof(file));
sg_route_vars_iter(route, route_download_file_cb, file);
if (strlen(file) == 0)
strcpy(file, "Download");
sg_str_printf(
page, "<html><head><title>Download</title></head><body>%s</body></html>",
file);
sg_httpres_send(holder->res, sg_str_content(page), "text/html", 200);
sg_str_free(page);
}
static void route_about_cb(__SG_UNUSED void *cls, struct sg_route *route) {
struct holder *holder = sg_route_user_data(route);
sg_httpres_send(
holder->res,
"<html><head><title>About</title></head><body>About</body></html>",
"text/html", 200);
}
static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req,
struct sg_httpres *res) {
struct sg_router *router = cls;
struct holder holder = {req, res};
if (sg_router_dispatch(router, sg_httpreq_path(req), &holder) != 0)
sg_httpres_send(
res, "<html><head><title>Not found</title></head><body>404</body></html>",
"text/html", 404);
}
int main(void) {
struct sg_route *routes = NULL;
struct sg_router *router;
struct sg_httpsrv *srv;
sg_routes_add(&routes, "/home", route_home_cb, NULL);
sg_routes_add(&routes, "/download", route_download_cb, NULL);
sg_routes_add(&routes, "/download/(?P<file>[a-z]+)", route_download_cb, NULL);
sg_routes_add(&routes, "/about", route_about_cb, NULL);
router = sg_router_new(routes);
srv = sg_httpsrv_new(req_cb, router);
if (!sg_httpsrv_listen(srv, 0 /* 0 = port chosen randomly */, false)) {
sg_httpsrv_free(srv);
return EXIT_FAILURE;
}
fprintf(stdout, "Server running at http://localhost:%d\n",
sg_httpsrv_port(srv));
fflush(stdout);
getchar();
sg_httpsrv_free(srv);
sg_routes_cleanup(&routes);
sg_router_free(router);
return EXIT_SUCCESS;
}