static void slowdata_timer_cb(evutil_socket_t fd, short event, void * arg) { evhtp_request_t * req = (evhtp_request_t *)arg; /* Write an additional 1 byte */ evbuf_t* buf = evbuffer_new(); evbuffer_add(buf, "0", 1); evhtp_send_reply_chunk(req, buf); evbuffer_drain(buf, -1); ++slow_data_bytes; if (slow_data_bytes == 10) { /* Reached our limit, close the reply */ evhtp_send_reply_chunk_end(req); evtimer_del(timer_event); } else { /* Keep going */ struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 500000; evtimer_add(timer_event, &tv); } evbuffer_free(buf); }
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); }
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); }
static void data_cb(evhtp_request_t * req, void * arg) { evbuf_t * buf; buf = evbuffer_new(); evhtp_send_reply_chunk_start(req, EVHTP_RES_OK); evbuffer_add(buf, "SUCCESS", strlen("SUCCESS")); evhtp_send_reply_chunk(req, buf); evbuffer_drain(buf, -1); evhtp_send_reply_chunk_end(req); evbuffer_free(buf); }
static void badchunk_transfer_cb(evhtp_request_t * req, void * arg) { /* Start the chunk */ evhtp_send_reply_chunk_start(req, EVHTP_RES_OK); /* Send a few chunks with a bogus GET in the middle */ send_chunk(req, "DATA", "%d\r\n", strlen("DATA")); send_chunk(req, "GET /index.html HTTP/1.1", "", 0); send_chunk(req, "MOREDATA", "%d\r\n", strlen("DATA")); /* Flush the connection */ bufferevent_flush(req->conn->bev, EV_WRITE, BEV_FLUSH); /* Close the chunk */ evhtp_send_reply_chunk_end(req); }
static void test_chunking(evhtp_request_t * req, void * arg) { const char * chunk_str; evbuf_t * buf; int i = 0; buf = evbuffer_new(); evhtp_send_reply_chunk_start(req, EVHTP_RES_OK); while ((chunk_str = chunk_strings[i++]) != NULL) { evbuffer_add(buf, chunk_str, strlen(chunk_str)); evhtp_send_reply_chunk(req, buf); evbuffer_drain(buf, -1); } evhtp_send_reply_chunk_end(req); evbuffer_free(buf); }
void send_file(evhtp_request_t* req, char* path) { struct stat st; unsigned char data[1024]; int fd; int n; evbuf_t* buf = NULL; if (stat(path, &st) < 0 || S_ISDIR(st.st_mode)) goto file_error; fd = open(path, O_RDONLY); if (fd < 0) goto file_error; buf = evbuffer_new(); evhtp_headers_add_header(req->headers_out, evhtp_header_new("Content-Type", guess_content_type(path), 0, 0)); evhtp_send_reply_chunk_start(req, EVHTP_RES_OK); do { n = read(fd, data, sizeof(data)); evbuffer_add(buf, data, n); evhtp_send_reply_chunk(req, buf); evbuffer_drain(buf, -1); } while (n > 0); close(fd); evhtp_send_reply_chunk_end(req); evbuffer_free(buf); return; file_error: evbuffer_add_printf(req->buffer_out, "ERROR:%s", path); evhtp_send_reply(req, EVHTP_RES_OK); }