Ejemplo n.º 1
0
void request_handler(struct evhttp_request *req, void *arg)
{
    const char *uri;
    uri = evhttp_request_uri(req);

    if(strncmp(uri, "/updates/", 9) != 0){
        evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
        if (settings.verbose > 0)
            fprintf(stderr, "URL not found.. needs to be /updates/... but was %s\n", uri);
        return;
    }
    
    const char *rkey;
    struct evkeyvalq args;
    TAILQ_INIT(&args);

    evhttp_parse_query(uri, &args);
    
    rkey = evhttp_find_header(&args, "rkey");
    
    if (NULL == rkey) {
        evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request");
        if (settings.verbose > 0)
            fprintf(stderr, "RKey param not found in request URI %s\n", uri);
        evhttp_clear_headers(&args);
        return;
    }
    
    fprintf(stderr, "Using RKey: %s\n", rkey);
    
    char *cached = fetch_memcached(rkey);
    
    if (NULL == cached) {
        evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request");
        fprintf(stderr, "RKey %s not found in Memcache!\n", rkey);
        evhttp_clear_headers(&args);
        return;
    }
    
    int uid = atoi(cached);

    fprintf(stderr, "Great, found RKey in Memcached: %s = %s and now UID %d\n", rkey, cached, uid);
    
	struct evbuffer *buf = evbuffer_new();
    
    evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8");
    evhttp_add_header(req->output_headers, "Connection", "keep-alive");
    evhttp_add_header(req->output_headers, "Cache-Control", "no-cache");
    
    evhttp_send_reply_start(req, HTTP_OK, "OK");
    evbuffer_add_printf(buf, "Welcome, RKey: ‘%s’\n", rkey);
    evhttp_send_reply_chunk(req, buf);
    evbuffer_free(buf);

    clients[uid] = req;
    evhttp_clear_headers(&args);
    free(cached);
    
    evhttp_connection_set_closecb( req->evcon, cleanup, &slots[uid] );
}
Ejemplo n.º 2
0
static int upnpc_get_desc(upnpc_device_t * d, const char * url)
{
	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;

	/* if(d->root_desc_location == NULL) {
		return -1;
	} */
	if(!parseURL(url/*d->root_desc_location*/, 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(d->desc_conn == NULL) {
		d->desc_conn = evhttp_connection_base_new(d->parent->base, NULL, hostname, port);
	}
#ifdef DEBUG
	evhttp_connection_set_closecb(d->desc_conn, upnpc_conn_close_cb, d);
#endif /* DEBUG */
	/*evhttp_connection_set_timeout(p->desc_conn, 600);*/
	req = evhttp_request_new(upnpc_desc_received/*callback*/, d);
	headers = evhttp_request_get_output_headers(req);
	evhttp_add_header(headers, "Host", hostname_port);
	evhttp_add_header(headers, "Connection", "close");
	/*evhttp_add_header(headers, "User-Agent", "***");*/
	return evhttp_make_request(d->desc_conn, req, EVHTTP_REQ_GET, path);
}
Ejemplo n.º 3
0
// 不同的线程,会调用这个接口
void HTTPConnectionManager::newRequest(void* req, std::string uri, std::string content)
{
    HTTPReply m_reply;

    // 取出对端的IP
    ev_uint16_t port;
    char *addr = NULL;
    evhttp_connection_get_peer(((evhttp_request*)req)->evcon, &addr , &port);
    char address[20];
    strcpy(address, addr);

    m_requestHandler->handleRequest(address, uri, content, m_reply);

    //if(m_reply.status ==  > 0)    // 如果是request处理好了,就回复
    {
        struct evbuffer *buf = evbuffer_new();
        evbuffer_add_printf(buf, "%s", m_reply.m_content.c_str());
		evhttp_add_header(evhttp_request_get_output_headers((evhttp_request*)req), "Connection", "close");
        evhttp_add_header(evhttp_request_get_output_headers((evhttp_request*)req), \
            m_reply.m_headers[0].m_name.c_str(), m_reply.m_headers[0].m_value.c_str());
        evhttp_send_reply((evhttp_request*)req, HTTP_OK, "OK", buf);
		evbuffer_free(buf);

        LOG_T("<== [" << m_reply.m_content << "]");
    }
}
Ejemplo n.º 4
0
// create S3 and setup HTTP connection request
struct evhttp_request *s3http_connection_create_request (S3HttpConnection *con,
    void (*cb)(struct evhttp_request *, void *), void *arg,
    const gchar *auth_str)
{    
    struct evhttp_request *req;
    gchar auth_key[300];
    struct tm *cur_p;
	time_t t = time(NULL);
    struct tm cur;
    char date[50];
    //char hostname[1024];

	gmtime_r(&t, &cur);
	cur_p = &cur;

    snprintf (auth_key, sizeof (auth_key), "AWS %s:%s", application_get_access_key_id (con->app), auth_str);

    req = evhttp_request_new (cb, arg);
    evhttp_add_header (req->output_headers, "Authorization", auth_key);
    evhttp_add_header (req->output_headers, "Host", application_get_host_header (con->app));
		
    if (strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S GMT", cur_p) != 0) {
			evhttp_add_header (req->output_headers, "Date", date);
		}
    return req;
}
Ejemplo n.º 5
0
void Subscriber::start(){
	bufferevent_enable(req->evcon->bufev, EV_READ);
	evhttp_connection_set_closecb(req->evcon, on_sub_disconnect, this);
	evhttp_add_header(req->output_headers, "Connection", "keep-alive");
	//evhttp_add_header(req->output_headers, "Cache-Control", "no-cache");
	//evhttp_add_header(req->output_headers, "Expires", "0");
	evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=utf-8");
	evhttp_send_reply_start(req, HTTP_OK, "OK");

	if(this->type == POLL){
		//
	}else if(this->type == IFRAME){
		struct evbuffer *buf = evhttp_request_get_output_buffer(this->req);
		evbuffer_add_printf(buf, "%s\n", iframe_header.c_str());
		evhttp_send_reply_chunk(this->req, buf);
	}
	
	// send buffered messages
	if(this->seq_next == 0){
		this->seq_next = channel->seq_next;
	}
	if(!channel->msg_list.empty() && channel->seq_next != this->seq_next){
		this->send_old_msgs();
	}
}
Ejemplo n.º 6
0
struct evhttp_request* EvHttpSyncClient::newReqest()
{
    struct evhttp_request* pReq = evhttp_request_new(handleResponse, this);
    if (pReq == NULL) 
    {
        FX_LOG(ERROR, "evhttp_request_new FAILED");
        setError(ST_ERROR, "evhttp_request_new FAILED");
        return NULL;
    }

    int rv = evhttp_add_header(evhttp_request_get_output_headers(pReq), 
                               "Host", m_sHost.c_str());
    if (rv != 0) 
    {
        FX_LOG(ERROR, "evhttp_add_header FAILED");
        setError(ST_ERROR, "evhttp_add_header FAILED");
        return NULL;
    }

    rv = evhttp_add_header(evhttp_request_get_output_headers(pReq),
                           "Connection", "keep-alive");
    if (rv != 0) 
    {
        FX_LOG(ERROR, "evhttp_add_header FAILED");
        setError(ST_ERROR, "evhttp_add_header FAILED");
        return NULL;
    }
    return pReq;
}
Ejemplo n.º 7
0
static void http_set_headers(struct evhttp_request *req, struct server_socket *sock, bool longpoll)
{
	evhttp_clear_headers(req->output_headers);

	/* Add header to debug load balancing */
	if (srv.ourhost)
		evhttp_add_header(req->output_headers, "X-Server", srv.ourhost);

	/* copy X-Forwarded-For header to remote_host, if a trusted proxy provides it */
	if (sock->cfg->proxy && !strcmp(req->remote_host, sock->cfg->proxy)) {
		const char *hdr;
		hdr = evhttp_find_header(req->input_headers, "X-Forwarded-For");
		if (hdr) {
			free(req->remote_host);
			req->remote_host = strdup(hdr);
		}
	}

	evhttp_add_header(req->output_headers,
			  "Content-Type", "application/json");
	if (!longpoll && !srv.disable_lp)
		evhttp_add_header(req->output_headers, "X-Long-Polling", "/LP");
	if (!srv.disable_roll_ntime)
		evhttp_add_header(req->output_headers, "X-Roll-NTime", "Y");
}
Ejemplo n.º 8
0
void Subscriber::start(){
#ifndef WIN32
	bufferevent_enable(req->evcon->bufev, EV_READ);
#endif
	evhttp_connection_set_closecb(req->evcon, on_sub_disconnect, this);
	evhttp_add_header(req->output_headers, "Connection", "keep-alive");
	evhttp_add_header(req->output_headers, "Content-Type", "text/html;charset=UTF-8");
	evhttp_send_reply_start(req, HTTP_OK, "OK");

	if (this->type == POLL){
	}
	else if (this->type == IFRAME){
		struct evbuffer *buf = evbuffer_new();
		evbuffer_add_printf(buf, "%s\n", iframe_header.c_str());
		evhttp_send_reply_chunk(this->req, buf);
		evbuffer_free(buf);
	}

	if (this->seq_next == 0)
	{
		this->seq_next = channel->seq_next;
	}
	if (!channel->msg_list.empty() && channel->seq_next != this->seq_next){
		this->send_old_msgs();
	}
}
Ejemplo n.º 9
0
 void URLConnection::request( const std::string& url, URLConnectionCompleteCB callback, void* callback_data )
 ///
 /// Function to request a page via the GET protocol
 ///
 /// @param url
 ///     The url to request (note, this does NOT include the host)
 ///
 /// @param callback
 ///     Callback to call on success / failure of the request
 ///
 /// @param callback_data
 ///     Data to send back with callback
 ///
 {
     // create a struct for handling the real callback for the user of URLConnection
     // should be deallocated in the url_connection_callback function
     ConnectionCallbackData* cb_data = new ConnectionCallbackData();
     cb_data->callback = callback;
     cb_data->data = callback_data;
     
     evhttp_request* request = evhttp_request_new(url_connection_callback, cb_data);
     
     // set up appropriate headers for a GET request
     char* host_address;
     ev_uint16_t port;
     evhttp_connection_get_peer(mConnection, &host_address, &port);
     evhttp_add_header( request->output_headers, "Host", host_address );
     evhttp_add_header( request->output_headers, "User-Agent", mUserAgent.c_str() );		
     evhttp_make_request(mConnection, request, EVHTTP_REQ_GET, url.c_str());
     
 }
Ejemplo n.º 10
0
static void
http_bad_header_test(void)
{
	struct evkeyvalq headers;

	fprintf(stdout, "Testing HTTP Header filtering: ");

	TAILQ_INIT(&headers);

	if (evhttp_add_header(&headers, "One", "Two") != 0)
		goto fail;
	
	if (evhttp_add_header(&headers, "One\r", "Two") != -1)
		goto fail;

	if (evhttp_add_header(&headers, "One\n", "Two") != -1)
		goto fail;

	if (evhttp_add_header(&headers, "One", "Two\r") != -1)
		goto fail;

	if (evhttp_add_header(&headers, "One", "Two\n") != -1)
		goto fail;

	evhttp_clear_headers(&headers);

	fprintf(stdout, "OK\n");
	return;
fail:
	fprintf(stdout, "FAILED\n");
	exit(1);
}
Ejemplo n.º 11
0
static void http_request_cb(struct evhttp_request *req, void *arg)
{
    struct context *ctx = (struct context *) arg;
    const char *uri = evhttp_request_get_uri(req);
    struct evbuffer *evb = NULL;
    const char *type;

    if (evhttp_request_get_command(req) != EVHTTP_REQ_GET)
    {
        evhttp_send_error(req, HTTP_BADREQUEST, 0);
        return;
    }

    printf("GET: %s",  uri);

    if (strcmp(uri,"/") == 0)
    {
        type = "text/html";
        evb = evbuffer_new();
        evbuffer_add_printf(evb, "<html><body><h1>Hello World!</h1></body></html>");
        evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", type);
        evhttp_send_reply(req, 200, "OK", evb);
        printf(" - 200 - ok\n");
    }
    else if (strcmp(uri,"/sample") == 0)
    {
        evb = evbuffer_new();
        if (get_sample(ctx) == 0)
        {
            if (format_sample_response(ctx, evb) == 0)
            {
                evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/json");
                evhttp_send_reply(req, 200, "OK", evb);
                printf(" - 200 - ok\n");
            }
            else
            {
                internal_error(req, evb);
            }
        }
        else
        {
            internal_error(req, evb);
        }
    }
    else
    {
        type = "text/plain";
        evb = evbuffer_new();
        evbuffer_add_printf(evb, "Not Found");
        evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", type);
        evhttp_send_reply(req, 404, "Not Found", evb);
        printf(" - 404 - not found\n");
    }

    if (evb)
        evbuffer_free(evb);
}
Ejemplo n.º 12
0
void
create_request(const char *url) {
    struct evhttp_uri *http_uri = NULL;
    const char *host;
    struct bufferevent *bev;
    struct evhttp_connection *evcon = NULL;
    struct evhttp_request *req;


    struct evkeyvalq *output_headers;

    http_uri = evhttp_uri_parse(url);
    if (NULL != http_uri) {
        host = evhttp_uri_get_host(http_uri);
        if (NULL != host) {

            bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
            evcon = evhttp_connection_base_bufferevent_new(base, dnsbase, bev, host, HTTP_PORT);
            if (NULL != evcon) {

                evhttp_connection_set_timeout(evcon, TIMEOUT);

                req = evhttp_request_new(http_request_done, bev);
                if (NULL != req) {
                    output_headers = evhttp_request_get_output_headers(req);

                    evhttp_add_header(output_headers, "Accept", "text/plain;q=0.8");
                    evhttp_add_header(output_headers, "Host", host);
                    evhttp_add_header(output_headers, "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
                    evhttp_add_header(output_headers, "Connection", "close");

                    if (0 == evhttp_make_request(evcon, req, EVHTTP_REQ_GET, url)) {
                        ++n_pending_requests;
                    } else {
                        evhttp_request_free(req);
                        fprintf(stderr, "evhttp_make_request() failed\n");
                    }
                    //evhttp_request_free(req);
                } else {
                    fprintf(stderr, "evhttp_request_new() failed\n");
                }

                //evhttp_connection_free(evcon);
            } else {
                fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
            }


        } else {
            fprintf(stderr, "url must have a host %s\n", url);
        }
        evhttp_uri_free(http_uri);
    } else {
        fprintf(stderr, "malformed url %s\n", url);
    }

}
Ejemplo n.º 13
0
void http_handler(struct evhttp_request *req, void *arg)
{
	if (strcmp(req->uri, JSON_RPC_ENDPOINT) == 0 || strcmp(req->uri, WS_ENDPOINT) == 0)
		return;

	char *path = (strcmp(req->uri, "/") == 0) ? get_path(ROOT_URI) : get_path(req->uri);
	if (path == NULL)
		goto error;

	int len = get_file_size(path);
	if (len == -1) {
		evhttp_send_error(req, HTTP_NOT_FOUND_CODE, HTTP_NOT_FOUND_REASON);
		free(path);
		return;
	}

	void *buf = get_file_content(path);
	if (buf == NULL)
		goto error;

	struct evbuffer *evbuf = evbuffer_new();
	if (evbuf == NULL)
		goto error;

	if (evbuffer_add(evbuf, buf, len) == -1) {
		evbuffer_free(evbuf);
		goto error;
	}

	if (string_ends_by(path, CSS_POSTFIX)) {
		evhttp_add_header(req->output_headers, "Content-Type", "text/css");
	}

	if (string_ends_by(path, JS_POSTFIX)) {
		evhttp_add_header(req->output_headers, "Content-Type", "text/javascript");
	}

	if (string_ends_by(path, PNG_POSTFIX)) {
		evhttp_add_header(req->output_headers, "Content-Type", "image/png");
	}

	evhttp_send_reply(req, HTTP_OK_CODE, HTTP_OK_REASON, evbuf);

	free(path);
	free(buf);
	free(evbuf);

	return;

error:
	free(path);
	free(buf);
	evhttp_send_error(req, HTTP_INTERNAL_ERROR_CODE, HTTP_INTERNAL_ERROR_REASON);
}
Ejemplo n.º 14
0
static void got_api(struct evhttp_request* req, void* userdata)
{
    struct req_data* d = (struct req_data*) userdata;

    if(req->response_code != 200)
    {
        evhttp_send_error(d->req, 500, "Internal Server Error");
        free_req_data(&d);
        fprintf(stderr, "info.c:got_api(): api returned http status %d\n", (int)(req->response_code));
        return;
    }

    XML_Parser parser = XML_ParserCreate(NULL);
    int done;
    int depth = 0;
    XML_SetUserData(parser, d);
    XML_SetElementHandler(parser, startElement, endElement);
    XML_SetCharacterDataHandler(parser, xmlData);

    char buf[4096];
    int buflen;
    while((buflen = evbuffer_remove(req->input_buffer, buf, 4096)) > 0)
    {
        if(XML_Parse(parser, buf, buflen, 0) == XML_STATUS_ERROR)
        {
            evhttp_send_error(d->req, 500, "Internal Server Error");
            free_req_data(&d);
            fprintf(stderr, "info.c:got_api(): error parsing xml: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
            return;
        }
    }
    XML_ParserFree(parser);

    // return static result

    struct evbuffer *evbuf;
    evbuf = evbuffer_new();
    if (buf == NULL)
    {
        evhttp_send_error(d->req, 500, "Internal Server Error");
        free_req_data(&d);
        fprintf(stderr, "info.c:got_api(): couldn't alloc evbuffer\n");
        return;
    }

    evhttp_remove_header(d->req->output_headers, "content-type");
    evhttp_add_header(d->req->output_headers, "content-type", "application/json");
    evhttp_add_header(d->req->output_headers, "cache-control", "no-cache");
    evbuffer_add_printf(evbuf, "{ \"eventName\": \"%s\", \"description\": \"%s\", \"serverTime\": %d, \"startTime\": %d, \"duration\": %d, \"login\": \"%s\", \"title\": \"%s\", \"profile_image\": \"%s\" }\n", d->event_title, d->description, (int)time(0), (int)(d->start), d->duration, d->login, d->channel_title, d->profile_image);
    evhttp_send_reply(d->req, 200, "OK", evbuf);
    evbuffer_free(evbuf);
    free_req_data(&d);
}
Ejemplo n.º 15
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;
}
Ejemplo n.º 16
0
Archivo: http.c Proyecto: snip/aprsc
static void http_status(struct evhttp_request *r)
{
	char *json;
	
	struct evkeyvalq *headers = evhttp_request_get_output_headers(r);
	http_header_base(headers, tick);
	evhttp_add_header(headers, "Content-Type", "application/json; charset=UTF-8");
	evhttp_add_header(headers, "Cache-Control", "max-age=9");
	
	json = status_json_string(0, 0);
	http_send_reply_ok(r, headers, json, strlen(json), 1);
	free(json);
}
Ejemplo n.º 17
0
/* 处理模块 */
void httpcws_handler(struct evhttp_request *req, void *arg)
{	
        struct evbuffer *buf;
        buf = evbuffer_new();
		
		/* 分析URL参数 */
		struct evkeyvalq httpcws_http_query;
		evhttp_parse_query(evhttp_request_uri(req), &httpcws_http_query);
		
		/* 接收POST表单信息 */
		const char *tcsql_input_postbuffer = (const char*) EVBUFFER_DATA(req->input_buffer);		
		
		/* 接收GET表单参数 */
		const char *httpcws_input_words = evhttp_find_header (&httpcws_http_query, "w");

		const char *httpcws_output_tmp = NULL;
		char *httpcws_output_words = "\0";
		if (tcsql_input_postbuffer != NULL) {
			char *tcsql_input_postbuffer_tmp = (char *) malloc(EVBUFFER_LENGTH(req->input_buffer)+1);
			memset (tcsql_input_postbuffer_tmp, '\0', EVBUFFER_LENGTH(req->input_buffer)+1);
			strncpy(tcsql_input_postbuffer_tmp, tcsql_input_postbuffer, EVBUFFER_LENGTH(req->input_buffer));
			char *decode_uri = urldecode(tcsql_input_postbuffer_tmp);
			free(tcsql_input_postbuffer_tmp);
			httpcws_output_tmp = ICTCLAS_ParagraphProcess(decode_uri, 0);
			free(decode_uri);
			httpcws_output_words = strdup(httpcws_output_tmp);
			trim (httpcws_output_words);
		} else if (httpcws_input_words != NULL) {
			char *httpcws_input_words_tmp = strdup(httpcws_input_words);
			char *decode_uri = urldecode(httpcws_input_words_tmp);
			free(httpcws_input_words_tmp);
			httpcws_output_tmp = ICTCLAS_ParagraphProcess(decode_uri, 0);
			free(decode_uri);
			httpcws_output_words = strdup(httpcws_output_tmp);
			trim (httpcws_output_words);
		} else {
			httpcws_output_words = strdup("");
		}
		
		/* 输出内容给客户端 */
		evhttp_add_header(req->output_headers, "Server", "HTTPCWS/1.0.0");
		evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=GB2312");
		evhttp_add_header(req->output_headers, "Connection", "close");
		evbuffer_add_printf(buf, "%s", httpcws_output_words);
        evhttp_send_reply(req, HTTP_OK, "OK", buf);
		
		free(httpcws_output_words);
		evhttp_clear_headers(&httpcws_http_query);
		evbuffer_free(buf);	
}
Ejemplo n.º 18
0
Archivo: http.c Proyecto: snip/aprsc
static void http_header_base(struct evkeyvalq *headers, int last_modified)
{
	char dbuf[80];
	
	http_date(dbuf, sizeof(dbuf), tick);
	
	evhttp_add_header(headers, "Server", verstr_http);
	evhttp_add_header(headers, "Date", dbuf);
	
	if (last_modified) {
		http_date(dbuf, sizeof(dbuf), last_modified);
		evhttp_add_header(headers, "Last-Modified", dbuf);
	}
}
Ejemplo n.º 19
0
static void httpd_handler(struct evhttp_request *req, void *arg) {
	struct evbuffer *buf;
	buf = evbuffer_new();

	//get http request url
	char *decode_uri = strdup((char*) evhttp_request_uri(req));
	struct evkeyvalq httpd_query;
	evhttp_parse_query(decode_uri, &httpd_query);
	free(decode_uri);

	//recive http params
	const char *start = evhttp_find_header(&httpd_query, "start");
	const char *httpd_charset = evhttp_find_header(&httpd_query, "charset");

	//send http header charset
	if (httpd_charset != NULL && strlen(httpd_charset) <= 40) {
		char content_type[64] = { 0 };
		sprintf(content_type, "text/plain; charset=%s", httpd_charset);
		evhttp_add_header(req->output_headers, "Content-Type", content_type);
	} else {
		evhttp_add_header(req->output_headers, "Content-Type", "text/plain");
	}

	evhttp_add_header(req->output_headers, "Connection", "keep-alive");
	evhttp_add_header(req->output_headers, "Cache-Control", "no-cache");
	evbuffer_add_printf(buf, "%s\n", "HTTRACK_OK");

	char start_t[255] = {0x00};
	sprintf(start_t, "%s", start);
	if (strcmp(start_t, "1") == 0) {

		struct hts_proj proj;
		proj.depth = 5;
		proj.priority = PRIORITY_ONLY_HTML;
		proj.action = ACTION_ONLY_FILES;

		sprintf(proj.name, "%s", "dianying.yisou.com");
		sprintf(proj.urls, "%s", "http://dianying.yisou.com/");

		new_dl_thread();
	}

	//send content to client
	evhttp_send_reply(req, HTTP_OK, "OK", buf);

	//free buf
	evhttp_clear_headers(&httpd_query);
	evbuffer_free(buf);
}
Ejemplo n.º 20
0
void CHttpServer::http_reponse(struct evhttp_request *req, 
						 struct evkeyvalq *params, 
						 int statcode, 
						 char *statreason)
{
	struct evbuffer *buff = evbuffer_new();

	evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=UTF-8");
	evhttp_add_header(req->output_headers, "Connection", "close");
	evhttp_send_reply(req, statcode, statreason, buff);
	evhttp_clear_headers(params);
	evbuffer_free(buff);
	
	return;
}
Ejemplo n.º 21
0
static void
rsp_send_error(struct evhttp_request *req, char *errmsg)
{
  struct evbuffer *evbuf;
  struct evkeyvalq *headers;
  mxml_node_t *reply;
  mxml_node_t *status;
  mxml_node_t *node;

  /* We'd use mxmlNewXML(), but then we can't put any attributes
   * on the root node and we need some.
   */
  reply = mxmlNewElement(MXML_NO_PARENT, RSP_XML_ROOT);

  node = mxmlNewElement(reply, "response");
  status = mxmlNewElement(node, "status");

  /* Status block */
  node = mxmlNewElement(status, "errorcode");
  mxmlNewText(node, 0, "1");

  node = mxmlNewElement(status, "errorstring");
  mxmlNewText(node, 0, errmsg);

  node = mxmlNewElement(status, "records");
  mxmlNewText(node, 0, "0");

  node = mxmlNewElement(status, "totalrecords");
  mxmlNewText(node, 0, "0");

  evbuf = mxml_to_evbuf(reply);
  mxmlDelete(reply);

  if (!evbuf)
    {
      httpd_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");

      return;
    }

  headers = evhttp_request_get_output_headers(req);
  evhttp_add_header(headers, "Content-Type", "text/xml; charset=utf-8");
  evhttp_add_header(headers, "Connection", "close");

  httpd_send_reply(req, HTTP_OK, "OK", evbuf, HTTPD_SEND_NO_GZIP);

  evbuffer_free(evbuf);
}
Ejemplo n.º 22
0
void
dispatch(runner *run, int reqno)
{
    struct evhttp_connection *evcon = run->evcon;
    struct evhttp_request *evreq;
    struct request *req;

    if((req = calloc(1, sizeof(*req))) == nil)
        panic("calloc");

    run->req = req;
    req->evcon = evcon;
    req->evcon_reqno = reqno;

    evreq = evhttp_request_new(&recvcb, run);
    if(evreq == nil)
        panic("evhttp_request_new");

    req->evreq = evreq;

    evreq->response_code = -1;
    evhttp_add_header(evreq->output_headers, "Host", http_hosthdr);

    gettimeofday(&req->starttv, nil);
    evtimer_set(&req->timeoutev, timeoutcb, run);
    evtimer_add(&req->timeoutev, &timeouttv);
    debug("dispatch(): evtimer_add(&req->timeoutev, &timeouttv);\n");

    counts.conns++;
    evhttp_make_request(evcon, evreq, EVHTTP_REQ_GET, params.path);
}
/**
 *
 * @param *request the opaque data structure containing the request infos
 * @param *privParams global parameters set when the callback was associated
 * @return nothing
 */
void testing (struct evhttp_request *request, void *privParams) {
	struct evbuffer *buffer;
	struct evkeyvalq headers;
	const char *q;
	// Parse the query for later lookups
	evhttp_parse_query (evhttp_request_get_uri (request), &headers);

	// lookup the 'q' GET parameter 
	q = evhttp_find_header (&headers, "q");

	// Create an answer buffer where the data to send back to the browser will be appened
	buffer = evbuffer_new ();
	evbuffer_add (buffer, "coucou !", 8);
	evbuffer_add_printf (buffer, "%s", q);

	// Add a HTTP header, an application/json for the content type here
	evhttp_add_header (evhttp_request_get_output_headers (request),
			"Content-Type", "text/plain");

	// Tell we're done and data should be sent back
	evhttp_send_reply(request, HTTP_OK, "OK", buffer);

	// Free up stuff
	evhttp_clear_headers (&headers);

	evbuffer_free (buffer);

	return;
}
Ejemplo n.º 24
0
int Server::close(struct evhttp_request *req){
	HttpQuery query(req);
	std::string cname = query.get_str("cname", "");

	Channel *channel = this->get_channel_by_name(cname);
	if(!channel){
		log_warn("channel %s not found", cname.c_str());
		struct evbuffer *buf = evhttp_request_get_output_buffer(req);
		evbuffer_add_printf(buf, "channel[%s] not connected\n", cname.c_str());
		evhttp_send_reply(req, 404, "Not Found", buf);
		return 0;
	}
	log_debug("close channel: %s, subs: %d", cname.c_str(), channel->subs.size);
		
	// response to publisher
	evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=utf-8");
	struct evbuffer *buf = evhttp_request_get_output_buffer(req);
	evbuffer_add_printf(buf, "ok %d\n", channel->seq_next);
	evhttp_send_reply(req, 200, "OK", buf);
	
	channel->close();
	this->free_channel(channel);

	return 0;
}
Ejemplo n.º 25
0
static void http_handle_blob_get(struct evhttp_request *req) {
	evkeyvalq* out_headers = evhttp_request_get_output_headers(req);
	if (!out_headers) {
		evhttp_send_reply(req, HTTP_INTERNAL, "can't get headers", 0);
		return;
	}

	int r = evhttp_add_header(out_headers, "Content-Type", "text/plain");
	if (r == -1) {
		evhttp_send_reply(req, HTTP_INTERNAL, "can't add header", 0);
		return;
	}

	evbuffer* out = evbuffer_new();
	if (!out) {
		evhttp_send_reply(req, HTTP_INTERNAL, "can't make buffer", 0);
		return;
	}

	std::string hello("hello world");

	evbuffer_add(out, &*hello.begin(), hello.length());
	evhttp_send_reply(req, HTTP_OK, "OK", out);
	evbuffer_free(out);
}
Ejemplo n.º 26
0
/**** uri: /pinyin?str=*
 *
 */
static void http_pinyin_callback(struct evhttp_request *request, void *data) {

    //parse uri
    struct evkeyvalq params_get;
    PARSE_URI(request, params_get);
    SinoparserServer* serv = (SinoparserServer*) data;


    //get "str"
    char const *str;
    PARAM_GET_STR(str, &params_get, "str", true);
    serv->parser.change_text((char*)str);
    serv->parser.parse_against_index(serv->db.mandarin_words);
    std::string pinyin = serv->parser.romanize();

    //prepare output
    struct evbuffer *buffer = evbuffer_new();

    output_xml_header(buffer);
    romanization_output_xml(pinyin, buffer);
    output_xml_footer(buffer);

    //send
    evhttp_add_header(request->output_headers, "Content-Type", "TEXT/XML; charset=UTF8");

    evhttp_send_reply(request, HTTP_OK, "", buffer);

}
Ejemplo n.º 27
0
Archivo: httpd.c Proyecto: aol/thrasher
void
httpd_put_favicon (struct evhttp_request *req, void *arg)
{
    struct evbuffer *buf;

    static unsigned char thrash_ico[] = {
       0x00,   0x00,   0x01,   0x00,   0x01,   0x00,   0x10,   0x10,   0x02,   0x00,   0x01,   0x00,   0x01,   0x00,   0xb0,   0x00,
       0x00,   0x00,   0x16,   0x00,   0x00,   0x00,   0x28,   0x00,   0x00,   0x00,   0x10,   0x00,   0x00,   0x00,   0x20,   0x00,
       0x00,   0x00,   0x01,   0x00,   0x01,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
       0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
       0x00,   0x00,   0xff,   0xff,   0xff,   0x00,   0xff,   0xff,   0x00,   0x00,   0xff,   0xff,   0x00,   0x00,   0xb6,   0x6d,
       0x00,   0x00,   0xb5,   0xad,   0x00,   0x00,   0x87,   0xad,   0x00,   0x00,   0xb4,   0x61,   0x00,   0x00,   0xb5,   0xed,
       0x00,   0x00,   0xce,   0x6d,   0x00,   0x00,   0xff,   0xff,   0x00,   0x00,   0xed,   0xad,   0x00,   0x00,   0xed,   0xad,
       0x00,   0x00,   0xed,   0xab,   0x00,   0x00,   0xec,   0x23,   0x00,   0x00,   0xed,   0xad,   0x00,   0x00,   0x85,   0xa3,
       0x00,   0x00,   0xff,   0xff,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
       0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
       0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
       0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
       0x00,   0x00,   0x00,   0x00,   0x00,   0x00,                              
    };

    buf = evbuffer_new();

    evhttp_add_header(req->output_headers, "Content-Type", "text/x-icon");

    evbuffer_add(buf, thrash_ico, sizeof(thrash_ico));

    evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}
Ejemplo n.º 28
0
int Server::info(struct evhttp_request *req){
	HttpQuery query(req);
	std::string cname = query.get_str("cname", "");

	evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=utf-8");
	struct evbuffer *buf = evbuffer_new();
	if(!cname.empty()){
		Channel *channel = this->get_channel_by_name(cname);
		int onlines = channel? channel->subs.size : 0;
		evbuffer_add_printf(buf,
			"{\"cname\": \"%s\", \"subscribers\": %d}\n",
			cname.c_str(),
			onlines);
	}else{
		evbuffer_add_printf(buf,
			"{\"version\": \"%s\", \"channels\": %d, \"subscribers\": %d}\n",
			ICOMET_VERSION,
			used_channels.size,
			subscribers);
	}
	evhttp_send_reply(req, 200, "OK", buf);
	evbuffer_free(buf);

	return 0;
}
Ejemplo n.º 29
0
static void internal_error(struct evhttp_request *req, struct evbuffer *evb)
{
    evbuffer_add_printf(evb, "{\"status\":\"failed\"}");
    evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/json");
    evhttp_send_reply(req, 500, "Internal Error", evb);
    printf(" - 500 - error\n");
}
Ejemplo n.º 30
0
void
http_basic_cb(struct evhttp_request *req, void *arg)
{
	struct evbuffer *evb = evbuffer_new();
	int empty = evhttp_find_header(req->input_headers, "Empty") != NULL;
	event_debug(("%s: called\n", __func__));
	evbuffer_add_printf(evb, "This is funny");
	
	/* For multi-line headers test */
	{
		const char *multi =
		    evhttp_find_header(req->input_headers,"X-multi");
		if (multi) {
			if (strcmp("END", multi + strlen(multi) - 3) == 0)
				test_ok++;
			if (evhttp_find_header(req->input_headers, "X-Last"))
				test_ok++;
		}
	}

	/* injecting a bad content-length */
	if (evhttp_find_header(req->input_headers, "X-Negative"))
		evhttp_add_header(req->output_headers,
		    "Content-Length", "-100");

	/* allow sending of an empty reply */
	evhttp_send_reply(req, HTTP_OK, "Everything is fine",
	    !empty ? evb : NULL);

	evbuffer_free(evb);
}