example_httpauth.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. static bool strmatch(const char *s1, const char *s2) {
  33. if (!s1 || !s2)
  34. return false;
  35. return strcmp(s1, s2) == 0;
  36. }
  37. static bool auth_cb(__SG_UNUSED void *cls, struct sg_httpauth *auth,
  38. __SG_UNUSED struct sg_httpreq *req,
  39. __SG_UNUSED struct sg_httpres *res) {
  40. bool pass;
  41. sg_httpauth_set_realm(auth, "My realm");
  42. pass = strmatch(sg_httpauth_usr(auth), "abc") &&
  43. strmatch(sg_httpauth_pwd(auth), "123");
  44. if (!pass)
  45. sg_httpauth_deny(auth,
  46. "<html><head><title>Denied</title></head><body><font "
  47. "color=\"red\">Go away</font></body></html>",
  48. "text/html; charset=utf-8");
  49. return pass;
  50. }
  51. static void err_cb(__SG_UNUSED void *cls, const char *err) {
  52. fprintf(stderr, "%s", err);
  53. fflush(stderr);
  54. }
  55. static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req,
  56. struct sg_httpres *res) {
  57. sg_httpres_send(res,
  58. "<html><head><title>Secret</title></head><body><font "
  59. "color=\"green\">Secret page</font></body></html>",
  60. "text/html; charset=utf-8", 200);
  61. }
  62. int main(int argc, const char *argv[]) {
  63. struct sg_httpsrv *srv;
  64. uint16_t port;
  65. if (argc != 2) {
  66. printf("%s <PORT>\n", argv[0]);
  67. return EXIT_FAILURE;
  68. }
  69. port = strtol(argv[1], NULL, 10);
  70. srv = sg_httpsrv_new2(auth_cb, req_cb, err_cb, NULL);
  71. if (!sg_httpsrv_listen(srv, port, false)) {
  72. sg_httpsrv_free(srv);
  73. return EXIT_FAILURE;
  74. }
  75. fprintf(stdout, "Server running at http://localhost:%d\n",
  76. sg_httpsrv_port(srv));
  77. fflush(stdout);
  78. getchar();
  79. sg_httpsrv_free(srv);
  80. return EXIT_SUCCESS;
  81. }