basicauthentication.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Feel free to use this example code in any way
  2. you see fit (Public Domain) */
  3. #include <sys/types.h>
  4. #ifndef _WIN32
  5. #include <sys/select.h>
  6. #include <sys/socket.h>
  7. #else
  8. #include <winsock2.h>
  9. #endif
  10. #include <microhttpd.h>
  11. #include <time.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #define PORT 8888
  16. static enum MHD_Result
  17. answer_to_connection (void *cls, struct MHD_Connection *connection,
  18. const char *url, const char *method,
  19. const char *version, const char *upload_data,
  20. size_t *upload_data_size, void **req_cls)
  21. {
  22. struct MHD_BasicAuthInfo *auth_info;
  23. enum MHD_Result ret;
  24. struct MHD_Response *response;
  25. (void) cls; /* Unused. Silent compiler warning. */
  26. (void) url; /* Unused. Silent compiler warning. */
  27. (void) version; /* Unused. Silent compiler warning. */
  28. (void) upload_data; /* Unused. Silent compiler warning. */
  29. (void) upload_data_size; /* Unused. Silent compiler warning. */
  30. if (0 != strcmp (method, "GET"))
  31. return MHD_NO;
  32. if (NULL == *req_cls)
  33. {
  34. *req_cls = connection;
  35. return MHD_YES;
  36. }
  37. auth_info = MHD_basic_auth_get_username_password3 (connection);
  38. if (NULL == auth_info)
  39. {
  40. static const char *page =
  41. "<html><body>Authorization required</body></html>";
  42. response = MHD_create_response_from_buffer_static (strlen (page), page);
  43. ret = MHD_queue_basic_auth_required_response3 (connection,
  44. "admins",
  45. MHD_YES,
  46. response);
  47. }
  48. else if ((strlen ("root") != auth_info->username_len) ||
  49. (0 != memcmp (auth_info->username, "root",
  50. auth_info->username_len)) ||
  51. /* The next check against NULL is optional,
  52. * if 'password' is NULL then 'password_len' is always zero. */
  53. (NULL == auth_info->password) ||
  54. (strlen ("pa$$w0rd") != auth_info->password_len) ||
  55. (0 != memcmp (auth_info->password, "pa$$w0rd",
  56. auth_info->password_len)))
  57. {
  58. static const char *page =
  59. "<html><body>Wrong username or password</body></html>";
  60. response = MHD_create_response_from_buffer_static (strlen (page), page);
  61. ret = MHD_queue_basic_auth_required_response3 (connection,
  62. "admins",
  63. MHD_YES,
  64. response);
  65. }
  66. else
  67. {
  68. static const char *page = "<html><body>A secret.</body></html>";
  69. response = MHD_create_response_from_buffer_static (strlen (page), page);
  70. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  71. }
  72. if (NULL != auth_info)
  73. MHD_free (auth_info);
  74. MHD_destroy_response (response);
  75. return ret;
  76. }
  77. int
  78. main (void)
  79. {
  80. struct MHD_Daemon *daemon;
  81. daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,
  82. &answer_to_connection, NULL, MHD_OPTION_END);
  83. if (NULL == daemon)
  84. return 1;
  85. (void) getchar ();
  86. MHD_stop_daemon (daemon);
  87. return 0;
  88. }