/*
* Copyright (c) 2013-2015 the CivetWeb developers
* Copyright (c) 2013 No Face Press, LLC
* License http://opensource.org/licenses/mit-license.php MIT License
*/
/* Simple example program on how to use Embedded C interface. */
#ifdef _WIN32
#include
#else
#include
#endif
#include
#include "civetweb.h"
#define DOCUMENT_ROOT "."
#ifdef USE_IPV6
#define PORT "8888,[::]:8886"
#else
#define PORT "8888"
#endif
#define EXAMPLE_URI "/example"
#define EXIT_URI "/exit"
int exitNow = 0;
int ExampleHandler(struct mg_connection *conn, void *cbdata)
{
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "");
mg_printf(conn, "This is an example text from a C handler
");
mg_printf(conn, "To see a page from the A handler click A
");
mg_printf(conn, "To see a page from the A handler click A/A
");
mg_printf(conn, "To see a page from the A/B handler click A/B
");
mg_printf(conn, "To see a page from the B handler (0) click B
");
mg_printf(conn, "To see a page from the B handler (1) click B/A
");
mg_printf(conn, "To see a page from the B handler (2) click B/B
");
mg_printf(conn, "To see a page from the *.foo handler click xy.foo
");
mg_printf(conn, "To test websocket handler click websocket
");
mg_printf(conn, "To exit click exit
", EXIT_URI);
mg_printf(conn, "\n");
return 1;
}
int ExitHandler(struct mg_connection *conn, void *cbdata)
{
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
mg_printf(conn, "Bye!\n");
exitNow = 1;
return 1;
}
int AHandler(struct mg_connection *conn, void *cbdata)
{
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "");
mg_printf(conn, "This is the A handler!!!
");
mg_printf(conn, "\n");
return 1;
}
int ABHandler(struct mg_connection *conn, void *cbdata)
{
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "");
mg_printf(conn, "This is the AB handler!!!
");
mg_printf(conn, "\n");
return 1;
}
int BXHandler(struct mg_connection *conn, void *cbdata)
{
/* Handler may access the request info using mg_get_request_info */
const struct mg_request_info * req_info = mg_get_request_info(conn);
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "");
mg_printf(conn, "This is the BX handler %i!!!
", (int)cbdata);
mg_printf(conn, "The actual uri is %s
", req_info->uri);
mg_printf(conn, "\n");
return 1;
}
int FooHandler(struct mg_connection *conn, void *cbdata)
{
/* Handler may access the request info using mg_get_request_info */
const struct mg_request_info * req_info = mg_get_request_info(conn);
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "");
mg_printf(conn, "This is the Foo handler!!!
");
mg_printf(conn, "The request was:
%s %s HTTP/%s
",
req_info->request_method, req_info->uri, req_info->http_version);
mg_printf(conn, "\n");
return 1;
}
int WebSocketStartHandler(struct mg_connection *conn, void *cbdata)
{
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
mg_printf(conn, "\n");
mg_printf(conn, "\n\n");
mg_printf(conn, "\n");
mg_printf(conn, "Embedded websocket example\n");
#ifdef USE_WEBSOCKET
/* mg_printf(conn, "\n"); ... xhtml style */
mg_printf(conn, "\n");
mg_printf(conn, "\n\n");
mg_printf(conn, "No websocket connection yet
\n");
#else
mg_printf(conn, "\n\n");
mg_printf(conn, "Example not compiled with USE_WEBSOCKET\n");
#endif
mg_printf(conn, "\n\n");
return 1;
}
#ifdef USE_WEBSOCKET
#define MAX_WS_CLIENTS 5 /* just for the test: a small number that can be reached */
/* a real server should use a much higher number here */
struct t_ws_client {
struct mg_connection * conn;
int state;
} static ws_clients[MAX_WS_CLIENTS];
#define ASSERT(x) {if (!(x)) {fprintf(stderr, "Assertion failed in line %u\n", (unsigned)__LINE__);}}
static unsigned long cnt;
int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata)
{
struct mg_context *ctx = mg_get_context(conn);
int reject = 1;
int i;
mg_lock_context(ctx);
for (i=0; iconn == conn);
ASSERT(client->state == 1);
client->state = 2;
}
int WebsocketDataHandler(struct mg_connection * conn, int bits, char * data, size_t len, void *cbdata)
{
struct t_ws_client * client = mg_get_user_connection_data(conn);
ASSERT(client->conn == conn);
ASSERT(client->state >= 1);
fprintf(stdout, "Websocket got data:\r\n");
fwrite(data, len, 1, stdout);
fprintf(stdout, "\r\n\r\n");
return 1;
}
void WebSocketCloseHandler(const struct mg_connection * conn, void *cbdata)
{
struct mg_context *ctx = mg_get_context(conn);
struct t_ws_client * client = mg_get_user_connection_data(conn);
ASSERT(client->conn == conn);
ASSERT(client->state >= 1);
mg_lock_context(ctx);
client->state = 0;
client->conn = NULL;
mg_unlock_context(ctx);
fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n");
}
void InformWebsockets(struct mg_context *ctx)
{
char text[32];
int i;
sprintf(text, "%lu", ++cnt);
mg_lock_context(ctx);
for (i=0; i