Exemple #1
0
static void curl_set_handle_opts(struct dload_payload *payload,
		CURL *curl, char *error_buffer)
{
	alpm_handle_t *handle = payload->handle;
	const char *useragent = getenv("HTTP_USER_AGENT");
	struct stat st;

	/* the curl_easy handle is initialized with the alpm handle, so we only need
	 * to reset the handle's parameters for each time it's used. */
	curl_easy_reset(curl);
	curl_easy_setopt(curl, CURLOPT_URL, payload->fileurl);
	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
	curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, dload_progress_cb);
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, (void *)payload);
	curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
	curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 10L);
	curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, dload_parseheader_cb);
	curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)payload);
	curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
	curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, dload_sockopt_cb);
	curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, (void *)handle);
	curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

	_alpm_log(handle, ALPM_LOG_DEBUG, "url: %s\n", payload->fileurl);

	if(payload->max_size) {
		_alpm_log(handle, ALPM_LOG_DEBUG, "maxsize: %jd\n",
				(intmax_t)payload->max_size);
		curl_easy_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
				(curl_off_t)payload->max_size);
	}

	if(useragent != NULL) {
		curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent);
	}

	if(!payload->allow_resume && !payload->force && payload->destfile_name &&
			stat(payload->destfile_name, &st) == 0) {
		/* start from scratch, but only download if our local is out of date. */
		curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
		curl_easy_setopt(curl, CURLOPT_TIMEVALUE, (long)st.st_mtime);
		_alpm_log(handle, ALPM_LOG_DEBUG,
				"using time condition: %lu\n", (long)st.st_mtime);
	} else if(stat(payload->tempfile_name, &st) == 0 && payload->allow_resume) {
		/* a previous partial download exists, resume from end of file. */
		payload->tempfile_openmode = "ab";
		curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)st.st_size);
		_alpm_log(handle, ALPM_LOG_DEBUG,
				"tempfile found, attempting continuation from %jd bytes\n",
				(intmax_t)st.st_size);
		payload->initial_size = st.st_size;
	}
}
Exemple #2
0
void ds3_connection_release(ds3_connection_pool* pool, ds3_connection* connection) {
    g_mutex_lock(&pool->mutex);

    curl_easy_reset(connection);
    pool->tail = _pool_inc(pool, pool->tail);

    g_mutex_unlock(&pool->mutex);
    g_cond_signal(&pool->available_connections);
}
Exemple #3
0
    void
    _ResetHandle() {
        curl_easy_reset(handle_);
#ifdef  DEBUG
        curl_easy_setopt(handle_, CURLOPT_VERBOSE, 1L);
#ifdef  CRAZY_VERBOSE
        curl_easy_setopt(handle_, CURLOPT_DEBUGFUNCTION, _CurlTrace);
#endif
#endif
    }
