int main(int /* argc */, char *argv[]) { std::string app_name(argv[0]); unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT); std::cout << app_name << ": " << port_number << std::endl; try { // The asio io_service. ASIO::io_service io_service; // Create the HTTP server, attach the request handler http_server_type http_server(io_service); http_server.request_received_event(request_handler); // Accept IPV4 connections on the default port (80) ASIO_ERROR_CODE error(http_server.accept_connections()); if (error) { std::cerr << "Error: " << error.message() << std::endl; return 1; } // Start the server io_service.run(); } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; return 1; } return 0; }
int main( void ) { uint8_t i; halInit(); chSysInit(); // Mount the SD card sdcStart( &SDCD1, NULL ); sdcConnect( &SDCD1 ); f_mount( 0, & SDC_FS ); // Creates the blinker thread. chThdCreateStatic( waThread1, sizeof( waThread1 ), NORMALPRIO, Thread1, NULL ); // Creates the LWIP threads (it changes priority internally). chThdCreateStatic( wa_lwip_thread, LWIP_THREAD_STACK_SIZE, NORMALPRIO + 3, lwip_thread, NULL ); // Initialize web server http_server(); // Normal main() thread activity while( TRUE ) { chThdSleepMilliseconds( 1000 ); } }
int HttpServer<ServerContext, RequestFactory>::main(const std::vector<std::string>& args) { Poco::Net::ServerSocket server_socket(config().getInt("Service.port", port_)); Poco::Net::HTTPServer http_server( new RequestFactory(context_), server_socket, new Poco::Net::HTTPServerParams); http_server.start(); waitForTerminationRequest(); http_server.stop(); return Poco::Util::Application::EXIT_OK; }
int main(int argc, char *argv[]) { // Get a port number from the user if (argc <= 1) { std::cerr << "Usage: simple_http_server [port number]\n" << "E.g. simple_http_server 80" << std::endl; return 1; } std::string port(argv[1]); unsigned short portNumber(atoi(port.c_str())); std::cout << "HTTP server port: " << portNumber << std::endl; try { // create an io_service for the tcp port boost::asio::io_service io_service; // create an http_server http_server_type http_server(io_service, portNumber); // connect the request and chunk received callback functions http_server.request_received_event(request_receiver); http_server.chunk_received_event(chunk_receiver); // start accepting http connections http_server.start_accept(); // The signal set is used to register for termination notifications boost::asio::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) signals_.async_wait(boost::bind(&handle_stop)); // run the io_service to start communications io_service.run(); } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }
int main(int argc, char** argv) { stormy::SetupLoggers(); auto& logger = Poco::Logger::get("main"); logger.information("==== Aqcuisition started. ===="); auto station_settings = stormy::LoadStationsFromFile("config/meteo_stations.json"); auto metrics_settings = stormy::LoadMeteoElementsFromFile("config/meteo_elements.json"); // register Python-based parse scripts stormy::PyScriptStorage central_storage; central_storage.Push("AGHReader", new stormy::PyParseScript("./AGHReader.py")); central_storage.Push("ECOCLIMA_MeteoParser", new stormy::PyParseScript("./ECOCLIMA_MeteoParser.py")); central_storage.Push("StacjameteoReader", new stormy::PyParseScript("./StacjameteoReader.py")); auto& db_handler = stormy::db::MongoHandler::get(); db_handler.set_expiration_seconds(3600 * 72); db_handler.ClearStations(); db_handler.InsertStations(station_settings); db_handler.ClearMeteoElements(); db_handler.InsertMeteoElements(metrics_settings); stormy::util::TaskScheduler acq_scheduler; for (auto station : station_settings) { auto parse_script = central_storage.Fetch(station.parse_script); if (parse_script != nullptr) { stormy::AcquisitionContext task_context = { 0, stormy::common::SecondsToMiliseconds(station.update_time), station, db_handler, parse_script }; acq_scheduler.Schedule<stormy::AcquisitionTask>(task_context); } else { logger.error("Missing parse script with class: " + station.parse_script); } } stormy::common::db::expiration::Engine expiration_engine(db_handler); expiration_engine.ScheduleEverySeconds(3600); stormy::net::AcquisitionHttpServer http_server({db_handler}, 8080); http_server.run(argc, argv); getchar(); }
int main(int argc, char **argv) { //av_register_all(); //avformat_network_init(); unsetenv("http_proxy"); /* Kill the http_proxy */ parse_config(); signal(SIGPIPE, SIG_IGN); if (http_server() < 0) { http_log("Could not start server\n"); exit(1); } return 0; }
int main(int argc, char *argv[]) { int port = 8000; printf("Usage: %s [port=8000]\n", argv[0]); printf("try 'curl http://localhost:port/xxx/\n"); printf(" or 'curl http://localhost:port/thread/xxx/\n"); printf(" or 'curl -d \"MSG\" http://localhost:port/xxx/\n"); printf(" or 'curl -d \"MSG\" http://localhost:port/thread/xxx/\n"); if (argc > 1) port = atoi(argv[1]); // should catch execeptions if not sure the port is valid tws::HttpServer http_server(port, &tws::HttpServer::default_handler, 4); http_server.run(); return 0; }
/* * Configuration thread. */ static void *configuration_thread(void *arg) { int port = (options_get()->seen_ui_port? options_get()->val_ui_port: PROGRAM_UI_PORT); if (port <= 0 || port > UINT16_MAX) { error("unable to start user interface server; expected a port number " "0..%u, found %d", UINT16_MAX, port); } struct config_s config; config_get(&config); bool launch = !options_get()->seen_no_launch_ui && config.launch_ui; // Register an exit handler. http_register_callback("exit", user_exit); log("starting %s user interface http://localhost:%u/", PROGRAM_NAME, port); http_server(port, config_callback, launch); return NULL; }
void start_http_server(sp_uint32 _port, sp_uint32 _nkeys, int fd) { nkeys = _nkeys; EventLoopImpl ss; // set host, port and packet size NetworkOptions options; options.set_host(LOCALHOST); options.set_port(_port); options.set_max_packet_size(BUFSIZ << 4); // start the server TestHttpServer http_server(&ss, options); // use pipe to block clients before server enters event loop int sent; write(fd, &sent, sizeof(int)); ss.loop(); }
int main(int argc, char *argv[]) { std::string app_name(argv[0]); unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT); // Get a port number from the user (the default is 80) if (argc > 2) { std::cerr << "Usage: " << app_name << " [port number]\n" << "E.g. " << app_name << " " << port_number << std::endl; return 1; } else if (argc == 2) { std::string port(argv[1]); port_number = atoi(port.c_str()); } std::cout << app_name << ": " << port_number << std::endl; try { // create an io_service for the server ASIO::io_service io_service; // create an http_server and connect the request handler http_server_type http_server(io_service); http_server.request_received_event(request_handler); // connect the optional handler callback functions http_server.chunk_received_event(chunk_handler); http_server.request_expect_continue_event(expect_continue_handler); http_server.invalid_request_event(invalid_request_handler); http_server.socket_connected_event(connected_handler); http_server.socket_disconnected_event(disconnected_handler); http_server.message_sent_event(message_sent_handler); // set the connection timeout (10 seconds) http_server.set_timeout(10000); // set the connection buffer sizes http_server.set_rx_buffer_size(16384); http_server.tcp_server()->set_receive_buffer_size(16384); http_server.tcp_server()->set_send_buffer_size(16384); // start accepting http connections on the port ASIO_ERROR_CODE error(http_server.accept_connections(port_number)); if (error) { std::cerr << "Error: " << error.message() << std::endl; return 1; } // The signal set is used to register termination notifications ASIO::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) // register the handle_stop callback signals_.async_wait([&http_server] (ASIO_ERROR_CODE const& error, int signal_number) { handle_stop(error, signal_number, http_server); }); // run the io_service to start communications io_service.run(); std::cout << "io_service.run complete, shutdown successful" << std::endl; } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { NPT_COMPILER_UNUSED(argc); NPT_HttpRequestHandler *handler, *custom_handler; NPT_Reference<NPT_DataBuffer> buffer; NPT_Size size; bool result; PLT_RingBufferStreamReference ringbuffer_stream(new PLT_RingBufferStream()); /* parse command line */ ParseCommandLine(argv); /* create http server */ PLT_HttpServer http_server(Options.port?Options.port:80); NPT_String url = "http://127.0.0.1:" + NPT_String::FromInteger(http_server.GetPort()); NPT_String custom_url = url; if (!Options.path.IsEmpty()) { /* extract folder path */ int index1 = Options.path.ReverseFind('\\'); int index2 = Options.path.ReverseFind('/'); if (index1 <= 0 && index2 <=0) { fprintf(stderr, "ERROR: invalid path\n"); exit(1); } NPT_DirectoryEntryInfo info; NPT_CHECK_SEVERE(NPT_DirectoryEntry::GetInfo(Options.path, &info)); /* add file request handler */ handler = new NPT_HttpFileRequestHandler( Options.path.Left(index1>index2?index1:index2), "/"); http_server.AddRequestHandler(handler, "/", true); /* build url*/ url += "/" + Options.path.SubString((index1>index2?index1:index2)+1); } else { /* create random data */ buffer = new NPT_DataBuffer(32768); buffer->SetDataSize(32768); /* add static handler */ handler = new NPT_HttpStaticRequestHandler(buffer->GetData(), buffer->GetDataSize(), "text/xml"); http_server.AddRequestHandler(handler, "/test"); /* build url*/ url += "/test"; } /* add custom handler */ NPT_InputStreamReference stream(ringbuffer_stream); custom_handler = new PLT_HttpCustomRequestHandler(stream, "text/xml"); http_server.AddRequestHandler(custom_handler, "/custom"); custom_url += "/custom"; /* start server */ NPT_CHECK_SEVERE(http_server.Start()); /* a task manager for the tests downloader */ PLT_TaskManager task_manager; /* small delay to let the server start */ NPT_System::Sleep(NPT_TimeInterval(1, 0)); /* execute tests */ result = Test1(&task_manager, url.GetChars(), size); if (!result) return -1; result = Test2(&task_manager, url.GetChars(), size); if (!result) return -1; result = Test3(&task_manager, custom_url.GetChars(), ringbuffer_stream, size); if (!result) return -1; NPT_System::Sleep(NPT_TimeInterval(1, 0)); http_server.Stop(); delete handler; delete custom_handler; return 0; }
int connection_handler(struct http_server_config *cfg) { int listen_fd, new_fd, set = 1; struct sockaddr_in listener_addr, client_addr; socklen_t addr_len = 0; pid_t pid; char p[10]; assert(cfg != NULL); /* Standard server side socket sequence*/ if((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { LOG(stdout, "socket() failure\n"); return -1; } if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(int)) == -1) { LOG(stdout, "setsockopt() failed"); return -1; } bzero(&listener_addr, sizeof(listener_addr)); listener_addr.sin_family = AF_INET; listener_addr.sin_port = htons(cfg->listen_port); listener_addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(listener_addr.sin_zero, '\0', sizeof(listener_addr.sin_zero)); if(bind(listen_fd, (struct sockaddr*)&listener_addr, sizeof listener_addr) == -1) { LOG(stdout, "bind() failed\n"); return -1; } if(listen(listen_fd, PENDING_CONN) == -1) { LOG(stdout, "listen() failed\n"); return -1; } sprintf(p, "HTTP server listening on port:%d\n", cfg->listen_port); LOG(stdout, p); while(1) { new_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &addr_len); if(new_fd == -1) { //log LOG(stdout, "accept() failed\n"); return -1; } LOG(stdout, "new connection accepted\n"); //fork a new process to handle this request if((pid = fork()) == -1) { //LOG Error LOG(stdout, "Error in fork\n"); } else if(pid == 0) { /*This is the child process. This will service the *request while parent goes back to listening.*/ LOG(stdout, "Servicing request\n"); http_server(cfg, new_fd); return 0; } else { close(new_fd); } } return 0; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { NPT_COMPILER_UNUSED(argc); NPT_HttpRequestHandler* handler; NPT_Reference<NPT_DataBuffer> buffer; bool result; /* parse command line */ ParseCommandLine(argv); /* create http server */ PLT_HttpServer http_server(Options.port?Options.port:8089); NPT_String url; if (!Options.path.IsEmpty()) { /* extract folder path */ int index1 = Options.path.ReverseFind('\\'); int index2 = Options.path.ReverseFind('/'); if (index1 <= 0 && index2 <=0) { fprintf(stderr, "ERROR: invalid path\n"); exit(1); } NPT_FileInfo info; NPT_CHECK_SEVERE(NPT_File::GetInfo(Options.path, &info)); /* add file request handler */ handler = new NPT_HttpFileRequestHandler( Options.path.Left(index1>index2?index1:index2), "/"); http_server.AddRequestHandler(handler, "/", true); /* build url */ url = "/" + Options.path.SubString((index1>index2?index1:index2)+1); } else { /* create random garbage data */ buffer = new NPT_DataBuffer(32768); buffer->SetDataSize(32768); /* add static handler */ handler = new NPT_HttpStaticRequestHandler(buffer->GetData(), buffer->GetDataSize(), "application/octet-stream"); http_server.AddRequestHandler(handler, "/test"); /* build url */ url = "/test"; } /* add custom handler */ PLT_RingBufferStreamReference ringbuffer_stream(new PLT_RingBufferStream()); NPT_InputStreamReference stream(ringbuffer_stream); NPT_HttpRequestHandler* custom_handler = new PLT_HttpCustomRequestHandler(stream, "text/xml"); http_server.AddRequestHandler(custom_handler, "/custom"); /* start server */ NPT_CHECK_SEVERE(http_server.Start()); /* a task manager for the tests downloader */ PLT_TaskManager task_manager; /* small delay to let the server start */ NPT_System::Sleep(NPT_TimeInterval(1.f)); /* execute tests */ NPT_Size size; NPT_COMPILER_UNUSED(size); #ifdef TEST1 result = Test1(&task_manager, NPT_HttpUrl("127.0.0.1", http_server.GetPort(), url), size); if (!result) return -1; #endif #ifdef TEST2 result = Test2(&task_manager, NPT_HttpUrl("127.0.0.1", http_server.GetPort(), url), size); if (!result) return -1; #endif #ifdef TEST3 result = Test3(&task_manager, NPT_HttpUrl("127.0.0.1", http_server.GetPort(), "/custom"), ringbuffer_stream, size); if (!result) return -1; #endif #ifdef TEST4 result = Test4(&task_manager, NPT_HttpUrl("127.0.0.1", http_server.GetPort(), "/custom"), NPT_TimeInterval(.1f)); if (!result) return -1; result = Test4(&task_manager, NPT_HttpUrl("127.0.0.1", http_server.GetPort(), "/custom"), NPT_TimeInterval(1.f)); if (!result) return -1; result = Test4(&task_manager, NPT_HttpUrl("127.0.0.1", http_server.GetPort(), "/custom"), NPT_TimeInterval(2.f)); if (!result) return -1; #endif #ifdef TEST5 result = Test5(NPT_HttpUrl("127.0.0.1", http_server.GetPort(), "/test")); if (!result) return -1; #endif NPT_System::Sleep(NPT_TimeInterval(1.f)); // abort server tasks that are waiting on ring buffer stream ringbuffer_stream->Abort(); http_server.Stop(); return 0; }
int main (int argc, char* argv[]) { /* * general definitions */ FILE *config_file; int retv; int start_http, start_https; char *c_token; /* read options */ if (argc > 1) { if (!strcmp("https", argv[0])) { printf("https\n"); start_http = 0; start_https = 1; } else if (!strcmp("http", argv[0])) { printf("http\n"); start_http = 1; start_https = 0; } else { printf("Incorrect option, usage:\n"); printf("server - starts both servers\n"); printf("server http - starts http server only"); printf("server https - starts https server only"); } } else { printf("oba\n"); start_http = 1; start_https = 1; } GDBM_FILE database = gdbm_open("session.db", 0, GDBM_WRCREAT | GDBM_NOMMAP , S_IRWXU, NULL); gdbm_close(database); /* read configuration file * recognized tokens: * * http: * HTTP_PORT listening port for http server * HTTP_MAX_CONN maximum number of simultaneous connections * HTTP_DIR http resources directory * HTTP_GET script invoked for GET method * HTTP_POST script invoked for POST method * * https: * HTTPS_PORT listening port for http server * HTTPS_MAX_CONN maximum number of simultaneous connections * HTTPS_DIR http resources directory * HTTPS_GET script invoked for GET method * HTTPS_POST script invoked for POST method * HTTPS_CERT certificate file * HTTPS_KEY private key file * HTTPS_PASS password for private key * HTTP_METH https method (SSLv2, SSLv23, SSLv3, TLSv1) */ /* config_file = fopen("server.conf", "r"); setvbuf(config_file, NULL, _IOFBF, 256); if (unlikely(config_file < 0)) { perror("Cannot read configuration file (server.conf)"); exit(EXIT_FAILURE); } c_token = strtok(config_file, " \n"); while(!feof(config_file)) { switch(strcmp( c_token = strtok(NULL, " \n"); } */ http_s.port = 80; http_s.conn_num = 32; http_s.dir = "http"; http_s.get_script = "get.sh"; http_s.post_script = "post.sh"; https_s.port = 443; https_s.conn_num = 32; https_s.dir = "https"; https_s.get_script = "get.sh"; https_s.post_script = "post.sh"; https_d.certificate_file = "mycert.pem"; https_d.key_file = "mykey.pem"; https_d.key_password = "******"; https_d.method = "TLSv1"; https_d.sess_db_name = "session.db"; https_d.admin_username = "******"; https_d.admin_password = "******"; https_d.guest_username = "******"; https_d.guest_password = "******"; if (start_http && start_https) { if (fork()) { printf("http server working\n"); http_server(); } else { https_server(); } } else { if (start_http) http_server(); if (start_https) https_server(); } }
int main(int argc, char **argv) { HttpRequest *req = NULL; int port = 8000; int c; char *didiwiki_home = NULL; int restore_WikiHelp = 0; struct in_addr address; /* default values */ debug = 0; //normal mode hostlogin = 0; //host will have to login nologin = 0; //users will have to login. dosendmail = 0; //don't send systematically email at each registration lgindex = 20; //print 20 files before to make a new index box /* by default bind server to "0.0.0.0" */ address.s_addr = inet_addr("0.0.0.0"); while (1) { static struct option long_options[] = { {"autologin", no_argument, 0, 'a'}, {"nologin", no_argument, 0, 'n'}, {"debug", no_argument, 0, 'd'}, {"version", no_argument, 0, 'v'}, {"listen", required_argument, 0, 'l'}, {"port", required_argument, 0, 'p'}, {"home", required_argument, 0, 'h'}, {"restore", no_argument, 0, 'r'}, {"sendmail", no_argument, 0, 's'}, {"index", required_argument, 0, 'i'}, {"help", no_argument, 0, 10 }, {0, 0, 0, 0} }; /* getopt_long stores the option index here */ int option_index = 0; c = getopt_long (argc, argv, "adl:p:h:i:rsv", long_options, &option_index); /* detect the end of the options */ if (c == -1) break; switch (c) { case 0: break; case 'i': //set index length lgindex = atoi(optarg); if (lgindex==0) lgindex=20; fprintf(stderr,"Index length = %i\n",lgindex); break; case 'a': //autologin for the localhost hostlogin = 1; fprintf(stderr,"Localhost is logged in.\n"); break; case 'n': //autologin any user nologin = 1; fprintf(stderr,"Any user registrered or not will be logged in.\n"); break; case 'd': debug = 1; break; case 'v': printf("CiWiki alias DidiWiki - version %s\n\n",VERSION); return 0; case 'p': //default port is 8000 port = atoi(optarg); break; case 'h': //default home directory is ~/.didiwiki didiwiki_home = optarg; break; case 'l': //listen a inet address if(inet_aton(optarg,&address) == 0) { fprintf(stderr, "didiwiki: invalid ip address \"%s\"\n", optarg); exit(1); } else address.s_addr = inet_addr(optarg); break; case 'r': //rewrite Wikihelp page restore_WikiHelp=1; break; case 's': dosendmail= 1; break; case 10: usage(); break; default: usage(); } } //end while wiki_init(didiwiki_home,restore_WikiHelp); if (debug) { req = http_request_new(); /* reads request from stdin */ } else { req = http_server(address, port); /* forks here */ } wiki_handle_http_request(req); return 0; }
int main(int argc, char *argv[]) { std::string app_name(argv[0]); unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT); // Get a port number from the user (the default is 80) if (argc > 2) { std::cerr << "Usage: " << app_name << " [port number]\n" << "E.g. " << app_name << " " << port_number << std::endl; return 1; } else if (argc == 2) { std::string port(argv[1]); port_number = atoi(port.c_str()); } std::cout << app_name << ": " << port_number << std::endl; try { // create an io_service for the server boost::asio::io_service io_service; // create an http_server and connect the request handler http_server_type http_server(io_service); http_server.request_received_event(request_handler); // connect the handler callback functions http_server.chunk_received_event(chunk_handler); http_server.request_expect_continue_event(expect_continue_handler); http_server.invalid_request_event(invalid_request_handler); http_server.socket_connected_event(connected_handler); http_server.socket_disconnected_event(disconnected_handler); http_server.message_sent_event(message_sent_handler); // start accepting http connections on the given port boost::system::error_code error(http_server.accept_connections(port_number)); if (error) { std::cerr << "Error: " << error.message() << std::endl; return 1; } // The signal set is used to register for termination notifications boost::asio::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) // register the handle_stop callback signals_.async_wait([&http_server] (boost::system::error_code const& error, int signal_number) { handle_stop(error, signal_number, http_server); }); // Determine the number of concurrent threads supported size_t no_of_threads(std::thread::hardware_concurrency()); std::cout << "No of threads: " << no_of_threads << std::endl; if (no_of_threads > 0) { // Create a thread pool for the threads and run the asio io_service // in each of the threads. std::vector<std::shared_ptr<std::thread> > threads; for (std::size_t i = 0; i < no_of_threads; ++i) { std::shared_ptr<std::thread> thread(std::make_shared<std::thread> ([&io_service](){ io_service.run(); })); threads.push_back(thread); } // Wait for all threads in the pool to exit. for (std::size_t i(0); i < threads.size(); ++i) threads[i]->join(); } else io_service.run(); std::cout << "io_service.run, all work has finished" << std::endl; } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }