如何阅读evbuffer并将其放入libevent中的字符串(char *)中

时间:2012-08-10 14:30:05

标签: c unix libevent

我正在使用libevent及其http API来编写一个能够编写C servlet的简单HTTP服务器。这个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上)

3 个答案:

答案 0 :(得分:2)

听起来像是要使用

ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data, size_t datalen)

它将缓冲区内容(最多datalen个字节)复制到内存区data。 它记录在libevent book

答案 1 :(得分:2)

这是一个有效的方法:

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);
}

答案 2 :(得分:2)

根据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);