//#define MULTI_THREAD_SUPPORT 1
int httpserver_start(int port, int nthreads, int backlog) {
	int r, i;
	int nfd = httpserver_bindsocket(port, backlog);
	if (nfd < 0) return -1;

#ifdef MULTI_THREAD_SUPPORT
	pthread_t ths[nthreads];
	for (i=0; i<nthreads; i++) {
		struct event_base *base = event_init();
		if (base == NULL) return -1;
		struct evhttp *httpd = evhttp_new(base);
		if (httpd == NULL) return -1;
		r = evhttp_accept_socket(httpd, nfd);
		if (r != 0) return -1;
		 evhttp_set_gencb(httpd, httpserver_generic_handler, NULL);
		r = pthread_create(&ths[i], NULL, httpserver_dispatch, base);
		if (r != 0) return -1;
	}
	
	for (i=0; i<nthreads; i++) {
		pthread_join(ths[i], NULL);
	}
#else
	struct event_base *base = event_init();
	if (base == NULL) return -1;
	struct evhttp *httpd = evhttp_new(base);
	if (httpd == NULL) return -1;
	r = evhttp_accept_socket(httpd, nfd);
	evhttp_set_gencb(httpd, httpserver_generic_handler, NULL);
	httpserver_dispatch(base);
#endif
}
Example #2
0
int httpserver_start(int port, int backlog)  
{  
    int ret;  
    int fd = httpserver_bindsocket(port, backlog);  
    if (fd < 0)   
    {     
        return -1;  
        printf("httpserver_bindsocket error !\n");  
    }  
    struct event_base *base = event_init();  
    if (base == NULL)   
    {  
        printf("event_init error !\n");  
        return -1;        
    }  
    struct evhttp *httpd = evhttp_new(base);  
    if (httpd == NULL)   
    {  
        printf("enhttp_new error !\n");  
        return -1;  
    }  
    ret = evhttp_accept_socket(httpd, fd);  
    if (ret != 0)   
    {  
        printf("evhttp_accept_socket error \n");  
        return -1;  
    }  
    evhttp_set_gencb(httpd, httpserver_handler, NULL);  
    event_base_dispatch(base);  
    return 0;  
}  
Example #3
0
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;
}
Example #4
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);
}
Example #5
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);

  // 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);
}
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;
}
Example #7
0
LibEventServer::LibEventServer(const ServerOptions &options)
  : Server(options.m_address, options.m_port, options.m_numThreads),
    m_accept_sock(options.m_serverFD),
    m_accept_sock_ssl(options.m_sslFD),
    m_dispatcher(options.m_numThreads, RuntimeOption::ServerThreadRoundRobin,
                 RuntimeOption::ServerThreadDropCacheTimeoutSeconds,
                 RuntimeOption::ServerThreadDropStack,
                 this, RuntimeOption::ServerThreadJobLIFOSwitchThreshold,
                 RuntimeOption::ServerThreadJobMaxQueuingMilliSeconds,
                 kNumPriorities, Util::num_numa_nodes()),
    m_dispatcherThread(this, &LibEventServer::dispatch) {
  m_eventBase = event_base_new();
  m_server = evhttp_new(m_eventBase);
  m_server_ssl = nullptr;
  evhttp_set_connection_limit(m_server, RuntimeOption::ServerConnectionLimit);
  evhttp_set_gencb(m_server, on_request, this);
#ifdef EVHTTP_PORTABLE_READ_LIMITING
  evhttp_set_read_limit(m_server, RuntimeOption::RequestBodyReadLimit);
#endif
  m_responseQueue.create(m_eventBase);

  if (!options.m_takeoverFilename.empty()) {
    m_takeover_agent.reset(new TakeoverAgent(options.m_takeoverFilename));
  }
}
Example #8
0
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);
}
Example #9
0
int serve(int port) {
    struct event_base *base = event_base_new();
    if (!base) {
        puts("could not initialize event_base");
        abort();
    }

    struct evhttp *http = evhttp_new(base);
    if (!http) {
        puts("could not initialize evhttp");
        abort();
    }

    evhttp_set_cb(http, "/master", get_master, NULL); // master
    evhttp_set_gencb(http, get_master_pgn, NULL);     // master/pgn/{8}

    struct evhttp_bound_socket *socket =
        evhttp_bind_socket_with_handle(http, "127.0.0.1", port);
    if (!socket) {
        printf("could not bind socket to http://127.0.0.1:%d/\n", port);
        return 1;
    }

    printf("listening on http://127.0.0.1:%d/ ...\n", port);

    return event_base_dispatch(base);
}
Example #10
0
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);
}
Example #11
0
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 (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);
}
Example #13
0
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);
}
Example #14
0
static void* thread_worker(void *arg) {
  struct args *args = (struct args*) arg;
  struct event_base *base = event_base_new();
  struct evhttp *evh = evhttp_new(base);
  evhttp_accept_socket(evh, args->fd);
  evhttp_set_gencb(evh, doit, (void *) evbuffer_new());
  event_base_dispatch(base);
}
Example #15
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);
}
Example #16
0
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
task_http_proxy(void) {
    struct event_base *base;
    struct evhttp *http;
    struct evhttp_bound_socket *handle;

    unsigned short port = PORT;

    log_write(INFO, "http-server: http proxy initialize.\n");

    /* As you konw */
    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
        return (1);

    if (signal(SIGCHLD, sig_chld) == SIG_ERR)
        return (1);

    /* Create a new base evObject */
    base = event_base_new();
    if (!base) {
        log_write(ERROR, "http-server: Couldn't create an event_base: exiting\n");
        return 1;
    }

    /* Create a new evhttp object to handle requests. */
    http = evhttp_new(base);
    if (!http) {
        log_write(ERROR, "http-server: Couldn't create evhttp. Exiting.\n");
        return 1;
    }

    /*
     * The path /post support post method
     * Receive shell command by post body
     */
    evhttp_set_cb(http, "/post", post_command_cb, NULL);

    /* We want to accept arbitrary requests, so we need to set a "generic"
     * cb.  We can also add callbacks for specific paths. */
    evhttp_set_gencb(http, other_cb, NULL);

    /* Now we tell the evhttp what port to listen on */
    handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", port);

    if (!handle) {
        log_write(ERROR, "http-server: Couldn't bind to port %d. Exiting.\n", (int)port);
        return 1;
    }

    log_write(INFO, "http-server: http proxy initialized done.\n");
    event_base_dispatch(base);

    return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
Example #18
0
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;
}
Example #19
0
LibEventServer::LibEventServer(const std::string &address, int port,
                               int thread, int timeoutSeconds)
  : Server(address, port, thread),
    m_accept_sock(-1),
    m_timeoutThreadData(thread, timeoutSeconds),
    m_timeoutThread(&m_timeoutThreadData, &TimeoutThread::run),
    m_dispatcher(thread, this),
    m_dispatcherThread(this, &LibEventServer::dispatch) {
  m_eventBase = event_base_new();
  m_server = evhttp_new(m_eventBase);
  evhttp_set_gencb(m_server, on_request, this);
  m_responseQueue.create(m_eventBase);
}
Example #20
0
    static void HHVM_METHOD(EventHttp, __construct, const Object &base) {
        evhttp_t *http;
        InternalResourceData *event_base_resource_data = FETCH_RESOURCE(base, InternalResourceData, s_event_base);
        event_base_t *event_base = (event_base_t *)event_base_resource_data->getInternalResourceData();
        http = evhttp_new(event_base);

        if(!http){
            raise_error("Failed to allocate space for new HTTP server(evhttp_new)");
        }

        Resource resource = Resource(NEWOBJ(EventHttpResourceData(http)));
        SET_RESOURCE(this_, resource, s_event_http);
    }
