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