int main(int argc, char *argv[]) { const char *options[] = {"document_root", DOCUMENT_ROOT, "listening_ports", PORT, "request_timeout_ms", "10000", "error_log_file", "error.log", #ifdef USE_WEBSOCKET "websocket_timeout_ms", "3600000", #endif #ifndef NO_SSL "ssl_certificate", "../../resources/cert/server.pem", #endif 0}; struct mg_callbacks callbacks; struct mg_context *ctx; struct mg_server_ports ports[32]; int port_cnt, n; int err = 0; /* Check if libcivetweb has been built with all required features. */ #ifdef USE_IPV6 if (!mg_check_feature(8)) { fprintf(stderr, "Error: Embedded example built with websocket support, " "but civetweb library build without.\n"); err = 1; } #endif #ifdef USE_WEBSOCKET if (!mg_check_feature(16)) { fprintf(stderr, "Error: Embedded example built with websocket support, " "but civetweb library build without.\n"); err = 1; } #endif #ifndef NO_SSL if (!mg_check_feature(2)) { fprintf(stderr, "Error: Embedded example built with SSL support, " "but civetweb library build without.\n"); err = 1; } #endif if (err) { fprintf(stderr, "Cannot start CivetWeb - inconsistent build.\n"); return EXIT_FAILURE; } /* Start CivetWeb web server */ memset(&callbacks, 0, sizeof(callbacks)); ctx = mg_start(&callbacks, 0, options); /* Add handler EXAMPLE_URI, to explain the example */ mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0); mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0); /* Add handler for /A* and special handler for /A/B */ mg_set_request_handler(ctx, "/A", AHandler, 0); mg_set_request_handler(ctx, "/A/B", ABHandler, 0); /* Add handler for /B, /B/A, /B/B but not for /B* */ mg_set_request_handler(ctx, "/B$", BXHandler, (void *)0); mg_set_request_handler(ctx, "/B/A$", BXHandler, (void *)1); mg_set_request_handler(ctx, "/B/B$", BXHandler, (void *)2); /* Add handler for all files with .foo extention */ mg_set_request_handler(ctx, "**.foo$", FooHandler, 0); /* Add HTTP site to open a websocket connection */ mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0); #ifdef USE_WEBSOCKET /* WS site for the websocket connection */ mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0); #endif /* List all listening ports */ memset(ports, 0, sizeof(ports)); port_cnt = mg_get_server_ports(ctx, 32, ports); printf("\n%i listening ports:\n\n", port_cnt); for (n = 0; n < port_cnt && n < 32; n++) { const char *proto = ports[n].is_ssl ? "https" : "http"; const char *host; if ((ports[n].protocol & 1) == 1) { /* IPv4 */ host = "127.0.0.1"; printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port); printf("Run example at %s://%s:%i%s\n", proto, host, ports[n].port, EXAMPLE_URI); printf( "Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI); printf("\n"); } if ((ports[n].protocol & 2) == 2) { /* IPv6 */ host = "[::1]"; printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port); printf("Run example at %s://%s:%i%s\n", proto, host, ports[n].port, EXAMPLE_URI); printf( "Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI); printf("\n"); } } /* Wait until the server should be closed */ while (!exitNow) { #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif #ifdef USE_WEBSOCKET InformWebsockets(ctx); #endif } /* Stop the server */ mg_stop(ctx); printf("Server stopped.\n"); printf("Bye!\n"); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { const char *options[] = { "document_root", DOCUMENT_ROOT, "listening_ports", PORT, "request_timeout_ms", "10000", "error_log_file", "error.log", #ifdef USE_WEBSOCKET "websocket_timeout_ms", "3600000", #endif #ifndef NO_SSL "ssl_certificate", "../../resources/cert/server.pem", "ssl_protocol_version", "3", "ssl_cipher_list", #ifdef USE_SSL_DH "ECDHE-RSA-AES256-GCM-SHA384:DES-CBC3-SHA:AES128-SHA:AES128-GCM-SHA256", #else "DES-CBC3-SHA:AES128-SHA:AES128-GCM-SHA256", #endif #endif "enable_auth_domain_check", "no", 0}; struct mg_callbacks callbacks; struct mg_context *ctx; struct mg_server_ports ports[32]; int port_cnt, n; int err = 0; /* Check if libcivetweb has been built with all required features. */ #ifdef USE_IPV6 if (!mg_check_feature(8)) { fprintf(stderr, "Error: Embedded example built with IPv6 support, " "but civetweb library build without.\n"); err = 1; } #endif #ifdef USE_WEBSOCKET if (!mg_check_feature(16)) { fprintf(stderr, "Error: Embedded example built with websocket support, " "but civetweb library build without.\n"); err = 1; } #endif #ifndef NO_SSL if (!mg_check_feature(2)) { fprintf(stderr, "Error: Embedded example built with SSL support, " "but civetweb library build without.\n"); err = 1; } #endif if (err) { fprintf(stderr, "Cannot start CivetWeb - inconsistent build.\n"); return EXIT_FAILURE; } /* Start CivetWeb web server */ memset(&callbacks, 0, sizeof(callbacks)); #ifndef NO_SSL callbacks.init_ssl = init_ssl; #endif callbacks.log_message = log_message; ctx = mg_start(&callbacks, 0, options); /* Check return value: */ if (ctx == NULL) { fprintf(stderr, "Cannot start CivetWeb - mg_start failed.\n"); return EXIT_FAILURE; } /* Add handler EXAMPLE_URI, to explain the example */ mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0); mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0); /* Add handler for /A* and special handler for /A/B */ mg_set_request_handler(ctx, "/A", AHandler, 0); mg_set_request_handler(ctx, "/A/B", ABHandler, 0); /* Add handler for /B, /B/A, /B/B but not for /B* */ mg_set_request_handler(ctx, "/B$", BXHandler, (void *)0); mg_set_request_handler(ctx, "/B/A$", BXHandler, (void *)1); mg_set_request_handler(ctx, "/B/B$", BXHandler, (void *)2); /* Add handler for all files with .foo extention */ mg_set_request_handler(ctx, "**.foo$", FooHandler, 0); /* Add handler for /close extention */ mg_set_request_handler(ctx, "/close", CloseHandler, 0); /* Add handler for /form (serve a file outside the document root) */ mg_set_request_handler(ctx, "/form", FileHandler, (void *)"../../test/form.html"); /* Add handler for form data */ mg_set_request_handler(ctx, "/handle_form.embedded_c.example.callback", FormHandler, (void *)0); /* Add a file upload handler for parsing files on the fly */ mg_set_request_handler(ctx, "/on_the_fly_form", FileUploadForm, (void *)"/on_the_fly_form.md5.callback"); mg_set_request_handler(ctx, "/on_the_fly_form.md5.callback", CheckSumHandler, (void *)0); /* Add handler for /cookie example */ mg_set_request_handler(ctx, "/cookie", CookieHandler, 0); /* Add handler for /postresponse example */ mg_set_request_handler(ctx, "/postresponse", PostResponser, 0); /* Add HTTP site to open a websocket connection */ mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0); /* Add HTTP site with auth */ mg_set_request_handler(ctx, "/auth", AuthStartHandler, 0); #ifdef USE_WEBSOCKET /* WS site for the websocket connection */ mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0); #endif /* List all listening ports */ memset(ports, 0, sizeof(ports)); port_cnt = mg_get_server_ports(ctx, 32, ports); printf("\n%i listening ports:\n\n", port_cnt); for (n = 0; n < port_cnt && n < 32; n++) { const char *proto = ports[n].is_ssl ? "https" : "http"; const char *host; if ((ports[n].protocol & 1) == 1) { /* IPv4 */ host = "127.0.0.1"; printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port); printf("Run example at %s://%s:%i%s\n", proto, host, ports[n].port, EXAMPLE_URI); printf( "Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI); printf("\n"); } if ((ports[n].protocol & 2) == 2) { /* IPv6 */ host = "[::1]"; printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port); printf("Run example at %s://%s:%i%s\n", proto, host, ports[n].port, EXAMPLE_URI); printf( "Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI); printf("\n"); } } /* Wait until the server should be closed */ while (!exitNow) { #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif #ifdef USE_WEBSOCKET InformWebsockets(ctx); #endif } /* Stop the server */ mg_stop(ctx); printf("Server stopped.\n"); printf("Bye!\n"); return EXIT_SUCCESS; }