/* _ * ___ __ _ __ _ _ _(_) * / __|/ _` |/ _` | | | | | * \__ \ (_| | (_| | |_| | | * |___/\__,_|\__, |\__,_|_| * |___/ * * Cross-platform library which helps to develop web servers or frameworks. * * Copyright (C) 2016-2019 Silvio Clecio * * 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:/home * # return "Download" * curl http://localhost:/download * # return "file: " * curl http://localhost:/download/ * # return "About" * curl http://localhost:/about * # return "404" * curl http://localhost:/other */ #include #include #include /* 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, "HomeHome", "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, "Download%s", 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, "AboutAbout", "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, "Not found404", "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[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; }