Exemple #1
0
void
httpd_driver(struct evhttp_request *req, void *arg)
{
    struct evbuffer *buf;

    buf = evbuffer_new();

    evhttp_add_header(req->output_headers, "Content-Type", "text/plain");
    evbuffer_add_printf(buf, "Thrashd version: %s [%s]\n", VERSION,
                        process_name);

    evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}
Exemple #2
0
static void server_rpc_cb(struct evhttp_request *req, void *arg) {
    struct rpc_context *rpc_context = (struct rpc_context *)arg;

    char *json = read_req_buffer(req);
    if(!json) {
        evhttp_send_error(req, 500, "Internal server error");
        return;
    }

    struct method_t *method = deserialize_method_call(json);
    if(!method) {
        evhttp_send_error(req, 500, "Error deserializing method call");
        free(json);
        return;
    }
    void *satellite_data;
    rpc_method_impl_t impl = search_method_entry(rpc_context->lookup, method->name, &satellite_data);
    if(!impl) {
        evhttp_send_error(req, 500, "Method not found");
    } else {
        //impl should also throw error
        char *err;
        struct data_t *result = impl(method->params, method->nparams, &err, satellite_data);
        char *json_result = NULL;

        struct evbuffer *evb = evbuffer_new();

        if(result && (json_result = serialize_result(result, NULL)) && evb) {
            if(evbuffer_add(evb, json_result, strlen(json_result))) {
                evhttp_send_error(req, 500, "Internal server error");
            } else {
                evhttp_send_reply(req, 200, "OK", evb);
            }
        } else {
            if(err) {
                evhttp_send_error(req, 500, err);
                free(err);
            } else {
                evhttp_send_error(req, 500, "Internal server error");
            }
        }

        if(evb) evbuffer_free(evb);
        if(json_result) free(json_result);
        if(result) free_data_t(result);
    }

    free(json);
    free_method_t(method);
}
Exemple #3
0
int
NeubotPoller_resolve(struct NeubotPoller *poller, const char *family,
    const char *address, neubot_hook_vos callback, void *opaque)
{
	struct ResolveContext *rc;
	int result;

	(void) poller;

	rc = calloc(1, sizeof (*rc));
	if (rc == NULL) {
		neubot_warn("resolve: calloc() failed");
		goto failure;
	}

	rc->callback = callback;
	rc->opaque = opaque;

	rc->names = evbuffer_new();
	if (rc->names == NULL) {
		neubot_warn("resolve: evbuffer_new() failed");
		goto failure;
	}

	if (strcmp(family, "PF_INET6") == 0)
		result = evdns_resolve_ipv6(address, DNS_QUERY_NO_SEARCH,
		    NeubotPoller_resolve_callback_internal, rc);
	else if (strcmp(family, "PF_INET") == 0)
		result = evdns_resolve_ipv4(address, DNS_QUERY_NO_SEARCH,
		    NeubotPoller_resolve_callback_internal, rc);
	else {
		neubot_warn("resolve: invalid family");
		goto failure;
	}

	if (result != 0) {
		neubot_warn("resolve: evdns_resolve_ipvX() failed");
		goto failure;
	}

	return (0);

    failure:
	if (rc != NULL && rc->names != NULL)
		evbuffer_free(rc->names);
	if (rc != NULL)
		free(rc);

	return (-1);
}
Exemple #4
0
void async_closelog(struct async_syslog_state *state) {
	if (state == NULL) //may happend upon initialization
		return;

	size_t i;
	for (i = 0; i < ASYNC_SYSLOG_BUFFER_SIZE; i++)
		evbuffer_free(state->buffers[i]);
	if (state->fd >= 0) {
		event_del(&state->event);
		close(state->fd);
	}
	free(state->ident);
	free(state);
}
Exemple #5
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);	
}
Exemple #6
0
static void http_metrics_request_cb(struct evhttp_request *req, void *arg)
{
    struct http *http = (struct http *)arg;
    struct evbuffer *evb = NULL;
    const char *uri = NULL;
    struct evhttp_uri *decoded = NULL;
    const char *query;
    struct metric_history *mh;

    uri = evhttp_request_get_uri(req);

    /* Decode the URI */
    decoded = evhttp_uri_parse(uri);
    if (!decoded)
    {
        evhttp_send_error(req, HTTP_BADREQUEST, 0);
        goto exit;
    }

    query = evhttp_uri_get_query(decoded);

    printf("metrics request %s\n", query ? query : "null");

    /* This holds the content we're sending. */
    evb = evbuffer_new();

    mh = http_find_metrics_from_query(http->metrics, query);
    if (!mh)
    {
        printf("Series not found in query: %s\n", query);
        http_format_error_response(evb, "metric not found");
    }
    else
    {
        http_format_metric_response(evb,mh);
    }

    /* add headers */
    evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/json");

    /* send the response */
    evhttp_send_reply(req, 200, "OK", evb);

exit:
    if (decoded)
        evhttp_uri_free(decoded);

    if (evb)
        evbuffer_free(evb);
}
Exemple #7
0
static void
send_reply_header(evhtp_request_t * req, const char * header) {
    evbuf_t   * buf   = evbuffer_new();
    const char* found = evhtp_header_find(req->headers_in, header);

    evhtp_send_reply_chunk_start(req, EVHTP_RES_OK);
    evbuffer_add(buf, found, strlen(found));
    evhtp_send_reply_chunk(req, buf);

    evbuffer_drain(buf, -1);

    evhtp_send_reply_chunk_end(req);
    evbuffer_free(buf);
}
void HTTPHandler::info_handle(struct evhttp_request *req, void *arg) {
  if (evhttp_request_get_command(req) != EVHTTP_REQ_GET)
    return;

  LOG(INFO) << "Request for server infomation.";

  // TODO(binfei): need memory pool
  struct evbuffer *databuf = evbuffer_new();
  evbuffer_add_printf(databuf, "hello world");
  evhttp_send_reply_start(req, 200, "OK");
  evhttp_send_reply_chunk(req, databuf);
  evhttp_send_reply_end(req);
  evbuffer_free(databuf);
}
Exemple #9
0
void buf_read_callback(struct bufferevent *incoming, void *arg) {
	struct evbuffer *evreturn;
	char *req;

	req = evbuffer_readline(incoming->input);
	if (req == NULL)
		return;
	
	evreturn = evbuffer_new();
	evbuffer_add_printf(evreturn, "You said %s\n", req);
	bufferevent_write_buffer(incoming, evreturn);
	evbuffer_free(evreturn);
	free(req);
}
//-----------------------------------------------------------------------------
void HTTPHandler::admin_handle(struct evhttp_request *req, void *arg) {
  //TODO(binfei): need ssl and sepecial command to do admin privilege
  LOG(INFO) << "Request for server admin.";
  // TODO(binfei): need memory pool
  struct evbuffer *databuf = evbuffer_new();
  evbuffer_add_printf(databuf, "Server will be stoped.");
  evhttp_send_reply_start(req, 200, "OK");
  evhttp_send_reply_chunk(req, databuf);
  evhttp_send_reply_end(req);
  evbuffer_free(databuf);
  
  HTTPServer *server = static_cast<HTTPServer*>(arg);
  server->Stop();
}
Exemple #11
0
void web_json(struct evhttp_request *req, cJSON *root) {
	struct evbuffer *evb = evbuffer_new();
	char *out;

	out = cJSON_Print(root);
	cJSON_Delete(root);

	evbuffer_add(evb, out, strlen(out));
	evhttp_add_header(req->output_headers, "Content-Type", "application/json");
	evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
	evbuffer_free(evb);

	free(out);
}
int HttpServerMsgHandler::processMessage(void *arg) {
	struct evhttp_request *req = (struct evhttp_request *)arg;

	const char *cmdtype;
	struct evkeyvalq *headers;
	struct evkeyval *header;

	switch (evhttp_request_get_command(req)) {
	case EVHTTP_REQ_GET: cmdtype = "GET"; break;
	case EVHTTP_REQ_POST: cmdtype = "POST"; break;
	case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
	case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
	case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
	case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
	case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
	case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
	case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
	default: cmdtype = "unknown"; break;
	}

	printf("Received a %s request for %s\nHeaders:\n",
		cmdtype, evhttp_request_get_uri(req));

	headers = evhttp_request_get_input_headers(req);
	for (header = headers->tqh_first; header;
		header = header->next.tqe_next) {
			printf("  %s: %s\n", header->key, header->value);
	}

	ConnectionManager *conn = ConnectionManager::getInstance();
	struct evbuffer *return_buffer=evbuffer_new();

	long long total_req = conn->getTotalRequests();
	long long total_res = conn->getTotalResponses();
	unsigned int conns = conn->getConnections();

	long long timeouts = conn->getTimeoutRequests();
	long long err_res = conn->getErrorResponses();
	long long conn_except = conn->getConnExceptions();
	long long ok_res = conn->getOkResponses();

	evbuffer_add_printf(return_buffer,"    total request: %lld<br>	   total response: %lld<br>	   connnections: %u<br><br><hr><br>	ok: %lld	\
		<br>	timeout: %lld<br>	http error: %lld<br>    connExcept: %u<br>"
		, total_req, total_res, conns, ok_res, timeouts, err_res, conn_except);
	evhttp_send_reply(req, HTTP_OK, "OK", return_buffer);
	evbuffer_free(return_buffer);

	return 0;
}
Exemple #13
0
/**
 * @brief
 * Handle the creation of a B+Tree session for this Tapioca connection without
 * destroying anything and providing the execution id
 *
 * @param[in] bpt_id
 * @param[in] open_flags
 * @param[in] execution_id
 * @param[out] rv - 0 if session already created; -1 on error ; otherwise
 	 	 	 	 bpt_id is returned
 */
