Added auth example

This commit is contained in:
Sergey Lyubka committed 2014-01-13 17:58:14 +00:00
1 parent 6b4f7e785d
commit cc636197bc
4 files changed
+53 -3

No files matched your search

+2
View File
@@ -24,6 +24,7 @@ all:
$(CC) post.c ../mongoose.c -o post $(CFLAGS)
$(CC) multi_threaded.c ../mongoose.c -o multi_threaded $(CFLAGS)
$(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
$(CC) auth.c ../mongoose.c -o auth $(CFLAGS)
# $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
# $(CC) chat.c ../mongoose.c -o chat $(CFLAGS)
@@ -41,6 +42,7 @@ windows:
$(CL) post.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
$(CL) multi_threaded.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
$(CL) upload.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
$(CL) auth.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
# $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
#$(CL) lua_dll.c $(CLFLAGS) $(DLL_FLAGS) /DLL $(LFLAGS) /SUBSYSTEM:WINDOWS /ENTRY:luaopen_lua_dll /EXPORT:luaopen_lua_dll /out:lua_dll.dll
+45
View File
@@ -0,0 +1,45 @@
#include <stdio.h>
#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");
// 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) {
fclose(fp);
}
return 1;
}
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);
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {
mg_poll_server(server, 1000);
}
mg_destroy_server(&server);
return 0;
}