2
0

example_httpcookie.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include <sagui.h>
  31. /* NOTE: Error checking has been omitted to make it clear. */
  32. #define CONTENT_TYPE "text/html; charset=utf-8"
  33. #define BEGIN_PAGE "<html><head><title>Cookies</title></head><body>"
  34. #define END_PAGE "</body></html>"
  35. #define INITIAL_PAGE BEGIN_PAGE "Use F5 to refresh this page ..." END_PAGE
  36. #define COUNT_PAGE BEGIN_PAGE "Refresh number: %d" END_PAGE
  37. #define COOKIE_NAME "refresh_count"
  38. static int strtoint(const char *str) {
  39. if (!str)
  40. return 0;
  41. return (int) strtol(str, NULL, 10);
  42. }
  43. static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req,
  44. struct sg_httpres *res) {
  45. struct sg_strmap **cookies;
  46. char str[100];
  47. int count;
  48. if (strcmp(sg_httpreq_path(req), "/favicon.ico") == 0) {
  49. sg_httpres_send(res, "", "", 204);
  50. return;
  51. }
  52. cookies = sg_httpreq_cookies(req);
  53. count = cookies ? strtoint(sg_strmap_get(*cookies, COOKIE_NAME)) : 0;
  54. if (count == 0) {
  55. snprintf(str, sizeof(str), INITIAL_PAGE);
  56. count = 1;
  57. } else {
  58. snprintf(str, sizeof(str), COUNT_PAGE, count);
  59. count++;
  60. }
  61. sg_httpres_send(res, str, CONTENT_TYPE, 200);
  62. snprintf(str, sizeof(str), "%d", count);
  63. sg_httpres_set_cookie(res, COOKIE_NAME, str);
  64. }
  65. int main(int argc, const char *argv[]) {
  66. struct sg_httpsrv *srv;
  67. uint16_t port;
  68. if (argc != 2) {
  69. printf("%s <PORT>\n", argv[0]);
  70. return EXIT_FAILURE;
  71. }
  72. port = strtol(argv[1], NULL, 10);
  73. srv = sg_httpsrv_new(req_cb, NULL);
  74. if (!sg_httpsrv_listen(srv, port, false)) {
  75. sg_httpsrv_free(srv);
  76. return EXIT_FAILURE;
  77. }
  78. fprintf(stdout, "Server running at http://localhost:%d\n",
  79. sg_httpsrv_port(srv));
  80. fflush(stdout);
  81. getchar();
  82. sg_httpsrv_free(srv);
  83. return EXIT_SUCCESS;
  84. }