Example #21
0
int http_init(struct http* http, struct event_base *base, struct metrics *metrics)
{
    char workdir[256];
    char docroot[256];
    char listen_addr[256];

    memset(http, 0, sizeof(struct http));

    http->metrics = metrics;

    /* TODO: read port from config file */
    http->port = 4000;

    /* TODO: read docroot from config file */
    if (getcwd(workdir,sizeof(workdir)) == NULL)
    {
        printf("Could not get working directory\n");
        return ERROR_FAIL;
    }
    snprintf(docroot, sizeof(docroot), "%s/docroot", workdir);
    http->docroot = strdup(docroot);

    /* TODO: should also read a bind address */

    /* Create a new evhttp object to handle requests. */
    http->http = evhttp_new(base);
    if (!http->http)
    {
        printf("could not create evhttp.\n");
        return ERROR_FAIL;
    }

    evhttp_set_cb(http->http, "/metrics", http_metrics_request_cb, http);
    evhttp_set_cb(http->http, "/list", http_list_request_cb, http);

    evhttp_set_gencb(http->http, http_document_request_cb, http);

    /* Now we tell the evhttp what port to listen on */
    http->handle = evhttp_bind_socket_with_handle(http->http, "0.0.0.0", http->port);
    if (!http->handle)
    {
        printf("couldn't bind to http port %d.\n", (int)http->port);
        return ERROR_FAIL;
    }

    if (http_get_address(http, listen_addr, sizeof(listen_addr)) == S_OK)
        printf("http: listening at %s\n", listen_addr);

    return S_OK;
}
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);
    }
  }
}
Example #23
0
/*
 * main loop - construct the event_base and start the loop
 */