Exemple #4
0
json_t* authRequest(Parameters& params, std::string url, std::string signature, std::string content) {
  unsigned char digest[MD5_DIGEST_LENGTH];
  MD5((unsigned char*)signature.c_str(), strlen(signature.c_str()), (unsigned char*)&digest);
  char mdString[33];
  for (int i = 0; i < 16; i++) {
    sprintf(&mdString[i*2], "%02X", (unsigned int)digest[i]);
  }
  std::ostringstream oss;
  oss << content << "&sign=" << mdString;
  std::string postParameters = oss.str().c_str();
  struct curl_slist* headers = NULL;
  headers = curl_slist_append(headers, "contentType: application/x-www-form-urlencoded");
  CURLcode resCurl;
  if (params.curl) {
    curl_easy_setopt(params.curl, CURLOPT_POST,1L);
    curl_easy_setopt(params.curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(params.curl, CURLOPT_POSTFIELDS, postParameters.c_str());
    curl_easy_setopt(params.curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(params.curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    std::string readBuffer;
    curl_easy_setopt(params.curl, CURLOPT_WRITEDATA, &readBuffer);
    curl_easy_setopt(params.curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(params.curl, CURLOPT_CONNECTTIMEOUT, 10L);
    resCurl = curl_easy_perform(params.curl);
    json_t* root;
    json_error_t error;
    while (resCurl != CURLE_OK) {
      *params.logFile << "<OKCoin> Error with cURL. Retry in 2 sec..." << std::endl;
      sleep(4.0);
      resCurl = curl_easy_perform(params.curl);
    }
    root = json_loads(readBuffer.c_str(), 0, &error);
    while (!root) {
      *params.logFile << "<OKCoin> Error with JSON:\n" << error.text << std::endl;
      *params.logFile << "<OKCoin> Buffer:\n" << readBuffer.c_str() << std::endl;
      *params.logFile << "<OKCoin> Retrying..." << std::endl;
      sleep(4.0);
      readBuffer = "";
      resCurl = curl_easy_perform(params.curl);
      while (resCurl != CURLE_OK) {
        *params.logFile << "<OKCoin> Error with cURL. Retry in 2 sec..." << std::endl;
        sleep(4.0);
        readBuffer = "";
        resCurl = curl_easy_perform(params.curl);
      }
      root = json_loads(readBuffer.c_str(), 0, &error);
    }
    curl_slist_free_all(headers);
    curl_easy_reset(params.curl);
    return root;
  } else {
    *params.logFile << "<OKCoin> Error with cURL init." << std::endl;
    return NULL;
  }
}
qeo_mgmt_client_retcode_t qeo_mgmt_curl_util_https_get_with_cb(qeo_mgmt_client_ctx_t *mg_ctx,
                                                     const char* url,
                                                     char *header,
                                                     qeo_mgmt_client_ssl_ctx_cb ssl_cb,
                                                     void *ssl_cookie,
                                                     curl_write_callback data_cb,
                                                     void *write_cookie)
{
    curl_ssl_ctx_helper curlsslhelper = { ssl_cb, ssl_cookie };
    qeo_mgmt_client_retcode_t ret = QMGMTCLIENT_EFAIL;
    const char *cafile = NULL;
    const char *capath = NULL;
    //Setting CURLOPT_CAINF and PATH is mandatory; otherwise SSL_CTX_FUNCTION will not be called.
    curl_opt_helper opts[] = {
        { CURLOPT_SSL_VERIFYHOST, (void*)2 }, /* ensure certificate matches host */
        { CURLOPT_SSL_VERIFYPEER, (void*)0 }, /* ensure certificate is valid */
        { CURLOPT_CAINFO, NULL },
        { CURLOPT_CAPATH, NULL },
        { CURLOPT_SSL_CTX_FUNCTION, (void*)qeo_mgmt_curl_sslctx_cb },
        { CURLOPT_SSL_CTX_DATA, (void*)&curlsslhelper }
    };
    bool reset = false;

    do {
        if ((mg_ctx == NULL ) || (mg_ctx->curl_ctx == NULL) ||  (url == NULL) || (ssl_cb == NULL) || (data_cb == NULL)){
            ret = QMGMTCLIENT_EINVAL;
            break;
        }
        if (QEO_UTIL_OK != qeo_platform_get_cacert_path(&cafile, &capath)) {
            ret = QMGMTCLIENT_EFAIL;
            break;
        }
        /* insert values into options array */
        opts[2].cookie = (void*)cafile;
        opts[3].cookie = (void*)capath;
        reset = true;
        if (CURLE_OK != qeo_mgmt_curl_util_set_opts(opts, sizeof(opts) / sizeof(curl_opt_helper), mg_ctx)) {
            ret = QMGMTCLIENT_EINVAL;
            break;
        }
        ret = qeo_mgmt_curl_util_http_get_with_cb(mg_ctx, url, header, data_cb, write_cookie);
        reset = false;/* Already done. */
    } while (0);

    if (reset == true){
        /* Make sure we reset all configuration for next calls */
        curl_easy_reset(mg_ctx->curl_ctx);
    }

    if (ret != QMGMTCLIENT_OK) {
        qeo_log_w("Failure in https_get_%s",url);
    }
    return ret;

}
void return_connection(CURL *curl)
{
	curl_easy_reset(curl) ;
	pthread_mutex_lock(&pool_mut);
	if(curl_pool.free_connect_count >= curl_pool.buffer_connect_count)
		curl_easy_cleanup(curl) ;
	else
		curl_pool.curl_free_connect[(curl_pool.free_connect_count)++] = curl ;
	curl_pool.used_connect_count-- ;
	pthread_mutex_unlock(&pool_mut);
}
Exemple #7
0
SEXP R_handle_reset(SEXP ptr){
  //reset all fields
  reference *ref = get_ref(ptr);
  set_form(ref, NULL);
  set_headers(ref, NULL);
  curl_easy_reset(ref->handle);

  //restore default settings
  set_handle_defaults(ref);
  return ScalarLogical(1);
}
Exemple #8
0
bool curlHttpRequest::Request(const std::string &url, const std::string &request, std::string method, std::string &response, int timeout)
{
	m_response_code = 0;  

	response.clear();  
	response.reserve(64 * 1024);  

	curl_easy_reset(m_pcurl);  
	curl_easy_setopt(m_pcurl, CURLOPT_URL, url.c_str());  
	curl_easy_setopt(m_pcurl, CURLOPT_NOSIGNAL, 1);  
	curl_easy_setopt(m_pcurl, CURLOPT_TIMEOUT, timeout);  
	curl_easy_setopt(m_pcurl, CURLOPT_WRITEFUNCTION,curlHttpRequest::WriteData);  
	curl_easy_setopt(m_pcurl, CURLOPT_WRITEDATA, &response);  
	if(method == "get")  
	{  
		curl_easy_setopt(m_pcurl, CURLOPT_HTTPGET, 1);  
	}  
	else if(method == "post")  
	{  
		curl_easy_setopt(m_pcurl, CURLOPT_POST, 1L);  
		curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDSIZE, request.length());  
		curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDS, request.c_str());  
	}  
	else if(method == "put")  
	{  
		curl_easy_setopt(m_pcurl, CURLOPT_CUSTOMREQUEST, "PUT");  
		curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDSIZE, request.length());  
		curl_easy_setopt(m_pcurl, CURLOPT_POSTFIELDS, request.c_str());  
	}  
	else if(method == "delete")  
	{  
		curl_easy_setopt(m_pcurl, CURLOPT_CUSTOMREQUEST, "DELETE");  
	}  
	else  
	{  
		return false;  
	}  

	CURLcode rc = curl_easy_perform(m_pcurl);  
	if(rc != CURLE_OK)  
	{  
		m_error =  std::string(curl_easy_strerror(rc));  
		return false;  
	}  

	rc = curl_easy_getinfo(m_pcurl, CURLINFO_RESPONSE_CODE , &m_response_code);   
	if(rc != CURLE_OK)  
	{  
		m_error =  std::string(curl_easy_strerror(rc));  
		return false;  
	}  

	return true;  
}
Exemple #9
0
    void http_reset_request (
                              http_t * http
                              )
    {
        if ( unlikely (! http) )
        {
            return;
        }

        curl_easy_reset (http->curl);
        http_clear_form (http);
    }
/* 初始化HttpClient,使用前务必调用 */
void HttpClient_Init(HttpClient *client) {
//{{{
    
    curl_version_info_data *version;

    if (client->curl == NULL) {
        client->curl = curl_easy_init();
        client->responseText = HttpBuffer_New();
        client->responseHeader = HttpBuffer_New();
    } else {
        curl_easy_reset(client->curl);
    }
    
    /* 重置失败次数 */ 
    client->fail_times        = 0;
    client->retry_times       = 5;
    client->retry_interval    = 1;

    /* 重置失败回调 */
    client->fail_reset_callback = NULL;
    client->fail_reset_context  = NULL;

    /* 清空body cstr */
    if (client->c_responseText != NULL) {
        free(client->c_responseText);
        client->c_responseText = NULL;
    }

    /* 清空header cstr */
    if (client->c_responseHeader != NULL) {
        free(client->c_responseText);
        client->c_responseText = NULL;
    }

    /* curl 初始化 */
    curl_easy_setopt(client->curl, CURLOPT_WRITEFUNCTION, _HttpClient_WriteData);
    curl_easy_setopt(client->curl, CURLOPT_WRITEDATA, client);

    curl_easy_setopt(client->curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(client->curl, CURLOPT_SSL_VERIFYHOST, 0);
    /* 选个快点的加密算法 */
    version = curl_version_info(CURLVERSION_NOW);
    if (strstr(version->ssl_version, "OpenSSL") != NULL) {
        curl_easy_setopt(client->curl, CURLOPT_SSL_CIPHER_LIST, "RC4-SHA");
    } else if (strstr(version->ssl_version, "NSS") != NULL) {
        curl_easy_setopt(client->curl, CURLOPT_SSL_CIPHER_LIST, "rc4,rsa_rc4_128_sha");
    }

    /* 设置默认超时时间 */
    curl_easy_setopt(client->curl, CURLOPT_TIMEOUT, 20);
    curl_easy_setopt(client->curl, CURLOPT_CONNECTTIMEOUT, 5);
}
Exemple #11
0
int test(char *URL)
{
  CURLM *multi;
  CURL *easy;

  if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
    fprintf(stderr, "curl_global_init() failed\n");
    return TEST_ERR_MAJOR_BAD;
  }

  if ((multi = curl_multi_init()) == NULL) {
    fprintf(stderr, "curl_multi_init() failed\n");
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }

  if ((easy = curl_easy_init()) == NULL) {
    fprintf(stderr, "curl_easy_init() failed\n");
    curl_multi_cleanup(multi);
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }

  curl_multi_setopt(multi, CURLMOPT_PIPELINING, 1);

  curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, fwrite);
  curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1);
  curl_easy_setopt(easy, CURLOPT_URL, URL);

  curl_multi_add_handle(multi, easy);
  if (perform(multi) != CURLM_OK)
    printf("retrieve 1 failed\n");

  curl_multi_remove_handle(multi, easy);
  curl_easy_reset(easy);

  curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1);
  curl_easy_setopt(easy, CURLOPT_URL, arg2);

  curl_multi_add_handle(multi, easy);
  if (perform(multi) != CURLM_OK)
    printf("retrieve 2 failed\n");

  curl_multi_remove_handle(multi, easy);
  curl_easy_cleanup(easy);
  curl_multi_cleanup(multi);
  curl_global_cleanup();

  printf("Finished!\n");

  return 0;
}
Exemple #12
0
SCM DLL_PUBLIC
cl_easy_reset (SCM handle)
{
  handle_post_t *c_handle;

  SCM_ASSERT (_scm_is_handle (handle), handle, SCM_ARG1, "%curl-easy-reset");

  c_handle = _scm_to_handle (handle);

  curl_easy_reset (c_handle->handle);

  return SCM_UNSPECIFIED;
}
Exemple #13
0
/* ****************************************************************************
*
* curl_context_cleanup - 
*/
void curl_context_cleanup(void)
{
  for (std::map<std::string, struct curl_context>::iterator it = contexts.begin(); it != contexts.end(); ++it)
  {
    curl_easy_reset(it->second.curl);
    curl_easy_cleanup(it->second.curl);
    it->second.curl = NULL;
    release_curl_context(&it->second, true);
  }

  contexts.clear();
  curl_global_cleanup();
}
Exemple #14
0
///============================CLEAN BEFORE CLOSING==================================///
void Browser::clean()
{
    init();
    curl_easy_reset(curl);
    history_.clear();

    struct curl_slist *headers    = NULL;
    headers = curl_slist_append(headers, "Accept:");
    curl_slist_free_all(headers);

    curl_easy_cleanup(curl);
    curl    = curl_easy_init();
}
Exemple #15
0
/**
* @brief 开始文件下载任务
* @author LuChenQun
* @date 2015/07/05
* @return int 任务结果
*/
int NetWork::startDownloadFile()
{

    int code = CURLE_OK;

    QByteArray urlBa = m_url.toLocal8Bit();
    char *url = urlBa.data();

    // 初始化文件
    code = initFile();
    if (code != NETWORK_INIT_FILE_SUCCESS)
    {
        return code;
    }

    curl_easy_reset(m_curl);
    curl_easy_setopt(m_curl, CURLOPT_URL, url);
    curl_easy_setopt(m_curl, CURLOPT_LOW_SPEED_LIMIT, 10);
    curl_easy_setopt(m_curl, CURLOPT_LOW_SPEED_TIME, 300);
    curl_easy_setopt(m_curl, CURLOPT_HEADER, 1);
    curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1);

    // 获取文件大小
    code = curl_easy_perform(m_curl);
    double fileSize = 0;
    curl_easy_getinfo(m_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fileSize);
    emitFileSize(fileSize);
    if (getDiskFreeSpace("e:/") <= (m_fileSize))
    {
        code = NETWORK_DISK_NO_SPACE;
        return code;
    }

    curl_easy_setopt(m_curl, CURLOPT_HEADER, 0);
    curl_easy_setopt(m_curl, CURLOPT_NOBODY, 0);
    curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeData);
    curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this);
    curl_easy_setopt(m_curl, CURLOPT_RESUME_FROM_LARGE, m_breakPoint);	// 断点续传
    curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, progress);		// 进度
    curl_easy_setopt(m_curl, CURLOPT_XFERINFODATA, this);
    curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1L);      // 多线程需要注意的
    curl_easy_setopt(m_curl, CURLOPT_FORBID_REUSE, 1);

    code = curl_easy_perform(m_curl);

    m_downloadFile.flush();
    m_downloadFile.close();

    return code;
}
Exemple #16
0
static void request_deinitialize(Request *request)
{
    if (request->headers) {
        curl_slist_free_all(request->headers);
    }
    
    error_parser_deinitialize(&(request->errorParser));

    // curl_easy_reset prevents connections from being re-used for some
    // reason.  This makes HTTP Keep-Alive meaningless and is very bad for
    // performance.  But it is necessary to allow curl to work properly.
    // xxx todo figure out why
    curl_easy_reset(request->curl);
}
Exemple #17
0
CURL* Qiniu_Client_reset(Qiniu_Client* self)
{
	CURL* curl = (CURL*)self->curl;

	curl_easy_reset(curl);
	Qiniu_Buffer_Reset(&self->b);
	Qiniu_Buffer_Reset(&self->respHeader);
	if (self->root != NULL) {
		cJSON_Delete(self->root);
		self->root = NULL;
	}

	return curl;
}
Exemple #18
0
int
dcontext_prepare(struct dcontext *dctx)
{
    sassert(dctx);
    sassert(dctx->d_savefile);
    
    CURL *c = dctx->d_handle = curl_easy_init();

    curl_easy_reset(c);

    curl_easy_setopt(c, CURLOPT_FAILONERROR, 1L);
    curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, 10L);
    curl_easy_setopt(c, CURLOPT_FILETIME, 1L);
    curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(c, CURLOPT_LOW_SPEED_LIMIT, 1024L);
    curl_easy_setopt(c, CURLOPT_LOW_SPEED_TIME, 10L);
    curl_easy_setopt(c, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);    

    
    curl_easy_setopt(c, CURLOPT_URL, dctx->d_url);

    curl_easy_setopt(c, CURLOPT_ERRORBUFFER, &dctx->d_ebuffer[0]);


    /* no progress for now */
    curl_easy_setopt(c, CURLOPT_NOPROGRESS, 1L);
