/* * 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 "." #define PORT "8888" #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 here

"); mg_printf(conn, "

To see a page from the A/B handler click here

"); mg_printf(conn, "

To see a page from the *.foo handler click here

"); mg_printf(conn, "

To test websocket handler click here

"); mg_printf(conn, "

To exit click here

", 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 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 1024 static struct mg_connection * ws_clients[MAX_WS_CLIENTS]; static unsigned long cnt; int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata) { int reject = 0; fprintf(stdout, "Websocket client %s\r\n\r\n", reject ? "rejected" : "accepted"); return reject; } void WebSocketReadyHandler(struct mg_connection * conn, void *cbdata) { struct mg_context *ctx = mg_get_context(conn); int i; const char * text = "Hello from the websocket ready handler"; mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text)); fprintf(stdout, "Client added to the set of webserver connections\r\n\r\n"); mg_lock_context(ctx); for (i=0; i