int answer_to_connection( void* cls, struct MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { char* user; char* pass; int authorized; struct MHD_Response* response; int ret; if (strncmp(method, "GET", 4) != 0) { return MHD_NO; } if (*con_cls == NULL) { *con_cls = connection; return MHD_YES; } pass = NULL; user = MHD_basic_auth_get_username_password(connection, &pass); authorized = (user != NULL) && (strncmp(user, USER, strlen(USER)) == 0) && (strncmp(pass, PASSWORD, strlen(PASSWORD)) == 0); if (user != NULL) { free(user); } if (pass != NULL) { free(pass); } if (authorized) { const char* page = "<html><body>Authorized!</body></html>"; response = MHD_create_response_from_buffer( strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); } else { const char* page = "<html><body>Unauthorized!</body></html>"; response = MHD_create_response_from_buffer( strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_basic_auth_fail_response( connection, "Tutorial Example 5 Realm", response); } MHD_destroy_response(response); return ret; }
int http_response::enqueue_response_basic( MHD_Connection* connection, MHD_Response* response ) { return MHD_queue_basic_auth_fail_response( connection, realm.c_str(), response ); }
static int ahc_echo (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { static int aptr; const char *me = cls; struct MHD_Response *response; int ret; char *user; char *pass; int fail; if (0 != strcmp (method, "GET")) return MHD_NO; /* unexpected method */ if (&aptr != *ptr) { /* do never respond on first call */ *ptr = &aptr; return MHD_YES; } *ptr = NULL; /* reset when done */ /* require: "Aladdin" with password "open sesame" */ pass = NULL; user = MHD_basic_auth_get_username_password (connection, &pass); fail = ( (user == NULL) || (0 != strcmp (user, "Aladdin")) || (0 != strcmp (pass, "open sesame") ) ); if (fail) { response = MHD_create_response_from_buffer (strlen (DENIED), (void *) DENIED, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response); } else { response = MHD_create_response_from_buffer (strlen (me), (void *) me, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); } if (NULL != user) free (user); if (NULL != pass) free (pass); MHD_destroy_response (response); return ret; }
static int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { char *user; char *pass; int fail; int ret; struct MHD_Response *response; if (0 != strcmp (method, "GET")) return MHD_NO; if (NULL == *con_cls) { *con_cls = connection; return MHD_YES; } pass = NULL; user = MHD_basic_auth_get_username_password (connection, &pass); fail = ( (user == NULL) || (0 != strcmp (user, "root")) || (0 != strcmp (pass, "pa$$w0rd") ) ); if (user != NULL) free (user); if (pass != NULL) free (pass); if (fail) { const char *page = "<html><body>Go away.</body></html>"; response = MHD_create_response_from_buffer (strlen (page), (void *) page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_basic_auth_fail_response (connection, "my realm", response); } else { const char *page = "<html><body>A secret.</body></html>"; response = MHD_create_response_from_buffer (strlen (page), (void *) page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); } MHD_destroy_response (response); return ret; }
int CWebServer::AskForAuthentication(const HTTPRequest& request) const { struct MHD_Response *response = create_response(0, nullptr, MHD_NO, MHD_NO); if (!response) { CLog::Log(LOGERROR, "CWebServer[%hu]: unable to create HTTP Unauthorized response", m_port); return MHD_NO; } int ret = AddHeader(response, MHD_HTTP_HEADER_CONNECTION, "close"); if (!ret) { CLog::Log(LOGERROR, "CWebServer[%hu]: unable to prepare HTTP Unauthorized response", m_port); MHD_destroy_response(response); return MHD_NO; } LogResponse(request, MHD_HTTP_UNAUTHORIZED); ret = MHD_queue_basic_auth_fail_response(request.connection, "XBMC", response); MHD_destroy_response(response); return ret; }