static int upnpc_get_desc(upnpc_device_t * d, const char * url) { char hostname[MAXHOSTNAMELEN+1]; char hostname_port[MAXHOSTNAMELEN+1+6]; unsigned short port; char * path; unsigned int scope_id; struct evhttp_request * req; struct evkeyvalq * headers; /* if(d->root_desc_location == NULL) { return -1; } */ if(!parseURL(url/*d->root_desc_location*/, hostname, &port, &path, &scope_id)) { return -1; } if(port != 80) snprintf(hostname_port, sizeof(hostname_port), "%s:%hu", hostname, port); else strncpy(hostname_port, hostname, sizeof(hostname_port)); if(d->desc_conn == NULL) { d->desc_conn = evhttp_connection_base_new(d->parent->base, NULL, hostname, port); } #ifdef DEBUG evhttp_connection_set_closecb(d->desc_conn, upnpc_conn_close_cb, d); #endif /* DEBUG */ /*evhttp_connection_set_timeout(p->desc_conn, 600);*/ req = evhttp_request_new(upnpc_desc_received/*callback*/, d); headers = evhttp_request_get_output_headers(req); evhttp_add_header(headers, "Host", hostname_port); evhttp_add_header(headers, "Connection", "close"); /*evhttp_add_header(headers, "User-Agent", "***");*/ return evhttp_make_request(d->desc_conn, req, EVHTTP_REQ_GET, path); }
virtual void SetUp() { handler_.reset(new HTTPHandler(&DoMyFunc)); base_ = event_base_new(); dnsbase_ = evdns_base_new(base_, 1); connection_ = evhttp_connection_base_new(base_, dnsbase_, "0.0.0.0", 8080); request_ = evhttp_request_new(NULL, NULL); evhttp_make_request(connection_, request_, EVHTTP_REQ_GET, ""); }
int upnpc_event_subscribe(upnpc_device_t * p) { char hostname[MAXHOSTNAMELEN+1]; char hostname_port[MAXHOSTNAMELEN+1+6]; unsigned short port; char * path; unsigned int scope_id; struct evhttp_request * req; struct evkeyvalq * headers; char callback_header[7+15+1+5+9+2+1]; if(p->parent->http_server == NULL) { /* HTTP server to receive event notifications */ p->parent->http_server = evhttp_new(p->parent->base); if(p->parent->http_server == NULL) { debug_printf("evhttp_new() FAILED\n"); return -1; } evhttp_set_ext_method_cmp(p->parent->http_server, ext_methods_cb); evhttp_set_allowed_methods(p->parent->http_server, EVHTTP_REQ_NOTIFY); evhttp_set_cb(p->parent->http_server, "/evt_conn", upnpc_event_conn_req, p); if(evhttp_bind_socket(p->parent->http_server, p->parent->local_address, p->parent->local_port) < 0) { debug_printf("evhttp_bind_socket() FAILED\n"); return -1; } } /*if(!parseURL(p->event_cif_url, hostname, &port, &path, &scope_id)) {*/ if(!parseURL(p->event_conn_url, hostname, &port, &path, &scope_id)) { return -1; } if(port != 80) snprintf(hostname_port, sizeof(hostname_port), "%s:%hu", hostname, port); else strncpy(hostname_port, hostname, sizeof(hostname_port)); if(p->soap_conn == NULL) { p->soap_conn = evhttp_connection_base_new(p->parent->base, NULL, hostname, port); } evhttp_connection_set_ext_method_cmp(p->soap_conn, ext_methods_cb); req = evhttp_request_new(upnpc_subscribe_response, p); headers = evhttp_request_get_output_headers(req); /*buffer = evhttp_request_get_output_buffer(req);*/ evhttp_add_header(headers, "Host", hostname_port); /*evhttp_add_header(headers, "User-Agent", "***");*/ snprintf(callback_header, sizeof(callback_header), "<http://%s:%hu/evt_conn>", p->parent->local_address, p->parent->local_port); evhttp_add_header(headers, "Callback", callback_header); evhttp_add_header(headers, "NT", "upnp:event"); /*evhttp_add_header(headers, "NTS", "");*/ evhttp_add_header(headers, "Timeout", "3600"); /*evbuffer_add(buffer, body, body_len);*/ evhttp_make_request(p->soap_conn, req, EVHTTP_REQ_SUBSCRIBE, path); p->state |= UPNPC_DEVICE_SOAP_REQ; return 0; }
HttpClient_YPL::HttpClient_YPL() { m_base = event_base_new(); m_dnsbase = evdns_base_new(m_base, 1); //URL=http\://14.23.90.103/api/service m_host = "14.23.90.103"; m_port = 80; //m_host = "222.76.151.10"; //m_port = 51010; m_conn = evhttp_connection_base_new(m_base, m_dnsbase, m_host.c_str(), m_port); m_buffer = NULL; m_bufferSize = 0; }
// create S3HttpConnection object // establish HTTP connections to S3 gpointer s3http_connection_create (Application *app) { S3HttpConnection *con; int port; AppConf *conf; con = g_new0 (S3HttpConnection, 1); if (!con) { LOG_err (CON_LOG, "Failed to create S3HttpConnection !"); return NULL; } conf = application_get_conf (app); con->app = app; con->bucket_name = g_strdup (application_get_bucket_name (app)); con->is_acquired = FALSE; port = application_get_port (app); // if no port is specified, libevent returns -1 if (port == -1) { port = conf->http_port; } LOG_debug (CON_LOG, "Connecting to %s:%d", application_get_host (app), port ); // XXX: implement SSL con->evcon = evhttp_connection_base_new ( application_get_evbase (app), application_get_dnsbase (app), application_get_host (app), port ); if (!con->evcon) { LOG_err (CON_LOG, "Failed to create evhttp_connection !"); return NULL; } evhttp_connection_set_timeout (con->evcon, conf->timeout); evhttp_connection_set_retries (con->evcon, conf->retries); evhttp_connection_set_closecb (con->evcon, s3http_connection_on_close, con); return (gpointer)con; }
void URLConnection::setAddress( const std::string& address, int port ) /// /// Set the host address to be used for this request /// /// @param address /// Host Address /// /// @param port /// Host Port /// { if( mConnection ) { evhttp_connection_free( mConnection ); } mConnection = evhttp_connection_base_new(mEventBase, NULL, address.c_str(), port); }
void http_request::renew_request() { /* free connections & request */ if (m_cn) evhttp_connection_free(m_cn); #if !defined(_EVENT_NUMERIC_VERSION) || _EVENT_NUMERIC_VERSION < 0x02000000 m_cn = evhttp_connection_new(m_host.c_str(), m_port); evhttp_connection_set_base(m_cn, m_base); #else m_cn = evhttp_connection_base_new( m_base, NULL, m_host.c_str(), m_port); #endif m_req = evhttp_request_new(http_request::download_callback, this); evhttp_make_request(m_cn, m_req, m_type, m_query.c_str()); evhttp_add_header(m_req->output_headers, "Host", m_host.c_str()); }
void EvHttpSyncClient::open(const std::string& sHost, int32_t nPort) { m_sHost = sHost; m_nPort = nPort; m_bOwnEventBase = false; if (!m_evbase) { FX_EVENT_BASE_NEW(m_evbase); m_bOwnEventBase = true; } m_pConn = evhttp_connection_base_new(m_evbase, NULL, m_sHost.c_str(), (unsigned short)m_nPort); if (!m_pConn) { FIRTEX_THROW_AND_LOG(NetworkException, "evhttp_connection_base_new FAILED"); } setTimeout(DEFAULT_TIMEOUT); setRetries(DEFAULT_MAX_RETRY); evhttp_connection_set_closecb(m_pConn, onCloseConnection, this); m_lastState = ST_OK; }
static int upnpc_send_soap_request(upnpc_device_t * p, const char * url, const char * service, const char * method, const struct upnp_args * args, int arg_count) { char action[128]; char * body; const char fmt_soap[] = "<?xml version=\"1.0\"?>\r\n" "<" SOAPPREFIX ":Envelope " "xmlns:" SOAPPREFIX "=\"http://schemas.xmlsoap.org/soap/envelope/\" " SOAPPREFIX ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" "<" SOAPPREFIX ":Body>" "<" SERVICEPREFIX ":%s xmlns:" SERVICEPREFIX "=\"%s\">" "%s" "</" SERVICEPREFIX ":%s>" "</" SOAPPREFIX ":Body></" SOAPPREFIX ":Envelope>" "\r\n"; int body_len; char hostname[MAXHOSTNAMELEN+1]; char hostname_port[MAXHOSTNAMELEN+1+6]; unsigned short port; char * path; unsigned int scope_id; char * args_xml = NULL; struct evhttp_request * req; struct evkeyvalq * headers; struct evbuffer * buffer; if(p->state & UPNPC_DEVICE_SOAP_REQ) { debug_printf("%s: another SOAP request in progress\n", __func__); return UPNPC_ERR_REQ_IN_PROGRESS; } if(arg_count > 0) { int i; size_t l, n; for(i = 0, l = 0; i < arg_count; i++) { /* <ELT>VAL</ELT> */ l += strlen(args[i].elt) * 2 + strlen(args[i].val) + 5; } args_xml = malloc(++l); if(args_xml == NULL) { return -1; } for(i = 0, n = 0; i < arg_count && n < l; i++) { /* <ELT>VAL</ELT> */ n += snprintf(args_xml + n, l - n, "<%s>%s</%s>", args[i].elt, args[i].val, args[i].elt); } } body_len = snprintf(NULL, 0, fmt_soap, method, service, args_xml?args_xml:"", method); body = malloc(body_len + 1); if(body == NULL) { free(args_xml); return -1; } if(snprintf(body, body_len + 1, fmt_soap, method, service, args_xml?args_xml:"", method) != body_len) { debug_printf("%s: snprintf() returned strange value...\n", __func__); } free(args_xml); args_xml = NULL; if(!parseURL(url, hostname, &port, &path, &scope_id)) { free(body); return -1; } if(port != 80) snprintf(hostname_port, sizeof(hostname_port), "%s:%hu", hostname, port); else strncpy(hostname_port, hostname, sizeof(hostname_port)); snprintf(action, sizeof(action), "\"%s#%s\"", service, method); if(p->soap_conn == NULL) { p->soap_conn = evhttp_connection_base_new(p->parent->base, NULL, hostname, port); } req = evhttp_request_new(upnpc_soap_response, p); headers = evhttp_request_get_output_headers(req); buffer = evhttp_request_get_output_buffer(req); evhttp_add_header(headers, "Host", hostname_port); evhttp_add_header(headers, "SOAPAction", action); evhttp_add_header(headers, "Content-Type", "text/xml"); /*evhttp_add_header(headers, "User-Agent", "***");*/ /*evhttp_add_header(headers, "Cache-Control", "no-cache");*/ /*evhttp_add_header(headers, "Pragma", "no-cache");*/ evbuffer_add(buffer, body, body_len); evhttp_make_request(p->soap_conn, req, EVHTTP_REQ_POST, path); free(body); p->state |= UPNPC_DEVICE_SOAP_REQ; return 0; }
/* Thread: main (pairing) */ static int send_pairing_request(struct remote_info *ri, char *req_uri, int family) { struct evhttp_connection *evcon; struct evhttp_request *req; char *address; unsigned short port; int ret; switch (family) { case AF_INET: if (!ri->v4_address) return -1; address = ri->v4_address; port = ri->v4_port; break; case AF_INET6: if (!ri->v6_address) return -1; address = ri->v6_address; port = ri->v6_port; break; default: return -1; } evcon = evhttp_connection_base_new(evbase_main, NULL, address, port); if (!evcon) { DPRINTF(E_LOG, L_REMOTE, "Could not create connection for pairing with %s\n", ri->pi.name); return -1; } req = evhttp_request_new(pairing_request_cb, ri); if (!req) { DPRINTF(E_WARN, L_REMOTE, "Could not create HTTP request for pairing\n"); goto request_fail; } ret = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, req_uri); if (ret < 0) { DPRINTF(E_WARN, L_REMOTE, "Could not make pairing request\n"); goto request_fail; } DPRINTF(E_DBG, L_REMOTE, "Pairing requested to %s\n", req_uri); ri->evcon = evcon; return 0; request_fail: evhttp_connection_free(evcon); return -1; }
int http_client_request(struct http_client_ctx *ctx) { struct evhttp_connection *evcon; struct evhttp_request *req; struct evkeyvalq *headers; char hostname[PATH_MAX]; char path[PATH_MAX]; char s[PATH_MAX]; int port; int ret; ctx->ret = -1; av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, path, sizeof(path), ctx->url); if (strlen(hostname) == 0) { DPRINTF(E_LOG, L_HTTP, "Error extracting hostname from URL: %s\n", ctx->url); return ctx->ret; } if (port <= 0) snprintf(s, PATH_MAX, "%s", hostname); else snprintf(s, PATH_MAX, "%s:%d", hostname, port); if (port <= 0) port = 80; if (strlen(path) == 0) { path[0] = '/'; path[1] = '\0'; } ctx->evbase = event_base_new(); if (!ctx->evbase) { DPRINTF(E_LOG, L_HTTP, "Could not create or find http client event base\n"); return ctx->ret; } evcon = evhttp_connection_base_new(ctx->evbase, NULL, hostname, (unsigned short)port); if (!evcon) { DPRINTF(E_LOG, L_HTTP, "Could not create connection to %s\n", hostname); event_base_free(ctx->evbase); return ctx->ret; } evhttp_connection_set_timeout(evcon, HTTP_CLIENT_TIMEOUT); /* Set up request */ req = evhttp_request_new(request_cb, ctx); if (!req) { DPRINTF(E_LOG, L_HTTP, "Could not create request to %s\n", hostname); evhttp_connection_free(evcon); event_base_free(ctx->evbase); return ctx->ret; } #ifndef HAVE_LIBEVENT2_OLD if (ctx->headers_only) evhttp_request_set_header_cb(req, request_header_cb); #endif headers = evhttp_request_get_output_headers(req); evhttp_add_header(headers, "Host", s); evhttp_add_header(headers, "Content-Length", "0"); evhttp_add_header(headers, "User-Agent", "forked-daapd/" VERSION); evhttp_add_header(headers, "Icy-MetaData", "1"); /* Make request */ DPRINTF(E_INFO, L_HTTP, "Making request for http://%s%s\n", s, path); ret = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, path); if (ret < 0) { DPRINTF(E_LOG, L_HTTP, "Error making request for http://%s%s\n", s, path); evhttp_connection_free(evcon); event_base_free(ctx->evbase); return ctx->ret; } event_base_dispatch(ctx->evbase); evhttp_connection_free(evcon); event_base_free(ctx->evbase); return ctx->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; }