authorization_example.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2008 Christian Grothoff (and other contributing authors)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. /**
  17. * @file authorization_example.c
  18. * @brief example for how to use libmicrohttpd with HTTP authentication
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include <microhttpd.h>
  23. #ifdef _WIN32
  24. #ifndef WIN32_LEAN_AND_MEAN
  25. #define WIN32_LEAN_AND_MEAN 1
  26. #endif /* !WIN32_LEAN_AND_MEAN */
  27. #include <windows.h>
  28. #endif
  29. #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
  30. #define DENIED "<html><head><title>Access denied</title></head><body>Access denied</body></html>"
  31. static int
  32. ahc_echo (void *cls,
  33. struct MHD_Connection *connection,
  34. const char *url,
  35. const char *method,
  36. const char *version,
  37. const char *upload_data, size_t *upload_data_size, void **ptr)
  38. {
  39. static int aptr;
  40. const char *me = cls;
  41. struct MHD_Response *response;
  42. int ret;
  43. char *user;
  44. char *pass;
  45. int fail;
  46. (void)url; /* Unused. Silent compiler warning. */
  47. (void)version; /* Unused. Silent compiler warning. */
  48. (void)upload_data; /* Unused. Silent compiler warning. */
  49. (void)upload_data_size; /* Unused. Silent compiler warning. */
  50. if (0 != strcmp (method, "GET"))
  51. return MHD_NO; /* unexpected method */
  52. if (&aptr != *ptr)
  53. {
  54. /* do never respond on first call */
  55. *ptr = &aptr;
  56. return MHD_YES;
  57. }
  58. *ptr = NULL; /* reset when done */
  59. /* require: "Aladdin" with password "open sesame" */
  60. pass = NULL;
  61. user = MHD_basic_auth_get_username_password (connection,
  62. &pass);
  63. fail = ( (NULL == user) ||
  64. (0 != strcmp (user, "Aladdin")) ||
  65. (0 != strcmp (pass, "open sesame") ) );
  66. if (fail)
  67. {
  68. response = MHD_create_response_from_buffer (strlen (DENIED),
  69. (void *) DENIED,
  70. MHD_RESPMEM_PERSISTENT);
  71. ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response);
  72. }
  73. else
  74. {
  75. response = MHD_create_response_from_buffer (strlen (me),
  76. (void *) me,
  77. MHD_RESPMEM_PERSISTENT);
  78. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  79. }
  80. if (NULL != user)
  81. MHD_free (user);
  82. if (NULL != pass)
  83. MHD_free (pass);
  84. MHD_destroy_response (response);
  85. return ret;
  86. }
  87. int
  88. main (int argc, char *const *argv)
  89. {
  90. struct MHD_Daemon *d;
  91. unsigned int port;
  92. if ( (argc != 2) ||
  93. (1 != sscanf (argv[1], "%u", &port)) ||
  94. (UINT16_MAX < port) )
  95. {
  96. fprintf (stderr,
  97. "%s PORT\n", argv[0]);
  98. return 1;
  99. }
  100. d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
  101. atoi (argv[1]),
  102. NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
  103. if (d == NULL)
  104. return 1;
  105. fprintf (stderr, "HTTP server running. Press ENTER to stop the server\n");
  106. (void) getc (stdin);
  107. MHD_stop_daemon (d);
  108. return 0;
  109. }