#if 0
    curl_easy_setopt(c, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(c, CURLOPT_PROGRESSFUNCTION, dcontext_progress_func);
    curl_easy_setopt(c, CURLOPT_PROGRESSDATA, dctx);
#endif

    /* no header function for now */
#if 0
    curl_easy_setopt(c, CURLOPT_HEADERFUNCTION, dcontext_header_func);
    curl_easy_setopt(c, CURLOPT_WRITEHEADER, dctx);
#endif
    
    if ((dctx->d_savefp = fopen(dctx->d_savefile, "wb")) == NULL) {
        goto error;
    }

    curl_easy_setopt(c, CURLOPT_WRITEDATA, dctx->d_savefp);

    return 0;
    
error:
    fprintf(stderr, "failed: %s\n", strerror(errno));
    return -1;
}
Exemple #19
0
/**
 * Complete a multipart upload.
 * @param s3Comm [in] S3COMM handle.
 * @param curl [in] CURL handle for the current transfer.
 * @param fileId [in] Upload ID for the multipart uploads.
 * @param parts [in] Number of parts in the multipart upload.
 * @param hostname [in] Host name for the S3 request.
 * @param filepath [in] Remote file path of the file.
 * @param uploadId [in] Upload ID for the multipart upload.
 * @return Nothing.
 */
STATIC void
CompleteMultipartUpload(
	S3COMM        *s3Comm,
	CURL          *curl,
	sqlite3_int64 fileId,
	int           parts,
	const char    *hostname,
	const char    *filepath,
	const char    *uploadId
	                    )
{
	struct curl_slist *headers;
	int               i;
	char              *url;
	FILE              *upFile;
	const char        *etag;
	int               status;

	/* Build the parts list. */
	if( Query_AllPartsUploaded( fileId ) )
	{
		upFile = tmpfile( );
		fputs( "<CompleteMultipartUpload>\n", upFile );
		for( i = 1; i < parts + 1; i++ )
		{
			etag = Query_GetPartETag( fileId, i );
			fprintf( upFile, "<Part><PartNumber>%d</PartNumber>"
					 "<ETag>%s</ETag></Part>\n", i, etag );
		}
		fputs( "</CompleteMultipartUpload>\n", upFile );

		/* Upload the parts list. */
		url = malloc( strlen( filepath )
					  + strlen( "?uploadId=" )
					  + strlen( uploadId ) + sizeof( char ) );
		headers = BuildS3Request( s3Comm, "POST", hostname, NULL, url );
		curl_easy_reset( curl );
		curl_easy_setopt( curl, CURLOPT_READDATA, upFile );
		curl_easy_setopt( curl, CURLOPT_HTTPHEADER, headers );
		curl_easy_setopt( curl, CURLOPT_URL, url );
		status = curl_easy_perform( curl );
		if( status != 0 )
		{
			fprintf( stderr, "Could not complete multipart upload.\n" );
		}
		DeleteCurlSlistAndContents( headers );
		fclose( upFile );
	}
}
void CURLSession::setup(const Request & request) {
	
  if(!m_curl) {
		return;
	}
	
	curl_easy_reset(m_curl);
	
	curl_easy_setopt(m_curl, CURLOPT_URL, request.url().c_str());
	curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, request.followRedirects() ? 1L : 0L);
	
	curl_easy_setopt(m_curl, CURLOPT_USERAGENT, m_userAgent.c_str());
	
	curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, 1L);
	
}
Exemple #21
0
static void QBox_Client_initcall(QBox_Client* self, const char* url)
{
	CURL* curl = (CURL*)self->curl;

	curl_easy_reset(curl);
	QBox_Buffer_Reset(&self->b);
	if (self->root != NULL) {
		cJSON_Delete(self->root);
		self->root = NULL;
	}

	curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
	curl_easy_setopt(curl, CURLOPT_URL, url);
}
Exemple #22
0
int
web_add_ref (void *state, const arrow_id_t *id)
{
  web_ctx_t *ctx = (web_ctx_t *) state;
  CURL *curl = ctx->curl_ctx;
  const char *data = "addref=1\n";
  iobuf_t iob;
  char *urlbuf;
  int urllen;
  CURLcode curlret;

  iob.data = (void *) data;
  iob.length = strlen (data);
  iob.position = 0;
  
  urllen = strlen(ctx->baseurl) + strlen(WEB_STORE_PATH) + 33;
  urlbuf = (char *) malloc (urllen);
  if (urlbuf == NULL)
    return -1;
  snprintf (urlbuf, urllen, "%s%s%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
                            "%02x%02x%02x%02x%02x%02x"
            ctx->baseurl, WEB_STORE_PATH,
            id->strong[ 0], id->strong[ 1], id->strong[ 2], id->strong[ 3],
            id->strong[ 4], id->strong[ 5], id->strong[ 6], id->strong[ 7],
            id->strong[ 8], id->strong[ 9], id->strong[10], id->strong[11],
            id->strong[12], id->strong[13], id->strong[14], id->strong[15]);
  
  curl_easy_reset(curl);
  curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
  curl_easy_setopt(curl, CURLOPT_READDATA, &iob);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
  curl_easy_setopt(curl, CURLOPT_POST, 1);
  curl_easy_setopt(curl, CURLOPT_URL, urlbuf);
  curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
  curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  if (ctx->userpass != NULL)
    curl_easy_setopt(curl, CURLOPT_USERPWD, ctx->userpass);
  
  curlret = curl_easy_perform(curl);
  if (curlret != 0)
    {
      free (urlbuf);
      return -1;
    }
  free (urlbuf);
  return 0;
}
Exemple #23
0
void *curl_post(CURL **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3)
{
    struct MemoryStruct chunk; CURL *cHandle; long code; struct curl_slist *headers = 0;
    if ( (cHandle= *cHandlep) == NULL )
		*cHandlep = cHandle = curl_easy_init();
    else curl_easy_reset(cHandle);
    //#ifdef DEBUG
	curl_easy_setopt(cHandle,CURLOPT_VERBOSE, 1);
    //#endif
	curl_easy_setopt(cHandle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )");
	curl_easy_setopt(cHandle,CURLOPT_SSL_VERIFYPEER,0);
	//curl_easy_setopt(cHandle,CURLOPT_SSLVERSION,1);
	curl_easy_setopt(cHandle,CURLOPT_URL,url);
  	curl_easy_setopt(cHandle,CURLOPT_CONNECTTIMEOUT,10);
    if ( userpass != 0 && userpass[0] != 0 )
        curl_easy_setopt(cHandle,CURLOPT_USERPWD,userpass);
	if ( postfields != 0 && postfields[0] != 0 )
    {
        curl_easy_setopt(cHandle,CURLOPT_POST,1);
		curl_easy_setopt(cHandle,CURLOPT_POSTFIELDS,postfields);
    }
    if ( hdr0 != NULL && hdr0[0] != 0 )
    {
        //printf("HDR0.(%s) HDR1.(%s) HDR2.(%s) HDR3.(%s)\n",hdr0!=0?hdr0:"",hdr1!=0?hdr1:"",hdr2!=0?hdr2:"",hdr3!=0?hdr3:"");
        headers = curl_slist_append(headers,hdr0);
        if ( hdr1 != 0 && hdr1[0] != 0 )
            headers = curl_slist_append(headers,hdr1);
        if ( hdr2 != 0 && hdr2[0] != 0 )
            headers = curl_slist_append(headers,hdr2);
        if ( hdr3 != 0 && hdr3[0] != 0 )
            headers = curl_slist_append(headers,hdr3);
    } //headers = curl_slist_append(0,"Expect:");
    if ( headers != 0 )
        curl_easy_setopt(cHandle,CURLOPT_HTTPHEADER,headers);
    //res = curl_easy_perform(cHandle);
    memset(&chunk,0,sizeof(chunk));
    curl_easy_setopt(cHandle,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);
    curl_easy_setopt(cHandle,CURLOPT_WRITEDATA,(void *)&chunk);
    curl_easy_perform(cHandle);
    curl_easy_getinfo(cHandle,CURLINFO_RESPONSE_CODE,&code);
    if ( headers != 0 )
        curl_slist_free_all(headers);
    if ( code != 200 )
        printf("(%s) server responded with code %ld (%s)\n",url,code,chunk.memory);
    return(chunk.memory);
}
Exemple #24
0
int test(char *URL)
{
  CURLcode res;
  CURL *curl;

  if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
    fprintf(stderr, "curl_global_init() failed\n");
    return TEST_ERR_MAJOR_BAD;
  }

  if((curl = curl_easy_init()) == NULL) {
    fprintf(stderr, "curl_easy_init() failed\n");
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }

  test_setopt(curl, CURLOPT_URL, URL);
  test_setopt(curl, CURLOPT_HEADER, 1L);
  test_setopt(curl, CURLOPT_REFERER, "http://example.com/the-moo");
  test_setopt(curl, CURLOPT_USERAGENT, "the-moo agent next generation");
  test_setopt(curl, CURLOPT_COOKIE, "name=moo");
  test_setopt(curl, CURLOPT_VERBOSE, 1L);

  res = curl_easy_perform(curl);
  if(res) {
    fprintf(stderr, "retrieve 1 failed\n");
    goto test_cleanup;
  }

  curl_easy_reset(curl);

  test_setopt(curl, CURLOPT_URL, URL);
  test_setopt(curl, CURLOPT_HEADER, 1L);
  test_setopt(curl, CURLOPT_VERBOSE, 1L);

  res = curl_easy_perform(curl);
  if(res)
    fprintf(stderr, "retrieve 2 failed\n");

