Skip to content
Snippets Groups Projects
Commit ea9a60a5 authored by Sergey Lyubka's avatar Sergey Lyubka
Browse files

Updated examples to conform to the evented API

parent 458320cf
No related branches found
No related tags found
No related merge requests found
CFLAGS = -W -Wall -I.. -pthread -g -pipe $(CFLAGS_EXTRA) CFLAGS = -W -Wall -I.. -pthread -g -pipe $(CFLAGS_EXTRA)
RM = rm -rf RM = rm -rf
MSVC = ../../vc6
CL = $(MSVC)/bin/cl
CLFLAGS = /MD /TC /nologo $(CFLAGS_EXTRA) /W3 \
/I$(MSVC)/include /I.. /Dsnprintf=_snprintf
LFLAGS = /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
MSVC = ../../vc6
RM = del /q /f RM = del /q /f
CC = $(MSVC)/bin/cl $(CLFLAGS) CC = $(MSVC)/bin/cl $(CLFLAGS)
CLFLAGS = /MD /TC /nologo $(CFLAGS_EXTRA) /W3 \
/I$(MSVC)/include /I.. /Dsnprintf=_snprintf
LFLAGS = /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86
else else
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
DLL_FLAGS += -shared
ifeq ($(UNAME_S),Linux) ifeq ($(UNAME_S),Linux)
CFLAGS += -ldl CFLAGS += -ldl
endif endif
ifeq ($(UNAME_S),Darwin) ifeq ($(UNAME_S),Darwin)
# DLL_FLAGS += -bundle -undefined dynamic_lookup -dynamiclib
DLL_FLAGS += -flat_namespace -undefined suppress -dynamiclib
endif endif
endif endif
all: hello websocket server post multi_threaded upload auth
# To enable Lua in a server, uncomment following lines # To enable Lua in a server, uncomment following lines
LUA = ../lua-5.2.3/src LUA = ../lua-5.2.3/src
#CFLAGS += -I$(LUA) -L$(LUA) -llua #CFLAGS += -I$(LUA) -L$(LUA) -llua
all: websocket_html.c hello: hello.c ../mongoose.c
$(CC) hello.c ../mongoose.c -o hello $(CFLAGS) $(CC) hello.c ../mongoose.c -o hello $(CFLAGS)
websocket: websocket_html.c websocket.c ../mongoose.c
$(CC) websocket.c websocket_html.c ../mongoose.c -o websocket $(CFLAGS) $(CC) websocket.c websocket_html.c ../mongoose.c -o websocket $(CFLAGS)
server: server.c ../mongoose.c
$(CC) server.c ../mongoose.c -o server $(CFLAGS)
post: post.c ../mongoose.c
$(CC) post.c ../mongoose.c -o post $(CFLAGS) $(CC) post.c ../mongoose.c -o post $(CFLAGS)
multi_threaded: multi_threaded.c ../mongoose.c
$(CC) multi_threaded.c ../mongoose.c -o multi_threaded $(CFLAGS) $(CC) multi_threaded.c ../mongoose.c -o multi_threaded $(CFLAGS)
upload: upload.c ../mongoose.c
$(CC) upload.c ../mongoose.c -o upload $(CFLAGS) $(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
auth: auth.c ../mongoose.c
$(CC) auth.c ../mongoose.c -o auth $(CFLAGS) $(CC) auth.c ../mongoose.c -o auth $(CFLAGS)
$(CC) server.c ../mongoose.c -o server $(CFLAGS)
websocket_html.c: websocket.html websocket_html.c: websocket.html
perl mkdata.pl $< > $@ perl mkdata.pl $< > $@
......
...@@ -2,25 +2,29 @@ ...@@ -2,25 +2,29 @@
#include <string.h> #include <string.h>
#include "mongoose.h" #include "mongoose.h"
static int auth_handler(struct mg_connection *conn) { static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
int result = MG_AUTH_FAIL; // Not authorized
FILE *fp; if (ev == MG_AUTH) {
int result = MG_FALSE; // Not authorized
// To populate passwords file, do FILE *fp;
// mongoose -A my_passwords.txt mydomain.com admin admin
if ((fp = fopen("my_passwords.txt", "r")) != NULL) { // To populate passwords file, do
result = mg_authorize_digest(conn, fp); // mongoose -A my_passwords.txt mydomain.com admin admin
fclose(fp); if ((fp = fopen("my_passwords.txt", "r")) != NULL) {
result = mg_authorize_digest(conn, fp);
fclose(fp);
}
return result;
} }
return result; return MG_FALSE;
} }
int main(void) { int main(void) {
struct mg_server *server = mg_create_server(NULL); struct mg_server *server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_set_option(server, "document_root", "."); 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")); printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) { for (;;) {
......
...@@ -2,19 +2,25 @@ ...@@ -2,19 +2,25 @@
#include <string.h> #include <string.h>
#include "mongoose.h" #include "mongoose.h"
// This function will be called by mongoose on every new request static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
static int index_html(struct mg_connection *conn) { int result = MG_FALSE;
mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
return MG_REQUEST_PROCESSED; if (ev == MG_REQ_BEGIN) {
mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
result = MG_TRUE;
} else if (ev == MG_AUTH) {
result = MG_TRUE;
}
return result;
} }
int main(void) { int main(void) {
struct mg_server *server; struct mg_server *server;
// Create and configure the server // Create and configure the server
server = mg_create_server(NULL); server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_set_request_handler(server, index_html);
// Serve request. Hit Ctrl-C to terminate the program // Serve request. Hit Ctrl-C to terminate the program
printf("Starting on port %s\n", mg_get_option(server, "listening_port")); printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
......
...@@ -2,11 +2,17 @@ ...@@ -2,11 +2,17 @@
// Start a browser and hit refresh couple of times. The replies will // Start a browser and hit refresh couple of times. The replies will
// come from both server instances. // come from both server instances.
static int request_handler(struct mg_connection *conn) { static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
mg_send_header(conn, "Content-Type", "text/plain"); if (ev == MG_REQ_BEGIN) {
mg_printf_data(conn, "This is a reply from server instance # %s", mg_send_header(conn, "Content-Type", "text/plain");
(char *) conn->server_param); mg_printf_data(conn, "This is a reply from server instance # %s",
return MG_REQUEST_PROCESSED; (char *) conn->server_param);
return MG_TRUE;
} else if (ev == MG_AUTH) {
return MG_TRUE;
} else {
return MG_FALSE;
}
} }
static void *serve(void *server) { static void *serve(void *server) {
...@@ -17,11 +23,8 @@ static void *serve(void *server) { ...@@ -17,11 +23,8 @@ static void *serve(void *server) {
int main(void) { int main(void) {
struct mg_server *server1, *server2; struct mg_server *server1, *server2;
server1 = mg_create_server((void *) "1"); server1 = mg_create_server((void *) "1", ev_handler);
server2 = mg_create_server((void *) "2"); server2 = mg_create_server((void *) "2", ev_handler);
mg_set_request_handler(server1, request_handler);
mg_set_request_handler(server2, request_handler);
// Make both server1 and server2 listen on the same socket // Make both server1 and server2 listen on the same socket
mg_set_option(server1, "listening_port", "8080"); mg_set_option(server1, "listening_port", "8080");
......
...@@ -10,7 +10,7 @@ static const char *html_form = ...@@ -10,7 +10,7 @@ static const char *html_form =
"<input type=\"submit\" />" "<input type=\"submit\" />"
"</form></body></html>"; "</form></body></html>";
static int handler(struct mg_connection *conn) { static void send_reply(struct mg_connection *conn) {
char var1[500], var2[500]; char var1[500], var2[500];
if (strcmp(conn->uri, "/handle_post_request") == 0) { if (strcmp(conn->uri, "/handle_post_request") == 0) {
...@@ -33,18 +33,30 @@ static int handler(struct mg_connection *conn) { ...@@ -33,18 +33,30 @@ static int handler(struct mg_connection *conn) {
// Show HTML form. // Show HTML form.
mg_send_data(conn, html_form, strlen(html_form)); mg_send_data(conn, html_form, strlen(html_form));
} }
}
return MG_REQUEST_PROCESSED; static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_REQ_BEGIN) {
send_reply(conn);
return MG_TRUE;
} else if (ev == MG_AUTH) {
return MG_TRUE;
} else {
return MG_FALSE;
}
} }
int main(void) { int main(void) {
struct mg_server *server = mg_create_server(NULL); struct mg_server *server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_set_request_handler(server, 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 (;;) {
mg_poll_server(server, 1000); mg_poll_server(server, 1000);
} }
mg_destroy_server(&server); mg_destroy_server(&server);
return 0; return 0;
} }
...@@ -380,7 +380,7 @@ static void start_mongoose(int argc, char *argv[]) { ...@@ -380,7 +380,7 @@ static void start_mongoose(int argc, char *argv[]) {
char *options[MAX_OPTIONS]; char *options[MAX_OPTIONS];
int i; int i;
if ((server = mg_create_server(NULL)) == NULL) { if ((server = mg_create_server(NULL, NULL)) == NULL) {
die("%s", "Failed to start Mongoose."); die("%s", "Failed to start Mongoose.");
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include <string.h> #include <string.h>
#include "mongoose.h" #include "mongoose.h"
static int index_html(struct mg_connection *conn) { static void send_index_page(struct mg_connection *conn) {
const char *data; const char *data;
int data_len; int data_len;
char var_name[100], file_name[100]; char var_name[100], file_name[100];
...@@ -29,17 +29,25 @@ static int index_html(struct mg_connection *conn) { ...@@ -29,17 +29,25 @@ static int index_html(struct mg_connection *conn) {
} }
mg_printf_data(conn, "%s", "</body></html>"); mg_printf_data(conn, "%s", "</body></html>");
}
return MG_REQUEST_PROCESSED; static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_REQ_BEGIN) {
send_index_page(conn);
return MG_TRUE;
} else if (ev == MG_AUTH) {
return MG_TRUE;
} else {
return MG_FALSE;
}
} }
int main(void) { int main(void) {
struct mg_server *server; struct mg_server *server;
// Create and configure the server // Create and configure the server
server = mg_create_server(NULL); server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_set_request_handler(server, index_html);
// Serve request. Hit Ctrl-C to terminate the program // Serve request. Hit Ctrl-C to terminate the program
printf("Starting on port %s\n", mg_get_option(server, "listening_port")); printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
......
#include <string.h> #include <string.h>
#include <time.h>
#include "mongoose.h" #include "mongoose.h"
extern const char *find_embedded_file(const char *, size_t *); extern const char *find_embedded_file(const char *, size_t *);
static int iterate_callback(struct mg_connection *c) { static int iterate_callback(struct mg_connection *c, enum mg_event ev) {
if (c->is_websocket) { static int counter = 0;
if (ev == MG_POLL && c->is_websocket) {
char buf[20]; char buf[20];
int len = snprintf(buf, sizeof(buf), "%d", * (int *) c->callback_param); int len = snprintf(buf, sizeof(buf), "%d", counter++);
mg_websocket_write(c, 1, buf, len); mg_websocket_write(c, 1, buf, len);
} }
return MG_REQUEST_PROCESSED; return MG_TRUE;
} }
static int index_html(struct mg_connection *conn) { static int send_reply(struct mg_connection *conn) {
size_t index_size; size_t index_size;
const char *index_html = find_embedded_file("websocket.html", &index_size); const char *index_html = find_embedded_file("websocket.html", &index_size);
...@@ -22,27 +24,37 @@ static int index_html(struct mg_connection *conn) { ...@@ -22,27 +24,37 @@ static int index_html(struct mg_connection *conn) {
// Echo websocket data back to the client. // Echo websocket data back to the client.
mg_websocket_write(conn, 1, conn->content, conn->content_len); mg_websocket_write(conn, 1, conn->content, conn->content_len);
return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ? return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ?
MG_CLIENT_CLOSE : MG_CLIENT_CONTINUE; MG_FALSE : MG_TRUE;
} else { } else {
mg_send_header(conn, "Content-Type", "text/html"); mg_send_header(conn, "Content-Type", "text/html");
mg_send_data(conn, index_html, index_size); mg_send_data(conn, index_html, index_size);
return MG_REQUEST_PROCESSED; return MG_TRUE;
}
}
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_REQ_BEGIN) {
return send_reply(conn);
} else if (ev == MG_AUTH) {
return MG_TRUE;
} else {
return MG_FALSE;
} }
} }
int main(void) { int main(void) {
struct mg_server *server = mg_create_server(NULL); struct mg_server *server = mg_create_server(NULL, ev_handler);
unsigned int current_timer = 0, last_timer = 0; time_t current_timer = 0, last_timer = time(NULL);
mg_set_option(server, "listening_port", "8080"); mg_set_option(server, "listening_port", "8080");
mg_set_request_handler(server, index_html);
printf("Started on port %s\n", mg_get_option(server, "listening_port")); printf("Started on port %s\n", mg_get_option(server, "listening_port"));
for (;;) { for (;;) {
current_timer = mg_poll_server(server, 100); mg_poll_server(server, 100);
current_timer = time(NULL);
if (current_timer - last_timer > 0) { if (current_timer - last_timer > 0) {
last_timer = current_timer; last_timer = current_timer;
mg_iterate_over_connections(server, iterate_callback, &current_timer); mg_iterate_over_connections(server, iterate_callback);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment