example_router_srv.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2019 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /*
  27. * Tests:
  28. *
  29. * # return "Home"
  30. * curl http://localhost:<PORT>/home
  31. * # return "Download"
  32. * curl http://localhost:<PORT>/download
  33. * # return "file: <FILENAME>"
  34. * curl http://localhost:<PORT>/download/<FILENAME>
  35. * # return "About"
  36. * curl http://localhost:<PORT>/about
  37. * # return "404"
  38. * curl http://localhost:<PORT>/other
  39. */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <sagui.h>
  43. /* NOTE: Error checking has been omitted to make it clear. */
  44. struct holder {
  45. struct sg_httpreq *req;
  46. struct sg_httpres *res;
  47. };
  48. static int route_download_file_cb(void *cls, const char *name,
  49. const char *val) {
  50. sprintf(cls, "%s: %s", name, val);
  51. return 0;
  52. }
  53. static void route_home_cb(__SG_UNUSED void *cls, struct sg_route *route) {
  54. struct holder *holder = sg_route_user_data(route);
  55. sg_httpres_send(
  56. holder->res,
  57. "<html><head><title>Home</title></head><body>Home</body></html>",
  58. "text/html", 200);
  59. }
  60. static void route_download_cb(__SG_UNUSED void *cls, struct sg_route *route) {
  61. struct holder *holder = sg_route_user_data(route);
  62. struct sg_str *page = sg_str_new();
  63. char file[256];
  64. memset(file, 0, sizeof(file));
  65. sg_route_vars_iter(route, route_download_file_cb, file);
  66. if (strlen(file) == 0)
  67. strcpy(file, "Download");
  68. sg_str_printf(
  69. page, "<html><head><title>Download</title></head><body>%s</body></html>",
  70. file);
  71. sg_httpres_send(holder->res, sg_str_content(page), "text/html", 200);
  72. sg_str_free(page);
  73. }
  74. static void route_about_cb(__SG_UNUSED void *cls, struct sg_route *route) {
  75. struct holder *holder = sg_route_user_data(route);
  76. sg_httpres_send(
  77. holder->res,
  78. "<html><head><title>About</title></head><body>About</body></html>",
  79. "text/html", 200);
  80. }
  81. static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req,
  82. struct sg_httpres *res) {
  83. struct sg_router *router = cls;
  84. struct holder holder = {req, res};
  85. if (sg_router_dispatch(router, sg_httpreq_path(req), &holder) != 0)
  86. sg_httpres_send(
  87. res, "<html><head><title>Not found</title></head><body>404</body></html>",
  88. "text/html", 404);
  89. }
  90. int main(void) {
  91. struct sg_route *routes = NULL;
  92. struct sg_router *router;
  93. struct sg_httpsrv *srv;
  94. sg_routes_add(&routes, "/home", route_home_cb, NULL);
  95. sg_routes_add(&routes, "/download", route_download_cb, NULL);
  96. sg_routes_add(&routes, "/download/(?P<file>[a-z]+)", route_download_cb, NULL);
  97. sg_routes_add(&routes, "/about", route_about_cb, NULL);
  98. router = sg_router_new(routes);
  99. srv = sg_httpsrv_new(req_cb, router);
  100. if (!sg_httpsrv_listen(srv, 0 /* 0 = port chosen randomly */, false)) {
  101. sg_httpsrv_free(srv);
  102. return EXIT_FAILURE;
  103. }
  104. fprintf(stdout, "Server running at http://localhost:%d\n",
  105. sg_httpsrv_port(srv));
  106. fflush(stdout);
  107. getchar();
  108. sg_httpsrv_free(srv);
  109. sg_routes_cleanup(&routes);
  110. sg_router_free(router);
  111. return EXIT_SUCCESS;
  112. }