Commit f977757a authored by Sergey Lyubka's avatar Sergey Lyubka

Added mg_set_auth_handler()

parent 2392156f
...@@ -2,38 +2,25 @@ ...@@ -2,38 +2,25 @@
#include <string.h> #include <string.h>
#include "mongoose.h" #include "mongoose.h"
static int index_html(struct mg_connection *conn) { static int auth_handler(struct mg_connection *conn) {
mg_send_header(conn, "Content-Type", "text/html"); int result = 0; // Not authorized
mg_printf_data(conn, "%s", FILE *fp;
"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");
// To populate passwords file, do // To populate passwords file, do
// mongoose -A my_passwords.txt mydomain.com admin admin // mongoose -A my_passwords.txt mydomain.com admin admin
if ((fp = fopen("my_passwords.txt", "r")) != NULL) {
if (mg_authorize_digest(conn, fp)) { result = 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) {
fclose(fp); fclose(fp);
} }
return 1; return result;
} }
int main(void) { int main(void) {
struct mg_server *server = mg_create_server(NULL); struct mg_server *server = mg_create_server(NULL);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_add_uri_handler(server, "/", index_html); mg_set_option(server, "document_root", ".");
mg_add_uri_handler(server, "/secret", secret_html); mg_set_auth_handler(server, auth_handler);
printf("Starting on port %s\n", mg_get_option(server, "listening_port")); printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) { for (;;) {
......
...@@ -273,6 +273,7 @@ struct mg_server { ...@@ -273,6 +273,7 @@ struct mg_server {
struct ll active_connections; struct ll active_connections;
struct ll uri_handlers; struct ll uri_handlers;
mg_handler_t error_handler; mg_handler_t error_handler;
mg_handler_t auth_handler;
char *config_options[NUM_OPTIONS]; char *config_options[NUM_OPTIONS];
void *server_data; void *server_data;
#ifdef MONGOOSE_USE_SSL #ifdef MONGOOSE_USE_SSL
...@@ -3358,6 +3359,13 @@ static void open_local_endpoint(struct connection *conn) { ...@@ -3358,6 +3359,13 @@ static void open_local_endpoint(struct connection *conn) {
const char *dir_lst = conn->server->config_options[ENABLE_DIRECTORY_LISTING]; const char *dir_lst = conn->server->config_options[ENABLE_DIRECTORY_LISTING];
#endif #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 // Call URI handler if one is registered for this URI
conn->endpoint.uh = find_uri_handler(conn->server, conn->mg_conn.uri); conn->endpoint.uh = find_uri_handler(conn->server, conn->mg_conn.uri);
if (conn->endpoint.uh != NULL) { if (conn->endpoint.uh != NULL) {
...@@ -4117,6 +4125,10 @@ void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) { ...@@ -4117,6 +4125,10 @@ void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) {
server->error_handler = 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) { void mg_set_listening_socket(struct mg_server *server, int sock) {
if (server->listening_sock != INVALID_SOCKET) { if (server->listening_sock != INVALID_SOCKET) {
closesocket(server->listening_sock); closesocket(server->listening_sock);
......
...@@ -65,6 +65,7 @@ const char *mg_set_option(struct mg_server *, const char *opt, const char *val); ...@@ -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); 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_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_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_valid_option_names(void);
const char *mg_get_option(const struct mg_server *server, const char *name); const char *mg_get_option(const struct mg_server *server, const char *name);
void mg_set_listening_socket(struct mg_server *, int sock); void mg_set_listening_socket(struct mg_server *, int sock);
......
...@@ -475,7 +475,6 @@ static int cb4(struct mg_connection *conn) { ...@@ -475,7 +475,6 @@ static int cb4(struct mg_connection *conn) {
} }
static int cb3(struct mg_connection *conn) { static int cb3(struct mg_connection *conn) {
printf("cb3: %d\n", conn->status_code);
fflush(stdout); fflush(stdout);
sprintf((char *) conn->connection_param, "%d", conn->status_code); sprintf((char *) conn->connection_param, "%d", conn->status_code);
return 1; return 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment