我正在使用libevent及其http API编写一个简单的HTTP服务器,该服务器能够编写C servlets。这个servlet可以正常使用GET,但是现在我要用POST发送一些数据,并且我想读取传入的事件缓冲区evb。我想打印/检查evb中存在的数据,但我不能。您知道如何将evb (evbuffer)中的数据放入char*变量中吗?我只看到了操纵缓冲区的方法,但没有读取它。我试过了:
evb->
代码如下:
#include <stdio.h>
#include "servlet.h"
void servlet(struct evhttp_request *req, struct evbuffer *evb) {
time_t now;
time(&now);
evbuffer_add_printf(evb, "<html>\n <head>\n"
" <title>%s</title>\n"
" </head>\n"
" <body>\n"
" <h1>%s</h1>\n"
" <p>Current time is: %s</p>",
"C servlet engine", "C servlet", ctime(&now));
evhttp_add_header(evhttp_request_get_output_headers(req),
"Content-Type", "text/html");
evhttp_send_reply(req, 200, "OK", evb);
}
I am trying this
void servlet(struct evhttp_request *req, struct evbuffer *evb) {
size_t len = evbuffer_get_length(evhttp_request_get_input_buffer(req));
struct evbuffer *in_evb = evhttp_request_get_input_buffer(req);
char *data;
evbuffer_copyout(in_evb, data, len);但我得到总线错误: 10 (我在mac上)
发布于 2012-08-10 23:00:42
听起来你想用
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data, size_t datalen)它将缓冲区内容(最多datalen字节)复制到内存区data。它在libevent book中有记录
发布于 2015-06-04 17:19:15
下面是一个有效的方法:
static void echo_read_cb(struct bufferevent *bev, void *ctx)
{
/* This callback is invoked when there is data to read on bev. */
struct evbuffer *input = bufferevent_get_input(bev);
struct evbuffer *output = bufferevent_get_output(bev);
size_t len = evbuffer_get_length(input);
char *data;
data = malloc(len);
evbuffer_copyout(input, data, len);
printf("we got some data: %s\n", data);
/* Copy all the data from the input buffer to the output buffer. */
evbuffer_add_buffer(output, input);
free(data);
}发布于 2017-01-25 15:49:19
根据libevent的buffer.h中的源代码,我们应该使用
int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);而不是
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);下面是从buffer.h借用的代码
/**
Read data from an evbuffer and drain the bytes read.
If more bytes are requested than are available in the evbuffer, we
only extract as many bytes as were available.
@param buf the evbuffer to be read from
@param data the destination buffer to store the result
@param datlen the maximum size of the destination buffer
@return the number of bytes read, or -1 if we can't drain the buffer.
*/
int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
/**
Read data from an evbuffer, and leave the buffer unchanged.
If more bytes are requested than are available in the evbuffer, we
only extract as many bytes as were available.
@param buf the evbuffer to be read from
@param data_out the destination buffer to store the result
@param datlen the maximum size of the destination buffer
@return the number of bytes read, or -1 if we can't drain the buffer.
*/
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);https://stackoverflow.com/questions/11903796
复制相似问题