static void playlistcontainer_loaded(sp_playlistcontainer *pc, void *userdata) { syslog(LOG_DEBUG, "playlistcontainer_loaded\n"); sp_session *session = userdata; struct state *state = sp_session_userdata(session); sp_playlistcontainer_remove_callbacks(pc, &playlistcontainer_callbacks, session); state->http = evhttp_new(state->event_base); evhttp_set_timeout(state->http, 60); evhttp_set_gencb(state->http, &handle_request, state); // Bind HTTP server int bind = evhttp_bind_socket(state->http, state->http_host, state->http_port); if (bind == -1) { syslog(LOG_WARNING, "Could not bind HTTP server socket to %s:%d", state->http_host, state->http_port); sp_session_logout(session); return; } syslog(LOG_DEBUG, "HTTP server listening on %s:%d", state->http_host, state->http_port); }
TEvhttpServer::TEvhttpServer(boost::shared_ptr<TAsyncBufferProcessor> processor, int port) : processor_(processor), eb_(NULL), eh_(NULL) { // Create event_base and evhttp. eb_ = event_base_new(); if (eb_ == NULL) { throw TException("event_base_new failed"); } eh_ = evhttp_new(eb_); if (eh_ == NULL) { event_base_free(eb_); throw TException("evhttp_new failed"); } // Bind to port. int ret = evhttp_bind_socket(eh_, NULL, port); if (ret < 0) { evhttp_free(eh_); event_base_free(eb_); throw TException("evhttp_bind_socket failed"); } // Register a handler. If you use the other constructor, // you will want to do this yourself. // Don't forget to unregister before destorying this TEvhttpServer. evhttp_set_cb(eh_, "/", request, (void*)this); }
static struct evhttp * http_setup(short *pport, struct event_base *base) { int i; struct evhttp *myhttp; short port = -1; /* Try a few different ports */ myhttp = evhttp_new(base); for (i = 0; i < 50; ++i) { if (evhttp_bind_socket(myhttp, "127.0.0.1", 8080 + i) != -1) { port = 8080 + i; break; } } if (port == -1) event_errx(1, "Could not start web server"); /* Register a callback for certain types of requests */ evhttp_set_cb(myhttp, "/test", http_basic_cb, NULL); evhttp_set_cb(myhttp, "/chunked", http_chunked_cb, NULL); evhttp_set_cb(myhttp, "/postit", http_post_cb, NULL); evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, NULL); evhttp_set_cb(myhttp, "/", http_dispatcher_cb, NULL); *pport = port; return (myhttp); }
int main(int argc, char** argv) { (void) signal(SIGINT, ex_program); event_init(); server = evhttp_new(0); if (!server) { fprintf(stderr,"[ERROR]\tError creating the server\n"); return 1; } int rc = evhttp_bind_socket(server, "127.0.0.1", 30080); if (rc) { fprintf(stderr, "[ERROR]\tError binding server to port!!!\n"); return 1; } /* Add a handler to handle the request */ /* default handler */ evhttp_set_cb(server, "/HEST", generic_request_handler, NULL); evhttp_set_gencb(server, generic_request_handler, NULL); fprintf(stderr, "[INFO]\tThe server is totally waiting for real!!!\n"); event_dispatch(); /* Brooom, brooom */ return 0; }
SinoparserServer::SinoparserServer(std::string address, int port, Database& ndb) { db = ndb; struct event_base *base = event_init(); struct evhttp *server = evhttp_new(base); int res = evhttp_bind_socket(server, address.c_str(), port); if (res != 0) { std::cout << "[ERROR] Could not start http server!" << std::endl; return; } db.debug(); evhttp_set_gencb(server, http_callback_default, this); evhttp_set_cb(server, "/pinyin", http_pinyin_callback, this); evhttp_set_cb(server, "/jyutping", http_jyutping_callback, this); evhttp_set_cb(server, "/trad", http_trad_callback, this); evhttp_set_cb(server, "/simp", http_simp_callback, this); evhttp_set_cb(server, "/guess_script", http_guess_script_callback, this); evhttp_set_cb(server, "/change_script", http_change_script_callback, this); evhttp_set_cb(server, "/all", http_all_callback, this); event_base_dispatch(base); }
static void StartEventHandler(KonohaContext *kctx, void *args) { KNH_ASSERT(EVENTAPI eventContext == NULL); struct EventContext *eventContext = (struct EventContext *)PLATAPI malloc_i(sizeof(struct EventContext)); bzero(eventContext, sizeof(struct EventContext)); ((KonohaFactory *)kctx->platApi)->EventModule.eventContext = eventContext; eventContext->safePointRef = (int *)&kctx->safepoint; eventContext->queue = (LocalQueue *)PLATAPI malloc_i(sizeof(LocalQueue)); LocalQueue_Init(kctx, eventContext->queue); eventContext->httpContext = httpContext; pthread_mutex_init(&eventContext->lock, NULL); pthread_cond_init(&eventContext->cond, NULL); KNH_ASSERT(args != NULL); Ip *ip = (Ip *)args; struct event_base *base = event_base_new(); struct evhttp *httpd = evhttp_new(base); KNH_ASSERT(evhttp_bind_socket(httpd, ip->host, ip->port) >= 0); evhttp_set_gencb(httpd, http_handler, (void *)eventContext); eventContext->base = base; eventContext->httpd = httpd; pthread_t t; pthread_create(&t, NULL, HttpEventListener, (void *)eventContext); }
int main (void) { struct event_base *ebase; struct evhttp *server; // Create a new event handler ebase = event_base_new ();; // Create a http server using that handler server = evhttp_new (ebase); // Limit serving GET requests evhttp_set_allowed_methods (server, EVHTTP_REQ_GET); // Set a test callback, /testing evhttp_set_cb (server, "/testing", testing, 0); // Set the callback for anything not recognized evhttp_set_gencb (server, notfound, 0); // Listen locally on port 32001 if (evhttp_bind_socket (server, "127.0.0.1", 32001) != 0) errx (1, "Could not bind to 127.0.0.1:32001"); // Start processing queries event_base_dispatch(ebase); // Free up stuff evhttp_free (server); event_base_free (ebase); }
int ulEventHttpServerSetup(const char *address, unsigned short port, const char* path, data_processor_function data_processor) { struct evhttp* evh = NULL; debug("Setting up event-based http server listening at %s:%d on path: %s", address, port, path); evh = evhttp_new(eventbase); if(evh != NULL) { info("Event-based http server at %s:%d on path: %s has been setup", address, port, path); } else { error("Setup of event-based http server at %s:%d on path: %s FAILED", address, port, path); return UL_RETURN_FAIL; } if(evhttp_bind_socket(evh, address, port) != -1) { info("Event-based http server socket bind with %s:%d OK", address, port); } else { error("Bind of event-based http server with %s:%d FAILED", address, port); return UL_RETURN_FAIL; } //when a request for a generic path comes to the server, trigger the ulEventHttpServerProcessRequest //function and also pass to it a pointer to an external data_processor function, which is //able to handle the received data specifically evhttp_set_gencb(evh, ulEventHttpServerProcessRequest, data_processor); sprintf(input_http_path, "/%s", path); debug("Setting up static path to: %s", input_http_path); return UL_RETURN_OK; }
int main (int argc, char **argv) { struct event_base *base = event_base_new(); struct evhttp *http = evhttp_new(base); int c; unsigned short port = 8080; while ((c = getopt(argc, argv, "p:l:")) != -1) { switch (c) { case 'p': port = atoi(optarg); break; case 'l': content_len = atol(optarg); if (content_len == 0) { fprintf(stderr, "Bad content length\n"); exit(1); } break; default: fprintf(stderr, "Illegal argument \"%c\"\n", c); exit(1); } } #ifndef WIN32 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return (1); #endif content = malloc(content_len); if (content == NULL) { fprintf(stderr, "Cannot allocate content\n"); exit(1); } else { int i = 0; for (i = 0; i < content_len; ++i) content[i] = (i & 255); } evhttp_set_cb(http, "/ind", http_basic_cb, NULL); fprintf(stderr, "/ind - basic content (memory copy)\n"); #ifdef _EVENT2_EVENT_H_ evhttp_set_cb(http, "/ref", http_ref_cb, NULL); fprintf(stderr, "/ref - basic content (reference)\n"); #endif fprintf(stderr, "Serving %d bytes on port %d\n", (int)content_len, port); evhttp_bind_socket(http, "0.0.0.0", port); event_base_dispatch(base); /* NOTREACHED */ return (0); }
static void start_srv (struct event_base *base, gchar *in_dir) { struct evhttp *http; http = evhttp_new (base); evhttp_bind_socket (http, "127.0.0.1", 8011); evhttp_set_gencb (http, on_srv_request, in_dir); }
static void start_srv (struct event_base *base, gchar *in_dir) { app->http_srv = evhttp_new (base); g_assert (app->http_srv); evhttp_bind_socket (app->http_srv, "127.0.0.1", 8011); evhttp_set_gencb (app->http_srv, on_srv_gen_request, in_dir); LOG_debug (HTTP_TEST, "SRV: started"); }
int upnpc_event_subscribe(upnpc_device_t * p) { char hostname[MAXHOSTNAMELEN+1]; char hostname_port[MAXHOSTNAMELEN+1+6]; unsigned short port; char * path; unsigned int scope_id; struct evhttp_request * req; struct evkeyvalq * headers; char callback_header[7+15+1+5+9+2+1]; if(p->parent->http_server == NULL) { /* HTTP server to receive event notifications */ p->parent->http_server = evhttp_new(p->parent->base); if(p->parent->http_server == NULL) { debug_printf("evhttp_new() FAILED\n"); return -1; } evhttp_set_ext_method_cmp(p->parent->http_server, ext_methods_cb); evhttp_set_allowed_methods(p->parent->http_server, EVHTTP_REQ_NOTIFY); evhttp_set_cb(p->parent->http_server, "/evt_conn", upnpc_event_conn_req, p); if(evhttp_bind_socket(p->parent->http_server, p->parent->local_address, p->parent->local_port) < 0) { debug_printf("evhttp_bind_socket() FAILED\n"); return -1; } } /*if(!parseURL(p->event_cif_url, hostname, &port, &path, &scope_id)) {*/ if(!parseURL(p->event_conn_url, hostname, &port, &path, &scope_id)) { return -1; } if(port != 80) snprintf(hostname_port, sizeof(hostname_port), "%s:%hu", hostname, port); else strncpy(hostname_port, hostname, sizeof(hostname_port)); if(p->soap_conn == NULL) { p->soap_conn = evhttp_connection_base_new(p->parent->base, NULL, hostname, port); } evhttp_connection_set_ext_method_cmp(p->soap_conn, ext_methods_cb); req = evhttp_request_new(upnpc_subscribe_response, p); headers = evhttp_request_get_output_headers(req); /*buffer = evhttp_request_get_output_buffer(req);*/ evhttp_add_header(headers, "Host", hostname_port); /*evhttp_add_header(headers, "User-Agent", "***");*/ snprintf(callback_header, sizeof(callback_header), "<http://%s:%hu/evt_conn>", p->parent->local_address, p->parent->local_port); evhttp_add_header(headers, "Callback", callback_header); evhttp_add_header(headers, "NT", "upnp:event"); /*evhttp_add_header(headers, "NTS", "");*/ evhttp_add_header(headers, "Timeout", "3600"); /*evbuffer_add(buffer, body, body_len);*/ evhttp_make_request(p->soap_conn, req, EVHTTP_REQ_SUBSCRIBE, path); p->state |= UPNPC_DEVICE_SOAP_REQ; return 0; }
HttpServer::HttpServer(std::string base_addr, int base_port) { base = NULL; base = event_init(); internal_url = base_addr; internal_port = base_port; // Bind the address to the internal callback method struct evhttp *httpd; httpd = evhttp_new(base); if (httpd != NULL) { if (evhttp_bind_socket(httpd, internal_url.c_str(), internal_port) == 0) { evhttp_set_gencb(httpd, process_request, NULL); } } }
static void run_request_test (struct event_base *evbase, struct evdns_base *dns_base, TestID test_id) { OutData *out; struct evbuffer *in_buf; char c = 'x'; LOG_debug (HTTP_TEST, "===================== TEST ID : %d =======================", test_id); out = g_new0 (OutData, 1); out->evbase = evbase; out->test_id = test_id; out->evhttp = evhttp_new (evbase); evhttp_bind_socket (out->evhttp, "127.0.0.1", 8080); evhttp_set_gencb (out->evhttp, on_request_gencb, out); //out->http = http_client_create (evbase, dns_base); in_buf = evbuffer_new (); http_client_set_cb_ctx (out->http, out); http_client_set_on_chunk_cb (out->http, on_input_data_cb); http_client_set_close_cb (out->http, on_http_close); //http_client_set_output_length (out->http, 1); //http_client_add_output_data (out->http, &c, 1); http_client_start_request_to_storage_url (out->http, Method_get, "/index.html", NULL, NULL); event_base_dispatch (evbase); http_client_destroy (out->http); LOG_debug (HTTP_TEST, "Resulting buff: %zd", evbuffer_get_length (in_buf)); evbuffer_free (in_buf); g_free (out->first_line); g_free (out->header_line); evconnlistener_free (out->listener); evtimer_del (out->timeout); event_free (out->timeout); evbuffer_free (out->out_buf); evbuffer_free (out->in_file); g_free (out); LOG_debug (HTTP_TEST, "===================== END TEST ID : %d =======================", test_id); }
int main (int argc, char **av) { struct event_base *ev_base; struct evhttp *httpd; ev_base = event_base_new(); httpd = evhttp_new(ev_base); if (evhttp_bind_socket(httpd, HTTPD_ADDR, HTTPD_PORT) < 0) { perror("evhttp_bind_socket"); exit(EXIT_FAILURE); } evhttp_set_gencb(httpd, req_handler, NULL); event_base_dispatch(ev_base); evhttp_free(httpd); event_base_free(ev_base); return 0; }
static void playlistcontainer_loaded(sp_playlistcontainer *pc, void *userdata) { syslog(LOG_DEBUG, "playlistcontainer_loaded\n"); sp_session *session = userdata; struct state *state = sp_session_userdata(session); sp_playlistcontainer_remove_callbacks(pc, &playlistcontainer_callbacks, session); state->http = evhttp_new(state->event_base); evhttp_set_timeout(state->http, 60); evhttp_set_gencb(state->http, &handle_request, state); // TODO(liesen): Make address and port configurable if (evhttp_bind_socket(state->http, "0.0.0.0", 1337) == -1) { syslog(LOG_WARNING, "Could not bind HTTP server socket"); sp_session_logout(session); } }
int http_init(void (*handler) (struct evhttp_request *req, void *arg)) { struct event_base * base = event_base_new(); struct evhttp * http_server = evhttp_new(base); if (!http_server) { printf("evhttp_new error\n"); return -1; } int ret = evhttp_bind_socket(http_server, HTTP_HOST, HTTP_PORT); if (ret != 0) { printf("bind socket error\n"); return -1; } evhttp_set_gencb(http_server, http_request_handler, NULL); printf("http server listen: \n"); event_base_dispatch(base); evhttp_free(http_server); }
static char *getUserInput(KonohaContext *kctx, char *buff, const char *cid, const char *host, int port) { struct event_base *base = event_base_new(); struct evhttp *httpd = evhttp_new(base); if(evhttp_bind_socket(httpd, host, port) < 0) { PLATAPI LoggerModule.syslog_i(5/*LOG_NOTICE*/, "{\"Method\": \"DScriptError\", \"CId\": \"%s\", \"Body\": \"couldn't bind socket\"}", cid); exit(1); } UserInput ui = {}; ui.base = base; ui.buff = buff; evhttp_set_gencb(httpd, userInput2Buff, (void *)&ui); event_base_dispatch(base); evhttp_free(httpd); event_base_free(base); return buff; }
void httpServerThread(GithubWebhooks * parent, std::uint16_t port) { parent->_eventBase = event_base_new(); parent->_evhttp = evhttp_new(parent->_eventBase); parent->_breakLoop = event_new(parent->_eventBase, -1, EV_READ, terminateServer, parent); event_add(parent->_breakLoop, nullptr); if (evhttp_bind_socket(parent->_evhttp , "0.0.0.0", port) == -1) { LOG(ERROR) << "Can't bind socket on port " << port; return; } evhttp_set_gencb(parent->_evhttp , httpHandler, parent); evthread_use_pthreads(); evthread_make_base_notifiable(parent->_eventBase); if (event_base_dispatch(parent->_eventBase) == -1) LOG(ERROR) << "Failed to start event loop"; }
int webserver_init(void) { struct evhttp *httpd; httpd = evhttp_new(base); evutil_gettimeofday(&start_time, NULL); if (httpd == NULL) return -1; if (evhttp_bind_socket(httpd, bind_addr, server_port) == -1) return -1; #ifdef WITH_GEOIP if (gi) geoip_header = "<th>Country</th>"; else geoip_header = ""; #else geoip_header = ""; #endif evhttp_set_cb(httpd, "/holddowns", httpd_put_holddowns, NULL); evhttp_set_cb(httpd, "/holddowns.html", httpd_put_holddowns_html, NULL); evhttp_set_cb(httpd, "/config", httpd_put_config, NULL); evhttp_set_cb(httpd, "/config.html", httpd_put_config, (void *)1); evhttp_set_cb(httpd, "/connections", httpd_put_connections, NULL); evhttp_set_cb(httpd, "/connections.html", httpd_put_connections_html, NULL); evhttp_set_cb(httpd, "/addrs", httpd_put_addrs, NULL); evhttp_set_cb(httpd, "/addrs.html", httpd_put_addrs_html, NULL); evhttp_set_cb(httpd, "/uris", httpd_put_uris, NULL); evhttp_set_cb(httpd, "/uris.html", httpd_put_uris_html, NULL); evhttp_set_cb(httpd, "/hosts", httpd_put_hosts, NULL); evhttp_set_cb(httpd, "/hosts.html", httpd_put_hosts_html, NULL); evhttp_set_cb(httpd, "/favicon.ico", httpd_put_favicon, NULL); evhttp_set_cb(httpd, "/action", httpd_action, NULL); evhttp_set_gencb(httpd, httpd_driver, NULL); return 0; }
StatSrv *stat_srv_create (Application *app) { StatSrv *stat_srv; stat_srv = g_new0 (StatSrv, 1); stat_srv->app = app; stat_srv->q_op_history = g_queue_new (); stat_srv->boot_time = time (NULL); // stats server is disabled if (!conf_get_boolean (application_get_conf (stat_srv->app), "statistics.enabled")) { return stat_srv; } stat_srv->http = evhttp_new (application_get_evbase (app)); if (!stat_srv->http) { LOG_err (STAT_LOG, "Failed to create statistics server !"); return NULL; } // bind if (evhttp_bind_socket (stat_srv->http, conf_get_string (application_get_conf (stat_srv->app), "statistics.host"), conf_get_int (application_get_conf (stat_srv->app), "statistics.port")) == -1) { LOG_err (STAT_LOG, "Failed to bind statistics server to %s:%d", conf_get_string (application_get_conf (stat_srv->app), "statistics.host"), conf_get_int (application_get_conf (stat_srv->app), "statistics.port") ); return NULL; } // install handlers evhttp_set_cb (stat_srv->http, conf_get_string (application_get_conf (stat_srv->app), "statistics.stats_path"), stat_srv_on_stats_cb, stat_srv); evhttp_set_gencb (stat_srv->http, stat_srv_on_gen_cb, stat_srv); return stat_srv; }
int main(int argc, char **argv) { struct event_config *cfg = ld_event_config_new(); struct event_base *base; struct evhttp *http; int i; int c; int use_iocp = 0; unsigned short port = 8080; char *endptr = NULL; #ifdef _WIN32 WSADATA WSAData; WSAStartup(0x101, &WSAData); #else if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return (1); #endif for (i = 1; i < argc; ++i) { if (*argv[i] != '-') continue; c = argv[i][1]; if ((c == 'p' || c == 'l') && i + 1 >= argc) { fprintf(stderr, "-%c requires argument.\n", c); exit(1); } switch (c) { case 'p': if (i+1 >= argc || !argv[i+1]) { fprintf(stderr, "Missing port\n"); exit(1); } port = (int)strtol(argv[i+1], &endptr, 10); if (*endptr != '\0') { fprintf(stderr, "Bad port\n"); exit(1); } break; case 'l': if (i+1 >= argc || !argv[i+1]) { fprintf(stderr, "Missing content length\n"); exit(1); } content_len = (size_t)strtol(argv[i+1], &endptr, 10); if (*endptr != '\0' || content_len == 0) { fprintf(stderr, "Bad content length\n"); exit(1); } break; #ifdef _WIN32 case 'i': use_iocp = 1; evthread_use_windows_threads(); ld_event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP); break; #endif default: fprintf(stderr, "Illegal argument \"%c\"\n", c); exit(1); } } base = ld_event_base_new_with_config(cfg); if (!base) { fprintf(stderr, "creating event_base failed. Exiting.\n"); return 1; } http = evhttp_new(base); content = malloc(content_len); if (content == NULL) { fprintf(stderr, "Cannot allocate content\n"); exit(1); } else { int i = 0; for (i = 0; i < (int)content_len; ++i) content[i] = (i & 255); } evhttp_set_cb(http, "/ind", http_basic_cb, NULL); fprintf(stderr, "/ind - basic content (memory copy)\n"); evhttp_set_cb(http, "/ref", http_ref_cb, NULL); fprintf(stderr, "/ref - basic content (reference)\n"); fprintf(stderr, "Serving %d bytes on port %d using %s\n", (int)content_len, port, use_iocp? "IOCP" : ld_event_base_get_method(base)); evhttp_bind_socket(http, "0.0.0.0", port); if (use_iocp) { struct timeval tv={99999999,0}; ld_event_base_loopexit(base, &tv); } ld_event_base_dispatch(base); /* NOTREACHED */ return (0); }
int main(int argc, char **argv) { int ret; ret = parse_options(&argc, &argv); if ( ret >= 0 ) return ret; moFactory::init(); signal(SIGTERM, signal_term); signal(SIGINT, signal_term); if ( config_pipelinefn != "" ) { pipeline = pipeline_parse_file(config_pipelinefn); if ( pipeline == NULL ) { return 2; } } else if ( config_httpserver == false ) { std::cerr << "ERROR : no pipeline or webserver to start!" << std::endl; return 3; } // no default pipeline ? create one ! if ( pipeline == NULL ) pipeline = new moPipeline(); if ( config_httpserver ) { #ifdef WIN32 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); #else signal(SIGPIPE, SIG_IGN); #endif base = event_init(); server = evhttp_new(NULL); evhttp_bind_socket(server, "127.0.0.1", 7500); evhttp_set_cb(server, "/", web_index, NULL); evhttp_set_cb(server, "/factory/list", web_factory_list, NULL); evhttp_set_cb(server, "/factory/describe", web_factory_desribe, NULL); evhttp_set_cb(server, "/pipeline/create", web_pipeline_create, NULL); evhttp_set_cb(server, "/pipeline/remove", web_pipeline_remove, NULL); evhttp_set_cb(server, "/pipeline/status", web_pipeline_status, NULL); evhttp_set_cb(server, "/pipeline/connect", web_pipeline_connect, NULL); evhttp_set_cb(server, "/pipeline/set", web_pipeline_set, NULL); evhttp_set_cb(server, "/pipeline/get", web_pipeline_get, NULL); evhttp_set_cb(server, "/pipeline/stream", web_pipeline_stream, NULL); evhttp_set_cb(server, "/pipeline/start", web_pipeline_start, NULL); evhttp_set_cb(server, "/pipeline/stop", web_pipeline_stop, NULL); evhttp_set_cb(server, "/pipeline/quit", web_pipeline_quit, NULL); evhttp_set_cb(server, "/pipeline/dump", web_pipeline_dump, NULL); evhttp_set_gencb(server, web_file, NULL); } while ( want_quit == false ) { // FIXME remove this hack !!! cvWaitKey(config_delay); // update pipeline if ( pipeline->isStarted() ) { pipeline->poll(); // check for error in pipeline while ( pipeline->haveError() ) { std::cerr << "Pipeline error: " << pipeline->getLastError() << std::endl; if ( test_mode ) want_quit = true; } } // got a server, update if ( server != NULL ) event_base_loop(base, EVLOOP_ONCE|EVLOOP_NONBLOCK); } if ( server != NULL ) evhttp_free(server); if ( base != NULL ) event_base_free(base); delete pipeline; moFactory::cleanup(); }
int LibEventServer::getAcceptSocket() { m_accept_sock = evhttp_bind_socket(m_server, m_address.c_str(), m_port); return m_accept_sock; }
/* Thread: main */ int httpd_init(void) { unsigned short port; int v6enabled; int ret; httpd_exit = 0; v6enabled = cfg_getbool(cfg_getsec(cfg, "general"), "ipv6"); evbase_httpd = event_base_new(); if (!evbase_httpd) { DPRINTF(E_FATAL, L_HTTPD, "Could not create an event base\n"); return -1; } ret = rsp_init(); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "RSP protocol init failed\n"); goto rsp_fail; } ret = daap_init(); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "DAAP protocol init failed\n"); goto daap_fail; } ret = dacp_init(); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "DACP protocol init failed\n"); goto dacp_fail; } #ifdef USE_EVENTFD exit_efd = eventfd(0, EFD_CLOEXEC); if (exit_efd < 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not create eventfd: %s\n", strerror(errno)); goto pipe_fail; } #else # if defined(__linux__) ret = pipe2(exit_pipe, O_CLOEXEC); # else ret = pipe(exit_pipe); # endif if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not create pipe: %s\n", strerror(errno)); goto pipe_fail; } #endif /* USE_EVENTFD */ #ifdef USE_EVENTFD event_set(&exitev, exit_efd, EV_READ, exit_cb, NULL); #else event_set(&exitev, exit_pipe[0], EV_READ, exit_cb, NULL); #endif event_base_set(evbase_httpd, &exitev); event_add(&exitev, NULL); evhttpd = evhttp_new(evbase_httpd); if (!evhttpd) { DPRINTF(E_FATAL, L_HTTPD, "Could not create HTTP server\n"); goto evhttp_fail; } port = cfg_getint(cfg_getsec(cfg, "library"), "port"); /* We are binding v6 and v4 separately, and we allow v6 to fail * as IPv6 might not be supported on the system. * We still warn about the failure, in case there's another issue. */ ret = evhttp_bind_socket(evhttpd, "0.0.0.0", port); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not bind INADDR_ANY:%d\n", port); goto bind_fail; } if (v6enabled) { ret = evhttp_bind_socket(evhttpd, "::", port); if (ret < 0) DPRINTF(E_WARN, L_HTTPD, "Could not bind IN6ADDR_ANY:%d (that's OK)\n", port); } evhttp_set_gencb(evhttpd, httpd_gen_cb, NULL); ret = pthread_create(&tid_httpd, NULL, httpd, NULL); if (ret != 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not spawn HTTPd thread: %s\n", strerror(errno)); goto thread_fail; } return 0; thread_fail: bind_fail: evhttp_free(evhttpd); evhttp_fail: #ifdef USE_EVENTFD close(exit_efd); #else close(exit_pipe[0]); close(exit_pipe[1]); #endif pipe_fail: dacp_deinit(); dacp_fail: daap_deinit(); daap_fail: rsp_deinit(); rsp_fail: event_base_free(evbase_httpd); return -1; }
static bool HHVM_METHOD(EventHttp, bind, const String &address, int64_t port) { EventHttpResourceData *resource_data = FETCH_RESOURCE(this_, EventHttpResourceData, s_event_http); return evhttp_bind_socket((evhttp_t *)resource_data->getInternalResourceData(), address.c_str(), port) == 0; }
int main(int argc, char*argv[]) { printf("Hello, world!\n"); event_config *conf = event_config_new(); #ifdef WIN32 WORD wVersionRequested; WSADATA wsaData; int err; /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we could not find a usable */ /* Winsock DLL. */ printf("WSAStartup failed with error: %d\n", err); return 1; } evthread_use_windows_threads(); event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP); #endif base = event_base_new_with_config(conf); const char ** methods = event_get_supported_methods(); int loop; std::cout << "Version: " << event_get_version() << std::endl; std::cout << "Method: " << event_base_get_method(base) << std::endl; std::cout << "Features: 0x" << std::hex << event_base_get_features(base) << std::endl; std::cout << "Base: " << base << std::endl; while (*methods) { std::cout << "Method: " << *methods++ << std::endl; } event_set_log_callback(_log_cb); /* The caller has already set up fd1, fd2 somehow, and make them nonblocking. */ if (0) { evutil_socket_t fd1 = 1; evutil_socket_t fd2 = 1; struct timeval five_seconds = { 5, 0 }; struct event *ev1 = event_new(base, fd1, EV_TIMEOUT | EV_READ/*|EV_PERSIST*/, cb_func, (char*) "Reading event"); struct event *ev2 = event_new(base, fd2, EV_WRITE/*|EV_PERSIST*/, cb_func, (char*) "Writing event"); event_add(ev1, &five_seconds); event_add(ev2, NULL); std::cout << "\nEntering loop" << std::endl; loop = event_base_loop(base, 0); std::cout << "Exiting loop: " << loop << std::endl; } // http server evhttp *ev_http = evhttp_new(base); int http_port = 9090; // evhttp_bind_socket expects its PORT param in host byte order. Sigh. int r = evhttp_bind_socket(ev_http, "0.0.0.0", http_port); // This return value is undocumented (!), but this seems to work. if (r == -1) { std::cerr << "could not open port " << http_port << std::endl; return 3; } evhttp_set_gencb(ev_http, http_handle_generic, 0); //evhttp_set_cb(ev_http, "/", http_handle_root); std::cout << "\nEntering loop" << std::endl; loop = event_base_loop(base, 0); std::cout << "Exiting loop: " << loop << std::endl; evhttp_free(ev_http); event_base_free(base); event_config_free(conf); return 0; }
/* Thread: main */ int httpd_init(void) { int v6enabled; unsigned short port; int ret; httpd_exit = 0; evbase_httpd = event_base_new(); if (!evbase_httpd) { DPRINTF(E_FATAL, L_HTTPD, "Could not create an event base\n"); return -1; } ret = rsp_init(); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "RSP protocol init failed\n"); goto rsp_fail; } ret = daap_init(); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "DAAP protocol init failed\n"); goto daap_fail; } ret = dacp_init(); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "DACP protocol init failed\n"); goto dacp_fail; } streaming_init(); #ifdef USE_EVENTFD exit_efd = eventfd(0, EFD_CLOEXEC); if (exit_efd < 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not create eventfd: %s\n", strerror(errno)); goto pipe_fail; } exitev = event_new(evbase_httpd, exit_efd, EV_READ, exit_cb, NULL); #else ret = pipe2(exit_pipe, O_CLOEXEC); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not create pipe: %s\n", strerror(errno)); goto pipe_fail; } exitev = event_new(evbase_httpd, exit_pipe[0], EV_READ, exit_cb, NULL); #endif /* USE_EVENTFD */ if (!exitev) { DPRINTF(E_FATAL, L_HTTPD, "Could not create exit event\n"); goto event_fail; } event_add(exitev, NULL); evhttpd = evhttp_new(evbase_httpd); if (!evhttpd) { DPRINTF(E_FATAL, L_HTTPD, "Could not create HTTP server\n"); goto event_fail; } v6enabled = cfg_getbool(cfg_getsec(cfg, "general"), "ipv6"); port = cfg_getint(cfg_getsec(cfg, "library"), "port"); if (v6enabled) { ret = evhttp_bind_socket(evhttpd, "::", port); if (ret < 0) { DPRINTF(E_LOG, L_HTTPD, "Could not bind to port %d, falling back to IPv4\n", port); v6enabled = 0; } } if (!v6enabled) { ret = evhttp_bind_socket(evhttpd, "0.0.0.0", port); if (ret < 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not bind to port %d (forked-daapd already running?)\n", port); goto bind_fail; } } evhttp_set_gencb(evhttpd, httpd_gen_cb, NULL); ret = pthread_create(&tid_httpd, NULL, httpd, NULL); if (ret != 0) { DPRINTF(E_FATAL, L_HTTPD, "Could not spawn HTTPd thread: %s\n", strerror(errno)); goto thread_fail; } return 0; thread_fail: bind_fail: evhttp_free(evhttpd); event_fail: #ifdef USE_EVENTFD close(exit_efd); #else close(exit_pipe[0]); close(exit_pipe[1]); #endif pipe_fail: streaming_deinit(); dacp_deinit(); dacp_fail: daap_deinit(); daap_fail: rsp_deinit(); rsp_fail: event_base_free(evbase_httpd); return -1; }
static void http_basic_test(void) { struct timeval tv; struct bufferevent *bev; int fd; const char *http_request; short port = -1; test_ok = 0; fprintf(stdout, "Testing Basic HTTP Server: "); http = http_setup(&port, NULL); /* bind to a second socket */ if (evhttp_bind_socket(http, "127.0.0.1", port + 1) == -1) { fprintf(stdout, "FAILED (bind)\n"); exit(1); } fd = http_connect("127.0.0.1", port); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_readcb, http_writecb, http_errorcb, NULL); /* first half of the http request */ http_request = "GET /test HTTP/1.1\r\n" "Host: some"; bufferevent_write(bev, http_request, strlen(http_request)); timerclear(&tv); tv.tv_usec = 10000; event_once(-1, EV_TIMEOUT, http_complete_write, bev, &tv); event_dispatch(); if (test_ok != 3) { fprintf(stdout, "FAILED\n"); exit(1); } /* connect to the second port */ bufferevent_free(bev); EVUTIL_CLOSESOCKET(fd); fd = http_connect("127.0.0.1", port + 1); /* Stupid thing to send a request */ bev = bufferevent_new(fd, http_readcb, http_writecb, http_errorcb, NULL); http_request = "GET /test HTTP/1.1\r\n" "Host: somehost\r\n" "Connection: close\r\n" "\r\n"; bufferevent_write(bev, http_request, strlen(http_request)); event_dispatch(); bufferevent_free(bev); EVUTIL_CLOSESOCKET(fd); evhttp_free(http); if (test_ok != 5) { fprintf(stdout, "FAILED\n"); exit(1); } fprintf(stdout, "OK\n"); }
int main(int argc, char** argv) { // Initialize cURL before creating threads curl_global_init(CURL_GLOBAL_DEFAULT); // Set up our exception signal handler for asserts and segfaults. signal(SIGABRT, exception_handler); signal(SIGSEGV, exception_handler); // Initialize the global configuration. __globals = new Globals(); __globals->update_config(); // Create components TimerStore *store = new TimerStore(); Replicator* controller_rep = new Replicator(); Replicator* handler_rep = new Replicator(); HTTPCallback* callback = new HTTPCallback(); TimerHandler* handler = new TimerHandler(store, handler_rep, callback); Controller* controller = new Controller(controller_rep, handler); // Create an event reactor. struct event_base* base = event_base_new(); if (!base) { std::cerr << "Couldn't create an event_base: exiting" << std::endl; return 1; } // Create an HTTP server instance. struct evhttp* http = evhttp_new(base); if (!http) { std::cerr << "Couldn't create evhttp: exiting" << std::endl; return 1; } // Register a callback for the "/ping" path. evhttp_set_cb(http, "/ping", Controller::controller_ping_cb, NULL); // Register a callback for the "/timers" path, we have to do this with the // generic callback as libevent doesn't support regex paths. evhttp_set_gencb(http, Controller::controller_cb, controller); // Bind to the correct port std::string bind_address; int bind_port; __globals->get_bind_address(bind_address); __globals->get_bind_port(bind_port); evhttp_bind_socket(http, bind_address.c_str(), bind_port); // Start the reactor, this blocks the current thread event_base_dispatch(base); // Event loop is completed, terminate. // // After this point nothing will use __globals so it's safe to delete // it here. delete __globals; __globals = NULL; curl_global_cleanup(); return 0; }