janus_turnrest_response *janus_turnrest_request(void) { janus_mutex_lock(&api_mutex); if(api_server == NULL) { janus_mutex_unlock(&api_mutex); return NULL; } /* Prepare the request URI */ char query_string[512]; g_snprintf(query_string, 512, "service=turn"); if(api_key != NULL) { char buffer[256]; g_snprintf(buffer, 256, "&api=%s", api_key); g_strlcat(query_string, buffer, 512); } char request_uri[1024]; g_snprintf(request_uri, 1024, "%s?%s", api_server, query_string); JANUS_LOG(LOG_VERB, "Sending request: %s\n", request_uri); janus_mutex_unlock(&api_mutex); /* Prepare the libcurl context */ CURLcode res; CURL *curl = curl_easy_init(); if(curl == NULL) { JANUS_LOG(LOG_ERR, "libcurl error\n"); return NULL; } curl_easy_setopt(curl, CURLOPT_URL, request_uri); curl_easy_setopt(curl, CURLOPT_POST, 1); /* FIXME Some servers don't like a POST with no data */ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); /* FIXME Max 10 seconds */ /* For getting data, we use an helper struct and the libcurl callback */ janus_turnrest_buffer data; data.buffer = malloc(1); data.size = 0; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, janus_turnrest_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data); curl_easy_setopt(curl, CURLOPT_USERAGENT, "Janus/1.0"); /* Send the request */ res = curl_easy_perform(curl); if(res != CURLE_OK) { JANUS_LOG(LOG_ERR, "Couldn't send the request: %s\n", curl_easy_strerror(res)); free(data.buffer); curl_easy_cleanup(curl); return NULL; } /* Cleanup the libcurl context */ curl_easy_cleanup(curl); /* Process the response */ JANUS_LOG(LOG_VERB, "Got %zu bytes from the TURN REST API server\n", data.size); JANUS_LOG(LOG_VERB, "%s\n", data.buffer); json_error_t error; json_t *root = json_loads(data.buffer, 0, &error); if(!root) { JANUS_LOG(LOG_ERR, "Couldn't parse response: error on line %d: %s", error.line, error.text); free(data.buffer); return NULL; } free(data.buffer); json_t *username = json_object_get(root, "username"); if(!username) { JANUS_LOG(LOG_ERR, "Invalid response: missing username\n"); return NULL; } if(!json_is_string(username)) { JANUS_LOG(LOG_ERR, "Invalid response: username should be a string\n"); return NULL; } json_t *password = json_object_get(root, "password"); if(!password) { JANUS_LOG(LOG_ERR, "Invalid response: missing password\n"); return NULL; } if(!json_is_string(password)) { JANUS_LOG(LOG_ERR, "Invalid response: password should be a string\n"); return NULL; } json_t *ttl = json_object_get(root, "ttl"); if(ttl && (!json_is_integer(ttl) || json_integer_value(ttl) < 0)) { JANUS_LOG(LOG_ERR, "Invalid response: ttl should be a positive integer\n"); return NULL; } json_t *uris = json_object_get(root, "uris"); if(!uris) { JANUS_LOG(LOG_ERR, "Invalid response: missing uris\n"); return NULL; } if(!json_is_array(uris) || json_array_size(uris) == 0) { JANUS_LOG(LOG_ERR, "Invalid response: uris should be a non-empty array\n"); return NULL; } /* Turn the response into a janus_turnrest_response object we can use */ janus_turnrest_response *response = calloc(1, sizeof(janus_turnrest_response)); response->username = g_strdup(json_string_value(username)); response->password = g_strdup(json_string_value(password)); response->ttl = ttl ? json_integer_value(ttl) : 0; response->servers = NULL; size_t i = 0; for(i=0; i<json_array_size(uris); i++) { json_t *uri = json_array_get(uris, i); if(uri == NULL || !json_is_string(uri)) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI (not a string)...\n"); continue; } const char *turn_uri = json_string_value(uri); if(strstr(turn_uri, "turn:") != turn_uri && strstr(turn_uri, "turns:") != turn_uri) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI '%s' (not a TURN URI)...\n", turn_uri); continue; } janus_turnrest_instance *instance = calloc(1, sizeof(janus_turnrest_instance)); instance->transport = NICE_RELAY_TYPE_TURN_UDP; if(strstr(turn_uri, "turns:") == turn_uri) instance->transport = NICE_RELAY_TYPE_TURN_TLS; else if(strstr(turn_uri, "transport=tcp") == turn_uri) instance->transport = NICE_RELAY_TYPE_TURN_TCP; gchar **parts = NULL; if(strstr(turn_uri, "?") != NULL) { parts = g_strsplit(turn_uri, "?", -1); turn_uri = parts[0]; } gchar **uri_parts = g_strsplit(turn_uri, ":", -1); /* Resolve the TURN URI address */ struct hostent *he = gethostbyname(uri_parts[1]); if(he == NULL) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI '%s' (could not resolve the address)...\n", uri_parts[1]); g_strfreev(uri_parts); continue; } struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; if(addr_list[0] == NULL) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI '%s' (could not resolve the address)...\n", uri_parts[1]); g_strfreev(uri_parts); continue; } instance->server = g_strdup(inet_ntoa(*addr_list[0])); if(uri_parts[2] == NULL) { /* No port? USe 3478 by default */ instance->port = 3478; } else { instance->port = atoi(uri_parts[2]); } g_strfreev(uri_parts); g_strfreev(parts); /* Add the server to the list */ response->servers = g_list_append(response->servers, instance); } if(response->servers == NULL) { JANUS_LOG(LOG_ERR, "Couldn't find any valid TURN URI in the response...\n"); janus_turnrest_response_destroy(response); return NULL; } /* Done */ return response; }
CURLcode check(char *response, size_t length, const char *username, const char* password, const char* proxy, int stype) { CURL *curl = curl_easy_init(); if(!curl) { fdo_log(GENLOG, "cURL Init Error: (?!)"); return CURLE_FAILED_INIT; //?!?!?!?! } struct memstruct CurlStruct; CurlStruct.memory = malloc(1); *CurlStruct.memory = 0; CurlStruct.size = 0; //Prepare custom headers. char *userenc = curl_easy_escape(curl, username, 0); if(!userenc) { free(CurlStruct.memory); return CURLE_FAILED_INIT; } char *passenc = curl_easy_escape(curl, password, 0); if(!passenc) { free(CurlStruct.memory); curl_free(passenc); passenc = NULL; return CURLE_FAILED_INIT; } size_t plen = snprintf(NULL, 0, "rem=on&username=%s&password=%s&submit=Log+In&mod=www&ssl=1&dest=account_settings.ws", userenc, passenc); plen += 1; char *post = malloc(plen); if(!post) { free(CurlStruct.memory); curl_free(userenc); userenc = NULL; curl_free(passenc); passenc = NULL; return CURLE_FAILED_INIT; } snprintf(post, plen, "rem=on&username=%s&password=%s&submit=Log+In&mod=www&ssl=1&dest=account_settings.ws", userenc, passenc); curl_free(userenc); userenc = NULL; curl_free(passenc); passenc = NULL; curl_easy_setopt(curl, CURLOPT_URL, "https://secure.runescape.com/m=weblogin/login.ws"); //This may change in the future. curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0"); //Possibly change this in the future. They block useragents when they're old(Probably my fault) curl_easy_setopt(curl, CURLOPT_REFERER, "https://secure.runescape.com/m=weblogin/loginform.ws?mod=www&ssl=1&reauth=1&dest=account_settings.ws"); //Likewise, may be needed to change. curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); //When followlocation takes place. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); curl_easy_setopt(curl, CURLOPT_HEADER, 0); curl_easy_setopt(curl, CURLOPT_ENCODING, "identity"); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); curl_easy_setopt(curl, CURLOPT_PROXYTYPE, stype); curl_easy_setopt(curl, CURLOPT_PROXY, proxy); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, StoreCurl); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&CurlStruct); CURLcode res = curl_easy_perform(curl); if(res != CURLE_OK) { fdo_log(DBGLOG, "cURL Error: %s, Proxy: %s.(type: %d)", curl_easy_strerror(res), proxy, stype); } else { if(CurlStruct.memory != NULL) { strncpy(response, CurlStruct.memory, length); response[length-1] = '\0'; } else { response = NULL; } } curl_easy_cleanup(curl); free(CurlStruct.memory); CurlStruct.memory = NULL; free(post); post = NULL; return res; }
char* curl_post_form(const char *url,\ const char *postdata,\ const char *proxy,\ const char *cookie,\ int flag_cookie) { long timeout = 10800; long connect_timeout = 15; long low_speed_limit = 1024; long low_speed_time = 60; struct mem_node *mem_head = NULL; long response_code; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // for thread safe curl_easy_setopt(curl,CURLOPT_URL,url); //url地址 curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postdata); //post参数 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connect_timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, low_speed_limit); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, low_speed_time); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,&curl_read); //对返回的数据进行操作的函数地址 curl_easy_setopt(curl,CURLOPT_WRITEDATA,&mem_head); //这是write_data的第四个参数值 curl_easy_setopt(curl,CURLOPT_POST,1); //设置非0表示本次操作为post curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1); //设置为非0,响应头信息location if(flag_cookie) { curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"./cookie.txt"); curl_easy_setopt(curl,CURLOPT_COOKIEJAR,"./cookie.txt"); } // curl_easy_setopt(easy_handle, CURLOPT_PROXY,proxy); CURLcode rc = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); curl_easy_cleanup(curl); if (rc!=CURLE_OK) { return NULL; }else if (response_code!=200 && response_code!=206){ struct mem_node *p = mem_head; while(p){ struct mem_node *q = p; p = p->next; free(q->buffer); free(q); } return NULL; }else{ struct mem_node *p = mem_head; size_t size = 0; while(p){ size += p->size; p = p->next; } char *content = (char*)malloc(size+1); p = mem_head; size = 0; while(p){ memcpy(content+size, p->buffer, p->size); size += p->size; struct mem_node *q = p; p = p->next; free(q->buffer); free(q); } content[size] = 0; return content; } }
bool stratum_connect(struct stratum_ctx *sctx, const char *url) { CURL *curl; int rc; pthread_mutex_lock(&sctx->sock_lock); if (sctx->curl) curl_easy_cleanup(sctx->curl); sctx->curl = curl_easy_init(); if (!sctx->curl) { applog(LOG_ERR, "CURL initialization failed"); pthread_mutex_unlock(&sctx->sock_lock); return false; } curl = sctx->curl; if (!sctx->sockbuf) { sctx->sockbuf = (char*)calloc(RBUFSIZE, 1); sctx->sockbuf_size = RBUFSIZE; } sctx->sockbuf[0] = '\0'; pthread_mutex_unlock(&sctx->sock_lock); if (url != sctx->url) { free(sctx->url); sctx->url = strdup(url); } free(sctx->curl_url); sctx->curl_url = (char*)malloc(strlen(url)); sprintf(sctx->curl_url, "http%s", strstr(url, "://")); if (opt_protocol) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_URL, sctx->curl_url); curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, sctx->curl_err_str); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1); if (opt_proxy && opt_proxy_type != CURLPROXY_HTTP) { curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy); curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type); } else if (getenv("http_proxy")) { if (getenv("all_proxy")) curl_easy_setopt(curl, CURLOPT_PROXY, getenv("all_proxy")); else if (getenv("ALL_PROXY")) curl_easy_setopt(curl, CURLOPT_PROXY, getenv("ALL_PROXY")); else curl_easy_setopt(curl, CURLOPT_PROXY, ""); } #if LIBCURL_VERSION_NUM >= 0x070f06 curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb); #endif #if LIBCURL_VERSION_NUM >= 0x071101 curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket_grab_cb); curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sctx->sock); #endif curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1); rc = curl_easy_perform(curl); if (rc) { applog(LOG_ERR, "Stratum connection failed: %s", sctx->curl_err_str); curl_easy_cleanup(curl); sctx->curl = NULL; return false; } #if LIBCURL_VERSION_NUM < 0x071101 /* CURLINFO_LASTSOCKET is broken on Win64; only use it as a last resort */ curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, (long *)&sctx->sock); #endif return true; }
/* * Performs http_query and saves possible result (first body line of reply) * to pvar. */ int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post) { CURL *curl; CURLcode res; str value, post_value; char *url, *at, *post; char* stream; long stat; pv_spec_t *dst; pv_value_t val; double download_size; if (fixup_get_svalue(_m, (gparam_p)_url, &value) != 0) { LM_ERR("cannot get page value\n"); return -1; } curl = curl_easy_init(); if (curl == NULL) { LM_ERR("failed to initialize curl\n"); return -1; } url = pkg_malloc(value.len + 1); if (url == NULL) { curl_easy_cleanup(curl); LM_ERR("cannot allocate pkg memory for url\n"); return -1; } memcpy(url, value.s, value.len); *(url + value.len) = (char)0; curl_easy_setopt(curl, CURLOPT_URL, url); if (_post) { /* Now specify we want to POST data */ curl_easy_setopt(curl, CURLOPT_POST, 1L); if (fixup_get_svalue(_m, (gparam_p)_post, &post_value) != 0) { LM_ERR("cannot get post value\n"); pkg_free(url); return -1; } post = pkg_malloc(post_value.len + 1); if (post == NULL) { curl_easy_cleanup(curl); pkg_free(url); LM_ERR("cannot allocate pkg memory for post\n"); return -1; } memcpy(post, post_value.s, post_value.len); *(post + post_value.len) = (char)0; curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); } curl_easy_setopt(curl, CURLOPT_NOSIGNAL, (long)1); curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)http_query_timeout); stream = NULL; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); res = curl_easy_perform(curl); pkg_free(url); if (_post) { pkg_free(post); } curl_easy_cleanup(curl); if (res != CURLE_OK) { LM_ERR("failed to perform curl\n"); return -1; } curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &stat); if ((stat >= 200) && (stat < 400)) { curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size); LM_DBG("http_query download size: %u\n", (unsigned int)download_size); /* search for line feed */ at = memchr(stream, (char)10, download_size); if (at == NULL) { /* not found: use whole stream */ at = stream + (unsigned int)download_size; } val.rs.s = stream; val.rs.len = at - stream; LM_DBG("http)query result: %.*s\n", val.rs.len, val.rs.s); val.flags = PV_VAL_STR; dst = (pv_spec_t *)_dst; dst->setf(_m, &dst->pvp, (int)EQ_T, &val); } return stat; }
int main(void) { CURL *curl; CURLM *mcurl; int still_running = 1; struct timeval mp_start; struct curl_slist *recipients = NULL; struct upload_status upload_ctx; upload_ctx.lines_read = 0; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(!curl) return 1; mcurl = curl_multi_init(); if(!mcurl) return 2; /* This is the URL for your mailserver */ curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); /* Note that this option isn't strictly required, omitting it will result in * libcurl sending the MAIL FROM command with empty sender data. All * autoresponses should have an empty reverse-path, and should be directed * to the address in the reverse-path which triggered them. Otherwise, they * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details. */ curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); /* Add two recipients, in this particular case they correspond to the * To: and Cc: addressees in the header, but they could be any kind of * recipient. */ recipients = curl_slist_append(recipients, TO); recipients = curl_slist_append(recipients, CC); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); /* We're using a callback function to specify the payload (the headers and * body of the message). You could just use the CURLOPT_READDATA option to * specify a FILE pointer to read from. */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); /* Tell the multi stack about our easy handle */ curl_multi_add_handle(mcurl, curl); /* Record the start time which we can use later */ mp_start = tvnow(); /* We start some action by calling perform right away */ curl_multi_perform(mcurl, &still_running); while(still_running) { struct timeval timeout; fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; int rc; long curl_timeo = -1; /* Initialise the file descriptors */ FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* Set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(mcurl, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* Get file descriptors from the transfers */ curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the function calls. On success, the value of maxfd is guaranteed to be greater or equal than -1. We call select(maxfd + 1, ...), specially in case of (maxfd == -1), we call select(0, ...), which is basically equal to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) { fprintf(stderr, "ABORTING: Since it seems that we would have run forever.\n"); break; } switch(rc) { case -1: /* select error */ break; case 0: /* timeout */ default: /* action */ curl_multi_perform(mcurl, &still_running); break; } } /* Free the list of recipients */ curl_slist_free_all(recipients); /* Always cleanup */ curl_multi_remove_handle(mcurl, curl); curl_multi_cleanup(mcurl); curl_easy_cleanup(curl); curl_global_cleanup(); return 0; }
int test(char *URL) { int res = 0; CURL *curl[NUM_HANDLES]; int running; char done=FALSE; CURLM *m; int i, j; struct timeval ml_start; struct timeval mp_start; char ml_timedout = FALSE; char mp_timedout = FALSE; char target_url[256]; if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { fprintf(stderr, "curl_global_init() failed\n"); return TEST_ERR_MAJOR_BAD; } if ((m = curl_multi_init()) == NULL) { fprintf(stderr, "curl_multi_init() failed\n"); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD; } /* get NUM_HANDLES easy handles */ for(i=0; i < NUM_HANDLES; i++) { curl[i] = curl_easy_init(); if(!curl[i]) { fprintf(stderr, "curl_easy_init() failed " "on handle #%d\n", i); for (j=i-1; j >= 0; j--) { curl_multi_remove_handle(m, curl[j]); curl_easy_cleanup(curl[j]); } curl_multi_cleanup(m); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD + i; } sprintf(target_url, "%s%04i", URL, i + 1); target_url[sizeof(target_url) - 1] = '\0'; curl_easy_setopt(curl[i], CURLOPT_URL, target_url); /* go verbose */ curl_easy_setopt(curl[i], CURLOPT_VERBOSE, 1L); /* include headers */ curl_easy_setopt(curl[i], CURLOPT_HEADER, 1L); /* add handle to multi */ if ((res = (int)curl_multi_add_handle(m, curl[i])) != CURLM_OK) { fprintf(stderr, "curl_multi_add_handle() failed, " "on handle #%d with code %d\n", i, res); curl_easy_cleanup(curl[i]); for (j=i-1; j >= 0; j--) { curl_multi_remove_handle(m, curl[j]); curl_easy_cleanup(curl[j]); } curl_multi_cleanup(m); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD + i; } } curl_multi_setopt(m, CURLMOPT_PIPELINING, 1L); ml_timedout = FALSE; ml_start = tutil_tvnow(); fprintf(stderr, "Start at URL 0\n"); while (!done) { fd_set rd, wr, exc; int max_fd; struct timeval interval; interval.tv_sec = 1; interval.tv_usec = 0; if (tutil_tvdiff(tutil_tvnow(), ml_start) > MAIN_LOOP_HANG_TIMEOUT) { ml_timedout = TRUE; break; } mp_timedout = FALSE; mp_start = tutil_tvnow(); while (res == CURLM_CALL_MULTI_PERFORM) { res = (int)curl_multi_perform(m, &running); if (tutil_tvdiff(tutil_tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) { mp_timedout = TRUE; break; } if (running <= 0) { done = TRUE; /* bail out */ break; } } if (mp_timedout || done) break; if (res != CURLM_OK) { fprintf(stderr, "not okay???\n"); break; } FD_ZERO(&rd); FD_ZERO(&wr); FD_ZERO(&exc); max_fd = 0; if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) { fprintf(stderr, "unexpected failured of fdset.\n"); res = 189; break; } if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) { fprintf(stderr, "bad select??\n"); res = 195; break; } res = CURLM_CALL_MULTI_PERFORM; } if (ml_timedout || mp_timedout) { if (ml_timedout) fprintf(stderr, "ml_timedout\n"); if (mp_timedout) fprintf(stderr, "mp_timedout\n"); fprintf(stderr, "ABORTING TEST, since it seems " "that it would have run forever.\n"); res = TEST_ERR_RUNS_FOREVER; } /* cleanup NUM_HANDLES easy handles */ for(i=0; i < NUM_HANDLES; i++) { curl_multi_remove_handle(m, curl[i]); curl_easy_cleanup(curl[i]); } curl_multi_cleanup(m); curl_global_cleanup(); return res; }
// ------------------------------------------------------------------ // Funcion que realiza un post al WS // ------------------------------------------------------------------ int PostToWS(const char *url, const char *postData, char result[2048]) { char tmp[1024]; char tmpb[1024]; int exitStatus = 0; CURL* curl; CURLcode res; struct WritePOST wtext; struct ReadPOST rtext; init_struct(&rtext); /* Headers */ struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "charsets: utf-8"); // Parameters for call wtext.ptr = postData; wtext.sizeleft = strlen(postData); /* Initialise libcurl */ curl_global_init(CURL_GLOBAL_ALL); /* Get a curl handle */ curl = curl_easy_init(); if(curl) { //printf("%s", url); /* First set the URL that is about to receive our POST. */ curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); /* Now specify we want to POST data */ curl_easy_setopt(curl, CURLOPT_POST, 1L); /* we want to use our own read function */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); /* pointer to pass to our read function */ curl_easy_setopt(curl, CURLOPT_READDATA, &wtext); /* get verbose debug output please */ //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); /* Set the expected POST size. If you want to POST large amounts of data, consider CURLOPT_POSTFIELDSIZE_LARGE */ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)wtext.sizeleft); /* we want to use our own write function */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); /* pointer to pass to our write function */ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rtext); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); if(res) { sprintf(tmp, "ERROR: %s", curl_easy_strerror(res)); printf("%s\n", tmp); exitStatus = 0; } else { if(rtext.ptr != NULL) { if(strlen(rtext.ptr) > 0) { sprintf(result, "%s", rtext.ptr); // Imprimir resultados del WS printf("\nA: %s\n", result); } else { printf("\nA: if(strlen(rtext.ptr) > 0) es falso"); } free(rtext.ptr); } exitStatus = 1; } /* always cleanup */ curl_easy_cleanup(curl); } else { exitStatus = 0; } /* Return the exit status */ return exitStatus; }
void email::send() { if( ! activated ) { printf( "Send mail attempted: %s", content.c_str() ); exit(0); } else { // Check maximum sent count if( sent_count > 3 ) { dump( "Too much email sent. Exiting for safety...\n" ); exit(0); } else { dump( "Sending an email...\n" ); dump( "%s\n" , subject.c_str()); dump( "Content: %s\n", content.c_str() ); } // Correct return code for( uint n=0; n<content.size(); n++ ) { if( content[n] == '\n' && content[n+1] == 0 ) content[n] = 0; } // Set message payload_text[2] = subject.c_str(); payload_text[4] = content.c_str(); } #if ENABLE_LCURL CURL *curl; CURLcode res; struct curl_slist *recipients = NULL; struct upload_status upload_ctx; upload_ctx.lines_read = 0; curl = curl_easy_init(); if (curl) { /* This is the URL for your mailserver. Note the use of port 587 here, * instead of the normal SMTP port (25). Port 587 is commonly used for * secure mail submission (see RFC4403), but you should use whatever * matches your server configuration. */ curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.yoursever.com:587"); /* In this example, we'll start with a plain text connection, and upgrade * to Transport Layer Security (TLS) using the STARTTLS command. Be careful * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer * will continue anyway - see the security discussion in the libcurl * tutorial for more details. */ curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); /* If your server doesn't have a valid certificate, then you can disable * part of the Transport Layer Security protection by setting the * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false). * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); * That is, in general, a bad idea. It is still better than sending your * authentication details in plain text though. * Instead, you should get the issuer certificate (or the host certificate * if the certificate is self-signed) and add it to the set of certificates * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See * docs/SSLCERTS for more information. */ // curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem"); /* A common reason for requiring transport security is to protect * authentication details (user names and passwords) from being "snooped" * on the network. Here is how the user name and password are provided: */ curl_easy_setopt(curl, CURLOPT_USERNAME, "*****@*****.**"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "yourpassword"); /* value for envelope reverse-path */ curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM); /* Add two recipients, in this particular case they correspond to the * To: and Cc: addressees in the header, but they could be any kind of * recipient. */ recipients = curl_slist_append(recipients, TO); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); /* In this case, we're using a callback function to specify the data. You * could just use the CURLOPT_READDATA option to specify a FILE pointer to * read from. */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); /* Since the traffic will be encrypted, it is very useful to turn on debug * information within libcurl to see what is happening during the transfer. */ // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 0); /* send the message (including headers) */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); exit(0); } /* free the list of recipients and clean up */ curl_slist_free_all(recipients); curl_easy_cleanup(curl); // Clear message body content.clear(); // Record sent count sent_count ++; } #endif }
int query_tool_set_powered(const char* tool_id, bool powered) { CURL* handle; CURLcode error_code; char buf[1024]; long response = 0; struct curl_slist *headers = NULL; #ifdef DEBUG_EVENT_RESPONSE FILE *fdebug; fdebug = fopen("debug.html", "w"); #endif handle = curl_easy_init(); if (handle == NULL) return 1; headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); sprintf(buf, "PUBLIC_KEY: %s", public_key); headers = curl_slist_append(headers, buf); sprintf(buf, "PRIVATE_KEY: %s", private_key); headers = curl_slist_append(headers, buf); // For new ModWSGI version. Delete previous // authentication headers when Roboclub8 fully migrated sprintf(buf, "PUBLIC-KEY: %s", public_key); headers = curl_slist_append(headers, buf); sprintf(buf, "PRIVATE-KEY: %s", private_key); headers = curl_slist_append(headers, buf); json_object* json = json_object_new_object(); json_object_object_add(json, "powered", json_object_new_boolean(powered)); sprintf(buf, "%s/api/machines/%s/", server, tool_id); error_code = curl_easy_setopt(handle, CURLOPT_URL, buf); if (error_code) goto error; /* TODO disabling host and peer verification should theoretically be removed * eventually */ error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L); if (error_code) goto error; error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L); if (error_code) goto error; #ifdef DEBUG_EVENT_RESPONSE error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug); #else error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore); #endif if (error_code) goto error; error_code = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PUT"); if (error_code) goto error; error_code = curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); if (error_code) goto error; error_code = curl_easy_setopt(handle, CURLOPT_POSTFIELDS, json_object_to_json_string(json)); if (error_code) goto error; error_code = curl_easy_perform(handle); if (error_code) goto error; error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response); if (error_code) goto error; if (response >= 400) log_print("ERROR: response %ld from %s", response, buf); else if (response > 200) log_print("WARNING: response %ld from %s", response, buf); curl_easy_cleanup(handle); curl_slist_free_all(headers); #ifdef DEBUG_EVENT_RESPONSE fclose(fdebug); #endif // Free json object json_object_put(json); // return error if it's not a 200-level response return response >= 300; error: log_print("ERROR: curl: %s", curl_easy_strerror(error_code)); curl_easy_cleanup(handle); curl_slist_free_all(headers); #ifdef DEBUG_EVENT_RESPONSE fclose(fdebug); #endif // Free json object json_object_put(json); return 1; }
int query_tools(struct tool_t*** tools) { log_print("Requesting machines from server"); CURL* handle; CURLcode error_code; char url[1024]; long response = 0; handle = curl_easy_init(); if (handle == NULL) return 0; // only retrieve machines that have Tooltron boxes on them sprintf(url, "%s/api/machines/?toolbox_id__isnull=False", server); error_code = curl_easy_setopt(handle, CURLOPT_URL, url); if (error_code) goto error; buffer_idx = 0; error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_buffer); if (error_code) goto error; error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL); if (error_code) goto error; error_code = curl_easy_perform(handle); if (error_code) goto error; error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response); if (error_code) goto error; if (response >= 400) log_print("ERROR: response %ld from %s", response, url); else if (response > 200) log_print("WARNING: response %ld from %s", response, url); struct json_object *json_tools = json_tokener_parse((char*)buffer); int num_tools = json_object_array_length(json_tools); log_print("Read %d tools from server", num_tools); (*tools) = (struct tool_t**)malloc(num_tools * sizeof(struct tool_t*)); if(!tools) { log_print("Out of memory mallocing tool array"); return -1; } int i; for(i = 0; i < num_tools; i++) { json_object* json_tool = json_object_array_get_idx(json_tools, i); json_object* json_tool_id = json_object_object_get(json_tool, "id"); json_object* json_tool_toolbox_id = json_object_object_get(json_tool, "toolbox_id"); json_object* json_tool_name = json_object_object_get(json_tool, "type"); const char* tool_id = json_object_get_string(json_tool_id); const char* tool_name = json_object_get_string(json_tool_name); int tool_toolbox_id = json_object_get_int(json_tool_toolbox_id); log_print("Tool ID: %s, Tool Name: %s, Toolbox ID: %d", tool_id, tool_name, tool_toolbox_id); struct tool_t* tool = malloc(sizeof(struct tool_t)); if(!tool) { log_print("Out of memory mallocing tool"); return -1; } tool->id = malloc(strlen(tool_id) + 1); if(!tool->id) { log_print("Out of memory mallocing tool id"); return -1; } strcpy(tool->id, tool_id); tool->name = malloc(strlen(tool_name) + 1); if(!tool->name) { log_print("Out of memory mallocing tool name"); return -1; } strcpy(tool->name, tool_name); tool->address = tool_toolbox_id; tool->connected = 1; tool->state = TS_INIT; tool->user = 0; tool->event = NULL; tool->powered = false; (*tools)[i] = tool; } curl_easy_cleanup(handle); return num_tools; error: log_print("ERROR: curl: %s", curl_easy_strerror(error_code)); log_print("ERROR: when requesting machines from server"); curl_easy_cleanup(handle); return -1; }
/* * query_add_event * * Makes an HTTPS POST request to add an event to the CRM server, including * user, tool, start time, and stop time. Reads the password from password.txt. * Returns 0 if successful, or 1 if there was an error and the caller should * try the same event again later. * * Times are represented as strftime's "%F %T", which is like "YYYY-MM-DD * HH:MM:SS" with 24-hour time */ int query_add_event(struct event_t *event) { CURL* handle; CURLcode error_code; struct curl_httppost *formpost = NULL, *lastptr = NULL; char buf[1024]; struct tm *timeinfo; long response = 0; #ifdef DEBUG_EVENT_RESPONSE FILE *fdebug; fdebug = fopen("debug.html", "w"); #endif handle = curl_easy_init(); if (handle == NULL) return 1; timeinfo = localtime(&event->tstart); strftime(buf, sizeof(buf), "%F %T", timeinfo); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "tstart", CURLFORM_COPYCONTENTS, buf, CURLFORM_END); timeinfo = localtime(&event->tend); strftime(buf, sizeof(buf), "%F %T", timeinfo); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "tend", CURLFORM_COPYCONTENTS, buf, CURLFORM_END); sprintf(buf, "%08x", event->user); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "user_id", CURLFORM_COPYCONTENTS, buf, CURLFORM_END); sprintf(buf, "%d", event->tool_id); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "machine_id", CURLFORM_COPYCONTENTS, buf, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "succ", CURLFORM_COPYCONTENTS, event->succ? "1" : "0", CURLFORM_END); sprintf(buf, "%s/crm/add_card_event/", server); error_code = curl_easy_setopt(handle, CURLOPT_URL, buf); if (error_code) goto error; /* TODO disabling host and peer verification should theoretically be removed * eventually */ error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L); if (error_code) goto error; error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L); if (error_code) goto error; #ifdef DEBUG_EVENT_RESPONSE error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug); #else error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore); #endif if (error_code) goto error; error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost); if (error_code) goto error; error_code = curl_easy_perform(handle); if (error_code) goto error; error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response); if (error_code) goto error; if (response >= 400) log_print("ERROR: response %ld from %s", response, buf); else if (response > 200) log_print("WARNING: response %ld from %s", response, buf); curl_easy_cleanup(handle); curl_formfree(formpost); #ifdef DEBUG_EVENT_RESPONSE fclose(fdebug); #endif // return error if it's not a 200-level response return response >= 300; error: log_print("ERROR: curl: %s", curl_easy_strerror(error_code)); curl_easy_cleanup(handle); curl_formfree(formpost); #ifdef DEBUG_EVENT_RESPONSE fclose(fdebug); #endif return 1; }
/* feed_update() * Takes initialized feed with url set, fetches the feed from this url, * updates rest of Feed struct members and returns HTTP response code * we got from url's server. */ int newsfeed_update(struct newsfeed * feed, time_t last_update) { #if (defined(HAVE_LIBCURL) && defined(HAVE_EXPAT)) CURL * eh; CURLcode curl_res; struct newsfeed_parser_context * feed_ctx; unsigned int res; unsigned int timeout_value; long response_code; if (feed->feed_url == NULL) { res = NEWSFEED_ERROR_BADURL; goto err; } /* Init curl before anything else. */ eh = curl_easy_init(); if (eh == NULL) { res = NEWSFEED_ERROR_MEMORY; goto err; } /* Curl initialized, create parser context now. */ feed_ctx = malloc(sizeof(* feed_ctx)); if (feed_ctx == NULL) { res = NEWSFEED_ERROR_MEMORY; goto free_eh; } feed_ctx->parser = XML_ParserCreate(NULL); if (feed_ctx->parser == NULL) { res = NEWSFEED_ERROR_MEMORY; goto free_ctx; } feed_ctx->depth = 0; feed_ctx->str = mmap_string_sized_new(256); if (feed_ctx->str == NULL) { res = NEWSFEED_ERROR_MEMORY; goto free_praser; } feed_ctx->feed = feed; feed_ctx->location = 0; feed_ctx->curitem = NULL; feed_ctx->error = NEWSFEED_NO_ERROR; /* Set initial expat handlers, which will take care of choosing * correct parser later. */ newsfeed_parser_set_expat_handlers(feed_ctx); if (feed->feed_timeout != 0) timeout_value = feed->feed_timeout; else timeout_value = mailstream_network_delay.tv_sec; curl_easy_setopt(eh, CURLOPT_URL, feed->feed_url); curl_easy_setopt(eh, CURLOPT_NOPROGRESS, 1); #ifdef CURLOPT_MUTE curl_easy_setopt(eh, CURLOPT_MUTE, 1); #endif curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, newsfeed_writefunc); curl_easy_setopt(eh, CURLOPT_WRITEDATA, feed_ctx); curl_easy_setopt(eh, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(eh, CURLOPT_MAXREDIRS, 3); curl_easy_setopt(eh, CURLOPT_TIMEOUT, timeout_value); curl_easy_setopt(eh, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(eh, CURLOPT_USERAGENT, "libEtPan!"); /* Use HTTP's If-Modified-Since feature, if application provided * the timestamp of last update. */ if (last_update != -1) { curl_easy_setopt(eh, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); curl_easy_setopt(eh, CURLOPT_TIMEVALUE, last_update); } #if LIBCURL_VERSION_NUM >= 0x070a00 curl_easy_setopt(eh, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(eh, CURLOPT_SSL_VERIFYHOST, 0); #endif curl_res = curl_easy_perform(eh); if (curl_res != 0) { res = curl_error_convert(curl_res); goto free_str; } curl_easy_getinfo(eh, CURLINFO_RESPONSE_CODE, &response_code); curl_easy_cleanup(eh); if (feed_ctx->error != NEWSFEED_NO_ERROR) { res = feed_ctx->error; goto free_str; } /* Cleanup, we should be done. */ mmap_string_free(feed_ctx->str); XML_ParserFree(feed_ctx->parser); free(feed_ctx); feed->feed_response_code = (int) response_code; return NEWSFEED_NO_ERROR;; free_str: mmap_string_free(feed_ctx->str); free_praser: XML_ParserFree(feed_ctx->parser); free_ctx: free(feed_ctx); free_eh: curl_easy_cleanup(eh); err: return res; #else return NEWSFEED_ERROR_INTERNAL; #endif }
int main(int argc, char *argv[]) { CURL *curl; CURLcode ret; char *url = NULL; char *host_header = NULL; char *host_name = NULL; char *regex = NULL; struct timeval tvBegin, tvEnd, tvDiff; /* Commandline switches */ int verbose_level=0; int status_only = false; int measure_time = false; int nossl_verify = false; int follow_location = false; int fail_on_curl_error = false; int ssl_valid_date = false; struct curl_slist *headers = NULL; int i; int curl_timeout = TIMEOUT; char *curl_userpwd = NULL; ASN1_TIME * notAfter; time_t now; time_t expire; int time_left; pcre *re; int pcre_opts=0; const char *error; int erroffset; int ovector[OVECCOUNT]; int rc; int opt; wr_error = 0; wr_index = 0; /* First step, init curl */ curl = curl_easy_init(); if(!curl) { fprintf(stderr, "couldn't init curl\n"); exit(EXIT_FAILURE); } /* * if no arguments are given */ if(argc == 1) { fprintf(stderr, "This program needs arguments....\n\n"); print_arguments(argc, argv); print_help(1); } while((opt = getopt(argc, argv, "?VfcamMlsvp:t:u:h:r:i")) != -1) { switch(opt) { case 'V': fprintf(stderr,"%s %s\n\n", PACKAGE, VERSION); exit(0); break; case 'v': verbose_level++; break; case 'a': curl_easy_setopt(curl, CURLOPT_HEADER , true); break; case 'i': curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); nossl_verify = true; break; case 's': status_only = true; break; case 'm': measure_time = true; break; case 'M': pcre_opts |= PCRE_MULTILINE; break; case 'l': follow_location = true; curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true); break; case 'f': fail_on_curl_error = true; break; case 'p': curl_userpwd = optarg; break; case 'u': url = optarg; break; case 't': curl_timeout = atoi(optarg); break; case 'h': host_header = malloc(strlen("Host: ") + strlen(optarg) + 1); host_name = optarg; strcpy(host_header, "Host: "); strcat(host_header, optarg); headers = curl_slist_append(headers, host_header); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); break; case 'c': ssl_valid_date = true; case 'r': regex = optarg; break; case ':': fprintf(stderr, "%s: Error - Option `%c' needs a value\n\n", PACKAGE, optopt); print_arguments(argc, argv); print_help(1); break; case '?': fprintf(stderr, "%s: Error - No such option: `%c'\n", PACKAGE, optopt); print_arguments(argc, argv); print_help(1); } } if (verbose_level > 3){ curl_easy_setopt(curl, CURLOPT_VERBOSE , true); } if (verbose_level > 0){ fprintf(stderr, "%-17s %s\n", "URL", url); fprintf(stderr, "%-17s %s\n", "REGEX", regex); fprintf(stderr, "%-17s %i\n", "TIMEOUT", curl_timeout); fprintf(stderr, "%-17s %s\n", "HOST HEADER", host_header); fprintf(stderr, "%-17s %i\n\n", "STATUS ONLY", status_only); fprintf(stderr, "%-17s %s -t %i -u \"%s\" -r \"%s\"", "CMD ", PACKAGE, curl_timeout, url, regex); if ( status_only == true ){ fprintf(stderr, " -s"); } if ( measure_time == true ){ fprintf(stderr, " -m"); } if ( nossl_verify == true ){ fprintf(stderr, " -i"); } if ( follow_location == true ){ fprintf(stderr, " -l"); } if ( fail_on_curl_error == true ){ fprintf(stderr, " -f"); } if ( (pcre_opts & PCRE_MULTILINE) == PCRE_MULTILINE ){ fprintf(stderr, " -M"); } if (curl_userpwd != NULL){ fprintf(stderr, " -p %s", curl_userpwd); } if ( host_name != NULL ){ fprintf(stderr, " -h %s",host_name); } fprintf(stderr, "\n"); fprintf(stderr, "%-17s %s[\"-t\",\"%i\",\"-u\",\"%s\",\"-r\",\"%s\"", "ZABBIX 2.0 ITEM", PACKAGE, curl_timeout, url, regex); if ( status_only == true ){ fprintf(stderr, ",\"-s\""); } if ( measure_time == true ){ fprintf(stderr, ",\"-m\""); } if ( nossl_verify == true ){ fprintf(stderr, ",\"-i\""); } if ( follow_location == true ){ fprintf(stderr, ",\"-l\""); } if ( fail_on_curl_error == true ){ fprintf(stderr, ",\"-f\""); } if ( (pcre_opts & PCRE_MULTILINE) == PCRE_MULTILINE ){ fprintf(stderr, ",\"-M\""); } if (curl_userpwd != NULL){ fprintf(stderr, ",\"-p\",\"%s\"", curl_userpwd); } if ( host_name != NULL ){ fprintf(stderr, ",\"-h\",\"%s\"",host_name); } fprintf(stderr, "]\n"); } if (((url == NULL) || (regex == NULL)) && (ssl_valid_date == false)){ print_arguments(argc, argv); print_help(EXIT_FAILURE); } /* Tell curl the URL of the file we're going to retrieve */ curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5"); /* Tell curl that we'll receive data to the function write_data, and * also provide it with a context pointer for our error return. */ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &wr_error); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, curl_timeout); curl_easy_setopt(curl, CURLOPT_USERPWD, curl_userpwd); gettimeofday(&tvBegin, NULL); /* Initialize certificate array*/ for(i=0; i<MAX_CERTS;i++) { certificates[i] = 0; certificates_error[i] = X509_V_OK; } curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfunc); if(ssl_valid_date == true) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); } /* Allow curl to perform the action */ ret = curl_easy_perform(curl); /* Stop execution here if only status is needed */ if ((ret != 0) || (fail_on_curl_error == true)){ if (status_only == true){ printf("0"); if (verbose_level > 1){ fprintf(stderr,"returned: '0'\n"); } } else if (measure_time == true){ printf("0.0"); if (verbose_level > 1){ fprintf(stderr,"returned: '0.0'\n"); } } exit(EXIT_FAILURE); } /* Get days until certificate expires */ if(ssl_valid_date == true) { if(ret!=0 || certificates[0]==0) { exit(EXIT_FAILURE); } notAfter = X509_get_notAfter(certificates[0]); now = time(NULL); expire = ASN1_GetTimeT(notAfter); time_left = (expire-now)/(60*60*24); printf("%d",time_left); if (verbose_level > 1){ fprintf(stderr,"returned: '%d'\n",time_left); } curl_easy_cleanup(curl); exit(EXIT_SUCCESS); } re = pcre_compile(regex, /* the pattern */ pcre_opts, /* default options */ &error, /* for error message */ &erroffset, /* for error offset */ NULL); /* use default character tables */ rc = pcre_exec(re, /* the compiled pattern */ NULL, /* no extra data - we didn't study the pattern */ wr_buf, /* the subject string */ wr_index, /* the length of the subject */ 0, /* start at offset 0 in the subject */ 0, /* default options */ ovector, /* output vector for substring information */ OVECCOUNT); /* number of elements in the output vector */ if(verbose_level > 2) { fprintf(stderr, "out: >>>%s<<< [%i bytes]\n", wr_buf, wr_index); } /* Evaluate the match and output status */ if(rc < 0) { if (status_only == true) { printf("0"); if (verbose_level > 1){ fprintf(stderr,"returned: '0'\n"); } } else if (measure_time == true) { printf("0.0"); if (verbose_level > 1){ fprintf(stderr,"returned: '0.0'\n"); } } else { switch (rc) { case PCRE_ERROR_NOMATCH : fprintf(stderr,"String did not match the pattern\n"); break; case PCRE_ERROR_NULL : fprintf(stderr,"Something was null\n"); break; case PCRE_ERROR_BADOPTION : fprintf(stderr,"A bad option was passed\n"); break; case PCRE_ERROR_BADMAGIC : fprintf(stderr,"Magic number bad (compiled re corrupt?)\n"); break; case PCRE_ERROR_UNKNOWN_NODE : fprintf(stderr,"Something kooky in the compiled re\n"); break; case PCRE_ERROR_NOMEMORY : fprintf(stderr,"Ran out of memory\n"); break; default : fprintf(stderr,"Matching error %d\n", rc); break; } } pcre_free(re); /* Release memory used for the compiled pattern */ exit(EXIT_FAILURE); } if(rc == 2) { if (status_only == true) { printf("1"); if (verbose_level > 1) { fprintf(stderr,"returned: '1'\n"); } } else if (measure_time == true) { gettimeofday(&tvEnd, NULL); timeval_subtract(&tvDiff, &tvEnd, &tvBegin); printf("%ld.%06ld", tvDiff.tv_sec, tvDiff.tv_usec); if (verbose_level > 1){ fprintf(stderr,"measure time returned: '%ld.%06ld'\n", tvDiff.tv_sec, tvDiff.tv_usec); } } else { char *substring_start = NULL; int substring_length = 0; i = 1; substring_start = wr_buf + ovector[2 * i]; substring_length = ovector[2 * i + 1] - ovector[2 * i]; printf("%.*s", substring_length, substring_start); fprintf(stderr,"parsing returned: '%.*s'\n", substring_length, substring_start); } } curl_easy_cleanup(curl); exit(EXIT_SUCCESS); }
static int youtube_streamChange(interfaceMenu_t *pMenu, void *pArg) { CURLcode ret; CURL *hnd; char proxy[32]; char login[512]; char *str; static char url[MAX_URL]; static char url_tmp[MAX_URL]; static char curl_data[YOUTUBE_INFO_BUFFER_SIZE]; static char err_buff[CURL_ERROR_SIZE]; static curlBufferInfo_t buffer; char *fmt_url_map; //int supported_formats[] = { 18, 17, 34, 5, 0 }; int supported_formats[] = { 34, 18, 0 }; /* 37/1920x1080/9/0/115 22/1280x720/9/0/115 35/854x480/9/0/115 34/640x360/9/0/115 5/320x240/7/0/0 */ char *fmt_url; int i; int len_str; int len_url = 0; int videoIndex = (int)pArg; if( videoIndex == CHANNEL_CUSTOM ) { str = strstr( appControlInfo.mediaInfo.lastFile, "watch?v=" ); if( str == NULL ) { eprintf("%s: can't file YouTube ID in %s\n", __FUNCTION__, appControlInfo.mediaInfo.lastFile); interface_showMessageBox(_T("ERR_PLAY_FILE"), thumbnail_error, 3000); return -1; } str += 8; str[sizeof(youtubeInfo.current_id)-1] = 0; if( strlen(str) != YOUTUBE_ID_LENGTH-1 ) { eprintf("%s: invalid YouTube ID %s\n", __FUNCTION__, str); interface_showMessageBox(_T("ERR_PLAY_FILE"), thumbnail_error, 3000); return -1; } memcpy( youtubeInfo.current_id, str, sizeof(youtubeInfo.current_id) ); } else if( videoIndex < 0 || videoIndex >= youtubeInfo.count ) { eprintf("%s: there is no stream %d\n", __FUNCTION__, videoIndex); interface_showMessageBox(_T("ERR_PLAY_FILE"), thumbnail_error, 3000); return -1; } else { memcpy( youtubeInfo.current_id, youtubeInfo.videos[videoIndex].video_id, sizeof(youtubeInfo.current_id) ); } buffer.data = curl_data; buffer.size = sizeof(curl_data); buffer.pos = 0; curl_data[0] = 0; sprintf(url,"http://www.youtube.com/get_video_info?video_id=%s%s",youtubeInfo.current_id, "&eurl=&el=detailpage&ps=default&gl=US&hl=en"); hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(hnd, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(hnd, CURLOPT_URL, url); curl_easy_setopt(hnd, CURLOPT_ERRORBUFFER, err_buff); curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 2); curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 1); curl_easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT, 15); curl_easy_setopt(hnd, CURLOPT_TIMEOUT, 15); getParam(BROWSER_CONFIG_FILE, "HTTPProxyServer", "", proxy); if( proxy[0] != 0 ) { curl_easy_setopt(hnd, CURLOPT_PROXY, proxy); getParam(BROWSER_CONFIG_FILE, "HTTPProxyLogin", "", login); if( login[0] != 0 ) { str = &login[strlen(login)+1]; getParam(BROWSER_CONFIG_FILE, "HTTPProxyPasswd", "", str); if( *str != 0 ) { str[-1] = ':'; } curl_easy_setopt(hnd, CURLOPT_PROXYUSERPWD, login); } } ret = curl_easy_perform(hnd); eprintf("%s: video info for %s acquired (length %d)\n", __FUNCTION__, youtubeInfo.current_id, buffer.pos); eprintf("%s: YouTube URL %s\n", __FUNCTION__, url); curl_easy_cleanup(hnd); if (ret != 0) { eprintf("Youtube: Failed to get video info from '%s': %s\n", url, err_buff); interface_showMessageBox(_T("ERR_PLAY_FILE"), thumbnail_error, 3000); return 1; } /* fmt_url_map = strstr(curl_data, "fmt_list="); if( fmt_url_map ) { fmt_url_map += sizeof("fmt_list=")-1; for( str = fmt_url_map; *str && *str != '&'; str++ ); if( *str != '&' ) str = NULL; else *str = 0; if( utf8_urltomb(fmt_url_map, strlen(fmt_url_map)+1, url, sizeof(url)-1 ) > 0 ) { eprintf("%s: fmt_list='%s'\n", __FUNCTION__, url); } } */ //fmt_url_map = strstr(curl_data, "fmt_url_map="); fmt_url_map = strstr(curl_data, "url_encoded_fmt_stream_map="); if( fmt_url_map ) { //fmt_url_map += sizeof("fmt_url_map=")-1; fmt_url_map += sizeof("url_encoded_fmt_stream_map=")-1; for( str = fmt_url_map; *str && *str != '&'; str++ ); if( *str != '&' ) str = NULL; else *str = 0; for( i = 0; supported_formats[i] != 0; i++ ) { //sprintf(proxy, "%d%%7C", supported_formats[i]); sprintf(proxy, "itag%%3D%d", supported_formats[i]); // find format tag field if( (fmt_url = strstr( fmt_url_map, proxy )) != NULL ) { fmt_url += strlen(proxy) + 9;// skip "%2Curl%3D" str = strstr( fmt_url, "%26quality" ); // find end url if( str ) *str = 0; eprintf("%s: URL %s\n", __FUNCTION__, fmt_url); if( utf8_urltomb(fmt_url, strlen(fmt_url)+1, url_tmp, sizeof(url_tmp)-1 ) < 0 ) { eprintf("%s: Failed to decode '%s'\n", __FUNCTION__, fmt_url); } else { if( utf8_urltomb(url_tmp, strlen(url_tmp)+1, url, sizeof(url)-1 ) < 0 ) { eprintf("%s: Failed to decode '%s'\n", __FUNCTION__, url_tmp); } else { eprintf("%s: selected format %d\n", __FUNCTION__, supported_formats[i]); break; } } } } if( supported_formats[i] == 0 ) { interface_showMessageBox(_T("ERR_STREAM_NOT_SUPPORTED"), thumbnail_warning, 0); return 1; } } eprintf("Youtube: Playing '%s'\n", url); char *descr = NULL; char *thumbnail = NULL; if( videoIndex != CHANNEL_CUSTOM ) { youtubeInfo.index = videoIndex; if( interface_getMenuEntryInfo( (interfaceMenu_t*)&YoutubeMenu, videoIndex+1, login, sizeof(login) ) == 0 ) descr = login; thumbnail = youtubeInfo.videos[videoIndex].thumbnail[0] ? youtubeInfo.videos[videoIndex].thumbnail : NULL; appControlInfo.playbackInfo.channel = videoIndex+1; appControlInfo.playbackInfo.playlistMode = playlistModeYoutube; } else { youtubeInfo.index = 0; descr = appControlInfo.playbackInfo.description; thumbnail = appControlInfo.playbackInfo.thumbnail; } appControlInfo.playbackInfo.streamSource = streamSourceYoutube; media_playURL(screenMain, url, descr, thumbnail != NULL ? thumbnail : resource_thumbnails[thumbnail_youtube] ); return 0; }
int main(int argc, char *argv[]) { CURL *curl; conf_t conf[1]; int OptionIndex; struct tm *lt; struct tm *gmt; time_t tt; time_t tt_local; time_t tt_gmt; double tzonediffFloat; int tzonediffWord; char timeBuf[61]; char tzoneBuf[16]; int RetValue; OptionIndex = 0; ShowAllHeader = 0; /* Do not show HTTP Header */ AutoSyncTime = 0; /* Do not synchronise computer clock */ RetValue = 0; /* Successful Exit */ conf_init(conf); if (argc > 1) { while (OptionIndex < argc) { if (strncmp(argv[OptionIndex], "--server=", 9) == 0) snprintf(conf->timeserver, MAX_STRING, "%s", &argv[OptionIndex][9]); if (strcmp(argv[OptionIndex], "--showall") == 0) ShowAllHeader = 1; if (strcmp(argv[OptionIndex], "--synctime") == 0) AutoSyncTime = 1; if (strncmp(argv[OptionIndex], "--proxy-user="******"%s", &argv[OptionIndex][13]); if (strncmp(argv[OptionIndex], "--proxy=", 8) == 0) snprintf(conf->http_proxy, MAX_STRING, "%s", &argv[OptionIndex][8]); if ((strcmp(argv[OptionIndex], "--help") == 0) || (strcmp(argv[OptionIndex], "/?") == 0)) { showUsage(); return 0; } OptionIndex++; } } if (*conf->timeserver == 0) /* Use default server for time information */ snprintf(conf->timeserver, MAX_STRING, "%s", DefaultTimeServer[0]); /* Init CURL before usage */ curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if (curl) { SyncTime_CURL_Init(curl, conf->http_proxy, conf->proxy_user); /* Calculating time diff between GMT and localtime */ tt = time(0); lt = localtime(&tt); tt_local = mktime(lt); gmt = gmtime(&tt); tt_gmt = mktime(gmt); tzonediffFloat = difftime(tt_local, tt_gmt); tzonediffWord = (int)(tzonediffFloat/3600.0); if ((double)(tzonediffWord * 3600) == tzonediffFloat) snprintf(tzoneBuf, 15, "%+03d'00'", tzonediffWord); else snprintf(tzoneBuf, 15, "%+03d'30'", tzonediffWord); /* Get current system time and local time */ GetSystemTime(&SYSTime); GetLocalTime(&LOCALTime); snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ", DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay, MthStr[LOCALTime.wMonth-1], LOCALTime.wYear, LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds); fprintf(stderr, "Fetch: %s\n\n", conf->timeserver); fprintf(stderr, "Before HTTP. Date: %s%s\n\n", timeBuf, tzoneBuf); /* HTTP HEAD command to the Webserver */ SyncTime_CURL_Fetch(curl, conf->timeserver, "index.htm", HTTP_COMMAND_HEAD); GetLocalTime(&LOCALTime); snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ", DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay, MthStr[LOCALTime.wMonth-1], LOCALTime.wYear, LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds); fprintf(stderr, "\nAfter HTTP. Date: %s%s\n", timeBuf, tzoneBuf); if (AutoSyncTime == 3) { /* Synchronising computer clock */ if (!SetSystemTime(&SYSTime)) { /* Set system time */ fprintf(stderr, "ERROR: Unable to set system time.\n"); RetValue = 1; } else { /* Successfully re-adjusted computer clock */ GetLocalTime(&LOCALTime); snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ", DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay, MthStr[LOCALTime.wMonth-1], LOCALTime.wYear, LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds); fprintf(stderr, "\nNew System's Date: %s%s\n", timeBuf, tzoneBuf); } } /* Cleanup before exit */ conf_init(conf); curl_easy_cleanup(curl); } return RetValue; }
/* exported function documented in content/fetchers/curl.h */ nserror fetch_curl_register(void) { CURLcode code; curl_version_info_data *data; int i; lwc_string *scheme; const struct fetcher_operation_table fetcher_ops = { .initialise = fetch_curl_initialise, .acceptable = fetch_curl_can_fetch, .setup = fetch_curl_setup, .start = fetch_curl_start, .abort = fetch_curl_abort, .free = fetch_curl_free, .poll = fetch_curl_poll, .finalise = fetch_curl_finalise }; LOG("curl_version %s", curl_version()); code = curl_global_init(CURL_GLOBAL_ALL); if (code != CURLE_OK) { LOG("curl_global_init failed."); return NSERROR_INIT_FAILED; } fetch_curl_multi = curl_multi_init(); if (!fetch_curl_multi) { LOG("curl_multi_init failed."); return NSERROR_INIT_FAILED; } #if LIBCURL_VERSION_NUM >= 0x071e00 /* built against 7.30.0 or later: configure caching */ { CURLMcode mcode; int maxconnects = nsoption_int(max_fetchers) + nsoption_int(max_cached_fetch_handles); #undef SETOPT #define SETOPT(option, value) \ mcode = curl_multi_setopt(fetch_curl_multi, option, value); \ if (mcode != CURLM_OK) \ goto curl_multi_setopt_failed; SETOPT(CURLMOPT_MAXCONNECTS, maxconnects); SETOPT(CURLMOPT_MAX_TOTAL_CONNECTIONS, maxconnects); SETOPT(CURLMOPT_MAX_HOST_CONNECTIONS, nsoption_int(max_fetchers_per_host)); } #endif /* Create a curl easy handle with the options that are common to all fetches. */ fetch_blank_curl = curl_easy_init(); if (!fetch_blank_curl) { LOG("curl_easy_init failed"); return NSERROR_INIT_FAILED; } #undef SETOPT #define SETOPT(option, value) \ code = curl_easy_setopt(fetch_blank_curl, option, value); \ if (code != CURLE_OK) \ goto curl_easy_setopt_failed; if (verbose_log) { SETOPT(CURLOPT_VERBOSE, 1); } else { SETOPT(CURLOPT_VERBOSE, 0); } SETOPT(CURLOPT_ERRORBUFFER, fetch_error_buffer); if (nsoption_bool(suppress_curl_debug)) { SETOPT(CURLOPT_DEBUGFUNCTION, fetch_curl_ignore_debug); } SETOPT(CURLOPT_WRITEFUNCTION, fetch_curl_data); SETOPT(CURLOPT_HEADERFUNCTION, fetch_curl_header); SETOPT(CURLOPT_PROGRESSFUNCTION, fetch_curl_progress); SETOPT(CURLOPT_NOPROGRESS, 0); SETOPT(CURLOPT_USERAGENT, user_agent_string()); SETOPT(CURLOPT_ENCODING, "gzip"); SETOPT(CURLOPT_LOW_SPEED_LIMIT, 1L); SETOPT(CURLOPT_LOW_SPEED_TIME, 180L); SETOPT(CURLOPT_NOSIGNAL, 1L); SETOPT(CURLOPT_CONNECTTIMEOUT, nsoption_uint(curl_fetch_timeout)); if (nsoption_charp(ca_bundle) && strcmp(nsoption_charp(ca_bundle), "")) { LOG("ca_bundle: '%s'", nsoption_charp(ca_bundle)); SETOPT(CURLOPT_CAINFO, nsoption_charp(ca_bundle)); } if (nsoption_charp(ca_path) && strcmp(nsoption_charp(ca_path), "")) { LOG("ca_path: '%s'", nsoption_charp(ca_path)); SETOPT(CURLOPT_CAPATH, nsoption_charp(ca_path)); } /* Detect whether the SSL CTX function API works */ curl_with_openssl = true; code = curl_easy_setopt(fetch_blank_curl, CURLOPT_SSL_CTX_FUNCTION, NULL); if (code != CURLE_OK) { curl_with_openssl = false; } LOG("cURL %slinked against openssl", curl_with_openssl ? "" : "not "); /* cURL initialised okay, register the fetchers */ data = curl_version_info(CURLVERSION_NOW); for (i = 0; data->protocols[i]; i++) { if (strcmp(data->protocols[i], "http") == 0) { scheme = lwc_string_ref(corestring_lwc_http); } else if (strcmp(data->protocols[i], "https") == 0) { scheme = lwc_string_ref(corestring_lwc_https); } else { /* Ignore non-http(s) protocols */ continue; } if (fetcher_add(scheme, &fetcher_ops) != NSERROR_OK) { LOG("Unable to register cURL fetcher for %s", data->protocols[i]); } } return NSERROR_OK; curl_easy_setopt_failed: LOG("curl_easy_setopt failed."); return NSERROR_INIT_FAILED; #if LIBCURL_VERSION_NUM >= 0x071e00 curl_multi_setopt_failed: LOG("curl_multi_setopt failed."); return NSERROR_INIT_FAILED; #endif }
CurlHolder* Session::Impl::newHolder() { CurlHolder* holder = new CurlHolder(); holder->handle = curl_easy_init(); return holder; }
static gpointer download_thread(gpointer data) { CURL *curl = NULL; CURLcode res; FILE *fp = NULL; Task *task = (Task *)data; curl = curl_easy_init(); if(curl == NULL) { gdk_threads_enter(); g_signal_emit(task->downloader, signals[DOWNLOAD_FAILED], 0, task->url, _("curl_easy_init failed")); gdk_threads_leave(); goto _exit; } fp = fopen(task->file, "w"); if (fp == NULL) { gchar *message; message = g_strdup_printf(_("Unable to write file: %s"), task->file); gdk_threads_enter(); g_signal_emit(task->downloader, signals[DOWNLOAD_FAILED], 0, task->url, message); gdk_threads_leave(); g_free(message); goto _exit; } curl_easy_setopt(curl, CURLOPT_URL, task->url); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, (curl_progress_callback)my_progress_func); curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, task); res = curl_easy_perform(curl); if (res == CURLE_OK) { gdk_threads_enter(); g_signal_emit(task->downloader, signals[DOWNLOAD_FINISH], 0, task->url, task->file); gdk_threads_leave(); } else { gchar *message; message = g_strdup_printf(_("curl_easy_perform failed: %s"), curl_easy_strerror(res)); gdk_threads_enter(); g_signal_emit(task->downloader, signals[DOWNLOAD_FAILED], 0, task->url, message); gdk_threads_leave(); g_free(message); } _exit: if (fp) fclose(fp); if (curl) curl_easy_cleanup(curl); g_mutex_lock(task->downloader->priv->mutex); task->downloader->priv->tasks = g_slist_remove(task->downloader->priv->tasks, task); g_mutex_unlock(task->downloader->priv->mutex); g_free(task->url); g_free(task->file); g_free(task); return NULL; }
/* Initialize db->curl */ static int cx_init_curl (cx_t *db) /* {{{ */ { db->curl = curl_easy_init (); if (db->curl == NULL) { ERROR ("curl_xml plugin: curl_easy_init failed."); return (-1); } curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback); curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db); curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf); curl_easy_setopt (db->curl, CURLOPT_URL, db->url); curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L); if (db->user != NULL) { #ifdef HAVE_CURLOPT_USERNAME curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user); curl_easy_setopt (db->curl, CURLOPT_PASSWORD, (db->pass == NULL) ? "" : db->pass); #else size_t credentials_size; credentials_size = strlen (db->user) + 2; if (db->pass != NULL) credentials_size += strlen (db->pass); db->credentials = malloc (credentials_size); if (db->credentials == NULL) { ERROR ("curl_xml plugin: malloc failed."); return (-1); } ssnprintf (db->credentials, credentials_size, "%s:%s", db->user, (db->pass == NULL) ? "" : db->pass); curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials); #endif if (db->digest) curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); } curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L); curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST, db->verify_host ? 2L : 0L); if (db->cacert != NULL) curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert); if (db->headers != NULL) curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers); if (db->post_body != NULL) curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body); #ifdef HAVE_CURLOPT_TIMEOUT_MS if (db->timeout >= 0) curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout); else curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval())); #endif return (0); } /* }}} int cx_init_curl */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, int nCmdShow) { bool isSilentMode = false; FILE *pFile = NULL; bool launchSettingsDlg = false; bool isVerbose = false; string version = ""; if (lpszCmdLine && lpszCmdLine[0]) { launchSettingsDlg = isInList(FLAG_OPTIONS, lpszCmdLine); isVerbose = isInList(FLAG_VERBOSE, lpszCmdLine); version = getParamVal('v', lpszCmdLine); } hInst = hInstance; try { GupParameters gupParams("gup.xml"); GupExtraOptions extraOptions("gupOptions.xml"); GupNativeLang nativeLang("nativeLang.xml"); if (launchSettingsDlg) { if (extraOptions.hasProxySettings()) { proxySrv = extraOptions.getProxyServer(); proxyPort = extraOptions.getPort(); } if (::DialogBox(hInst, MAKEINTRESOURCE(IDD_PROXY_DLG), NULL, reinterpret_cast<DLGPROC>(proxyDlgProc))) extraOptions.writeProxyInfo("gupOptions.xml", proxySrv.c_str(), proxyPort); return 0; } msgBoxTitle = gupParams.getMessageBoxTitle(); abortOrNot = nativeLang.getMessageString("MSGID_ABORTORNOT"); std::string updateInfo; char errorBuffer[CURL_ERROR_SIZE]; // Get your software's current version. // If you pass the version number as the argument // then the version set in the gup.xml will be overrided if (lpszCmdLine && lpszCmdLine[0]) gupParams.setCurrentVersion(lpszCmdLine); // override silent mode if "-isVerbose" is passed as argument if (isVerbose) gupParams.setSilentMode(false); isSilentMode = gupParams.isSilentMode(); // Check on the web the availibility of update // Get the update package's location CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { std::string urlComplete = gupParams.getInfoLocation() + "?version="; if (version != "") urlComplete += version; else urlComplete += gupParams.getCurrentVersion(); curl_easy_setopt(curl, CURLOPT_URL, urlComplete.c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, TRUE); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getUpdateInfo); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &updateInfo); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); if (extraOptions.hasProxySettings()) { curl_easy_setopt(curl, CURLOPT_PROXY, extraOptions.getProxyServer().c_str()); curl_easy_setopt(curl, CURLOPT_PROXYPORT, extraOptions.getPort()); } res = curl_easy_perform(curl); curl_easy_cleanup(curl); } if (res != 0) { if (!isSilentMode) ::MessageBoxA(NULL, errorBuffer, "curl error", MB_OK); return -1; } GupDownloadInfo gupDlInfo(updateInfo.c_str()); if (!gupDlInfo.doesNeed2BeUpdated()) { if (!isSilentMode) { string noUpdate = nativeLang.getMessageString("MSGID_NOUPDATE"); if (noUpdate == "") noUpdate = MSGID_NOUPDATE; ::MessageBoxA(NULL, noUpdate.c_str(), gupParams.getMessageBoxTitle().c_str(), MB_OK); } return 0; } string updateAvailable = nativeLang.getMessageString("MSGID_UPDATEAVAILABLE"); if (updateAvailable == "") updateAvailable = MSGID_UPDATEAVAILABLE; int thirdButtonCmd = gupParams.get3rdButtonCmd(); int buttonStyle = thirdButtonCmd?MB_YESNOCANCEL:MB_YESNO; int dlAnswer = ::MessageBoxA(NULL, updateAvailable.c_str(), gupParams.getMessageBoxTitle().c_str(), buttonStyle); if (dlAnswer == IDNO) { return 0; } if (dlAnswer == IDCANCEL) { if (gupParams.getClassName() != "") { HWND h = ::FindWindowExA(NULL, NULL, gupParams.getClassName().c_str(), NULL); if (h) { ::SendMessage(h, thirdButtonCmd, gupParams.get3rdButtonWparam(), gupParams.get3rdButtonLparam()); } } return 0; } ::CreateThread(NULL, 0, launchProgressBar, NULL, 0, NULL); std::string dlDest = std::getenv("TEMP"); dlDest += "\\"; dlDest += ::PathFindFileNameA(gupDlInfo.getDownloadLocation().c_str()); char *ext = ::PathFindExtensionA(gupDlInfo.getDownloadLocation().c_str()); if (strcmp(ext, ".exe") != 0) dlDest += ".exe"; pFile = fopen(dlDest.c_str(), "wb"); // Download the install package from indicated location curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, gupDlInfo.getDownloadLocation().c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, TRUE); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getDownloadData); curl_easy_setopt(curl, CURLOPT_WRITEDATA, pFile); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, setProgress); curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, hProgressBar); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); if (extraOptions.hasProxySettings()) { curl_easy_setopt(curl, CURLOPT_PROXY, extraOptions.getProxyServer().c_str()); curl_easy_setopt(curl, CURLOPT_PROXYPORT, extraOptions.getPort()); } res = curl_easy_perform(curl); curl_easy_cleanup(curl); } if (res != 0) { if (!isSilentMode) ::MessageBoxA(NULL, errorBuffer, "curl error", MB_OK); if (doAbort) { string dlStopped = nativeLang.getMessageString("MSGID_DOWNLOADSTOPPED"); if (dlStopped == "") dlStopped = MSGID_DOWNLOADSTOPPED; ::MessageBoxA(NULL, dlStopped.c_str(), gupParams.getMessageBoxTitle().c_str(), MB_OK); } return -1; } fflush(pFile); fclose(pFile); pFile = NULL; if (gupParams.getClassName() != "") { HWND h = ::FindWindowExA(NULL, NULL, gupParams.getClassName().c_str(), NULL); if (h) { string msg = gupParams.getClassName(); string closeApp = nativeLang.getMessageString("MSGID_CLOSEAPP"); if (closeApp == "") closeApp = MSGID_CLOSEAPP; msg += closeApp; int installAnswer = ::MessageBoxA(NULL, msg.c_str(), gupParams.getMessageBoxTitle().c_str(), MB_YESNO); if (installAnswer == IDNO) { return 0; } } // kill all process of binary needs to be updated. while (h) { ::SendMessage(h, WM_CLOSE, 0, 0); h = ::FindWindowExA(NULL, NULL, gupParams.getClassName().c_str(), NULL); } } // execute the installer HINSTANCE result = ::ShellExecuteA(NULL, "open", dlDest.c_str(), "", ".", SW_SHOW); if ((unsigned long)result <= 32) // There's a problem (Don't ask me why, ask Microsoft) { return -1; } return 0; } catch (exception ex) { if (!isSilentMode) ::MessageBoxA(NULL, ex.what(), "Xml Exception", MB_OK); if (pFile != NULL) fclose(pFile); return -1; } catch (...) { if (!isSilentMode) ::MessageBoxA(NULL, "Unknown", "Unknown Exception", MB_OK); if (pFile != NULL) fclose(pFile); return -1; } }
struct account_info get_user_info(struct gss_account account, char *source) { char url[100]; sprintf(url, "%s://%s/api/users/show.xml%s", account.protocol, account.server, source); FILE *xml = fopen("temp/file.xml", "wb"); CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_USERPWD, account.user); curl_easy_setopt(curl, CURLOPT_PASSWORD, account.password); curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_xml); curl_easy_setopt(curl, CURLOPT_WRITEDATA, xml); curl_easy_perform(curl); curl_easy_cleanup(curl); fclose(xml); xml = fopen("temp/file.xml", "r"); fseek(xml, 0L, SEEK_END); int filesize = ftell(xml); rewind(xml); char *xml_data = (char *)malloc(filesize); fread(xml_data, filesize, filesize, xml); fclose(xml); char *error = (char *)malloc(512); char *output = (char *)malloc(512); int xml_data_size = strlen(xml_data); struct account_info info; if (parseXml(xml_data, xml_data_size, "<error>", 7, error, 512) > 0) { printf("Error: %s\n", error); info.screen_name[0] = '\0'; } else { if (parseXml(xml_data, xml_data_size, "<name>", 6, output, 512) > 0) { strcpy(info.name, output); } else { info.name[0] = '?'; info.name[1] = '\0'; } if (parseXml(xml_data, xml_data_size, "<screen_name>", 13, output, 512) > 0) { strcpy(info.screen_name, output); } else { info.screen_name[0] = '?'; info.screen_name[1] = '\0'; } if (parseXml(xml_data, xml_data_size, "<location>", 10, output, 512) > 0) { strcpy(info.location, output); } else { info.location[0] = '?'; info.location[1] = '\0'; } if (parseXml(xml_data, xml_data_size, "<description>", 13, output, 512) > 0) { strcpy(info.description, output); } else { info.description[0] = '?'; info.description[1] = '\0'; } if (parseXml(xml_data, xml_data_size, "<url>", 5, output, 512) > 0) { strcpy(info.url, output); } else { info.url[0] = '?'; info.url[1] = '\0'; } if (parseXml(xml_data, xml_data_size, "<followers_count>", 17, output, 512) > 0) { info.followers = atoi(output); } else { info.followers = -1; } if (parseXml(xml_data, xml_data_size, "<friends_count>", 15, output, 512) > 0) { info.friends = atoi(output); } else { info.friends = -1; } if (parseXml(xml_data, xml_data_size, "<statuses_count>", 16, output, 512) > 0) { info.statuses = atoi(output); } else { info.statuses = -1; } } free(output); free(error); free(xml_data); return info; }
int test(char *URL) { CURL *c; CURLM *m; int res = 0; int running; char done = FALSE; struct timeval ml_start; struct timeval mp_start; char ml_timedout = FALSE; char mp_timedout = FALSE; if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { fprintf(stderr, "curl_global_init() failed\n"); return TEST_ERR_MAJOR_BAD; } if ((c = curl_easy_init()) == NULL) { fprintf(stderr, "curl_easy_init() failed\n"); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD; } curl_easy_setopt(c, CURLOPT_PROXY, libtest_arg2); /* set in first.c */ curl_easy_setopt(c, CURLOPT_URL, URL); curl_easy_setopt(c, CURLOPT_USERPWD, "test:ing"); curl_easy_setopt(c, CURLOPT_PROXYUSERPWD, "test:ing"); curl_easy_setopt(c, CURLOPT_HTTPPROXYTUNNEL, 1L); curl_easy_setopt(c, CURLOPT_HEADER, 1L); if ((m = curl_multi_init()) == NULL) { fprintf(stderr, "curl_multi_init() failed\n"); curl_easy_cleanup(c); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD; } if ((res = (int)curl_multi_add_handle(m, c)) != CURLM_OK) { fprintf(stderr, "curl_multi_add_handle() failed, " "with code %d\n", res); curl_multi_cleanup(m); curl_easy_cleanup(c); curl_global_cleanup(); return TEST_ERR_MAJOR_BAD; } ml_timedout = FALSE; ml_start = tutil_tvnow(); while(!done) { fd_set rd, wr, exc; int max_fd; struct timeval interval; interval.tv_sec = 1; interval.tv_usec = 0; if (tutil_tvdiff(tutil_tvnow(), ml_start) > MAIN_LOOP_HANG_TIMEOUT) { ml_timedout = TRUE; break; } mp_timedout = FALSE; mp_start = tutil_tvnow(); while (res == CURLM_CALL_MULTI_PERFORM) { res = (int)curl_multi_perform(m, &running); if (tutil_tvdiff(tutil_tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) { mp_timedout = TRUE; break; } if (running <= 0) { done = TRUE; break; } } if (mp_timedout || done) break; if (res != CURLM_OK) { fprintf(stderr, "not okay???\n"); break; } FD_ZERO(&rd); FD_ZERO(&wr); FD_ZERO(&exc); max_fd = 0; if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) { fprintf(stderr, "unexpected failured of fdset.\n"); res = 89; break; } if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) { fprintf(stderr, "bad select??\n"); res = 95; break; } res = CURLM_CALL_MULTI_PERFORM; } if (ml_timedout || mp_timedout) { if (ml_timedout) fprintf(stderr, "ml_timedout\n"); if (mp_timedout) fprintf(stderr, "mp_timedout\n"); fprintf(stderr, "ABORTING TEST, since it seems " "that it would have run forever.\n"); res = TEST_ERR_RUNS_FOREVER; } curl_multi_remove_handle(m, c); curl_easy_cleanup(c); curl_multi_cleanup(m); curl_global_cleanup(); return res; }
CURLLoader(URLRequest &r) { mState = urlInit; if (!sCurlM) sCurlM = curl_multi_init(); mBytesTotal = -1; mBytesLoaded = 0; mHttpCode = 0; sLoaders++; mHandle = curl_easy_init(); if (!sCurlMap) sCurlMap = new CurlMap; mBufferRemaining = 0; mPutBuffer = 0; mBufferPos = 0; headerlist = NULL; curl_easy_setopt(mHandle, CURLOPT_URL, r.url); /* send all data to this function */ curl_easy_setopt(mHandle, CURLOPT_WRITEFUNCTION, staticOnData); curl_easy_setopt(mHandle, CURLOPT_WRITEDATA, (void *)this); curl_easy_setopt(mHandle, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(mHandle, CURLOPT_FOLLOWLOCATION, 1); if (r.authType!=0) { curl_easy_setopt(mHandle, CURLOPT_HTTPAUTH, r.authType); if (r.credentials && r.credentials[0]) curl_easy_setopt(mHandle, CURLOPT_USERPWD, r.credentials); } curl_easy_setopt(mHandle, CURLOPT_PROGRESSFUNCTION, staticOnProgress); curl_easy_setopt(mHandle, CURLOPT_PROGRESSDATA, (void *)this); curl_easy_setopt(mHandle, CURLOPT_ERRORBUFFER, mErrorBuf ); if (r.debug) curl_easy_setopt(mHandle, CURLOPT_VERBOSE, 1); curl_easy_setopt( mHandle, CURLOPT_COOKIEFILE, "" ); if (r.cookies && r.cookies[0]) curl_easy_setopt( mHandle, CURLOPT_COOKIE, r.cookies ); if (sCACertFile.empty()) curl_easy_setopt(mHandle, CURLOPT_SSL_VERIFYPEER, false); else curl_easy_setopt(mHandle, CURLOPT_CAINFO, sCACertFile.c_str()); if (r.method) { if (!strcmp(r.method,"POST")) { curl_easy_setopt(mHandle, CURLOPT_POST, true); if (r.postData.Ok()) { curl_easy_setopt(mHandle, CURLOPT_POSTFIELDSIZE, r.postData.Size()); curl_easy_setopt(mHandle, CURLOPT_COPYPOSTFIELDS, r.postData.Bytes()); } } else if (!strcmp(r.method,"PUT")) { // The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE. curl_easy_setopt(mHandle, CURLOPT_PUT, true); curl_easy_setopt(mHandle, CURLOPT_UPLOAD, 1); if (r.postData.Ok()) SetPutBuffer(r.postData.Bytes(),r.postData.Size()); } else if (!strcmp(r.method,"GET")) { // GET is the default, so this is not necessary but here for completeness. curl_easy_setopt(mHandle, CURLOPT_HTTPGET, true); } else if (!strcmp(r.method,"DELETE")) { curl_easy_setopt(mHandle, CURLOPT_CUSTOMREQUEST, r.method); } else { // unsupported method !! } } if (r.contentType) { std::vector<char> buffer; buffer.resize(512); snprintf(&buffer[0], buffer.size(), "Content-Type: %s", r.contentType); headerlist = curl_slist_append(headerlist, &buffer[0]); } headerlist = curl_slist_append(headerlist, "Expect:"); int n = r.headers.size(); if (n >= 0) { for(int i = 0; i < n; i++) { URLRequestHeader h = r.headers[i]; std::vector<char> buffer; buffer.resize(512); snprintf(&buffer[0], buffer.size(), "%s: %s", h.name, h.value); headerlist = curl_slist_append(headerlist, &buffer[0]); } } curl_easy_setopt(mHandle, CURLOPT_HTTPHEADER, headerlist); mErrorBuf[0] = '\0'; /* some servers don't like requests that are made without a user-agent field, so we provide one */ curl_easy_setopt(mHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); mState = urlLoading; if (sCurlMap->size()<MAX_ACTIVE) { StartProcessing(); } else { if (sCurlList==0) sCurlList = new CurlList; sCurlList->push_back(this); } }
static int wh_callback_init (wh_callback_t *cb) /* {{{ */ { struct curl_slist *headers; if (cb->curl != NULL) return (0); cb->curl = curl_easy_init (); if (cb->curl == NULL) { ERROR ("curl plugin: curl_easy_init failed."); return (-1); } curl_easy_setopt (cb->curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt (cb->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); headers = NULL; headers = curl_slist_append (headers, "Accept: */*"); if (cb->format == WH_FORMAT_JSON) headers = curl_slist_append (headers, "Content-Type: application/json"); else headers = curl_slist_append (headers, "Content-Type: text/plain"); headers = curl_slist_append (headers, "Expect:"); curl_easy_setopt (cb->curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt (cb->curl, CURLOPT_ERRORBUFFER, cb->curl_errbuf); curl_easy_setopt (cb->curl, CURLOPT_URL, cb->location); if (cb->user != NULL) { size_t credentials_size; credentials_size = strlen (cb->user) + 2; if (cb->pass != NULL) credentials_size += strlen (cb->pass); cb->credentials = (char *) malloc (credentials_size); if (cb->credentials == NULL) { ERROR ("curl plugin: malloc failed."); return (-1); } ssnprintf (cb->credentials, credentials_size, "%s:%s", cb->user, (cb->pass == NULL) ? "" : cb->pass); curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials); curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); } curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, (long) cb->verify_peer); curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYHOST, cb->verify_host ? 2L : 0L); curl_easy_setopt (cb->curl, CURLOPT_SSLVERSION, cb->sslversion); if (cb->cacert != NULL) curl_easy_setopt (cb->curl, CURLOPT_CAINFO, cb->cacert); if (cb->capath != NULL) curl_easy_setopt (cb->curl, CURLOPT_CAPATH, cb->capath); if (cb->clientkey != NULL && cb->clientcert != NULL) { curl_easy_setopt (cb->curl, CURLOPT_SSLKEY, cb->clientkey); curl_easy_setopt (cb->curl, CURLOPT_SSLCERT, cb->clientcert); if (cb->clientkeypass != NULL) curl_easy_setopt (cb->curl, CURLOPT_SSLKEYPASSWD, cb->clientkeypass); } wh_reset_buffer (cb); return (0); } /* }}} int wh_callback_init */
std::string VKBase::parseHTML() { std::string cookiestring = ""; _curl = curl_easy_init(); CURLcode result; std::string buffer = ""; // for response std::string url = ""; // for url char errorBuffer[CURL_ERROR_SIZE]; if(!_curl) { printf("%s\n", "ERROR: curl_easy_init()"); exit(0); } std::string scopes = ""; std::vector<std::string> vscopes = getScopes(); std::for_each(vscopes.begin(), vscopes.end(), [&scopes](std::string scope){ scopes += scope+"&"; }); url = "https://oauth.vk.com/authorize?client_id="; url += getClientId(); url += "&scope="+scopes+"redirect_uri=http://api.vkontakte.ru/blank.html&display=mobile&v=5.24&response_type=token&revoke=1"; curl_easy_setopt(_curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(_curl, CURLOPT_HEADER, 1); curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(_curl, CURLOPT_COOKIEFILE, cookiestring.c_str()); curl_easy_setopt(_curl, CURLOPT_PORT, 443); curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, &VKBase::writer); curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(_curl, CURLOPT_ERRORBUFFER, errorBuffer); result = curl_easy_perform(_curl); curlCheckError(_curl, result, errorBuffer); buffer.clear(); Authorization(); // grant access url = "https://login.vk.com/?act=grant_access&client_id="+getClientId(); url += "&settings=8&redirect_uri=http%3A%2F%2Fapi.vkontakte.ru%2Fblank.html&response_type=token&direct_hash=85bc6b0b250f6f44d1&token_type=0&v=5.24"; url += "&state=&display=mobile"; url += "&ip_h="+_ip_h; url += "&hash="+_hash; url += "&https=1"; curl_easy_setopt(_curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &buffer); result = curl_easy_perform(_curl); curlCheckError(_curl, result, errorBuffer); url = "https://api.vk.com/method/"+getMethod()+"?&access_token="+parseAccessTokenFromHeaders(buffer); buffer.clear(); curl_easy_setopt(_curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(_curl, CURLOPT_HEADER, 0); result = curl_easy_perform(_curl); curlCheckError(_curl, result, errorBuffer); Json::Value root; Json::Reader reader; bool bjsonparse = reader.parse(buffer, root); if(bjsonparse && root["error"].get("error_code", 0).asInt()) { err.b = true; err.msg = root["error"].get("error_msg", "false").asString(); }else{ err.b = false; err.msg = ""; } return buffer; }
int curl_http_file(const char * localpath, const char * remotepath, const char * referer,const char *cookie) { long timeout = 10800; long connect_timeout = 30; long low_speed_limit = 1024; long low_speed_time = 60; FILE *fp; if (!(fp=fopen(localpath, "wb"))){ perror(localpath); return 0; } long response_code; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); //for thread safe curl_easy_setopt(curl, CURLOPT_URL, remotepath); if(referer) curl_easy_setopt(curl, CURLOPT_REFERER, referer); if(cookie) curl_easy_setopt(curl, CURLOPT_COOKIE, cookie); curl_easy_setopt(curl, CURLOPT_USERAGENT, "icache"); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connect_timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, low_speed_limit); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, low_speed_time); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L); curl_easy_setopt(curl, CURLOPT_FILETIME, 1); //curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, deflate"); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); CURLcode rc = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); double len = 0; long filesize = 0; time_t last_modified = 0; curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &len); filesize = len; curl_easy_getinfo(curl, CURLINFO_FILETIME, &last_modified); curl_easy_cleanup(curl); fclose(fp); int success = 1; if (rc!=CURLE_OK || response_code!=200) { success = 0; }else { if (filesize!=-1) { long localsize = 0; struct stat fileinfo; if (stat(localpath, &fileinfo) == 0) localsize = fileinfo.st_size; if (filesize!=localsize) success = 0; } } if (success) { if (last_modified!=-1){ struct utimbuf amtime; amtime.actime = amtime.modtime = last_modified; utime(localpath, &amtime); } return 1; }else{ unlink(localpath); return 0; } }
int youtube_getVideoList(const char *url, youtubeEntryHandler pCallback, int page) { CURLcode ret; CURL *hnd; char proxy[32]; char login[512]; char *str; xmlConfigHandle_t list; xmlConfigHandle_t item; TiXmlNode *entry; TiXmlNode *child; TiXmlElement *elem; const char *video_name, *video_type, *video_url, *thumbnail; static char curl_data[YOUTUBE_LIST_BUFFER_SIZE]; static char err_buff[CURL_ERROR_SIZE]; static curlBufferInfo_t buffer; buffer.data = curl_data; buffer.size = sizeof(curl_data); buffer.pos = 0; if( url == 0 || pCallback == 0 ) return -2; curl_data[0] = 0; hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(hnd, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(hnd, CURLOPT_URL, url); curl_easy_setopt(hnd, CURLOPT_ERRORBUFFER, err_buff); curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 2); curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 1); curl_easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT, 15); curl_easy_setopt(hnd, CURLOPT_TIMEOUT, 15); getParam(BROWSER_CONFIG_FILE, "HTTPProxyServer", "", proxy); if( proxy[0] != 0 ) { curl_easy_setopt(hnd, CURLOPT_PROXY, proxy); getParam(BROWSER_CONFIG_FILE, "HTTPProxyLogin", "", login); if( login[0] != 0 ) { str = &login[strlen(login)+1]; getParam(BROWSER_CONFIG_FILE, "HTTPProxyPasswd", "", str); if( *str != 0 ) { str[-1] = ':'; } curl_easy_setopt(hnd, CURLOPT_PROXYUSERPWD, login); } } if (page == 1) { interface_showLoading(); interface_displayMenu(1); } ret = curl_easy_perform(hnd); dprintf("%s: page %d of '%s' acquired (length %d)\n", __FUNCTION__, page, youtubeInfo.search[0] ? youtubeInfo.search : "standard feeds", buffer.pos); curl_easy_cleanup(hnd); if (ret != 0) { eprintf("Youtube: Failed to get video list from '%s': %s\n", url, err_buff); if (page == 1) interface_hideLoading(); return -1; } list = xmlConfigParse( curl_data ); if (list == NULL || (item = xmlConfigGetElement(list, "feed", 0)) == NULL) { if (list) { eprintf("Youtube: parse error %s\n", ((TiXmlDocument*)list)->ErrorDesc()); xmlConfigClose(list); } if (page == 1)interface_hideLoading(); eprintf("Youtube: Failed to parse video list %d\n", page); return -1; } for ( entry = ((TiXmlNode *)item)->FirstChild(); entry != 0; entry = entry->NextSibling() ) { if (entry->Type() == TiXmlNode::TINYXML_ELEMENT && strcasecmp(entry->Value(), "entry") == 0) { item = xmlConfigGetElement(entry, "media:group", 0); if( item != NULL ) { video_name = (char *)xmlConfigGetText(item, "media:title"); video_url = NULL; thumbnail = NULL; for ( child = ((TiXmlNode *)item)->FirstChild(); child != 0 && (video_url == NULL || thumbnail == NULL); child = child->NextSibling() ) { if (child->Type() == TiXmlNode::TINYXML_ELEMENT ) { if(strcasecmp(child->Value(), "media:content") == 0) { elem = (TiXmlElement *)child; video_type = elem->Attribute("type"); if( strcmp( video_type, "application/x-shockwave-flash" ) == 0) { video_url = elem->Attribute("url"); } } else if(thumbnail== 0 && strcasecmp(child->Value(), "media:thumbnail") == 0) { elem = (TiXmlElement *)child; thumbnail = elem->Attribute("url"); } } } if( video_url != NULL ) { //dprintf("%s: Adding Youtube video '%s' url='%s' thumb='%s'\n", __FUNCTION__,video_name,video_url,thumbnail); pCallback(video_url,video_name,thumbnail); } } } } xmlConfigClose(list); if (page == 1) interface_hideLoading(); return ret; }
char* curl_http_content(const char *uri ,int flag_cookie) { long timeout = 10800; long connect_timeout = 15; long low_speed_limit = 1024; long low_speed_time = 60; struct mem_node *mem_head = NULL; long response_code; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // for thread safe curl_easy_setopt(curl, CURLOPT_URL, uri); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connect_timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, low_speed_limit); curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, low_speed_time); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curl_read); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem_head); if(flag_cookie) { curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"./cookie.txt"); curl_easy_setopt(curl,CURLOPT_COOKIEJAR,"./cookie.txt"); } CURLcode rc = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); curl_easy_cleanup(curl); if (rc!=CURLE_OK) { return NULL; }else if (response_code!=200 && response_code!=206){ struct mem_node *p = mem_head; while(p){ struct mem_node *q = p; p = p->next; free(q->buffer); free(q); } return NULL; }else{ struct mem_node *p = mem_head; size_t size = 0; while(p){ size += p->size; p = p->next; } char *content = (char*)malloc(size+1); p = mem_head; size = 0; while(p){ memcpy(content+size, p->buffer, p->size); size += p->size; struct mem_node *q = p; p = p->next; free(q->buffer); free(q); } content[size] = 0; return content; } }
void* pull_one_url(void *arg_cbget) { if (!arg_cbget) return NULL; cbget *arg = static_cast<cbget*>(arg_cbget); CURL *curl = NULL; if (!arg->_handler) { curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_MAXREDIRS,5); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); // do not check on SSL certificate. } else curl = arg->_handler; curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, arg->_connect_timeout_sec); curl_easy_setopt(curl, CURLOPT_TIMEOUT, arg->_transfer_timeout_sec); curl_easy_setopt(curl, CURLOPT_URL, arg->_url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, arg); if (!arg->_cookies.empty()) curl_easy_setopt(curl, CURLOPT_COOKIE, arg->_cookies.c_str()); if (!arg->_proxy_addr.empty()) { std::string proxy_str = arg->_proxy_addr + ":" + miscutil::to_string(arg->_proxy_port); curl_easy_setopt(curl, CURLOPT_PROXY, proxy_str.c_str()); } if (arg->_http_method == "POST") // POST request. { if (arg->_content) { curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void*)arg->_content->c_str()); if (arg->_content_size >= 0) curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, arg->_content_size); /*std::cerr << "curl_mget POST size: " << arg->_content_size << std::endl << "content: " << *arg->_content << std::endl;*/ } else curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); } else if (arg->_http_method == "PUT" || arg->_http_method == "DELETE") { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, arg->_http_method.c_str()); } struct curl_slist *slist=NULL; // useful headers. if (arg->_headers) { std::list<const char*>::const_iterator sit = arg->_headers->begin(); while (sit!=arg->_headers->end()) { slist = curl_slist_append(slist,(*sit)); ++sit; } if (arg->_content) { slist = curl_slist_append(slist,strdup(arg->_content_type.c_str())); slist = curl_slist_append(slist,strdup("Expect:")); } } curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); char errorbuffer[CURL_ERROR_SIZE]; curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &errorbuffer); try { int status = curl_easy_perform(curl); if (status != 0) // an error occurred. { arg->_status = status; errlog::log_error(LOG_LEVEL_ERROR, "curl error on url %s: %s",arg->_url,errorbuffer); if (arg->_output) { delete arg->_output; arg->_output = NULL; } } } catch (std::exception &e) { arg->_status = -1; errlog::log_error(LOG_LEVEL_ERROR, "Error %s in fetching remote data with curl.", e.what()); if (arg->_output) { delete arg->_output; arg->_output = NULL; } } catch (...) { arg->_status = -2; if (arg->_output) { delete arg->_output; arg->_output = NULL; } } if (!arg->_handler) curl_easy_cleanup(curl); if (slist) curl_slist_free_all(slist); return NULL; }