test_cleanup:

  curl_easy_cleanup(curl);
  curl_global_cleanup();

  return (int)res;
}
bool HTML5Win32::NFSHttp::SendPayLoadAndRecieve(const unsigned char* In, unsigned int InSize, unsigned char** Out, unsigned int& size)
{
	struct curl_slist *headerlist=NULL;
	static const char buf[] = "Expect:"; 

	curl_easy_setopt(curl, CURLOPT_URL, GURL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

	MemoryStruct chunk;
	chunk.memory = 0;
	chunk.size = 0; 

	if (  InSize != 0 )
	{
		curl_easy_setopt(curl, CURLOPT_POST, 1L);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, In);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, InSize);
		
	}
	else
	{
		curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
	}

	headerlist = curl_slist_append(headerlist, buf);
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
	CURLcode  result = curl_easy_perform(curl);

	/* check for errors */ 
	if(result != CURLE_OK) {
		fprintf(stderr, "curl_easy_perform() failed: %s\n",
		curl_easy_strerror(result));
		return false; 
	}

	*Out = chunk.memory; 
	size= chunk.size; 

	curl_easy_reset(curl);

	return true;
}
Exemple #26
0
bool WebTools::DownloadFile(const ssi_char_t *url, const ssi_char_t *pathOnDisk, const ssi_char_t *pathToCertificate, bool showProgress)
{
	bool result = false;
	init_curl();

	CURL *curl = curl_easy_init();

	if (url_exists(curl, url, pathToCertificate))
	{
		curl_easy_reset(curl);

		init_request(curl, url, pathToCertificate, showProgress);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

		FILE *file = fopen(pathOnDisk, "wb");
		if (file) {

			curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
			CURLcode code = curl_easy_perform(curl);
			fclose(file);

			if (code != CURLE_OK)
			{
				ssi_wrn(curl_easy_strerror(code));
				ssi_remove(pathOnDisk);
			}
			else
			{
				result = true;
			}
		}
		else
		{
			ssi_wrn("could not create file '%s'", pathOnDisk);
		}
	}
	else
	{
		ssi_wrn("url does not exist '%s'", url);
	}

	curl_easy_cleanup(curl);

	return result;
}
Exemple #27
0
bool HttpBase::OnExecute(IKernel * kernel) {
	OASSERT(_type != HT_NONE, "http request is not initialize");
	curl_easy_reset(_curl);
	curl_easy_setopt(_curl, CURLOPT_TIMEOUT, 10);
	curl_easy_setopt(_curl, CURLOPT_VERBOSE, 0L);
	curl_easy_setopt(_curl, CURLOPT_URL, _url);
	curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, WriteData);
	curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void*)this);

	if (_type == HT_POST) {
		curl_easy_setopt(_curl, CURLOPT_POST, 1);
		curl_easy_setopt(_curl, CURLOPT_POSTFIELDS, _field);
	}

	_errCode = curl_easy_perform(_curl);
	return _errCode == CURLE_OK;
}
Exemple #28
0
void makeHttpRequestAsync(const ADHttp::RequestType& type,
                          const std::string& url_in,
                          const ADHttp::Params& params_in,
                          const ADHttp::Callback& callback)
{
    CurlReqPtr req = CurlReq::create(callback, std::make_shared<ADCurlReader>());

    CURL* curl = req->curl;
    ADCurlReader* reader = req->reader.get();

    curl_easy_reset(curl);

    std::string params = paramsToUrl(curl, params_in);
    std::string url = url_in;

    req->curlStrings[CURLOPT_URL] = url;

    if(type == ADHttp::RequestType::POST)
    {
        curl_easy_setopt(curl, CURLOPT_POST, true);
        req->curlStrings[CURLOPT_POSTFIELDS] = params;
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req->curlStrings[CURLOPT_POSTFIELDS].c_str());
    }
    else
    {
        if(params.size() > 0)
        {
            url += "?";
            url += params;
        }

    }



    curl_easy_setopt(curl, CURLOPT_URL, req->curlStrings[CURLOPT_URL].c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, reader);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &ADCurlReader::handle);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

    CurlReqQueue::getInstance()->run(req);
}
Exemple #29
0
static void s_notify(pdu_t *p)
{
	char *name    = pdu_string(p, 1);
	char *ts      = pdu_string(p, 2);
	char *stale   = pdu_string(p, 3);
	char *code    = pdu_string(p, 4);
	char *summary = pdu_string(p, 5);

	if (pcre_exec(OPTIONS.re, OPTIONS.re_extra, name, strlen(name), 0, 0, NULL, 0) == 0) {
		char error[CURL_ERROR_SIZE], *message;
		request_t *r;
		struct curl_slist *headers = NULL;

		logger(LOG_INFO, "matched %s against /%s/, notifying", name, OPTIONS.match);

		message = string("%s is now *%s*\n> %s\n", name, code, summary);
		r = s_new_request(OPTIONS.avatar, OPTIONS.username, OPTIONS.channel, message);

		curl_easy_reset(OPTIONS.curl);
		headers = curl_slist_append(headers, "Content-type: application/json");
		curl_easy_setopt(OPTIONS.curl, CURLOPT_HTTPHEADER,    headers);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_WRITEFUNCTION, s_slack_writer);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_READFUNCTION,  s_slack_reader);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_READDATA,      r);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_USERAGENT,     UA);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_ERRORBUFFER,   error);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_URL,           OPTIONS.webhook);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_POST,          1);
		curl_easy_setopt(OPTIONS.curl, CURLOPT_POSTFIELDSIZE, r->len);
		/* FIXME: free headers */

		logger(LOG_INFO, "submitting data to %s", OPTIONS.webhook);
		if (curl_easy_perform(OPTIONS.curl) != CURLE_OK) {
			logger(LOG_ERR, "failed to notify via Slack webhook <%s>: %s", OPTIONS.webhook, error);
		}
	}

	free(name);
	free(ts);
	free(stale);
	free(code);
	free(summary);
}
Exemple #30
0
/**
 * perform a POST request on facebook graph api
 *
 * @note use this one to create object (album, photos, ...)
 *
 * @param ctx facebook context (token field must be set)
 * @param method the method to call on the facebook graph API, the methods should not start with '/' example: "me/albums"
 * @param args hashtable of the arguments to be added to the requests, might be null if none
 * @param files hashtable of the files to be send with the requests, might be null if none
 * @returns NULL if the request fails, or a JsonObject of the reply
 */