static void handle_bptree_initialize_bpt_session_no_commit(tcp_client* c,
		struct evbuffer* buffer)
{
	int rv;
	client_bpt_id *c_b = malloc(sizeof(client_bpt_id));
	bptree_session *bps = malloc(sizeof(bptree_session));
	memset(bps, 0,sizeof(bptree_session));
	uint16_t bpt_id;
	enum bptree_open_flags open_flags;
	enum bptree_insert_flags insert_flags;
	uint32_t execution_id;

	struct evbuffer* b = evbuffer_copy(buffer);
	transaction_set_get_cb(c->t, on_bptree_initialize_bpt_session_no_commit, c);
	if (bptree_message_incomplete(b)) return;

	c_b->id = c->id;
	evbuffer_remove(b,&bpt_id, sizeof(uint16_t));
	evbuffer_remove(b,&open_flags, sizeof(enum bptree_open_flags));
	evbuffer_remove(b,&insert_flags, sizeof(enum bptree_insert_flags));
	evbuffer_remove(b,&execution_id, sizeof(uint32_t));

	c_b->bpt_id = bpt_id;

	bps->bpt_id = bpt_id;
	bps->execution_id = execution_id;
	bps->tapioca_client_id = c->id;
	bps->insert_count = 0;
	bps->t = c->t;
	bps->cursor_node = NULL;

