Sagui library v3.5.0
Cross-platform library which helps to develop web servers or frameworks.
Loading...
Searching...
No Matches
example_router_srv.c

Simple example showing how to use the router feature in a HTTP server.

/* _
* ___ __ _ __ _ _ _(_)
* / __|/ _` |/ _` | | | | |
* \__ \ (_| | (_| | |_| | |
* |___/\__,_|\__, |\__,_|_|
* |___/
*
* 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);
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");
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);
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)
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)) {
return EXIT_FAILURE;
}
fprintf(stdout, "Server running at http://localhost:%d\n",
fflush(stdout);
getchar();
sg_router_free(router);
return EXIT_SUCCESS;
}
struct sg_httpsrv * sg_httpsrv_new(sg_httpreq_cb cb, void *cls) __attribute__((malloc))
void sg_httpsrv_free(struct sg_httpsrv *srv)
const char * sg_httpreq_path(struct sg_httpreq *req)
#define sg_httpres_send(res, val, content_type, status)
Definition sagui.h:1032
bool sg_httpsrv_listen(struct sg_httpsrv *srv, uint16_t port, bool threaded)
uint16_t sg_httpsrv_port(struct sg_httpsrv *srv)
int sg_router_dispatch(struct sg_router *router, const char *path, void *user_data)
int sg_routes_cleanup(struct sg_route **routes)
void * sg_route_user_data(struct sg_route *route)
struct sg_router * sg_router_new(struct sg_route *routes) __attribute__((malloc))
bool sg_routes_add(struct sg_route **routes, const char *pattern, sg_route_cb cb, void *cls)
void sg_router_free(struct sg_router *router)
int sg_route_vars_iter(struct sg_route *route, sg_vars_iter_cb cb, void *cls)
int const char * sg_str_content(struct sg_str *str)
struct sg_str * sg_str_new(void) __attribute__((malloc))
void sg_str_free(struct sg_str *str)
int sg_str_printf(struct sg_str *str, const char *fmt,...) __attribute__((format(printf