int main(int argc, char *argv[]) { const char * options[] = { "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0 }; struct mg_callbacks callbacks; struct mg_context *ctx; memset(&callbacks, 0, sizeof(callbacks)); ctx = mg_start(&callbacks, 0, options); mg_set_request_handler(ctx,EXAMPLE_URI, ExampleHandler,0); mg_set_request_handler(ctx,EXIT_URI, ExitHandler,0); mg_set_request_handler(ctx,"/a", AHandler,0); mg_set_request_handler(ctx,"/a/b", ABHandler,0); mg_set_request_handler(ctx,"**.foo$", FooHandler,0); printf("Browse files at http://localhost:%s/\n", PORT); printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI); printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI); while (!exitNow) { #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif } printf("Bye!\n"); return 0; }
int httpserver_init(unsigned port) { char str[16]; snprintf(str, sizeof(str), "%u", port); str[sizeof(str) - 1] = 0; const char* options[] = { "listening_ports", str, NULL, NULL }; memset(&s_httpserver_callbacks, 0, sizeof(s_httpserver_callbacks)); s_httpserver_ctx = mg_start(&s_httpserver_callbacks, NULL, options); if (s_httpserver_ctx == NULL) return -1; mg_set_request_handler(s_httpserver_ctx, "/" BASIC_INFO, httpserver_handle_basic_info, NULL); mg_set_request_handler(s_httpserver_ctx, "/" MEMORY_MAP, httpserver_handle_mmaps, NULL); mg_set_request_handler(s_httpserver_ctx, "/" MEMORY_MAP "/", httpserver_handle_mmaps, NULL); return 0; }
int main(void) { struct mg_server *server = mg_create_server(NULL); unsigned int current_timer = 0, last_timer = 0; 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); if (current_timer - last_timer > 0) { last_timer = current_timer; mg_iterate_over_connections(server, iterate_callback, ¤t_timer); } } mg_destroy_server(&server); return 0; }
int main(void) { struct mg_server *server; // Create and configure the server server = mg_create_server(NULL); 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")); for (;;) { mg_poll_server(server, 1000); } // Cleanup, and free server instance mg_destroy_server(&server); return 0; }
void initialize_server(void){ char tmpBuf[100]; server = mg_create_server(NULL); mg_set_option(server, "listening_port", globalOptions.port); mg_set_option(server, "run_as_user", "root"); #ifdef DEBUG mg_set_option(server, "document_root", "public"); #endif mg_set_request_handler(server, request_handler); sprintf( tmpBuf, "Initialized web server at port %s", mg_get_option(server, "listening_port") ); log_line(tmpBuf, LOG_INFO); for(;;) mg_poll_server(server, 1000); }
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) { int n, option_index = 0; struct mg_server *server = mg_create_server(NULL); unsigned int current_timer = 0, last_timer = 0; char *run_as_user = NULL; char radio_song_name[512]; const char *radio_path = NULL; char *radio_url = NULL; char radio_added_song[512]; config_t cfg; config_setting_t *settings; char config_file_name[512]; struct passwd *pw = getpwuid(getuid()); char *homedir = pw->pw_dir; sprintf(config_file_name, "%s/%s/%s", homedir, project_name, ympd_conf_file); printf("conf = %s\n", config_file_name); atexit(bye); mg_set_option(server, "listening_port", "8080"); mpd.port = 6600; strcpy(mpd.host, "127.0.0.1"); config_init(&cfg); if(! config_read_file(&cfg, config_file_name)) { printf("config file error %s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return(EXIT_FAILURE); } if (!config_lookup_string(&cfg, "application.radio_path", &radio_path)) { fprintf(stderr, "No 'radio_path' setting in configuration file.\n"); } /* drop privilges at last to ensure proper port binding */ if(run_as_user != NULL) { mg_set_option(server, "run_as_user", run_as_user); free(run_as_user); } if (!config_lookup_string(&cfg, "streamripper.url", &radio_url)) { fprintf(stderr, "No 'radio_url' setting in configuration file.\n"); } printf("url = %s\n", radio_url); start_streamripper(); printf("start_streamripper\n"); /* init_watch_radio(); printf("init_watch_radio\n"); add_watch_radio(radio_path); printf("add_watch_radio\n");*/ mg_set_http_close_handler(server, mpd_close_handler); mg_set_request_handler(server, server_callback); while (!force_exit) { current_timer = mg_poll_server(server, 200); if(current_timer - last_timer) { last_timer = current_timer; mpd_poll(server); /* if (radio_poll(radio_song_name)) { sprintf(radio_added_song, "%s%s", "radio/", radio_song_name); printf("%s\n", radio_added_song); mpd_run_update(mpd.conn, radio_added_song); sleep(1); mpd_run_add(mpd.conn, radio_added_song); }*/ } } stop_streamripper(); close_watch_radio(); mpd_disconnect(); mg_destroy_server(&server); config_destroy(&cfg); 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; }