	rv = bptree_initialize_bpt_session_no_commit(
			bps, bpt_id, open_flags, insert_flags, execution_id);
	if (rv == BPTREE_OP_TAPIOCA_NOT_READY) return;
	if (rv == BPTREE_OP_SUCCESS)
	{
		bptree_session *bps2;
//		assert(hashtable_search(client_bpt_ids, c_b) == NULL);
		bps2 = hashtable_search(client_bpt_ids, c_b);
		if (bps2 != NULL) free(bps2);
		assert(hashtable_insert(client_bpt_ids, c_b, bps));
		printf("Session_nocomm: %d:%d\n", c_b->id, c_b->bpt_id);
		fflush(stdout);
	}

	evbuffer_free(b);
	evbuffer_drain(buffer, evbuffer_get_length(buffer));
	send_result(c->buffer_ev, rv);
}
Exemple #14
0
void get_master_pgn(struct evhttp_request *req, void *context) {
    if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
        evhttp_send_error(req, HTTP_BADMETHOD, "Method Not Allowed");
        return;
    }

    const struct evhttp_uri *uri = evhttp_request_get_evhttp_uri(req);
    if (!uri) {
        puts("evhttp_request_get_evhttp_uri failed");
        return;
    }

    const char *path = evhttp_uri_get_path(uri);
    if (!path) {
        puts("evhttp_uri_get_path failed");
        return;
    }

    char game_id[9] = {};
    int end;
    if (1 != sscanf(path, "/master/pgn/%8s%n", game_id, &end) ||
            strlen(game_id) != 8 ||
            strlen(path) != end) {
        evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
        return;
    }

    size_t pgn_size;
    char *pgn = kcdbget(master_pgn_db, game_id, 8, &pgn_size);
    if (!pgn) {
        evhttp_send_error(req, HTTP_NOTFOUND, "Master PGN Not Found");
        return;
    }

    struct evbuffer *res = evbuffer_new();
    if (!res) {
        puts("could not allocate response buffer");
        abort();
    }

    struct evkeyvalq *headers = evhttp_request_get_output_headers(req);
    evhttp_add_header(headers, "Content-Type", "application/vnd.chess-pgn; charset=utf-8");

    evbuffer_add(res, pgn, pgn_size);
    kcfree(pgn);

    evhttp_send_reply(req, HTTP_OK, "OK", res);
    evbuffer_free(res);
}
Exemple #15
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);
}
Exemple #16
0
static struct evbuffer *
mxml_to_evbuf(mxml_node_t *tree)
{
  struct evbuffer *evbuf;
  char *xml;
  int ret;

  evbuf = evbuffer_new();
  if (!evbuf)
    {
      DPRINTF(E_LOG, L_RSP, "Could not create evbuffer for RSP reply\n");

      return NULL;
    }

  xml = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);
  if (!xml)
    {
      DPRINTF(E_LOG, L_RSP, "Could not finalize RSP reply\n");

      evbuffer_free(evbuf);
      return NULL;
    }

  ret = evbuffer_add(evbuf, xml, strlen(xml));
  free(xml);
  if (ret < 0)
    {
      DPRINTF(E_LOG, L_RSP, "Could not load evbuffer for RSP reply\n");

      evbuffer_free(evbuf);
      return NULL;
    }

  return evbuf;
}
Exemple #17
0
int
msg_attack_assign(struct msg *msg,
    const struct kill* value)
{
   struct evbuffer *tmp = NULL;
   if (msg->attack_set) {
     kill_clear(msg->attack_data);
     msg->attack_set = 0;
   } else {
     msg->attack_data = kill_new();
     if (msg->attack_data == NULL) {
       event_warn("%s: kill_new()", __func__);
       goto error;
     }
   }
   if ((tmp = evbuffer_new()) == NULL) {
     event_warn("%s: evbuffer_new()", __func__);
     goto error;
   }
   kill_marshal(tmp, value);
   if (kill_unmarshal(msg->attack_data, tmp) == -1) {
     event_warnx("%s: kill_unmarshal", __func__);
     goto error;
   }
   msg->attack_set = 1;
   evbuffer_free(tmp);
   return (0);
 error:
   if (tmp != NULL)
     evbuffer_free(tmp);
   if (msg->attack_data != NULL) {
     kill_free(msg->attack_data);
     msg->attack_data = NULL;
   }
   return (-1);
}
Exemple #18
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;
}
Exemple #19
0
void cweb_request_handler(struct evhttp_request *req, void *arg)
{
	struct evbuffer *returnbuffer = evbuffer_new();
	struct page *dest = http_router(req->uri);

	if (dest == NULL) {
		if (strstr(req->uri, ".html") != NULL)
			evbuffer_add_printf(returnbuffer, "<!doctype html>\n<html lang=zh>\n<head>\n\t"
				"<title>404 Not Found</title>\n</head>\n<body>\n</body>\n</html>");
	} else
		dest->get(returnbuffer);
	evhttp_send_reply(req, HTTP_OK, "Client", returnbuffer);
	evbuffer_free(returnbuffer);
	return;
}
Exemple #20
0
static void
badchunk_length_cb(evhtp_request_t * req, void * arg) {
    evbuf_t * buf = evbuffer_new();
    evbuf_t * output;

    /* Start the chunk */
    evhtp_send_reply_chunk_start(req, EVHTP_RES_OK);

    /* Send some data */
    send_chunk(req, "SUCCESS", "%d\r\n", strlen("SUCCESS"));

    /* Close the chunk */
    evhtp_send_reply_chunk_end(req);
    evbuffer_free(buf);
}
bool NFCHttpServer::ResponseMsg(const NFHttpRequest& req, const std::string& strMsg, NFWebStatus code,
                                const std::string& strReason)
{
    evhttp_request* pHttpReq = (evhttp_request*) req.req;
    //create buffer
    struct evbuffer* eventBuffer = evbuffer_new();
    //send data
    evbuffer_add_printf(eventBuffer, strMsg.c_str());
    evhttp_add_header(evhttp_request_get_output_headers(pHttpReq), "Content-Type", "text/html");
    evhttp_send_reply(pHttpReq, code, strReason.c_str(), eventBuffer);

    //free
    evbuffer_free(eventBuffer);
    return true;
}
Exemple #22
0
static void
task_free( struct tr_web_task * task )
{
    if( task->slist != NULL )
        curl_slist_free_all( task->slist );
    if( task->timer_event_isSet )
        evtimer_del( &task->timer_event );
    evbuffer_free( task->response );
    tr_free( task->host );
    tr_free( task->range );
    tr_free( task->resolved_url );
    tr_free( task->url );
    memset( task, TR_MEMORY_TRASH, sizeof( struct tr_web_task ) );
    tr_free( task );
}
Exemple #23
0
void
httpd_put_holddowns_html (struct evhttp_request *req, void *arg)
{
    struct evbuffer *buf;

    buf = evbuffer_new();
    httpd_put_html_start(buf, "Hold downs", TRUE);
    evbuffer_add_printf(buf, "<tr><th>Blocked IP</th>%s<th>Triggered By</th><th>Count</th><th>Velocity</th><th>Soft</th><th>Hard</th><th>Recent</th><th>Reason</th>%s</tr>", geoip_header, (http_password?"<th>Actions</th>":""));
    g_tree_foreach(current_blocks, (GTraverseFunc) fill_http_blocks_html, buf);
    httpd_put_html_end(buf);

    evhttp_add_header(req->output_headers, "Content-Type", "text/html");
    evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}
