void cmd_get(struct bufferevent *bev, void *ctx, int argc, char **argv) { struct evbuffer *output = bufferevent_get_output(bev), *buf = evbuffer_new(); if (*argv == NULL) { evbuffer_add_reference(output, "ERROR\r\n", 7, NULL, NULL); return; } do { answer_key(buf, *argv); if (evbuffer_get_length(buf) == 0) continue; #ifdef CONVERT_TO_UTF8 buf = evbuffer_b2u(buf); if (buf == NULL) { // Failed to convert buf = evbuffer_new(); continue; } #endif evbuffer_add_printf(output, "VALUE %s 0 %ld\r\n", *argv, evbuffer_get_length(buf)); evbuffer_add_buffer(output, buf); evbuffer_add_printf(output, "\r\n"); } while (*++argv); evbuffer_add_reference(output, "END\r\n", 5, NULL, NULL); evbuffer_free(buf); }
static void test_evbuffer_remove_buffer_with_empty(void *ptr) { struct evbuffer *src = evbuffer_new(); struct evbuffer *dst = evbuffer_new(); char buf[2]; evbuffer_validate(src); evbuffer_validate(dst); /* setup the buffers */ /* we need more data in src than we will move later */ evbuffer_add_reference(src, buf, sizeof(buf), no_cleanup, NULL); evbuffer_add_reference(src, buf, sizeof(buf), no_cleanup, NULL); /* we need one buffer in dst and one empty buffer at the end */ evbuffer_add(dst, buf, sizeof(buf)); evbuffer_add_reference(dst, buf, 0, no_cleanup, NULL); evbuffer_validate(src); evbuffer_validate(dst); /* move three bytes over */ evbuffer_remove_buffer(src, dst, 3); evbuffer_validate(src); evbuffer_validate(dst); end: evbuffer_free(src); evbuffer_free(dst); }
static void test_max_body(evhtp_request_t * req, void * arg) { evbuffer_add_reference(req->buffer_out, "test_max_body\n", 14, NULL, NULL); evhtp_send_reply(req, EVHTP_RES_OK); }
static void test_500_cb(evhtp_request_t * req, void * arg ) { evbuffer_add_reference(req->buffer_out, "test_500_cb\n", 12, NULL, NULL); evhtp_send_reply(req, EVHTP_RES_SERVERR); }
static void response_cb(evhtp_request_t * r, void * a) { evbuffer_add_reference(r->buffer_out, (const char *)a, payload_sz, NULL, NULL); evhtp_send_reply(r, EVHTP_RES_OK); }
static void test_default_cb(evhtp_request_t * req, void * arg) { evbuffer_add_reference(req->buffer_out, "test_default_cb\n", 16, NULL, NULL); evhtp_send_reply(req, EVHTP_RES_OK); }
void httpserver_process_request(struct evhttp_request *req) { //Process http request //... pthread_t tid = pthread_self(); printf("tid=%ld\n", tid); //char *post = EVBUFFER_DATA(req->input_buffer); //Get evbuffer data as post client data size_t length = evbuffer_get_length(req->input_buffer); if (length <= 0) { printf("recv http response len=%lu completely\n", length); return; } //获取post json数据 char *buffer = malloc(length+1); if (NULL == buffer) { return; } evbuffer_copyout(req->input_buffer, buffer, length); buffer[length] = 0; printf("recv http response [%s] len=%lu completely\n", buffer, length); //解析post json数据 dhcp_pair *pair = malloc(sizeof(dhcp_pair)); if (NULL == pair) { return; } memset(pair, 0, sizeof(dhcp_pair)); parse_request_json(buffer, pair); free(buffer); dhcp_leases_get(pair); if (pair->length == 0) { return; } struct evbuffer *buf = evbuffer_new(); printf("%s(%d)\n", __FUNCTION__, __LINE__); if (buf == NULL) return; char *response = form_response_json(pair); //printf("post=[%s]\n", post); printf("response=[%s]\n", response); //evbuffer_add_printf(buf, "Server Responsed. Requested: %s\n", evhttp_request_get_uri(req)); evbuffer_add_reference(buf, response, strlen(response), _buffer_clean, NULL); //free(response); evhttp_send_reply(req, HTTP_OK, "OK", buf); evbuffer_free(buf); //evbuffer_add_reference(req->buffer_out, response, strlen(response), _buffer_clean, NULL); //evhtp_send_reply(req, EVHTP_RES_OK); free(pair); }
static void http_ref_cb(struct evhttp_request *req, void *arg) { struct evbuffer *evb = evbuffer_new(); evbuffer_add_reference(evb, content, content_len, NULL, NULL); /* allow sending of an empty reply */ evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb); evbuffer_free(evb); }
static int evbuffer_slice(struct evbuffer *buf, int offset, int size) { int len = evbuffer_get_length(buf); if (offset + size > len) return -1; struct evbuffer *back = evbuffer_new(); evbuffer_add_buffer(back, buf); if (evbuffer_add_reference(buf, evbuffer_pullup(back, len) + offset, size, cleanup_evbuffer, back) == 0) return 0; evbuffer_free(back); return -1; }
static void submit_request(struct bufferevent *bev, struct request_ctx *req) { evbuffer_add_printf(bufferevent_get_output(bev), "%s %s HTTP/1.1\r\n" "Host: %s\r\n" "Connection: %s\r\n", req->method, req->path, req->host, conn_stash_is_keepalive(req->conn_stash) ? "Keep-Alive" : "close"); if (req->access_token != NULL) { evbuffer_add_printf(bufferevent_get_output(bev), "Authorization: Bearer %s\r\n", req->access_token); } if (strcmp(req->method, "POST") == 0) { evbuffer_add_printf(bufferevent_get_output(bev), "Content-Type: application/x-www-form-urlencoded\r\n"); } if (req->request_body != NULL) { evbuffer_add_printf(bufferevent_get_output(bev), "Content-Length: %zd\r\n", req->request_body_length); } evbuffer_add_printf(bufferevent_get_output(bev), "\r\n"); if (req->request_body != NULL) { evbuffer_add_reference(bufferevent_get_output(bev), req->request_body, req->request_body_length, (evbuffer_ref_cleanup_cb)NULL, NULL); } bufferevent_enable(bev, EV_READ|EV_WRITE); }
int _ws_send_data(ws_t ws, char *msg, uint64_t len, int no_copy) { // TODO: We supply a len of uint64_t, evbuffer_add uses size_t... assert(ws); LIBWS_LOG(LIBWS_TRACE, " Send the data (%llu bytes)", len); if (!ws->bev) { LIBWS_LOG(LIBWS_ERR, "Null bufferevent on send"); return -1; } // If in no copy mode we only add a reference to the passed // buffer to the underlying bufferevent, and let it use the // user supplied cleanup function when it has sent the data. // (Note that the header will never be sent like this). if (no_copy && ws->no_copy_cleanup_cb) { if (evbuffer_add_reference(bufferevent_get_output(ws->bev), (void *)msg, (size_t)len, _ws_builtin_no_copy_cleanup_wrapper, (void *)ws)) { LIBWS_LOG(LIBWS_ERR, "Failed to write reference to send buffer"); return -1; } } else { // Send like normal (this will copy the data). if (evbuffer_add(bufferevent_get_output(ws->bev), msg, (size_t)len)) { LIBWS_LOG(LIBWS_ERR, "Failed to write to send buffer"); return -1; } } return 0; }
void cmd_unknown(struct bufferevent *bev, void *ctx, int argc, char **argv) { static const char msg[] = "SERVER_ERROR Not implemented\r\n"; evbuffer_add_reference(bufferevent_get_output(bev), msg, strlen(msg), NULL, NULL); }
void cmd_version(struct bufferevent *bev, void *ctx, int argc, char **argv) { static const char msg[] = "VERSION 0.0.2\r\n"; evbuffer_add_reference(bufferevent_get_output(bev), msg, strlen(msg), NULL, NULL); }
static evbuffer * _evhtp_create_reply(evhtp_request_t * request, evhtp_res code) { evbuffer * buf = evbuffer_new(); if (evbuffer_get_length(request->buffer_out) && request->chunked == 0) { /* add extra headers (like content-length/type) if not already present */ if (!evhtp_header_find(request->headers_out, "Content-Length")) { std::ostringstream oss; oss<<evbuffer_get_length(request->buffer_out); std::string lstr=oss.str(); evhtp_headers_add_header(request->headers_out, evhtp_header_new("Content-Length", lstr.c_str(), 0, 1)); } if (!evhtp_header_find(request->headers_out, "Server")) { evhtp_headers_add_header(request->headers_out, evhtp_header_new("Server", "nginx/1.1.0", 0, 0)); } if (!evhtp_header_find(request->headers_out, "Content-Type")) { evhtp_headers_add_header(request->headers_out, evhtp_header_new("Content-Type", "text/plain", 0, 0)); } } else { if (!evhtp_header_find(request->headers_out, "Content-Length")) { const char * chunked = evhtp_header_find(request->headers_out, "transfer-encoding"); if (!chunked || !strstr(chunked, "chunked")) { evhtp_headers_add_header(request->headers_out, evhtp_header_new("Content-Length", "0", 0, 0)); } } } /* add the proper keep-alive type headers based on http version */ switch (request->proto) { case EVHTP_PROTO_11: if (request->keepalive == 0) { /* protocol is HTTP/1.1 but client wanted to close */ evhtp_headers_add_header(request->headers_out, evhtp_header_new("Connection", "close", 0, 0)); } break; case EVHTP_PROTO_10: if (request->keepalive == 1) { /* protocol is HTTP/1.0 and clients wants to keep established */ evhtp_headers_add_header(request->headers_out, evhtp_header_new("Connection", "keep-alive", 0, 0)); } break; default: /* this sometimes happens when a response is made but paused before * the method has been parsed */ request->conn->parser->set_major(1); request->conn->parser->set_minor(0); break; } /* switch */ /* add the status line */ evbuffer_add_printf(buf, "HTTP/%d.%d %d %s\r\n", request->conn->parser->get_major(), request->conn->parser->get_minor(), code, StatusCodeManager::instance().status_code_to_str(code)); evhtp_headers_for_each(request->headers_out, _evhtp_create_headers, buf); evbuffer_add_reference(buf, "\r\n", 2, NULL, NULL); if (evbuffer_get_length(request->buffer_out)) { evbuffer_add_buffer(buf, request->buffer_out); } return buf; } /* _evhtp_create_reply */