Added mg_set_auth_handler()

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

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 (;;) {

View File

@ -273,6 +273,7 @@ struct mg_server {
struct ll active_connections;
struct ll uri_handlers;
mg_handler_t error_handler;
mg_handler_t auth_handler;
char *config_options[NUM_OPTIONS];
void *server_data;
#ifdef MONGOOSE_USE_SSL
@ -3358,6 +3359,13 @@ static void open_local_endpoint(struct connection *conn) {
const char *dir_lst = conn->server->config_options[ENABLE_DIRECTORY_LISTING];
#endif
// Call auth handler
if (conn->server->auth_handler != NULL &&
conn->server->auth_handler(&conn->mg_conn) == 0) {
mg_send_digest_auth_request(&conn->mg_conn);
return;
}
// Call URI handler if one is registered for this URI
conn->endpoint.uh = find_uri_handler(conn->server, conn->mg_conn.uri);
if (conn->endpoint.uh != NULL) {
@ -4117,6 +4125,10 @@ void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) {
server->error_handler = handler;
}
void mg_set_auth_handler(struct mg_server *server, mg_handler_t handler) {
server->auth_handler = handler;
}
void mg_set_listening_socket(struct mg_server *server, int sock) {
if (server->listening_sock != INVALID_SOCKET) {
closesocket(server->listening_sock);

View File

@ -65,6 +65,7 @@ const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
unsigned int mg_poll_server(struct mg_server *, int milliseconds);
void mg_add_uri_handler(struct mg_server *, const char *uri, mg_handler_t);
void mg_set_http_error_handler(struct mg_server *, mg_handler_t);
void mg_set_auth_handler(struct mg_server *, mg_handler_t);
const char **mg_get_valid_option_names(void);
const char *mg_get_option(const struct mg_server *server, const char *name);
void mg_set_listening_socket(struct mg_server *, int sock);

View File

@ -475,7 +475,6 @@ static int cb4(struct mg_connection *conn) {
}
static int cb3(struct mg_connection *conn) {
printf("cb3: %d\n", conn->status_code);
fflush(stdout);
sprintf((char *) conn->connection_param, "%d", conn->status_code);
return 1;