Exemple #24
0
void
httpd_put_connections_html (struct evhttp_request *req, void *arg)
{
    struct evbuffer *buf;

    buf = evbuffer_new();
    httpd_put_html_start(buf, "Connections", TRUE);
    evbuffer_add_printf(buf, "<tr><th>Address</th><th>Port</th><th>Requests</th><th>Connection Date</th><th>Last Date</th></tr>");
    g_slist_foreach(current_connections, (GFunc) fill_current_connections_html, buf);
    httpd_put_html_end(buf);

    evhttp_add_header(req->output_headers, "Content-Type", "text/html");
    evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}
Exemple #25
0
void
evhtp_send_reply(evhtp_request_t * request, evhtp_res code) {
    evhtp_connection_t * c= request->conn;
    evbuf_t            * reply_buf;

    request->finished = 1;

    if (!(reply_buf = _evhtp_create_reply(request, code))) {
		delete request->conn;
		return ;
    }

    bufferevent_write_buffer(c->bev, reply_buf);
    evbuffer_free(reply_buf);
}
Exemple #26
0
void
httpd_put_uris_html (struct evhttp_request *req, void *arg)
{
    struct evbuffer *buf;

    buf = evbuffer_new();
    httpd_put_html_start(buf, "URIs", TRUE);
    evbuffer_add_printf(buf, "<tr><th>Address</th>%s<th>Connections</th><th>Timeout</th><th>Reason</th><th>URI (80 char max)</th>%s</tr>",geoip_header, (http_password?"<th>Actions</th>":""));
    g_hash_table_foreach(uri_table, (GHFunc) fill_http_uri_html, buf);
    httpd_put_html_end(buf);

    evhttp_add_header(req->output_headers, "Content-Type", "text/html");
    evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}