static JsonObject *fb_query_post(FBContext *ctx, const gchar *method, GHashTable *args, GHashTable *files)
{
  g_return_val_if_fail(ctx != NULL, NULL);
  g_return_val_if_fail(ctx->token != NULL, NULL);

  GString *url =g_string_new(FB_GRAPH_BASE_URL);
  g_string_append(url, method);

  HttppostFormList formlist;
  formlist.formpost = NULL;
  formlist.lastptr = NULL;

  curl_formadd(&(formlist.formpost),
               &(formlist.lastptr),
               CURLFORM_COPYNAME, "access_token",
               CURLFORM_COPYCONTENTS, ctx->token,
               CURLFORM_END);
  if (args != NULL)
    g_hash_table_foreach(args, (GHFunc)fb_query_post_add_form_arguments, &formlist);

  if (files != NULL)
    g_hash_table_foreach(files, (GHFunc)fb_query_post_add_file_arguments, &formlist);

  //send the requests
  GString *response = g_string_new("");
  curl_easy_reset(ctx->curl_ctx);
  curl_easy_setopt(ctx->curl_ctx, CURLOPT_URL, url->str);
#ifdef FACEBOOK_EXTRA_VERBOSE
  curl_easy_setopt(ctx->curl_ctx, CURLOPT_VERBOSE, 2);
#endif
  curl_easy_setopt(ctx->curl_ctx, CURLOPT_HTTPPOST, formlist.formpost);
  curl_easy_setopt(ctx->curl_ctx, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_easy_setopt(ctx->curl_ctx, CURLOPT_WRITEFUNCTION, curl_write_data_cb);
  curl_easy_setopt(ctx->curl_ctx, CURLOPT_WRITEDATA, response);
  int res = curl_easy_perform(ctx->curl_ctx);
  curl_formfree(formlist.formpost);
  g_string_free(url, TRUE);
  if (res != CURLE_OK) return NULL;
  //parse the response
  JsonObject *respobj = fb_parse_response(ctx, response);
  g_string_free(response, TRUE);
  return respobj;
}