int run_server(char* ip, short port, int timeout_s, int backlog) {
  struct event_base           *ev_base;
  struct event_config         *ev_cfg;
  struct evhttp               *ev_httpd;
  evutil_socket_t             socket_fd;
  int                         socket_opts_flag = 1;
  int                         worker_id, socket_opts_results;

  openlog("Backend", LOG_PID|LOG_NDELAY, LOG_LOCAL0);  

  ev_cfg = event_config_new();
  //event_config_set_flag(ev_cfg, EV_TIMEOUT|EV_PERSIST);
  ev_base = event_base_new_with_config(ev_cfg);
  
  if (!ev_base) {
    printf("ERROR: Failed to initialize HTTP event loop!\n");
    return -1;
  }
     
  // Set up httpd interface event
  ev_httpd = evhttp_new(ev_base);
  evhttp_set_timeout(ev_httpd, timeout_s);
  routes(ev_httpd, ev_base);

  socket_fd = create_socket(ip, port, timeout_s, backlog);
  socket_opts_results = setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY,
                                  (char *) socket_opts_flag, sizeof(int));

  if ( socket_opts_results < 0 ) {
    syslog( LOG_INFO, "Nagle DISABLED");
  } else {
    syslog( LOG_INFO, "Nagle ENABLED");
  }

  evhttp_accept_socket(ev_httpd, socket_fd);
  
  for (worker_id = 0; worker_id < WORKERS; worker_id++) {
    if (fork() == 0) {
      printf("Starting worker_id %d ... ", worker_id);
      worker(ev_base);
      printf("done.\n");
      exit(0);
    }
  }

  closelog();
  return 0;
}
Example #24
0
bool HttpService::Init()
{
    bool ret = false;
    do
    {
        evthread_use_pthreads();
        base_ = event_base_new();
        if (!base_)
        {
            LOG_ERROR("create event base failed!");
            break;
        }

        dnsbase_ = evdns_base_new(base_, 1);
        if (!dnsbase_)
        {
            LOG_ERROR("create dnsbase failed!");
            break;
        }

        http_server_ = evhttp_new(base_);
        if (!http_server_)
        {
            LOG_ERROR("create evhttp failed!");
            break;
        }

        evhttp_set_allowed_methods(http_server_, 
            EVHTTP_REQ_CONNECT |
            EVHTTP_REQ_GET |
            EVHTTP_REQ_POST |
            EVHTTP_REQ_HEAD |
            EVHTTP_REQ_PUT |
            EVHTTP_REQ_DELETE);

        if (evhttp_accept_socket(http_server_, sock_) != 0)
        {
            LOG_ERROR("accept socket failed!");
            break;
        }

        evhttp_set_gencb(http_server_, HttpGenericCallback, this);
        ret = true;

    } while(0);

    return ret;
}
Example #25
0
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);

}
Example #26
0
File: rpc.c Project: dyustc/searaft
int init_rpc_server(struct rpc_context *rpc_context, const struct rpc_target *dest) {
    //should check dest->proto and do http handling only when proto=HTTP
    rpc_context->http = evhttp_new(rpc_context->base);
    if(!rpc_context->http) {
        return 1;
    }
    evhttp_set_cb(rpc_context->http, "/rpc", server_rpc_cb, rpc_context);

    rpc_context->handle = evhttp_bind_socket_with_handle(
        rpc_context->http, dest->host, dest->port);
    if(!rpc_context->handle) {
        evhttp_free(rpc_context->http);
        return 1;
    }

    return 0;
}
Example #27
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);
  }
}
Example #28
0
int main(int argc, char **argv) {
	int cli;
	int sock = BindSocket(12345);
	struct config conf;
	conf.store = store_open("tracker.db");
	conf.max_peer_return=50;
	conf.min_interval=300;
	conf.max_peer_return=48;
	struct event_base *base = event_init();
	struct evhttp *httpd;
	httpd = evhttp_new(base);
	cli = evhttp_accept_socket(httpd, sock);
	evhttp_set_gencb(httpd, tracker, (void *)&conf);
	event_base_dispatch(base);

	return 0;
}
Example #29
0
int main(int argc,char** argv){

	evbase = event_base_new(); 
	if(!evbase){ 
		fprintf(stderr, "create evbase error!\n");
		exit(0); 
	}

	// 创建http server实例
	ev_ssl = evhttp_new(evbase);
	if(!ev_ssl){ 
		exit(0); 
	}

	// openssl 初始化 
	SSL_library_init(); 
	ERR_load_crypto_strings(); 
	SSL_load_error_strings(); 
	OpenSSL_add_all_algorithms();

	if (SSLeay() != OPENSSL_VERSION_NUMBER){ 
	
	}

	ev_ssl->ctx = get_ssl_ctx(certfile_url.c_str(),keyfile_url.c_str()); 
	ev_ssl->ssl_cb = bufferevent_openssl_socket_new;

	std::string ev_ssl_ip="192.168.1.10"; int ev_ssl_port = 8080;

	// evhttp_bind_socket_with_handle这个函数在原基础上追加一个参数,标示http server知否支持ssl(0:不支持 1:支持) 
	struct evhttp_bound_socket *ssl_handle = evhttp_bind_socket_with_handle(ev_ssl, ev_ssl_ip.c_str(), ev_ssl_port,1); 
	if(!ssl_handle){ 
		exit(0); 
	}
	
	struct evconnlistener *ssl_listener = evhttp_bound_socket_get_listener(ssl_handle); 
	evconnlistener_set_error_cb(ssl_listener, ssl_accept_error_cb);

	evhttp_set_cb(ev_ssl, "/ping", ping_handler, NULL);

	event_base_dispatch(evbase);

	evhttp_free(ev_ssl); event_base_free(evbase);

	return 0; 
}
Example #30
0
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;
}