Exemple #27
0
/**
 * This method sends a handshake to a connected server via proxy
 * The sever connection is handled with player->bev
 * 
 * @remarks Scope: private
 * 
 * @param player player struct to connect with
 */
void send_proxyhandshake(struct PL_entry *player)
{
  struct evbuffer *output = bufferevent_get_output(player->sev);
  struct evbuffer *tempbuf = evbuffer_new();
  
  uint8_t pid = PID_HANDSHAKE;
  int16_t n_ulen = htons(player->username->slen);
  
  evbuffer_add(tempbuf, &pid, sizeof(pid));
  evbuffer_add(tempbuf, &n_ulen, sizeof(n_ulen));
  evbuffer_add(tempbuf, player->username->data, player->username->slen);
  
  evbuffer_add_buffer(output,tempbuf);
  evbuffer_free(tempbuf);
}
Exemple #28
0
static void
send_simple_response (struct evhttp_request * req,
                      int                     code,
                      const char            * text)
{
  const char * code_text = tr_webGetResponseStr (code);
  struct evbuffer * body = evbuffer_new ();

  evbuffer_add_printf (body, "<h1>%d: %s</h1>", code, code_text);
  if (text)
    evbuffer_add_printf (body, "%s", text);
  evhttp_send_reply (req, code, code_text, body);

  evbuffer_free (body);
}
Exemple #29
0
/**
 * Spin out an error item to the client
 *
 * @param client placeholder with client request
 * @param text error message text
 */
static void handle_error(client_t *client, char *text) {
    struct evbuffer *evb;
    char *buffer;

    assert(client);
    assert(client->request);

    if((!client) || (!client->request)) {
        /* can't happen -- dispatcher catches this */
        close_client(client);
        return;
    }

    evb = evbuffer_new();

    buffer = (char*)malloc(strlen(text) + 10);  /* "3%s\t\t\t\n\r.\n\r", text */
    if(!buffer) {
        ERROR("malloc error");
        evbuffer_free(evb);
        close_client(client);
        return;
    }

    sprintf(buffer, "3%s\t\t\t\n\r.\n\r", text);
    evbuffer_add(evb, (void*)buffer, strlen(buffer));

    DEBUG("Queueing %d bytes for write on fd %d", strlen(buffer),
          client->fd);

    /* write low-water should already be zero */
    bufferevent_enable(client->buf_ev, EV_WRITE);
    bufferevent_write_buffer(client->buf_ev, evb);
    evbuffer_free(evb);

    return;
}
Exemple #30
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);
}