本文整理汇总了C++中curl_easy_strerror函数的典型用法代码示例。如果您正苦于以下问题:C++ curl_easy_strerror函数的具体用法?C++ curl_easy_strerror怎么用?C++ curl_easy_strerror使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curl_easy_strerror函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
/* 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://mainserver.example.net: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");
/* 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);
/* 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);
/* Send the message */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Free the list of recipients */
curl_slist_free_all(recipients);
/* Always cleanup */
curl_easy_cleanup(curl);
}
return (int)res;
}
示例2: http_post
static int http_post(void *handle, char *uri, const char *clientid, const char *username,const char *password, const char *topic, int acc)
{
struct http_backend *conf = (struct http_backend *)handle;
CURL *curl;
struct curl_slist *headerlist=NULL;
int re;
int respCode = 0;
int ok = FALSE;
char *url;
char *data;
if (username == NULL) {
return (FALSE);
}
clientid = (clientid && *clientid) ? clientid : "";
password = (password && *password) ? password : "";
topic = (topic && *topic) ? topic : "";
if ((curl = curl_easy_init()) == NULL) {
_fatal("create curl_easy_handle fails");
return (FALSE);
}
if (conf->hostheader != NULL)
curl_slist_append(headerlist, conf->hostheader);
curl_slist_append(headerlist, "Expect:");
//_log(LOG_NOTICE, "u=%s p=%s t=%s acc=%d", username, password, topic, acc);
url = (char *)malloc(strlen(conf->ip) + strlen(uri) + 20);
if (url == NULL) {
_fatal("ENOMEM");
return (FALSE);
}
sprintf(url, "http://%s:%d%s", conf->ip, conf->port, uri);
/* Hoping the 1024 is sufficient for curl_easy_escapes ... */
data = (char *)malloc(strlen(username) + strlen(password) + strlen(topic) + strlen(clientid) + 1024);
if (data == NULL) {
_fatal("ENOMEM");
return (FALSE);
}
sprintf(data, "username=%s&password=%s&topic=%s&acc=%d&clientid=%s",
curl_easy_escape(curl, username, 0),
curl_easy_escape(curl, password, 0),
curl_easy_escape(curl, topic, 0),
acc,
curl_easy_escape(curl, clientid, 0));
_log(LOG_DEBUG, "url=%s", url);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
re = curl_easy_perform(curl);
if (re == CURLE_OK) {
re = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respCode);
if (re == CURLE_OK && respCode == 200) {
ok = TRUE;
} else {
//_log(LOG_NOTICE, "http auth fail re=%d respCode=%d", re, respCode);
}
} else {
_log(LOG_DEBUG, "http req fail url=%s re=%s", url, curl_easy_strerror(re));
}
curl_easy_cleanup(curl);
curl_slist_free_all (headerlist);
free(url);
free(data);
return (ok);
}
示例3: main
int main(int argc, char **argv) {
extern int optind;
extern char *optarg;
int i;
char *outfile = NULL;
int de = 0;
while ((i = getopt(argc, argv, "o:d")) != -1) {
switch (i) {
case 'o':
outfile = optarg;
break;
case 'd':
de = 1;
break;
default:
usage(argv);
exit(EXIT_FAILURE);
}
}
if (argc - optind != 1) {
usage(argv);
exit(EXIT_FAILURE);
}
if (outfile == NULL && isatty(1)) {
fprintf(stderr, "Didn't specify -o and standard output is a terminal\n");
exit(EXIT_FAILURE);
}
FILE *outfp = stdout;
if (outfile != NULL) {
outfp = fopen(outfile, "wb");
if (outfp == NULL) {
perror(outfile);
exit(EXIT_FAILURE);
}
}
int width, height;
unsigned char *buf;
{
{
{
char *url = argv[optind];
CURL *curl = curl_easy_init();
if (curl == NULL) {
fprintf(stderr, "Curl won't start\n");
exit(EXIT_FAILURE);
}
struct data data;
data.buf = NULL;
data.len = 0;
data.nalloc = 0;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_receive);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "Can't retrieve %s: %s\n", url,
curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
struct image *i;
if (data.len >= 4 && memcmp(data.buf, "\x89PNG", 4) == 0) {
i = read_png(data.buf, data.len);
} else if (data.len >= 2 && memcmp(data.buf, "\xFF\xD8", 2) == 0) {
i = read_jpeg(data.buf, data.len);
} else {
fprintf(stderr, "Don't recognize file format\n");
free(data.buf);
curl_easy_cleanup(curl);
exit(EXIT_FAILURE);
}
free(data.buf);
curl_easy_cleanup(curl);
width = i->width;
height = i->height;
buf = malloc(i->width * i->height * 4);
int x, y;
for (y = 0; y < i->height; y++) {
for (x = 0; x < i->width; x++) {
if (i->depth == 4) {
double as = buf[((y) * width + x) * 4 + 3] / 255.0;
double rs = buf[((y) * width + x) * 4 + 0] / 255.0 * as;
//.........这里部分代码省略.........
示例4: main
int main(int argc, char **argv)
{
int res = 0;
CURLcode curlerr;
VCHI_INSTANCE_T vchiq_instance;
VCHI_CONNECTION_T *vchi_connection;
CEC_AllDevices_T logical_address;
uint16_t physical_address;
/* Make sure logs are written to disk */
setlinebuf(stdout);
setlinebuf(stderr);
if (argc > 2) {
printf("usage: %s [port]\n", argv[0]);
return -1;
}
if (argc == 2) {
port = atoi(argv[1]);
}
curlerr = curl_global_init(CURL_GLOBAL_NOTHING);
if ( curlerr ) {
printf("failed to init curl error=%d \"%s\"\n", curlerr,
curl_easy_strerror(curlerr));
return -1;
}
res = vchi_initialise(&vchiq_instance);
if ( res != VCHIQ_SUCCESS ) {
printf("failed to open vchiq instance\n");
return -1;
}
res = vchi_connect( NULL, 0, vchiq_instance );
if ( res != 0 ) {
printf( "VCHI connection failed\n" );
return -1;
}
vc_vchi_cec_init(vchiq_instance, &vchi_connection, 1);
if ( res != 0 ) {
printf( "VCHI CEC connection failed\n" );
return -1;
}
vc_cec_register_callback(((CECSERVICE_CALLBACK_T) cec_callback), NULL);
#if 0
vc_cec_register_all();
#endif
vc_cec_get_logical_address(&logical_address);
printf("logical_address: 0x%x\n", logical_address);
vc_cec_set_vendor_id(CEC_VENDOR_ID_BROADCOM);
vc_cec_set_osd_name("XBMC");
vc_cec_get_physical_address(&physical_address);
printf("physical_address: 0x%x\n", physical_address);
vc_cec_send_ActiveSource(physical_address, 0);
while (1) {
might_be_dimmed = 1;
sleep(10);
}
vchi_exit();
curl_global_cleanup();
return 0;
}
示例5: main
int main(int argc, char* const argv[])
{
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, "smtps://smtp.exmail.qq.com:465");
/* 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);
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
/* 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");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
/* 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, argv[1]);
curl_easy_setopt(curl, CURLOPT_PASSWORD, argv[2]);
/* 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);
recipients = curl_slist_append(recipients, CC);
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);
/* 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));
/* free the list of recipients and clean up */
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
return 0;
}
示例6: main
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
const char buf[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
/* Fill in the file upload field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "sendfile",
CURLFORM_FILE, "postit2.c",
CURLFORM_END);
/* Fill in the filename field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, "postit2.c",
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi");
if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
/* only disable 100-continue header if explicitly requested */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
/* 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_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
}
return 0;
}
示例7: curl_easy_init
char *request(const char *url)
{
CURL *curl;
CURLcode status;
char *data;
long code;
curl = curl_easy_init();
data = malloc(BUFFER_SIZE);
if(!curl || !data)
return NULL;
write_result write_result = {
.data = data,
.pos = 0
};
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
status = curl_easy_perform(curl);
if(status != 0)
{
fprintf(stderr, "error: unable to request data from %s:\n", url);
fprintf(stderr, "%s\n", curl_easy_strerror(status));
return NULL;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200)
{
fprintf(stderr, "error: server responded with code %ld\n", code);
return NULL;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
/* zero-terminate the result */
data[write_result.pos] = '\0';
return data;
}
list_t* getAlbums(const char* username)
{
char url[URL_SIZE];
char query[URL_SIZE];
char* response = NULL;
json_error_t error;
json_t *root;
json_t *album;
json_t *aid;
json_t *uid;
json_t *name;
json_t *count;
album_t *content;
list_t *list = NULL;
list_t* tmp = NULL;
list_t* prev = NULL;
CURL *curl;
if( (curl = curl_easy_init()) == NULL)
{
fprintf(stderr, "%s\n", "erreur curl_easy_init");
return NULL;
}
snprintf(query, URL_SIZE, QUERY_ALBUM,username,username);
strcpy(query,curl_easy_escape(curl, query, 0));
snprintf(url,URL_SIZE, FB_QUERY_URL,query,FB_TOKEN);
curl_easy_cleanup(curl);
response = request(url);
if(!response)
{
fprintf(stderr, "%s\n", "Erreur request");
return NULL;
}
root = json_loads(response,0,&error);
if(!root)
free(response);
if(!root)
{
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
return NULL;
}
for (int i = 0; i < json_array_size(root); i++)
{
//.........这里部分代码省略.........
示例8: main
int main(void)
{
int i;
CURL *curl;
CURLcode res;
FILE *headerfile;
const char *pPassphrase = NULL;
static const char *pCertFile = "testcert.pem";
static const char *pCACertFile="cacert.pem";
const char *pKeyName;
const char *pKeyType;
const char *pEngine;
#ifdef USE_ENGINE
pKeyName = "rsa_test";
pKeyType = "ENG";
pEngine = "chil"; /* for nChiper HSM... */
#else
pKeyName = "testkey.pem";
pKeyType = "PEM";
pEngine = NULL;
#endif
headerfile = fopen("dumpit", "w");
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
/* what call to write: */
curl_easy_setopt(curl, CURLOPT_URL, "HTTPS://your.favourite.ssl.site");
curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);
for(i = 0; i < 1; i++) /* single-iteration loop, just to break out from */
{
if (pEngine) /* use crypto engine */
{
if (curl_easy_setopt(curl, CURLOPT_SSLENGINE,pEngine) != CURLE_OK)
{ /* load the crypto engine */
fprintf(stderr,"can't set crypto engine\n");
break;
}
if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT,1L) != CURLE_OK)
{ /* set the crypto engine as default */
/* only needed for the first time you load
a engine in a curl object... */
fprintf(stderr,"can't set crypto engine as default\n");
break;
}
}
/* cert is stored PEM coded in file... */
/* since PEM is default, we needn't set it for PEM */
curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
/* set the cert for client authentication */
curl_easy_setopt(curl,CURLOPT_SSLCERT,pCertFile);
/* sorry, for engine we must set the passphrase
(if the key has one...) */
if (pPassphrase)
curl_easy_setopt(curl,CURLOPT_KEYPASSWD,pPassphrase);
/* if we use a key stored in a crypto engine,
we must set the key type to "ENG" */
curl_easy_setopt(curl,CURLOPT_SSLKEYTYPE,pKeyType);
/* set the private key (file or ID in engine) */
curl_easy_setopt(curl,CURLOPT_SSLKEY,pKeyName);
/* set the file with the certs vaildating the server */
curl_easy_setopt(curl,CURLOPT_CAINFO,pCACertFile);
/* disconnect if we can't validate server's cert */
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,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));
/* we are done... */
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
示例9: delete_meta
void delete_meta(int worker_no) {
long http_code = 0;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");
CURLcode curl_res;
char request_url[255] = {0};
snprintf(request_url, 255, "%s%s/%s", globals.elastic_path, current_data[worker_no].poolname, current_data[worker_no].filename);
current_data[worker_no].elastic = curl_easy_init();
if (NULL == current_data[worker_no].elastic) {
return;
}
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_USERAGENT, "WebRados");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FORBID_REUSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FRESH_CONNECT, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_NOSIGNAL, true);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEDATA, fopen("/dev/null", "w"));
#if LIBCURL_VERSION_MINOR >= 25
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_TCP_KEEPALIVE, true);
#endif
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_VERBOSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_URL, request_url);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_HTTPHEADER, headers);
curl_res = curl_easy_perform(current_data[worker_no].elastic);
curl_easy_getinfo(current_data[worker_no].elastic, CURLINFO_RESPONSE_CODE, &http_code);
if (curl_res != CURLE_OK || http_code >= 400) {
logger("Failed to open '%s' on remote server for delete - %s [HTTP code: %ld]", globals.elastic_path, curl_easy_strerror(curl_res), http_code);
}
if (headers != NULL) {
curl_slist_free_all(headers);
}
}
示例10: handleError
static void handleError(CURLcode code) {
if (code != CURLE_OK) {
throw std::runtime_error(std::string("CURL easy error: ") + curl_easy_strerror(code));
}
}
示例11: add_meta
void add_meta(int worker_no) {
long http_code = 0;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");
CURLcode curl_res;
char request_url[255] = {0};
char data[1024] = {0};
snprintf(request_url, 255, "%s%s/%s", globals.elastic_path, current_data[worker_no].poolname, current_data[worker_no].filename);
snprintf(data, 1024, "{\"Created\":\"%ld\",\"Bytes\":\"%ld\",\"Extension\":\"%s\",\"Mime\":\"%s\",\"Path\":\"%s\"}",
current_data[worker_no].metadata.c_time,
current_data[worker_no].metadata.f_size,
current_data[worker_no].metadata.extension,
current_data[worker_no].metadata.mime,
current_data[worker_no].metadata.path
);
current_data[worker_no].elastic = curl_easy_init();
if (NULL == current_data[worker_no].elastic) {
return;
}
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_USERAGENT, "WebRados");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FORBID_REUSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FRESH_CONNECT, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_NOSIGNAL, true);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEDATA, fopen("/dev/null", "w"));
#if LIBCURL_VERSION_MINOR >= 25
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_TCP_KEEPALIVE, true);
#endif
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_VERBOSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_URL, request_url);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) - 1);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_HTTPHEADER, headers);
curl_res = curl_easy_perform(current_data[worker_no].elastic);
curl_easy_getinfo(current_data[worker_no].elastic, CURLINFO_RESPONSE_CODE, &http_code);
if (curl_res != CURLE_OK || http_code >= 400) {
logger("Failed to open '%s' on remote server for indexing - %s [HTTP code: %ld]", globals.elastic_path, curl_easy_strerror(curl_res), http_code);
}
if (headers != NULL) {
curl_slist_free_all(headers);
}
}
示例12: main
int main(void)
{
CURLM *cm;
CURLMsg *msg;
long L;
unsigned int C = 0;
int M, Q, U = -1;
fd_set R, W, E;
struct timeval T;
curl_global_init(CURL_GLOBAL_ALL);
cm = curl_multi_init();
/* we can optionally limit the total amount of connections this multi handle
uses */
curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX);
for (C = 0; C < MAX; ++C) {
init(cm, C);
}
while (U) {
curl_multi_perform(cm, &U);
if (U) {
FD_ZERO(&R);
FD_ZERO(&W);
FD_ZERO(&E);
if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
fprintf(stderr, "E: curl_multi_fdset\n");
return EXIT_FAILURE;
}
if (curl_multi_timeout(cm, &L)) {
fprintf(stderr, "E: curl_multi_timeout\n");
return EXIT_FAILURE;
}
if (L == -1)
L = 100;
if (M == -1) {
#ifdef WIN32
Sleep(L);
#else
sleep(L / 1000);
#endif
} else {
T.tv_sec = L / 1000;
T.tv_usec = (L % 1000) * 1000;
if (0 > select(M + 1, &R, &W, &E, &T)) {
fprintf(stderr,
"E: select(%i,,,,%li): %i: %s\n",
M + 1, L, errno,
strerror(errno));
return EXIT_FAILURE;
}
}
}
while ((msg = curl_multi_info_read(cm, &Q))) {
if (msg->msg == CURLMSG_DONE) {
char *url;
CURL *e = msg->easy_handle;
curl_easy_getinfo(msg->easy_handle,
CURLINFO_PRIVATE, &url);
fprintf(stderr, "R: %d - %s <%s>\n",
msg->data.result,
curl_easy_strerror(msg->data.result),
url);
curl_multi_remove_handle(cm, e);
curl_easy_cleanup(e);
} else {
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
}
if (C < CNT) {
init(cm, C++);
U++; /* just to prevent it from remaining at 0 if there are more
URLs to get */
}
}
}
curl_multi_cleanup(cm);
curl_global_cleanup();
return EXIT_SUCCESS;
}
示例13: main
//.........这里部分代码省略.........
struct curl_httppost *last_post=NULL;
struct curl_forms forms[4];
struct FormData *form;
struct Form formread;
if (FormAddTest("simple COPYCONTENTS test", &httppost, &last_post,
CURLFORM_COPYNAME, name1, CURLFORM_COPYCONTENTS, value1,
CURLFORM_END))
++errors;
if (FormAddTest("COPYCONTENTS + CONTENTTYPE test", &httppost, &last_post,
CURLFORM_COPYNAME, name2, CURLFORM_COPYCONTENTS, value2,
CURLFORM_CONTENTTYPE, type2, CURLFORM_END))
++errors;
/* make null character at start to check that contentslength works
correctly */
name3[1] = '\0';
value3[1] = '\0';
if (FormAddTest("PTRNAME + NAMELENGTH + COPYNAME + CONTENTSLENGTH test",
&httppost, &last_post,
CURLFORM_PTRNAME, name3, CURLFORM_COPYCONTENTS, value3,
CURLFORM_CONTENTSLENGTH, value3length,
CURLFORM_NAMELENGTH, name3length, CURLFORM_END))
++errors;
if (FormAddTest("simple PTRCONTENTS test", &httppost, &last_post,
CURLFORM_COPYNAME, name4, CURLFORM_PTRCONTENTS, value4,
CURLFORM_END))
++errors;
/* make null character at start to check that contentslength works
correctly */
value5[1] = '\0';
if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH test", &httppost, &last_post,
CURLFORM_COPYNAME, name5, CURLFORM_PTRCONTENTS, value5,
CURLFORM_CONTENTSLENGTH, value5length, CURLFORM_END))
++errors;
/* make null character at start to check that contentslength works
correctly */
value6[1] = '\0';
if (FormAddTest("PTRCONTENTS + CONTENTSLENGTH + CONTENTTYPE test",
&httppost, &last_post,
CURLFORM_COPYNAME, name6, CURLFORM_PTRCONTENTS, value6,
CURLFORM_CONTENTSLENGTH, value6length,
CURLFORM_CONTENTTYPE, type6, CURLFORM_END))
++errors;
if (FormAddTest("FILE + CONTENTTYPE test", &httppost, &last_post,
CURLFORM_COPYNAME, name7, CURLFORM_FILE, value7,
CURLFORM_CONTENTTYPE, type7, CURLFORM_END))
++errors;
if (FormAddTest("FILE1 + FILE2 test", &httppost, &last_post,
CURLFORM_COPYNAME, name8, CURLFORM_FILE, value7,
CURLFORM_FILE, value8, CURLFORM_END))
++errors;
if (FormAddTest("FILE1 + FILE2 + FILE3 test", &httppost, &last_post,
CURLFORM_COPYNAME, name9, CURLFORM_FILE, value7,
CURLFORM_FILE, value8, CURLFORM_FILE, value7, CURLFORM_END))
++errors;
forms[0].option = CURLFORM_FILE;
forms[0].value = value7;
forms[1].option = CURLFORM_FILE;
forms[1].value = value8;
forms[2].option = CURLFORM_FILE;
forms[2].value = value7;
forms[3].option = CURLFORM_END;
if (FormAddTest("FILE1 + FILE2 + FILE3 ARRAY test", &httppost, &last_post,
CURLFORM_COPYNAME, name10, CURLFORM_ARRAY, forms,
CURLFORM_END))
++errors;
if (FormAddTest("FILECONTENT test", &httppost, &last_post,
CURLFORM_COPYNAME, name11, CURLFORM_FILECONTENT, value7,
CURLFORM_END))
++errors;
rc = Curl_getFormData(&form, httppost, NULL, &size);
if(rc != CURLE_OK) {
if(rc != CURLE_READ_ERROR) {
const char *errortext = curl_easy_strerror(rc);
fprintf(stdout, "\n==> Curl_getFormData error: %s\n", errortext);
}
return 0;
}
Curl_FormInit(&formread, form);
do {
nread = Curl_FormReader(buffer, 1, sizeof(buffer),
(FILE *)&formread);
if(nread < 1)
break;
fwrite(buffer, nread, 1, stdout);
} while(1);
fprintf(stdout, "size: %d\n", size);
if (errors)
fprintf(stdout, "\n==> %d Test(s) failed!\n", errors);
else
fprintf(stdout, "\nAll Tests seem to have worked (please check output)\n");
return 0;
}
示例14: BtcRpcPacketPtr
BtcRpcPacketPtr BtcRpcCurl::SendRpc(BtcRpcPacketPtr jsonString)
{
if(!curl)
{
return BtcRpcPacketPtr();
}
WaitMutex();
BtcRpcPacketPtr receivedData = BtcRpcPacketPtr(new BtcRpcPacket()); // used when receiving data
/* Now specify we want to POST data */
curl_easy_setopt(curl, CURLOPT_POST, 1L);
/* we want to use our own read function (called when sending) */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* pointer to pass to our read function (cointains data to send) */
curl_easy_setopt(curl, CURLOPT_READDATA, jsonString.get());
/* we want to use our own write function (called when receiving) */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
/* pointer to pass to our write function (we'll write received data into it) */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, receivedData.get());
/* get verbose debug output please */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
/*
If you use POST to a HTTP 1.1 server, you can send data without knowing
the size before starting the POST if you use chunked encoding. You
enable this by adding a header like "Transfer-Encoding: chunked" with
CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
specify the size in the request.
*/
#ifdef USE_CHUNKED
{
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
/* use curl_slist_free_all() after the *perform() call to free this
list again */
}
#else
/* Set the expected POST size. If you want to POST large amounts of data,
consider CURLOPT_POSTFIELDSIZE_LARGE */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, jsonString->size());
#endif
#ifdef DISABLE_EXPECT
{
/*
Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
header. You can disable this header with CURLOPT_HTTPHEADER as usual.
NOTE: if you want chunked transfer too, you need to combine these two
since you can only set one list of headers with CURLOPT_HTTPHEADER. */
/* A less good option would be to enforce HTTP 1.0, but that might also
have other implications. */
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "Expect:");
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
/* use curl_slist_free_all() after the *perform() call to free this
list again */
}
#endif
/* 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));
mutex = false;
return BtcRpcPacketPtr();
}
//if(receivedData->data != NULL)
// opentxs::Log::Output(0, receivedData->data);
// we have to copy the response because for some reason the next few lines set the smart pointer to NULL (?!?!??)
BtcRpcPacketPtr packetCopy = BtcRpcPacketPtr(new BtcRpcPacket(receivedData));
receivedData.reset();
int httpcode = 0;
curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE, &httpcode);
if (httpcode == 401)
{
std::printf("Error connecting to bitcoind: Wrong username or password\n");
std::cout.flush();
mutex = false;
return BtcRpcPacketPtr();
}
else if (httpcode == 500)
{
std::printf("Bitcoind internal server error\n");
//.........这里部分代码省略.........
示例15: RD_ListGroup
//.........这里部分代码省略.........
memset(&xml_data,0,sizeof(xml_data));
parser=XML_ParserCreate(NULL);
XML_SetUserData(parser,&xml_data);
XML_SetElementHandler(parser,__ListGroupElementStart,
__ListGroupElementEnd);
XML_SetCharacterDataHandler(parser,__ListGroupElementData);
snprintf(url,1500,"http://%s/rd-bin/rdxport.cgi",hostname);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"COMMAND",
CURLFORM_COPYCONTENTS,
"5",
CURLFORM_END);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"LOGIN_NAME",
CURLFORM_COPYCONTENTS,
username,
CURLFORM_END);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"PASSWORD",
CURLFORM_COPYCONTENTS,
passwd,
CURLFORM_END);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"TICKET",
CURLFORM_COPYCONTENTS,
ticket,
CURLFORM_END);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"GROUP_NAME",
CURLFORM_COPYCONTENTS,
groupname,
CURLFORM_END);
//
// Check if User Agent Present otherwise set to default
if (strlen(user_agent)> 0){
curl_easy_setopt(curl, CURLOPT_USERAGENT,user_agent);
}
else
{
strcpy(user_agent_string, RD_GetUserAgent());
strcat(user_agent_string,VERSION);
curl_easy_setopt(curl, CURLOPT_USERAGENT,user_agent_string);
}
curl_easy_setopt(curl,CURLOPT_WRITEDATA,parser);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,__ListGroupCallback);
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_POST,1);
curl_easy_setopt(curl,CURLOPT_HTTPPOST,first);
curl_easy_setopt(curl,CURLOPT_NOPROGRESS,1);
curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errbuf);
// curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
#ifdef RIVC_DEBUG_OUT
size_t len = strlen(errbuf);
fprintf(stderr, "\nlibcurl error: (%d)", res);
if (len)
fprintf(stderr, "%s%s", errbuf,
((errbuf[len-1] != '\n') ? "\n" : ""));
else
fprintf(stderr, "%s\n", curl_easy_strerror(res));
#endif
curl_easy_cleanup(curl);
return -1;
}
/* The response OK - so figure out if we got what we wanted.. */
curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&response_code);
curl_formfree(first);
curl_easy_cleanup(curl);
if (response_code > 199 && response_code < 300) {
*grp=xml_data.group;
*numrecs = 1;
return 0;
}
else {
#ifdef RIVC_DEBUG_OUT
fprintf(stderr," rd_listgroup Call Returned Error: %s\n",xml_data.strbuf);
#endif
return (int)response_code;
}
}