Updated examples to conform to the evented API

This commit is contained in:
Sergey Lyubka 2014-03-02 12:16:09 +00:00
parent 458320cffb
commit ea9a60a59a
8 changed files with 116 additions and 62 deletions

View File

@ -1,42 +1,51 @@
CFLAGS = -W -Wall -I.. -pthread -g -pipe $(CFLAGS_EXTRA)
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)
MSVC = ../../vc6
RM = del /q /f
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
UNAME_S := $(shell uname -s)
DLL_FLAGS += -shared
ifeq ($(UNAME_S),Linux)
CFLAGS += -ldl
endif
ifeq ($(UNAME_S),Darwin)
# DLL_FLAGS += -bundle -undefined dynamic_lookup -dynamiclib
DLL_FLAGS += -flat_namespace -undefined suppress -dynamiclib
endif
endif
all: hello websocket server post multi_threaded upload auth
# To enable Lua in a server, uncomment following lines
LUA = ../lua-5.2.3/src
#CFLAGS += -I$(LUA) -L$(LUA) -llua
all: websocket_html.c
hello: hello.c ../mongoose.c
$(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) 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)
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)
multi_threaded: multi_threaded.c ../mongoose.c
$(CC) multi_threaded.c ../mongoose.c -o multi_threaded $(CFLAGS)
upload: upload.c ../mongoose.c
$(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
auth: auth.c ../mongoose.c
$(CC) auth.c ../mongoose.c -o auth $(CFLAGS)
websocket_html.c: websocket.html
perl mkdata.pl $< > $@

View File

@ -2,25 +2,29 @@
#include <string.h>
#include "mongoose.h"
static int auth_handler(struct mg_connection *conn) {
int result = MG_AUTH_FAIL; // Not authorized
FILE *fp;
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
// To populate passwords file, do
// mongoose -A my_passwords.txt mydomain.com admin admin
if ((fp = fopen("my_passwords.txt", "r")) != NULL) {
result = mg_authorize_digest(conn, fp);
fclose(fp);
if (ev == MG_AUTH) {
int result = MG_FALSE; // Not authorized
FILE *fp;
// To populate passwords file, do
// mongoose -A my_passwords.txt mydomain.com admin admin
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) {
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, "document_root", ".");
mg_set_auth_handler(server, auth_handler);
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {

View File

@ -2,19 +2,25 @@
#include <string.h>
#include "mongoose.h"
// This function will be called by mongoose on every new request
static int index_html(struct mg_connection *conn) {
mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
return MG_REQUEST_PROCESSED;
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
int result = MG_FALSE;
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) {
struct mg_server *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_request_handler(server, index_html);
// Serve request. Hit Ctrl-C to terminate the program
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));

View File

@ -2,11 +2,17 @@
// Start a browser and hit refresh couple of times. The replies will
// come from both server instances.
static int request_handler(struct mg_connection *conn) {
mg_send_header(conn, "Content-Type", "text/plain");
mg_printf_data(conn, "This is a reply from server instance # %s",
(char *) conn->server_param);
return MG_REQUEST_PROCESSED;
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_REQ_BEGIN) {
mg_send_header(conn, "Content-Type", "text/plain");
mg_printf_data(conn, "This is a reply from server instance # %s",
(char *) conn->server_param);
return MG_TRUE;
} else if (ev == MG_AUTH) {
return MG_TRUE;
} else {
return MG_FALSE;
}
}
static void *serve(void *server) {
@ -17,11 +23,8 @@ static void *serve(void *server) {
int main(void) {
struct mg_server *server1, *server2;
server1 = mg_create_server((void *) "1");
server2 = mg_create_server((void *) "2");
mg_set_request_handler(server1, request_handler);
mg_set_request_handler(server2, request_handler);
server1 = mg_create_server((void *) "1", ev_handler);
server2 = mg_create_server((void *) "2", ev_handler);
// Make both server1 and server2 listen on the same socket
mg_set_option(server1, "listening_port", "8080");

View File

@ -10,7 +10,7 @@ static const char *html_form =
"<input type=\"submit\" />"
"</form></body></html>";
static int handler(struct mg_connection *conn) {
static void send_reply(struct mg_connection *conn) {
char var1[500], var2[500];
if (strcmp(conn->uri, "/handle_post_request") == 0) {
@ -33,18 +33,30 @@ static int handler(struct mg_connection *conn) {
// Show 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) {
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_request_handler(server, handler);
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {
mg_poll_server(server, 1000);
}
mg_destroy_server(&server);
return 0;
}

View File

@ -380,7 +380,7 @@ static void start_mongoose(int argc, char *argv[]) {
char *options[MAX_OPTIONS];
int i;
if ((server = mg_create_server(NULL)) == NULL) {
if ((server = mg_create_server(NULL, NULL)) == NULL) {
die("%s", "Failed to start Mongoose.");
}

View File

@ -5,7 +5,7 @@
#include <string.h>
#include "mongoose.h"
static int index_html(struct mg_connection *conn) {
static void send_index_page(struct mg_connection *conn) {
const char *data;
int data_len;
char var_name[100], file_name[100];
@ -29,17 +29,25 @@ static int index_html(struct mg_connection *conn) {
}
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) {
struct mg_server *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_request_handler(server, index_html);
// Serve request. Hit Ctrl-C to terminate the program
printf("Starting on port %s\n", mg_get_option(server, "listening_port"));

View File

@ -1,18 +1,20 @@
#include <string.h>
#include <time.h>
#include "mongoose.h"
extern const char *find_embedded_file(const char *, size_t *);
static int iterate_callback(struct mg_connection *c) {
if (c->is_websocket) {
static int iterate_callback(struct mg_connection *c, enum mg_event ev) {
static int counter = 0;
if (ev == MG_POLL && c->is_websocket) {
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);
}
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;
const char *index_html = find_embedded_file("websocket.html", &index_size);
@ -22,27 +24,37 @@ static int index_html(struct mg_connection *conn) {
// Echo websocket data back to the client.
mg_websocket_write(conn, 1, conn->content, conn->content_len);
return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ?
MG_CLIENT_CLOSE : MG_CLIENT_CONTINUE;
MG_FALSE : MG_TRUE;
} else {
mg_send_header(conn, "Content-Type", "text/html");
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) {
struct mg_server *server = mg_create_server(NULL);
unsigned int current_timer = 0, last_timer = 0;
struct mg_server *server = mg_create_server(NULL, ev_handler);
time_t current_timer = 0, last_timer = time(NULL);
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"));
for (;;) {
current_timer = mg_poll_server(server, 100);
mg_poll_server(server, 100);
current_timer = time(NULL);
if (current_timer - last_timer > 0) {
last_timer = current_timer;
mg_iterate_over_connections(server, iterate_callback, &current_timer);
mg_iterate_over_connections(server, iterate_callback);
}
}