0

I write a libevet program to get data from some web interface. the code like this: sometimes the struct evhttp_request *req of self->done is NULL. I cannot find why of this .

response->connecttime = spi_utils_float_time();
self->evreq = evhttp_request_new(self->done, task);
if (self->evreq == NULL) {
    SPI_LOG_ERROR("spider", "cannot create evhttp_request");
    goto download_error;
}

/* bson_print(&task->task); */

/** set header and body */
/* TODO: set timeout */
spi_http_request_split_header(self, self->header->str, self->evreq);
if (self->body != NULL)
    evbuffer_add_printf(self->evreq->output_buffer, (const char*)self->body->str);

    if (self->method == spi_http_post)
            cmd_type = EVHTTP_REQ_POST;

self->evcon = evhttp_connection_new(self->host->str, self->port);
if (self->evcon == NULL) {
    SPI_LOG_ERROR("spider", "evhttp_connection_new Failed!\n");
    goto download_error;
}

SPI_LOG_DEBUG("spider", "set retries: %d.", 5);
evhttp_connection_set_retries(self->evcon, 3);
SPI_LOG_DEBUG("spider", "set timeout: %d.", 60);
evhttp_connection_set_timeout(self->evcon, 20);

response->connectedtime = spi_utils_float_time();

response->crawltime = spi_utils_float_time();

SPI_LOG_DEBUG("spider", "evhttp_make_request");
if (evhttp_make_request(self->evcon, self->evreq, cmd_type, self->path->str) == -1) {
    SPI_LOG_ERROR("spider", "evhttp_make_request Failed.\n");
    goto download_error;
}
chjuheng
  • 21
  • 4

1 Answers1

0

(From your question it's not entirely clear where you're getting the NULL, but maybe what you mean is that)
the callback passed to evhttp_request_new might get NULL if there was a timeout.

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • the libevent call write method, is a block method. my program is a crawl spider, when requests is large ,write is blocked, cause a timeout. – chjuheng Sep 25 '13 at 03:36
  • I don't think there is a `write` function in libevent. If you're using the libc's `write` from `unistd.h` then you need to swtich the socket into non-blocking mode. – ArtemGr Sep 25 '13 at 09:59
  • libevent-1.4.14b-stable buffer.c int evbuffer_write(struct evbuffer *buffer, int fd) { int n; #ifndef WIN32 n = write(fd, buffer->buffer, buffer->off); #else n = send(fd, buffer->buffer, buffer->off, 0); #endif – chjuheng Sep 26 '13 at 04:12
  • `write` and `evbuffer_write` are two different functions. You don't need to post the full signature here to see that, BTW, it's obvious from the name. You might still need to switch the underlying socket into non-blocking mode even when using `evbuffer_write`. In any case, if you have a question about `evbuffer_write` behaviour you'd better use the http://stackoverflow.com/questions/ask to ask it. – ArtemGr Sep 27 '13 at 20:55