static void perform_ssl_connection (void) { struct evhttp_connection *con; struct evhttp_request *req; struct bufferevent *bev; SSL_CTX *sctx; SSL *ssl; sctx = SSL_CTX_new (SSLv23_client_method ()); assert (sctx); SSL_CTX_set_options (sctx, SSL_OP_NO_TLSv1_2); //SSL_CTX_set_options (sctx, SSL_OP_ALL); SSL_CTX_set_timeout (sctx, 3000); SSL_CTX_set_verify (sctx, SSL_VERIFY_PEER, SSLX_CTX_verify_callback); SSL_CTX_set_default_verify_paths (sctx); SSL_CTX_set_cipher_list (sctx, "RC4-MD5"); ssl = SSL_new (sctx); assert (ssl); bev = bufferevent_openssl_socket_new (evbase, -1, ssl, BUFFEREVENT_SSL_CONNECTING , BEV_OPT_CLOSE_ON_FREE); //bev = bufferevent_socket_new (evbase, -1, BEV_OPT_CLOSE_ON_FREE); assert (bev); con = evhttp_connection_base_bufferevent_new (evbase, dnsbase, bev, HOST, PORT); evhttp_connection_set_closecb (con, on_connection_close, NULL); evhttp_connection_set_timeout (con, 10); req = evhttp_request_new (on_response_cb, NULL); evhttp_add_header (req->output_headers, "Host", HOST); // evhttp_add_header (req->output_headers, "Connection", "Keep-Alive"); evhttp_make_request (con, req, EVHTTP_REQ_GET, "/index.html"); }
void LibEventServer::onRequest(struct evhttp_request *request) { // If we are in the process of crashing, we want to reject incoming work. // This will prompt the load balancers to choose another server. Using // shutdown rather than close has the advantage that it makes fewer changes // to the process (eg, it doesn't close the FD so if the FD number were // corrupted it would be mostly harmless). // // Setting accept sock to -1 will leak FDs. But we're crashing anyways. if (IsCrashing) { if (m_accept_sock != -1) { shutdown(m_accept_sock, SHUT_FBLISTEN); m_accept_sock = -1; } if (m_accept_sock_ssl != -1) { shutdown(m_accept_sock_ssl, SHUT_FBLISTEN); m_accept_sock_ssl = -1; } return; } if (RuntimeOption::EnableKeepAlive && RuntimeOption::ConnectionTimeoutSeconds > 0) { // before processing request, set the connection timeout // it's just writing a variable in libevent evhttp_connection_set_timeout(request->evcon, RuntimeOption::ConnectionTimeoutSeconds); } if (getStatus() == RunStatus::RUNNING) { RequestPriority priority = getRequestPriority(request); m_dispatcher.enqueue(LibEventJobPtr(new LibEventJob(request)), priority); } else { Logger::Error("throwing away one new request while shutting down"); } }
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 HttpClient_YPL::post() { //char *url = "/portal/sss"; char *url = "/api/service"; struct evhttp_request *req = evhttp_request_new(onRequestDone, this); evhttp_add_header(req->output_headers, "Host", m_host.c_str()); evhttp_make_request(m_conn, req, EVHTTP_REQ_POST, url); evhttp_connection_set_timeout(req->evcon, 3); evhttp_connection_set_closecb(req->evcon, onClose, this); //evbuffer_add(req->output_buffer, "123", 3); event_base_dispatch(m_base); }
void LibEventServer::onRequest(struct evhttp_request *request) { if (RuntimeOption::EnableKeepAlive && RuntimeOption::ConnectionTimeoutSeconds > 0) { // before processing request, set the connection timeout // it's just writing a variable in libevent evhttp_connection_set_timeout(request->evcon, RuntimeOption::ConnectionTimeoutSeconds); } if (getStatus() == RunStatus::RUNNING) { m_dispatcher.enqueue(LibEventJobPtr(new LibEventJob(request))); } else { Logger::Error("throwing away one new request while shutting down"); } }
// 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; }
static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, const std::vector<std::string>& args) { std::string host; // In preference order, we choose the following for the port: // 1. -rpcport // 2. port in -rpcconnect (ie following : in ipv4 or ]: in ipv6) // 3. default port for chain int port = BaseParams().RPCPort(); SplitHostPort(gArgs.GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host); port = gArgs.GetArg("-rpcport", port); // Obtain event base raii_event_base base = obtain_event_base(); // Synchronously look up hostname raii_evhttp_connection evcon = obtain_evhttp_connection_base(base.get(), host, port); evhttp_connection_set_timeout(evcon.get(), gArgs.GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT)); HTTPReply response; raii_evhttp_request req = obtain_evhttp_request(http_request_done, (void*)&response); if (req == nullptr) throw std::runtime_error("create http request failed"); #if LIBEVENT_VERSION_NUMBER >= 0x02010300 evhttp_request_set_error_cb(req.get(), http_error_cb); #endif // Get credentials std::string strRPCUserColonPass; bool failedToGetAuthCookie = false; if (gArgs.GetArg("-rpcpassword", "") == "") { // Try fall back to cookie-based authentication if no password is provided if (!GetAuthCookie(&strRPCUserColonPass)) { failedToGetAuthCookie = true; } } else { strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", ""); } struct evkeyvalq* output_headers = evhttp_request_get_output_headers(req.get()); assert(output_headers); evhttp_add_header(output_headers, "Host", host.c_str()); evhttp_add_header(output_headers, "Connection", "close"); evhttp_add_header(output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str()); // Attach request data std::string strRequest = rh->PrepareRequest(strMethod, args).write() + "\n"; struct evbuffer* output_buffer = evhttp_request_get_output_buffer(req.get()); assert(output_buffer); evbuffer_add(output_buffer, strRequest.data(), strRequest.size()); // check if we should use a special wallet endpoint std::string endpoint = "/"; if (!gArgs.GetArgs("-rpcwallet").empty()) { std::string walletName = gArgs.GetArg("-rpcwallet", ""); char *encodedURI = evhttp_uriencode(walletName.c_str(), walletName.size(), false); if (encodedURI) { endpoint = "/wallet/"+ std::string(encodedURI); free(encodedURI); } else { throw CConnectionFailed("uri-encode failed"); } } int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, endpoint.c_str()); req.release(); // ownership moved to evcon in above call if (r != 0) { throw CConnectionFailed("send http request failed"); } event_base_dispatch(base.get()); if (response.status == 0) { std::string responseErrorMessage; if (response.error != -1) { responseErrorMessage = strprintf(" (error code %d - \"%s\")", response.error, http_errorstring(response.error)); } throw CConnectionFailed(strprintf("Could not connect to the server %s:%d%s\n\nMake sure the bitcoind server is running and that you are connecting to the correct RPC port.", host, port, responseErrorMessage)); } else if (response.status == HTTP_UNAUTHORIZED) { if (failedToGetAuthCookie) { throw std::runtime_error(strprintf( "Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (%s)", GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str())); } else { throw std::runtime_error("Authorization failed: Incorrect rpcuser or rpcpassword"); } } else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR) throw std::runtime_error(strprintf("server returned HTTP error %d", response.status)); else if (response.body.empty()) throw std::runtime_error("no response from server"); // Parse reply UniValue valReply(UniValue::VSTR); if (!valReply.read(response.body)) throw std::runtime_error("couldn't parse reply from server"); const UniValue reply = rh->ProcessReply(valReply); if (reply.empty()) throw std::runtime_error("expected reply to have result, error and id properties"); return reply; }
int main(int argc, char **argv) { int r; struct evhttp_uri *http_uri = NULL; const char *url = NULL, *data_file = NULL; const char *crt = "/etc/ssl/certs/ca-certificates.crt"; const char *scheme, *host, *path, *query; char uri[256]; int port; int retries = 0; int timeout = -1; SSL_CTX *ssl_ctx = NULL; SSL *ssl = NULL; struct bufferevent *bev; struct evhttp_connection *evcon = NULL; struct evhttp_request *req; struct evkeyvalq *output_headers; struct evbuffer *output_buffer; int i; int ret = 0; enum { HTTP, HTTPS } type = HTTP; for (i = 1; i < argc; i++) { if (!strcmp("-url", argv[i])) { if (i < argc - 1) { url = argv[i + 1]; } else { syntax(); goto error; } } else if (!strcmp("-crt", argv[i])) { if (i < argc - 1) { crt = argv[i + 1]; } else { syntax(); goto error; } } else if (!strcmp("-ignore-cert", argv[i])) { ignore_cert = 1; } else if (!strcmp("-data", argv[i])) { if (i < argc - 1) { data_file = argv[i + 1]; } else { syntax(); goto error; } } else if (!strcmp("-retries", argv[i])) { if (i < argc - 1) { retries = atoi(argv[i + 1]); } else { syntax(); goto error; } } else if (!strcmp("-timeout", argv[i])) { if (i < argc - 1) { timeout = atoi(argv[i + 1]); } else { syntax(); goto error; } } else if (!strcmp("-help", argv[i])) { syntax(); goto error; } } if (!url) { syntax(); goto error; } #ifdef _WIN32 { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { printf("WSAStartup failed with error: %d\n", err); goto error; } } #endif // _WIN32 http_uri = evhttp_uri_parse(url); if (http_uri == NULL) { err("malformed url"); goto error; } scheme = evhttp_uri_get_scheme(http_uri); if (scheme == NULL || (strcasecmp(scheme, "https") != 0 && strcasecmp(scheme, "http") != 0)) { err("url must be http or https"); goto error; } host = evhttp_uri_get_host(http_uri); if (host == NULL) { err("url must have a host"); goto error; } port = evhttp_uri_get_port(http_uri); if (port == -1) { port = (strcasecmp(scheme, "http") == 0) ? 80 : 443; } path = evhttp_uri_get_path(http_uri); if (strlen(path) == 0) { path = "/"; } query = evhttp_uri_get_query(http_uri); if (query == NULL) { snprintf(uri, sizeof(uri) - 1, "%s", path); } else { snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query); } uri[sizeof(uri) - 1] = '\0'; #if OPENSSL_VERSION_NUMBER < 0x10100000L // Initialize OpenSSL SSL_library_init(); ERR_load_crypto_strings(); SSL_load_error_strings(); OpenSSL_add_all_algorithms(); #endif /* This isn't strictly necessary... OpenSSL performs RAND_poll * automatically on first use of random number generator. */ r = RAND_poll(); if (r == 0) { err_openssl("RAND_poll"); goto error; } /* Create a new OpenSSL context */ ssl_ctx = SSL_CTX_new(SSLv23_method()); if (!ssl_ctx) { err_openssl("SSL_CTX_new"); goto error; } #ifndef _WIN32 /* TODO: Add certificate loading on Windows as well */ /* Attempt to use the system's trusted root certificates. * (This path is only valid for Debian-based systems.) */ if (1 != SSL_CTX_load_verify_locations(ssl_ctx, crt, NULL)) { err_openssl("SSL_CTX_load_verify_locations"); goto error; } /* Ask OpenSSL to verify the server certificate. Note that this * does NOT include verifying that the hostname is correct. * So, by itself, this means anyone with any legitimate * CA-issued certificate for any website, can impersonate any * other website in the world. This is not good. See "The * Most Dangerous Code in the World" article at * https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html */ SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL); /* This is how we solve the problem mentioned in the previous * comment. We "wrap" OpenSSL's validation routine in our * own routine, which also validates the hostname by calling * the code provided by iSECPartners. Note that even though * the "Everything You've Always Wanted to Know About * Certificate Validation With OpenSSL (But Were Afraid to * Ask)" paper from iSECPartners says very explicitly not to * call SSL_CTX_set_cert_verify_callback (at the bottom of * page 2), what we're doing here is safe because our * cert_verify_callback() calls X509_verify_cert(), which is * OpenSSL's built-in routine which would have been called if * we hadn't set the callback. Therefore, we're just * "wrapping" OpenSSL's routine, not replacing it. */ SSL_CTX_set_cert_verify_callback(ssl_ctx, cert_verify_callback, (void *) host); #else // _WIN32 (void)crt; #endif // _WIN32 // Create event base base = event_base_new(); if (!base) { perror("event_base_new()"); goto error; } // Create OpenSSL bufferevent and stack evhttp on top of it ssl = SSL_new(ssl_ctx); if (ssl == NULL) { err_openssl("SSL_new()"); goto error; } #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME // Set hostname for SNI extension SSL_set_tlsext_host_name(ssl, host); #endif if (strcasecmp(scheme, "http") == 0) { bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); } else { type = HTTPS; bev = bufferevent_openssl_socket_new(base, -1, ssl, BUFFEREVENT_SSL_CONNECTING, BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS); } if (bev == NULL) { fprintf(stderr, "bufferevent_openssl_socket_new() failed\n"); goto error; } bufferevent_openssl_set_allow_dirty_shutdown(bev, 1); // For simplicity, we let DNS resolution block. Everything else should be // asynchronous though. evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev, host, port); if (evcon == NULL) { fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n"); goto error; } if (retries > 0) { evhttp_connection_set_retries(evcon, retries); } if (timeout >= 0) { evhttp_connection_set_timeout(evcon, timeout); } // Fire off the request req = evhttp_request_new(http_request_done, bev); if (req == NULL) { fprintf(stderr, "evhttp_request_new() failed\n"); goto error; } output_headers = evhttp_request_get_output_headers(req); evhttp_add_header(output_headers, "Host", host); evhttp_add_header(output_headers, "Connection", "close"); if (data_file) { /* NOTE: In production code, you'd probably want to use * evbuffer_add_file() or evbuffer_add_file_segment(), to * avoid needless copying. */ FILE * f = fopen(data_file, "rb"); char buf[1024]; size_t s; size_t bytes = 0; if (!f) { syntax(); goto error; } output_buffer = evhttp_request_get_output_buffer(req); while ((s = fread(buf, 1, sizeof(buf), f)) > 0) { evbuffer_add(output_buffer, buf, s); bytes += s; } evutil_snprintf(buf, sizeof(buf)-1, "%lu", (unsigned long)bytes); evhttp_add_header(output_headers, "Content-Length", buf); fclose(f); } r = evhttp_make_request(evcon, req, data_file ? EVHTTP_REQ_POST : EVHTTP_REQ_GET, uri); if (r != 0) { fprintf(stderr, "evhttp_make_request() failed\n"); goto error; } event_base_dispatch(base); goto cleanup; error: ret = 1; cleanup: if (evcon) evhttp_connection_free(evcon); if (http_uri) evhttp_uri_free(http_uri); event_base_free(base); if (ssl_ctx) SSL_CTX_free(ssl_ctx); if (type == HTTP && ssl) SSL_free(ssl); #if OPENSSL_VERSION_NUMBER < 0x10100000L EVP_cleanup(); ERR_free_strings(); #ifdef EVENT__HAVE_ERR_REMOVE_THREAD_STATE ERR_remove_thread_state(NULL); #else ERR_remove_state(0); #endif CRYPTO_cleanup_all_ex_data(); sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); #endif /*OPENSSL_VERSION_NUMBER < 0x10100000L */ #ifdef _WIN32 WSACleanup(); #endif return ret; }
IoObject *IoEvConnection_setTimeout_(IoEvConnection *self, IoObject *locals, IoMessage *m) { int timeoutInSeconds = IoMessage_locals_intArgAt_(m, locals, 0); evhttp_connection_set_timeout(CONN(self), timeoutInSeconds); return self; }
void EvHttpSyncClient::setTimeout(int32_t nTimeoutInSeconds) { evhttp_connection_set_timeout(m_pConn, (int)nTimeoutInSeconds); }
UniValue CallRPC(const std::string& strMethod, const UniValue& params) { std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT); int port = GetArg("-rpcport", BaseParams().RPCPort()); // Obtain event base raii_event_base base = obtain_event_base(); // Synchronously look up hostname raii_evhttp_connection evcon = obtain_evhttp_connection_base(base.get(), host, port); evhttp_connection_set_timeout(evcon.get(), GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT)); HTTPReply response; raii_evhttp_request req = obtain_evhttp_request(http_request_done, (void*)&response); if (req == NULL) throw std::runtime_error("create http request failed"); #if LIBEVENT_VERSION_NUMBER >= 0x02010300 evhttp_request_set_error_cb(req.get(), http_error_cb); #endif // Get credentials std::string strRPCUserColonPass; if (GetArg("-rpcpassword", "") == "") { // Try fall back to cookie-based authentication if no password is provided if (!GetAuthCookie(&strRPCUserColonPass)) { throw std::runtime_error(strprintf( _("Could not locate RPC credentials. No authentication cookie could be found, and no rpcpassword is set in the configuration file (%s)"), GetConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str())); } } else { strRPCUserColonPass = GetArg("-rpcuser", "") + ":" + GetArg("-rpcpassword", ""); } struct evkeyvalq* output_headers = evhttp_request_get_output_headers(req.get()); assert(output_headers); evhttp_add_header(output_headers, "Host", host.c_str()); evhttp_add_header(output_headers, "Connection", "close"); evhttp_add_header(output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str()); // Attach request data std::string strRequest = JSONRPCRequestObj(strMethod, params, 1).write() + "\n"; struct evbuffer* output_buffer = evhttp_request_get_output_buffer(req.get()); assert(output_buffer); evbuffer_add(output_buffer, strRequest.data(), strRequest.size()); int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, "/"); req.release(); // ownership moved to evcon in above call if (r != 0) { throw CConnectionFailed("send http request failed"); } event_base_dispatch(base.get()); if (response.status == 0) throw CConnectionFailed(strprintf("couldn't connect to server: %s (code %d)\n(make sure server is running and you are connecting to the correct RPC port)", http_errorstring(response.error), response.error)); else if (response.status == HTTP_UNAUTHORIZED) throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR) throw std::runtime_error(strprintf("server returned HTTP error %d", response.status)); else if (response.body.empty()) throw std::runtime_error("no response from server"); // Parse reply UniValue valReply(UniValue::VSTR); if (!valReply.read(response.body)) throw std::runtime_error("couldn't parse reply from server"); const UniValue& reply = valReply.get_obj(); if (reply.empty()) throw std::runtime_error("expected reply to have result, error and id properties"); return reply; }
int scan_metadata_icy(char *url, struct media_file_info *mfi) { struct evhttp_connection *evcon; struct evhttp_request *req; struct icy_ctx *ctx; int ret; int i; status = ICY_INIT; /* We can set this straight away */ mfi->url = strdup(url); ctx = (struct icy_ctx *)malloc(sizeof(struct icy_ctx)); if (!ctx) { DPRINTF(E_LOG, L_SCAN, "Out of memory for ICY metadata context\n"); goto no_icy; } memset(ctx, 0, sizeof(struct icy_ctx)); ctx->url = evhttp_encode_uri(url); /* TODO https */ av_url_split(NULL, 0, NULL, 0, ctx->hostname, sizeof(ctx->hostname), &ctx->port, ctx->path, sizeof(ctx->path), ctx->url); if (ctx->port < 0) ctx->port = 80; /* Resolve IP address */ ret = resolve_address(ctx->hostname, ctx->address, sizeof(ctx->address)); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not find IP address of %s\n", ctx->hostname); return -1; } DPRINTF(E_DBG, L_SCAN, "URL %s converted to hostname %s, port %d, path %s, IP %s\n", ctx->url, ctx->hostname, ctx->port, ctx->path, ctx->address); /* Set up connection */ evcon = evhttp_connection_new(ctx->address, (unsigned short)ctx->port); if (!evcon) { DPRINTF(E_LOG, L_SCAN, "Could not create connection to %s\n", ctx->hostname); goto no_icy; } evhttp_connection_set_base(evcon, evbase_main); evhttp_connection_set_timeout(evcon, ICY_TIMEOUT); /* Set up request */ req = evhttp_request_new(scan_icy_request_cb, mfi); if (!req) { DPRINTF(E_LOG, L_SCAN, "Could not create request to %s\n", ctx->hostname); evhttp_connection_free(evcon); goto no_icy; } req->header_cb = scan_icy_header_cb; evhttp_add_header(req->output_headers, "Host", ctx->hostname); evhttp_add_header(req->output_headers, "Icy-MetaData", "1"); /* Make request */ status = ICY_WAITING; ret = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, ctx->path); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Could not make request to %s\n", ctx->hostname); status = ICY_DONE; evhttp_connection_free(evcon); goto no_icy; } DPRINTF(E_INFO, L_SCAN, "Making request to %s asking for ICY (Shoutcast) metadata\n", url); /* Can't count on server support for ICY metadata, so * while waiting for a reply make a parallel call to scan_metadata_ffmpeg. * This call will also determine final return value. */ no_icy: ret = scan_metadata_ffmpeg(url, mfi); /* Wait till ICY request completes or we reach timeout */ for (i = 0; (status == ICY_WAITING) && (i <= ICY_TIMEOUT); i++) sleep(1); free_icy(ctx); DPRINTF(E_DBG, L_SCAN, "scan_metadata_icy exiting with status %d after waiting %d sec\n", status, i); return ret; }
// Request dispatcher static void handle_request(struct evhttp_request *request, void *userdata) { evhttp_connection_set_timeout(request->evcon, 1); evhttp_add_header(evhttp_request_get_output_headers(request), "Server", "[email protected]/spotify-api-server"); // Check request method int http_method = evhttp_request_get_command(request); switch (http_method) { case EVHTTP_REQ_GET: case EVHTTP_REQ_PUT: case EVHTTP_REQ_POST: break; default: evhttp_send_error(request, HTTP_NOTIMPL, "Not Implemented"); return; } struct state *state = userdata; sp_session *session = state->session; char *uri = evhttp_decode_uri(evhttp_request_get_uri(request)); char *entity = strtok(uri, "/"); if (entity == NULL) { evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request"); free(uri); return; } // Handle requests to /user/<user_name>/inbox if (strncmp(entity, "user", 4) == 0) { char *username = strtok(NULL, "/"); if (username == NULL) { evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request"); free(uri); return; } char *action = strtok(NULL, "/"); handle_user_request(request, action, username, session); free(uri); return; } // Handle requests to /playlist/<playlist_uri>/<action> if (strncmp(entity, "playlist", 8) != 0) { evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request"); free(uri); return; } char *playlist_uri = strtok(NULL, "/"); if (playlist_uri == NULL) { switch (http_method) { case EVHTTP_REQ_PUT: case EVHTTP_REQ_POST: put_playlist(NULL, request, session); break; default: send_error(request, HTTP_BADREQUEST, "Bad Request"); break; } free(uri); return; } sp_link *playlist_link = sp_link_create_from_string(playlist_uri); if (playlist_link == NULL) { send_error(request, HTTP_NOTFOUND, "Playlist link not found"); free(uri); return; } if (sp_link_type(playlist_link) != SP_LINKTYPE_PLAYLIST) { sp_link_release(playlist_link); send_error(request, HTTP_BADREQUEST, "Not a playlist link"); free(uri); return; } sp_playlist *playlist = sp_playlist_create(session, playlist_link); sp_link_release(playlist_link); if (playlist == NULL) { send_error(request, HTTP_NOTFOUND, "Playlist not found"); free(uri); return; } sp_playlist_add_ref(playlist); // Dispatch request char *action = strtok(NULL, "/"); // Default request handler handle_playlist_fn request_callback = ¬_implemented; void *callback_userdata = session; switch (http_method) { case EVHTTP_REQ_GET: { if (action == NULL) { // Send entire playlist request_callback = &get_playlist; } else if (strncmp(action, "collaborative", 13) == 0) { request_callback = &get_playlist_collaborative; } else if (strncmp(action, "subscribers", 11) == 0) { request_callback = &get_playlist_subscribers; } } break; case EVHTTP_REQ_PUT: case EVHTTP_REQ_POST: { if (strncmp(action, "add", 3) == 0) { request_callback = &put_playlist_add_tracks; } else if (strncmp(action, "remove", 6) == 0) { request_callback = &put_playlist_remove_tracks; } else if (strncmp(action, "patch", 5) == 0) { callback_userdata = state; request_callback = &put_playlist_patch; } } break; } if (sp_playlist_is_loaded(playlist)) { request_callback(playlist, request, callback_userdata); } else { // Wait for playlist to load register_playlist_callbacks(playlist, request, request_callback, &playlist_state_changed_callbacks, callback_userdata); } free(uri); }
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; }