Added mg_set_auth_handler()

This commit is contained in:
Sergey Lyubka committed 2014-01-24 20:49:57 +00:00
1 parent 2392156fd4
commit f977757a9d
4 files changed
+21 -22

No files matched your search

+8 -21
View File
@@ -2,38 +2,25 @@
#include <string.h>
#include "mongoose.h"
static int index_html(struct mg_connection *conn) {
mg_send_header(conn, "Content-Type", "text/html");
mg_printf_data(conn, "%s",
"This link is password-protected: <a href=/secret>link</a>");
return 1;
}
static int secret_html(struct mg_connection *conn) {
static const char *passwords_file = "my_passwords.txt";
FILE *fp = fopen(passwords_file, "r");
static int auth_handler(struct mg_connection *conn) {
int result = 0; // Not authorized
FILE *fp;
// To populate passwords file, do
// mongoose -A my_passwords.txt mydomain.com admin admin
if (mg_authorize_digest(conn, fp)) {
mg_printf_data(conn, "%s", "Hi, here is a secret message!");
} else {
mg_send_digest_auth_request(conn);
}
if (fp != NULL) {
if ((fp = fopen("my_passwords.txt", "r")) != NULL) {
result = mg_authorize_digest(conn, fp);
fclose(fp);
}
return 1;
return result;
}
int main(void) {
struct mg_server *server = mg_create_server(NULL);
mg_set_option(server, "listening_port", "8080");
mg_add_uri_handler(server, "/", index_html);
mg_add_uri_handler(server, "/secret", secret_html);
mg_set_option(server, "document_root", ".");
mg_set_auth_handler(server, auth_handler);
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {