static char *stringify_field(const struct field *field) { char *str, *name, *type, *value, *intermediate; CURL *curl; curl = curl_easy_init(); if (!curl) return xstrdup(""); name = curl_easy_escape(curl, field->name, 0); type = curl_easy_escape(curl, field->type, 0); if (field->value_encrypted) value = curl_easy_escape(curl, field->value_encrypted, 0); else if (!strcmp(field->type, "checkbox") || !strcmp(field->type, "radio")) { xasprintf(&intermediate, "%s-%c", field->value, field->checked ? '1' : '0'); value = curl_easy_escape(curl, intermediate, 0); free(intermediate); } else value = curl_easy_escape(curl, field->value, 0); xasprintf(&str, "0\t%s\t%s\t%s\n", name, value, type); curl_free(name); curl_free(type); curl_free(value); curl_easy_cleanup(curl); return str; }
TEST(FileTransfer, DetectsIncorrectUploadFilePath) { CURL *curl; std::string source_file = "/accounts/1000/shared/camera/abcdefg.hij"; std::string target_url = "http://bojap.com/omg/uploader.php"; std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source_file.c_str(), 0), 0)); std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target_url.c_str(), 0), 0)); std::string expected = "upload error 1 " + source_escaped + " " + target_escaped + " 0"; webworks::FileUploadInfo upload_info; upload_info.sourceFile = source_file; upload_info.targetURL = target_url; upload_info.mimeType = "image/jpeg"; upload_info.fileKey = "image"; upload_info.fileName = "new_image.jpg"; upload_info.chunkedMode = 1; webworks::FileTransferCurl file_transfer; std::string result = file_transfer.Upload(&upload_info); EXPECT_EQ(expected, result); }
TEST(FileTransfer, DetectsUploadConnectionError) { CURL *curl; std::string source_file = "/accounts/1000/shared/documents/filetransfer_test.txt"; std::string target_url = "http://127.0.0.1/uploader.php"; std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source_file.c_str(), 0), 0)); std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target_url.c_str(), 0), 0)); std::string expected = "upload error 3 " + source_escaped + " " + target_escaped + " 0"; int file_result = createTestFile(source_file.c_str()); EXPECT_EQ(0, file_result); webworks::FileUploadInfo upload_info; upload_info.sourceFile = source_file; upload_info.targetURL = target_url; upload_info.mimeType = "text/plain"; upload_info.fileKey = "file"; upload_info.fileName = "test_file.txt"; upload_info.chunkedMode = 0; webworks::FileTransferCurl file_transfer; std::string result = file_transfer.Upload(&upload_info); EXPECT_EQ(expected, result); remove(source_file.c_str()); }
PCS_API PcsBool pcs_http_form_addbuffer(PcsHttp handle, PcsHttpForm *post, const char *param_name, const char *buffer, long buffer_size, const char *simulate_filename) { struct pcs_http *http = (struct pcs_http *)handle; struct http_post *formpost = (struct http_post *)(*post); char *escape_param_name, *escape_simulate_filename; PcsBool res = PcsTrue; if (!formpost) { formpost = (struct http_post *)pcs_malloc(sizeof(struct http_post)); if (!formpost) return PcsFalse; memset(formpost, 0, sizeof(struct http_post)); (*post) = (PcsHttpForm)formpost; } escape_param_name = curl_easy_escape(http->curl, param_name, 0); escape_simulate_filename = simulate_filename ? curl_easy_escape(http->curl, simulate_filename, 0) : NULL; if (curl_formadd(&(formpost->formpost), &(formpost->lastptr), CURLFORM_COPYNAME, escape_param_name, CURLFORM_BUFFER, escape_simulate_filename ? escape_simulate_filename : "part", CURLFORM_BUFFERPTR, buffer, CURLFORM_BUFFERLENGTH, buffer_size, CURLFORM_END)) res = PcsFalse; curl_free((void *)escape_param_name); if (escape_simulate_filename) curl_free((void *)escape_simulate_filename); return res; }
PCS_API PcsBool pcs_http_form_addbufferfile(PcsHttp handle, PcsHttpForm *post, const char *param_name, const char *simulate_filename, size_t(*read_func)(void *ptr, size_t size, size_t nmemb, void *userdata), void *userdata, size_t content_size) { struct pcs_http *http = (struct pcs_http *)handle; struct http_post *formpost = (struct http_post *)(*post); char *escape_param_name, *escape_simulate_filename; PcsBool res = PcsTrue; if (!formpost) { formpost = (struct http_post *)pcs_malloc(sizeof(struct http_post)); if (!formpost) return PcsFalse; memset(formpost, 0, sizeof(struct http_post)); (*post) = (PcsHttpForm)formpost; } escape_param_name = curl_easy_escape(http->curl, param_name, 0); escape_simulate_filename = simulate_filename ? curl_easy_escape(http->curl, simulate_filename, 0) : NULL; if (curl_formadd(&(formpost->formpost), &(formpost->lastptr), CURLFORM_COPYNAME, escape_param_name, CURLFORM_STREAM, userdata, CURLFORM_CONTENTSLENGTH, (long) content_size, CURLFORM_FILENAME, escape_simulate_filename ? escape_simulate_filename : "part", CURLFORM_END)) { res = PcsFalse; } curl_free((void *)escape_param_name); if (escape_simulate_filename) curl_free((void *)escape_simulate_filename); formpost->read_func = read_func; formpost->read_func_data = userdata; return res; }
PCS_API PcsBool pcs_http_form_addfile(PcsHttp handle, PcsHttpForm *post, const char *param_name, const char *filename, const char *simulate_filename) { struct pcs_http *http = (struct pcs_http *)handle; struct http_post *formpost = (struct http_post *)(*post); char *escape_param_name, *escape_simulate_filename; PcsBool res = PcsTrue; if (!formpost) { formpost = (struct http_post *)pcs_malloc(sizeof(struct http_post)); if (!formpost) return PcsFalse; memset(formpost, 0, sizeof(struct http_post)); (*post) = (PcsHttpForm)formpost; } escape_param_name = curl_easy_escape(http->curl, param_name, 0);//pcs_http_escape(handle, param_name); escape_simulate_filename = curl_easy_escape(http->curl, simulate_filename, 0);//pcs_http_escape(handle, simulate_filename); if (curl_formadd(&(formpost->formpost), &(formpost->lastptr), CURLFORM_COPYNAME, escape_param_name, CURLFORM_FILE, filename, CURLFORM_FILENAME, escape_simulate_filename, CURLFORM_END)) { res = PcsFalse; } curl_free((void *)escape_param_name);//pcs_free(escape_param_name); curl_free((void *)escape_simulate_filename);//pcs_free(escape_simulate_filename); return res; }
/** * Forge real URL to the API using given client config and parameters */ int influxdb_client_get_url_with_credential(s_influxdb_client *client, char (*buffer)[], size_t size, char *path, char *username, char *password) { char *username_enc = curl_easy_escape(NULL, username, 0); char *password_enc = curl_easy_escape(NULL, password, 0); (*buffer)[0] = '\0'; strncat(*buffer, client->schema, size); strncat(*buffer, "://", size); strncat(*buffer, client->host, size); strncat(*buffer, path, size); if (strchr(path, '?')) strncat(*buffer, "&", size); else strncat(*buffer, "?", size); strncat(*buffer, "u=", size); strncat(*buffer, username_enc, size); strncat(*buffer, "&p=", size); strncat(*buffer, password_enc, size); free(username_enc); free(password_enc); return strlen(*buffer); }
bool DumbRelayConsumer::check_authentication(const std::multimap<std::string, std::string> ¶ms) { // Construct the data for the post. std::ostringstream data; CURL *curl = new_curl_handle(); data << "openid.mode=check_authentication"; for(std::multimap<std::string, std::string>::const_iterator iter = params.begin(); iter != params.end(); ++iter) { if(iter->first.compare("openid.mode") == 0) continue; char *tmp; tmp = curl_easy_escape(curl, iter->first.c_str(), iter->first.size()); data << "&" << tmp << "="; curl_free(tmp); tmp = curl_easy_escape(curl, iter->second.c_str(), iter->second.size()); data << tmp; curl_free(tmp); } // Clean up after curl. curl_easy_cleanup(curl); // Prepare the curl handle. std::string content = contact_openid_provider(data.str()); // Check to see if we can find the is_valid:true response. return content.find("\nis_valid:true\n") != content.npos; }
int t3net_add_argument(T3NET_ARGUMENTS * arguments, const char * key, const char * val) { CURL * curl; if(arguments->count < T3NET_MAX_ARGUMENTS) { curl = curl_easy_init(); if(!curl) { return 0; } arguments->key[arguments->count] = curl_easy_escape(curl, key, 0); if(!arguments->key[arguments->count]) { return 0; } arguments->val[arguments->count] = curl_easy_escape(curl, val, 0); if(!arguments->val[arguments->count]) { curl_free(arguments->key[arguments->count]); return 0; } arguments->count++; return 1; } return 0; }
static int get_string_envs(CURL *curl, const char *required_env, char *querystring) { char *data = NULL; char *escaped_key = NULL; char *escaped_val = NULL; char *env_string = NULL; char *params_key[MAXPARAMSNUM]; char *env_names[MAXPARAMSNUM]; char *env_value[MAXPARAMSNUM]; int i, num = 0; //_log(LOG_DEBUG, "sys_envs=%s", sys_envs); env_string = (char *)malloc( strlen(required_env) + 20); if (env_string == NULL) { _fatal("ENOMEM"); return (-1); } sprintf(env_string, "%s", required_env); //_log(LOG_DEBUG, "env_string=%s", env_string); num = get_sys_envs(env_string, ",", "=", params_key, env_names, env_value); //sprintf(querystring, ""); for( i = 0; i < num; i++ ){ escaped_key = curl_easy_escape(curl, params_key[i], 0); escaped_val = curl_easy_escape(curl, env_value[i], 0); //_log(LOG_DEBUG, "key=%s", params_key[i]); //_log(LOG_DEBUG, "escaped_key=%s", escaped_key); //_log(LOG_DEBUG, "escaped_val=%s", escaped_envvalue); data = (char *)malloc(strlen(escaped_key) + strlen(escaped_val) + 1); if ( data == NULL ) { _fatal("ENOMEM"); return (-1); } sprintf(data, "%s=%s&", escaped_key, escaped_val); if ( i == 0 ) { sprintf(querystring, "%s", data); } else { strcat(querystring, data); } } if (data) free(data); if (escaped_key) free(escaped_key); if (escaped_val) free(escaped_val); free(env_string); return (num); }
void curl_post(std::string &sURL, std::string &postData){ CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { /* to keep the response */ std::stringstream responseData; char* safeurl = curl_easy_escape(curl, sURL.c_str(), 0); //hostname, api key, machine id, validShare, shareValue, rejectReason curl_easy_setopt(curl, CURLOPT_URL, safeurl); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_POST, 1); char* safepostData = curl_easy_escape(curl, postData.c_str(), 0); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, safepostData); /* setting a callback function to return the data */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_callback_func); /* passing the pointer to the response as the callback parameter */ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData); //curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ //if(res != CURLE_OK) // fprintf(stderr, "curl_easy_perform() failed: %s\n", // curl_easy_strerror(res)); /* always cleanup */ curl_free(safeurl); curl_free(safepostData); curl_easy_cleanup(curl); std::cout << std::endl << "Reponse from server: " << responseData.str() << std::endl; } curl_global_cleanup(); }
BOOL CUnBindDeviceHttpCMD::Init() { CNGString strTemp; char *efc = NULL; m_strfields.append("_ch="); strTemp = AUTH_TYPE_MAXNET; efc = curl_easy_escape(m_pEasyHandle, strTemp.c_str(), 0); m_strfields.append(efc); curl_free(efc); m_strfields.append("&deviceid="); strTemp = stReq.aszDeviceID; efc = curl_easy_escape(m_pEasyHandle, strTemp.c_str(), 0); m_strfields.append(efc); curl_free(efc); m_strfields.append("&username="******"%02x", (UINT8)abyDigest[byIdx]); strcat(szHexDigest, szTemp); } strTempUrl.append("?_sig="); strTemp = szHexDigest; efc = curl_easy_escape(m_pEasyHandle, strTemp.c_str(), 0); strTempUrl.append(efc); curl_free(efc); curl_easy_setopt(m_pEasyHandle, CURLOPT_URL, strTempUrl.c_str()); curl_easy_setopt(m_pEasyHandle, CURLOPT_ERRORBUFFER, m_szErrorBuff); curl_easy_setopt(m_pEasyHandle, CURLOPT_WRITEDATA, this); curl_easy_setopt(m_pEasyHandle, CURLOPT_POST, 1 ); curl_easy_setopt(m_pEasyHandle, CURLOPT_TIMEOUT, 2); curl_easy_setopt(m_pEasyHandle, CURLOPT_POSTFIELDS, m_strfields.c_str()); return TRUE; }
pb_status pb_getUserSessionKey( pastebin* _pb, char* _username, char* _password ) { // note that we don't store the username or password; just the key after it completes successfully. debugf( "Entering function with argument %s: %s, %s: %s\n", stringify( _username ), _username, stringify( _password ), _password ); if( !_username ) { debugf( "username is NULL!\n" ); return STATUS_USERNAME_IS_NULL; } if( !_password ) { debugf( "password is NULL!\n" ); return STATUS_PASSWORD_IS_NULL; } char* argu = (char*)malloc( sizeof(char)*1024 ); // 1kb should suffice. If it doesn't, your password is too long, bro char* user = (char*)malloc( sizeof(char)*strlen(_username)*3 ); char* pass = (char*)malloc( sizeof(char)*strlen(_password)*3 ); CURL* curl = curl_easy_init(); user = curl_easy_escape( curl, _username, strlen( _username ) ); pass = curl_easy_escape( curl, _password, strlen( _password ) ); struct pb_memblock chunk; chunk.memory = malloc(1); chunk.size = 0; curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, pb_memcopy ); curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void*)&chunk ); curl_easy_setopt( curl, CURLOPT_URL, PB_API_LOGIN_URL ); curl_easy_setopt( curl, CURLOPT_POST, 1 ); sprintf( argu, "api_dev_key=%s&api_user_name=%s&api_user_password=%s", _pb->devkey, user, pass ); debugf( "Curl postfields:\n%s\n", argu ); curl_easy_setopt( curl, CURLOPT_POSTFIELDS, argu ); curl_easy_setopt( curl, CURLOPT_NOBODY, 0 ); curl_easy_perform( curl ); debugf( "user key is:\n%s\n", chunk.memory ); _pb->userkey = chunk.memory; debugf( "Exiting function\n" ); return STATUS_OKAY; }
wxThread::ExitCode PastebinTask::TaskStart() { SetStatus(_("Sending to pastebin...")); // Create handle CURL *curl = curl_easy_init(); char errBuffer[CURL_ERROR_SIZE]; // URL encode wxCharBuffer contentBuf = m_content.ToUTF8(); char *content = curl_easy_escape(curl, contentBuf.data(), strlen(contentBuf)); wxCharBuffer posterBuf = m_author.ToUTF8(); char *poster = curl_easy_escape(curl, posterBuf.data(), strlen(posterBuf)); wxString postFields; postFields << "poster=" << poster << "&syntax=text&content=" << content; curl_easy_setopt(curl, CURLOPT_URL, "http://paste.ubuntu.com/"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlLambdaCallback); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cStr(postFields)); curl_easy_setopt(curl, CURLOPT_HEADER, true); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &errBuffer); wxString outString; wxStringOutputStream outStream(&outString); CurlLambdaCallbackFunction curlWrite = [&] (void *buffer, size_t size) -> size_t { outStream.Write(buffer, size); return outStream.LastWrite(); }; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curlWrite); int status = curl_easy_perform(curl); if (status == 0) { // Parse the response header for the redirect location. m_pasteURL = outString.Mid(outString.Find("Location: ") + 10); m_pasteURL = m_pasteURL.Mid(0, m_pasteURL.Find('\n')); return (ExitCode)1; } else { EmitErrorMessage(wxString::Format("Pastebin failed: %s", errBuffer)); return (ExitCode)0; } }
BOOL CNameHttpCMD::Init() { CNGString strTemp; char *efc = NULL; m_strfields.append("playerid="); strTemp = stNameInfo.dwPlayerID; efc = curl_easy_escape(m_pEasyHandle, strTemp.c_str(), 0); m_strfields.append(efc); curl_free(efc); m_strfields.append("&name="); strTemp = stNameInfo.strName; efc = curl_easy_escape(m_pEasyHandle, strTemp.c_str(), 0); m_strfields.append(efc); curl_free(efc); m_strfields.append("&_ch="); strTemp = stNameInfo.byAuthType; efc = curl_easy_escape(m_pEasyHandle, strTemp.c_str(), 0); m_strfields.append(efc); curl_free(efc); //地址 string strTempUrl = m_strUrl; UINT8 abyDigest[16] = {0}; string strInput = m_strfields; strInput.append(CENTER_SIGN); SDMD5(abyDigest, (UINT8*)strInput.c_str(), strInput.length()); CHAR szTemp[32] = {0}; CHAR szHexDigest[256] = {0}; for (UINT8 byIdx = 0; byIdx < 16; byIdx++) { sprintf(szTemp, "%02x", (UINT8)abyDigest[byIdx]); strcat(szHexDigest, szTemp); } strTempUrl.append("?_sig="); strTemp = szHexDigest; efc = curl_easy_escape(m_pEasyHandle, strTemp.c_str(), 0); strTempUrl.append(efc); curl_free(efc); curl_easy_setopt(m_pEasyHandle, CURLOPT_URL, strTempUrl.c_str()); curl_easy_setopt(m_pEasyHandle, CURLOPT_ERRORBUFFER, m_szErrorBuff); curl_easy_setopt(m_pEasyHandle, CURLOPT_WRITEDATA, this); curl_easy_setopt(m_pEasyHandle, CURLOPT_POST, 1 ); curl_easy_setopt(m_pEasyHandle, CURLOPT_POSTFIELDS, m_strfields.c_str()); return TRUE; }
inline std::string urlEncode(CURL* curl, const std::string text) { char* enc = curl_easy_escape(curl, text.c_str(), text.size()); std::string res(enc); curl_free(enc); return res; }
std::string AssociatedRelayConsumer::checkid_setup(const std::string &return_to, const std::string &trust_root) { // Try to find our assoc handle. const std::string *assoc_handle_ptr = lookup_assoc_handle(openid_provider()); if(!assoc_handle_ptr) assoc_handle_ptr = associate(); // If the handle is "DUMB", we have to fall back to the dumb consumer. if(assoc_handle_ptr->compare("DUMB") == 0) { delete assoc_handle_ptr; return DumbRelayConsumer::checkid_setup(return_to, trust_root); } // Move the string to the stack, so that return can free it. std::string assoc_handle(*assoc_handle_ptr); delete assoc_handle_ptr; // construct check_id url. std::string redirect_url(DumbRelayConsumer::checkid_setup(return_to, trust_root)); // cURL handle used for escaping. CURL *curl = new_curl_handle(); redirect_url.append("&openid.assoc_handle="); char *tmp = curl_easy_escape(curl, assoc_handle.c_str(), assoc_handle.size()); redirect_url.append(tmp); curl_free(tmp); // Clean up after curl. curl_easy_cleanup(curl); return redirect_url; }
std::string Curl::escape(const std::string &s) { char *cs = curl_easy_escape(0, s.c_str(), s.length()); std::string result(cs); curl_free(cs); return result; }
void * trans_thread(void *unuse){ CURLcode retcode; CURL *curl = curl_easy_init(); if(curl==NULL){ fprintf(stderr,"translate failed_1\n"); pthread_exit("-1"); } char buf[512],ret_buf[MAXBUF*2],*convert_str; sprintf(buf, "%s", APIkey); convert_space(str_to_trans); convert_str = curl_easy_escape(curl,str_to_trans,strlen(str_to_trans)); strcat(buf, convert_str); curl_easy_setopt(curl, CURLOPT_URL, buf); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret_buf); retcode = curl_easy_perform(curl); curl_easy_cleanup(curl); if(retcode != 0){ fprintf(stderr,"translate failed_2\n"); pthread_exit("-1"); } cJSON *json; json = cJSON_Parse(ret_buf); if(create_trans(&trans_res, json) == -1){ fprintf(stderr,"translate failed_3\n"); pthread_exit("-1"); } if(simple_flag == 1) clprint_simple(&trans_res); else clprint_full(&trans_res); curl_free(convert_str); pthread_exit("0"); }
void send_status(struct gss_account account, char *msg) { /* cURL functionality used just to URIencode the msg */ CURL *curl = curl_easy_init(); if(curl) { char *encoded_msg = curl_easy_escape(curl, msg, strlen(msg)); if(encoded_msg) { int amount = 31+strlen(encoded_msg); char *send = malloc(amount); snprintf(send, amount, "source=GnuSocialShell&status=%s", encoded_msg); if (loglevel >= LOG_DEBUG) { // OK? fprintf(stderr, "source=GnuSocialShell&status=%s", encoded_msg); } char *xml_data = send_to_api(account, send, "statuses/update.xml"); int xml_data_size = strlen(xml_data); if (FindXmlError(xml_data, strlen(xml_data)) < 0 && parseXml(xml_data, xml_data_size, "</status>", 9, NULL, 0) > 0) { struct status posted_status; posted_status = makeStatusFromRawSource(xml_data, xml_data_size); print_status(posted_status); } free(xml_data); free(send); curl_free(encoded_msg); } } }
char * twid_normalize_tweet(char *tweet_raw_bytes){ CURL *curl; curl = curl_easy_init(); char *tweet_post_body = NULL; /* considering the utf-8 encoding where one character takes 3 bytes */ /* the memory needed should be 140 * 3 = 420 bytes */ char *tweet_truncated = (char *)malloc(TWEET_MAX_LEN * 3 + 1); strncpy(tweet_truncated, tweet_raw_bytes, TWEET_MAX_LEN * 3); /* escape the post body */ char *tweet_escaped = curl_easy_escape(curl, tweet_truncated, 0); if (tweet_escaped){ int tweet_length = strlen(tweet_escaped); /* allocate memory for store the whole POST body */ tweet_post_body = (char *)malloc(tweet_length + 7); memset(tweet_post_body, 0, tweet_length + 7); /* a safe cast of string duplication */ strncpy(tweet_post_body, "status=", 7); strncat(tweet_post_body, tweet_escaped, tweet_length); /* concat the two strings */ } curl_easy_cleanup(curl); return tweet_post_body; }
void HttpRequest::escape( const std::string& sIn, std::string& sOut ) { sOut.clear(); char *efc = curl_easy_escape(curl_, sIn.c_str(), sIn.size()); sOut.append(efc); curl_free(efc); }
std::string UrlEncode(const std::string& value) { char* encoded = curl_easy_escape(handle_, value.c_str(), (int)value.length()); std::string retval (encoded); curl_free(encoded); return retval; }
static package bf_urlencode( Var arglist, Byte next, void *vdata, Objid progr) { Var r; CURL *curl_handle; const char *string = arglist.v.list[1].v.str; free_var(arglist); curl_global_init(CURL_GLOBAL_ALL); curl_handle = curl_easy_init(); r.type = TYPE_STR; char *output = curl_easy_escape(curl_handle,string,0); if(output == NULL) { curl_free(output); return make_error_pack(E_INVARG); } r.v.str = str_dup(output); curl_free(output); curl_easy_cleanup(curl_handle); curl_global_cleanup(); return make_var_pack(r); }
int main(int argc, char *argv[]) { FILE* stream = fopen(argv[1], "r"); bencoding_t* metainfo = read_bencoding(stream); fclose(stream); char info_hash[] = "12345678901234567890"; bencoding_t* ann = find_in_dict(metainfo, "announce"); char url[1024]; memcpy(url, ann->data.b_string.buffer, ann->data.b_string.size); url[ann->data.b_string.size] = '\0'; CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); char* hash = curl_easy_escape(curl, info_hash, 20); char req_string[1024]; snprintf(req_string, 1024, "%s?info_hash=%s&peer_id=%s&port=%d&downloaded=0&uploaded=0&left=1234789", url, hash, peer_id, 6881); puts(req_string); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, req_string); res = curl_easy_perform(curl); if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } destroy_bencoding(metainfo); return 0; }
/* 删除目录/文件 */ void BaiduPCS_Remove(BaiduPCS *api, const char *remote_file) { //{{{ HttpClient *client = api->client; char *url_buffer = api->util_buffer0; const char *token = api->token; const char *error = NULL; const char *response = NULL; cJSON *json = NULL; //需要释放 cJSON *item = NULL; char *path_encode = NULL; BaiduPCS_ResetError(api); path_encode = curl_easy_escape(client->curl, remote_file, 0); sprintf(url_buffer, "https://pcs.baidu.com/rest/2.0/pcs/file?" "access_token=%s" "&method=delete" "&path=%s", token, path_encode); curl_free(path_encode); path_encode = NULL; #ifdef DEBUG fprintf(stderr, "reqeust %s\n", url_buffer); #endif HttpClient_Init(client); HttpClient_PostHttpData(client, url_buffer, NULL); MAKE_JSON(); free: if (json != NULL) { cJSON_Delete(json); } }
/* THIS WON'T WORK WITH SPACES */ char * add_field(CURL *handle, char *postdata, char *field, char *unencoded) { int ampersand; char *encoded; ampersand = strlen(postdata) == 0 ? 0 : 1; if (ampersand) { postdata = realloc(postdata, strlen(postdata) + strlen(field) + 2); } else { postdata = realloc(postdata, strlen(postdata) + strlen(field) + 1); } if (postdata == NULL) { perror("realloc()"); exit(EXIT_FAILURE); } if (ampersand) { strcat(postdata, "&"); } strcat(postdata, field); encoded = curl_easy_escape(handle, unencoded, 0); if (encoded == NULL) { perror("curl_easy_escape()"); exit(EXIT_FAILURE); } postdata = realloc(postdata, strlen(postdata) + strlen(encoded) + 1); if (postdata == NULL) { perror("realloc()"); exit(EXIT_FAILURE); } strcat(postdata, encoded); curl_free(encoded); return postdata; }
char* debug_curl_easy_escape(CURL* curl, char* url, int length) { char* ret; ret = curl_easy_escape(curl, url, length); Dout(dc::curl, "curl_easy_escape(" << (AICURL*)curl << ", \"" << url << "\", " << length << ") = \"" << ret << '"'); return ret; }
int GetRecordingsForTitleUsingURL(const std::string& title, Json::Value& response) { int retval = E_FAILED; XBMC->Log(LOG_DEBUG, "GetRecordingsForTitleUsingURL"); CURL *curl; curl = curl_easy_init(); if(curl) { std::string command = "ForTheRecord/Control/RecordingsForProgramTitle/Television/"; char* pch = curl_easy_escape(curl, title.c_str(), 0); command += pch; curl_free(pch); retval = ForTheRecord::ForTheRecordJSONRPC(command, "?includeNonExisting=false", response); if(retval >= 0) { if (response.type() != Json::arrayValue) { retval = E_FAILED; XBMC->Log(LOG_NOTICE, "GetRecordingsForTitleUsingURL did not return a Json::arrayValue [%d].", response.type()); } } else { XBMC->Log(LOG_NOTICE, "GetRecordingsForTitleUsingURL remote call failed."); } curl_easy_cleanup(curl); } return retval; }
int main(int argc, char *argv[]) { char *p, out[LEN], cur[LEN]; int i=0; /* check to make sure an arg is provided */ if (argc < 2) { fprintf(stderr, "%s\n", "no URL provided"); return 1; } /* tokenize on '/' */ for (p = strtok(argv[1], "/"); p; p = strtok(NULL, "/")) { sprintf(cur, "%s", p); /* for first iteration, do not escape, and append an extra '/' if it ends with ':' to supply the protocol */ if (!i++) { if (cur[strlen(cur) - 1] == ':') { sprintf(out, "%s/", cur); } else { sprintf(out, "%s", p); } continue; } /* escape string, append to out */ sprintf(out, "%s/%s", out, curl_easy_escape(NULL, cur, 0)); } /* print the damn thing */ printf("%s\n", out); return 0; }