static void http_readcb(struct bufferevent *bev, void *arg) { const char *what = "This is funny"; event_debug(("%s: %s\n", __func__, EVBUFFER_DATA(bev->input))); if (evbuffer_find(bev->input, (const unsigned char*) what, strlen(what)) != NULL) { struct evhttp_request *req = evhttp_request_new(NULL, NULL); enum message_read_status done; req->kind = EVHTTP_RESPONSE; done = evhttp_parse_firstline(req, bev->input); if (done != ALL_DATA_READ) goto out; done = evhttp_parse_headers(req, bev->input); if (done != ALL_DATA_READ) goto out; if (done == 1 && evhttp_find_header(req->input_headers, "Content-Type") != NULL) test_ok++; out: evhttp_request_free(req); bufferevent_disable(bev, EV_READ); if (base) event_base_loopexit(base, NULL); else event_loopexit(NULL); } }
void LibEventServer::onResponse(int worker, evhttp_request *request, int code, LibEventTransport *transport) { int nwritten = 0; bool skip_sync = false; if (request->evcon == nullptr) { evhttp_request_free(request); return; } #ifdef _EVENT_USE_OPENSSL skip_sync = evhttp_is_connection_ssl(request->evcon); #endif int totalSize = 0; if (RuntimeOption::LibEventSyncSend && !skip_sync) { const char *reason = HttpProtocol::GetReasonString(code); timespec begin, end; Timer::GetMonotonicTime(begin); #ifdef EVHTTP_SYNC_SEND_REPORT_TOTAL_LEN nwritten = evhttp_send_reply_sync(request, code, reason, nullptr, &totalSize); #else nwritten = evhttp_send_reply_sync_begin(request, code, reason, nullptr); #endif Timer::GetMonotonicTime(end); int64_t delay = gettime_diff_us(begin, end); transport->onFlushBegin(totalSize); transport->onFlushProgress(nwritten, delay); } m_responseQueue.enqueue(worker, request, code, nwritten); }
static void HHVM_METHOD(EventHttpRequest, free) { EventHttpRequestResourceData *resource_data = FETCH_RESOURCE(this_, EventHttpRequestResourceData, s_event_http_request); if(resource_data->isInternal){ return; } evhttp_request_free((evhttp_request_t *) resource_data->getInternalResourceData()); }
void create_request(const char *url) { struct evhttp_uri *http_uri = NULL; const char *host; struct bufferevent *bev; struct evhttp_connection *evcon = NULL; struct evhttp_request *req; struct evkeyvalq *output_headers; http_uri = evhttp_uri_parse(url); if (NULL != http_uri) { host = evhttp_uri_get_host(http_uri); if (NULL != host) { bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); evcon = evhttp_connection_base_bufferevent_new(base, dnsbase, bev, host, HTTP_PORT); if (NULL != evcon) { evhttp_connection_set_timeout(evcon, TIMEOUT); req = evhttp_request_new(http_request_done, bev); if (NULL != req) { output_headers = evhttp_request_get_output_headers(req); evhttp_add_header(output_headers, "Accept", "text/plain;q=0.8"); evhttp_add_header(output_headers, "Host", host); evhttp_add_header(output_headers, "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); evhttp_add_header(output_headers, "Connection", "close"); if (0 == evhttp_make_request(evcon, req, EVHTTP_REQ_GET, url)) { ++n_pending_requests; } else { evhttp_request_free(req); fprintf(stderr, "evhttp_make_request() failed\n"); } //evhttp_request_free(req); } else { fprintf(stderr, "evhttp_request_new() failed\n"); } //evhttp_connection_free(evcon); } else { fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n"); } } else { fprintf(stderr, "url must have a host %s\n", url); } evhttp_uri_free(http_uri); } else { fprintf(stderr, "malformed url %s\n", url); } }
void PendingResponseQueue::process() { // clean up the pipe for next signals char buf[512]; if (read(m_ready.getOut(), buf, sizeof(buf)) < 0) { // an error occured but nothing we can really do } // making a copy so we don't hold up the mutex very long ResponsePtrVec responses; for (int i = 0; i < RuntimeOption::ResponseQueueCount; i++) { ResponseQueue &q = *m_responseQueues[i]; Lock lock(q.m_mutex); responses.insert(responses.end(), q.m_responses.begin(), q.m_responses.end()); q.m_responses.clear(); } for (unsigned int i = 0; i < responses.size(); i++) { Response &res = *responses[i]; evhttp_request *request = res.request; int code = res.code; if (request->evcon == nullptr) { evhttp_request_free(request); continue; } bool skip_sync = false; #ifdef _EVENT_USE_OPENSSL skip_sync = evhttp_is_connection_ssl(request->evcon); #endif if (res.chunked) { if (res.chunk) { if (res.firstChunk) { const char *reason = HttpProtocol::GetReasonString(code); evhttp_send_reply_start(request, code, reason); } evhttp_send_reply_chunk(request, res.chunk); } else { evhttp_send_reply_end(request); } } else if (RuntimeOption::LibEventSyncSend && !skip_sync) { evhttp_send_reply_sync_end(res.nwritten, request); } else { const char *reason = HttpProtocol::GetReasonString(code); evhttp_send_reply(request, code, reason, nullptr); } } }
void stream_request_readcb(struct bufferevent *bev, void *arg) { struct evhttp_request *req; struct StreamRequest *sr = (struct StreamRequest *)arg; _DEBUG("stream_request_readcb()\n"); switch (sr->state) { case read_firstline: req = evhttp_request_new(NULL, NULL); req->kind = EVHTTP_RESPONSE; // 1 is the constant ALL_DATA_READ in http-internal.h if (evhttp_parse_firstline(req, EVBUFFER_INPUT(bev)) == 1) { sr->state = read_headers; } evhttp_request_free(req); // dont break, try to parse the headers too case read_headers: req = evhttp_request_new(NULL, NULL); req->kind = EVHTTP_RESPONSE; // 1 is the constant ALL_DATA_READ in http-internal.h if (evhttp_parse_headers(req, EVBUFFER_INPUT(bev)) == 1) { if (sr->header_cb) { sr->header_cb(sr->bev, req->input_headers, sr->arg); } sr->state = read_body; } evhttp_request_free(req); break; case read_body: if (sr->read_cb) { sr->read_cb(sr->bev, sr->arg); } break; } }
static void rpc_grneral_request(char *ip, int port, const char *rpcname, void *req, marshal_func req_marshal, void *resp, unmarshal_func resp_unmarshal) { struct evhttp_connection *evcon = connection_pool_get_or_create_conn(conn_pool, ip, port); struct evhttp_request *evreq = evhttp_request_new(rpc_rpc_grneral_requestuest_cb, NULL); evhttp_request_own(evreq); // this means that I should free it my self req_marshal(evreq->output_buffer, req); if (evhttp_make_request(evcon, evreq, EVHTTP_REQ_POST, rpcname)) logging(LOG_ERROR, "error on make_request"); event_dispatch();//TODO if (resp_unmarshal(resp, evreq->input_buffer)) { logging(LOG_ERROR, "error on statfs_response_unmarshal"); } connection_pool_insert(conn_pool, ip, port, evcon); evhttp_request_free(evreq); }
void HTTPClient_t::reset() { if (respCode == HTTP_OK && request) { evhttp_request_free(request); } else { // if (conn) evhttp_connection_free(conn); // if (uri) evhttp_uri_free(uri); } uri = NULL; conn = NULL; request = NULL; status = HTTPClient_t::STATUS_UNCHECK; type = IHttpRequest::Type::NONE; respCode = 0; undone(); }
~EVHTTPRequest() { evhttp_request_free(req_); }
static void do_info(struct evhttp_request* req, void* userdata) { // Check request method if(req->type == EVHTTP_REQ_POST || req->type == EVHTTP_REQ_HEAD) { evhttp_send_error(req, 405, "Method Not Allowed"); return; } // Allocate userdata structure struct req_data* d; if(!(d = (struct req_data*) malloc(sizeof(struct req_data)))) { evhttp_send_error(req, 500, "Internal Server Error"); fprintf(stderr, "info.c:do_info(): couldn't malloc req_data\n"); return; } memset(d, 0, sizeof(struct req_data)); d->req = req; // Parse url query parameters char * decoded_uri; if(!(decoded_uri = evhttp_decode_uri(req->uri))) { free_req_data(&d); evhttp_send_error(req, 500, "Internal Server Error"); fprintf(stderr, "info.c:do_info(): couldn't decode uri\n"); return; } if(!(d->query_args = (struct evkeyvalq*) malloc(sizeof(struct evkeyvalq)))) { free_req_data(&d); evhttp_send_error(req, 500, "Internal Server Error"); fprintf(stderr, "info.c:do_info(): couldn't malloc evkeyvalq\n"); return; } memset(d->query_args, 0, sizeof(struct evkeyvalq)); evhttp_parse_query(decoded_uri, d->query_args); free(decoded_uri); // find id field; build request const char* id = evhttp_find_header(d->query_args, "id"); if(!id || strlen(id)>20) { free_req_data(&d); evhttp_send_error(req, 404, "Not Found"); return; } struct evhttp_request* api_req; if(!(api_req = evhttp_request_new(got_api, d))) { free_req_data(&d); evhttp_send_error(req, 500, "Internal Server Error"); fprintf(stderr, "info.c:do_info(): couldn't alloc evhttp_request"); return; } char api_url[64]; sprintf(api_url, "/api/event/show/%s.xml", id); evhttp_add_header(api_req->output_headers, "host", "api.justin.tv"); printf("%s\n", api_url); // issue request if(!(d->conn = evhttp_connection_new("api.justin.tv", 80))) { evhttp_request_free(api_req); free_req_data(&d); evhttp_send_error(req, 500, "Internal Server Error"); fprintf(stderr, "info.c:do_info(): couldn't alloc evhttp_connection\n"); return; } if(evhttp_make_request(d->conn, api_req, EVHTTP_REQ_GET, api_url) == -1) { free_req_data(&d); evhttp_send_error(req, 500, "Internal Server Error"); fprintf(stderr, "info.c:do_info(): error making api request\n"); } }
/* ****************************************************************************** * dgadmin_rest_sync_dmc_agent -- *//** * * \brief This routine constructs new request and sends to REST server. * * \param [in] req_body_buf A pointer to request input body buffer. * \param [in] cmd_type The method of the HTTP request. * \param [in] uri The URI of the HTTP request. * * \retval 0 Success * \retval >0 Failure * *****************************************************************************/ int dgadmin_rest_sync_dmc_agent(char *req_body_buf, enum evhttp_cmd_type cmd_type, const char *uri) { struct evhttp_request *new_request = NULL; struct evbuffer *new_req_body = NULL; char ip_addr_str[]="127.0.0.1"; dgadmin_rest_sync_response_args_t args; int ret = DOVE_STATUS_OK; do { /* step 1 - construct a new request */ memset(&args, 0, sizeof(args)); new_request = evhttp_request_new(dgadmin_rest_sync_response_handler, &args); if (new_request == NULL) { log_notice(ServiceUtilLogLevel,"ERROR"); ret = DOVE_STATUS_NO_MEMORY; break; } if (req_body_buf != NULL) { new_req_body = evbuffer_new(); if (new_req_body == NULL) { evhttp_request_free(new_request); ret = DOVE_STATUS_NO_MEMORY; log_notice(ServiceUtilLogLevel,"ERROR"); break; } evbuffer_add(new_req_body, req_body_buf, strlen(req_body_buf)); evbuffer_add_buffer(evhttp_request_get_output_buffer(new_request), new_req_body); } /* step 2 - forward the new request to local REST handler */ log_notice(ServiceUtilLogLevel, "Routing %s REQ to local REST handler[%s]", uri, ip_addr_str); ret = dove_rest_request_and_syncprocess(ip_addr_str, g_dgwy_rest_port, cmd_type, uri, new_request, NULL, DGADMIN_REST_SYNC_CONNECT_TIMEOUT); if (new_req_body) { evbuffer_free(new_req_body); } if (ret) { ret = DOVE_STATUS_ERROR; log_notice(ServiceUtilLogLevel,"ERROR"); break; } if ((args.res_code != HTTP_OK) && (args.res_code != 201)) { ret = DOVE_STATUS_ERROR; log_notice(ServiceUtilLogLevel,"ERROR"); break; } } while (0); if (args.req_body_buf) { free(args.req_body_buf); } return ret; }
int rpc_call(struct rpc_context *context, const struct rpc_target *dest, const struct method_t *method, rpc_callback client_cb, void *data) { //TODO: check the protocol in dest->proto and do http // request only if dest->proto == HTTP int res = 1; struct evhttp_connection *evcon = NULL; struct evhttp_request *req = NULL; char *json_method = NULL; struct client_rpc_callback_with_data *ctx = NULL; //TODO: can be make http_connection as part of peer_t? evcon = evhttp_connection_base_new( context->base, NULL, dest->host, dest->port); if (!evcon) { goto cleanup; } ctx = (struct client_rpc_callback_with_data *)malloc( sizeof(struct client_rpc_callback_with_data)); if(!ctx) { goto cleanup; } ctx->cb = client_cb; ctx->data = data; ctx->evcon = evcon; req = evhttp_request_new(http_request_done, ctx); if (!req) { goto cleanup; } char uri[256]; snprintf(uri, sizeof(uri)-1, "http://%s:%d/rpc", dest->host, dest->port); json_method = serialize_method_call(method); if(!json_method) { goto cleanup; } struct evbuffer *output_buffer = evhttp_request_get_output_buffer(req); evbuffer_add(output_buffer, json_method, strlen(json_method)); char content_length[20]; snprintf(content_length, sizeof(content_length)-1, "%d", (int)strlen(json_method)); struct evkeyvalq *output_headers = evhttp_request_get_output_headers(req); evhttp_add_header(output_headers, "Host", dest->host); evhttp_add_header(output_headers, "Connection", "close"); evhttp_add_header(output_headers, "Content-Length", content_length); printf("Sending req %p with ctx = %p\n", req, ctx); int r = evhttp_make_request(evcon, req, EVHTTP_REQ_POST, uri); if(!r) { res = 0; } cleanup: if(json_method) free(json_method); if(res && ctx) free(ctx); if(res && evcon) evhttp_connection_free(evcon); if(res && req) evhttp_request_free(req); return res; }