diff options
Diffstat (limited to 'docs/examples')
33 files changed, 326 insertions, 359 deletions
diff --git a/docs/examples/10-at-a-time.c b/docs/examples/10-at-a-time.c index 455529182..aa1862ec5 100644 --- a/docs/examples/10-at-a-time.c +++ b/docs/examples/10-at-a-time.c @@ -86,7 +86,7 @@ static const char *urls[] = { }; #define MAX 10 /* number of simultaneous transfers */ -#define CNT sizeof(urls)/sizeof(char *) /* total number of transfers to do */ +#define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */ static size_t cb(char *d, size_t n, size_t l, void *p) { diff --git a/docs/examples/Makefile.netware b/docs/examples/Makefile.netware index 9fe9db453..2d85e736a 100644 --- a/docs/examples/Makefile.netware +++ b/docs/examples/Makefile.netware @@ -60,7 +60,7 @@ endif TARGET = examples VERSION = $(LIBCURL_VERSION) COPYR = Copyright (C) $(LIBCURL_COPYRIGHT_STR) -DESCR = curl ($(LIBARCH)) +DESCR = cURL ($(LIBARCH)) MTSAFE = YES STACK = 8192 SCREEN = Example Program diff --git a/docs/examples/anyauthput.c b/docs/examples/anyauthput.c index 1c9f965d5..b1367deb8 100644 --- a/docs/examples/anyauthput.c +++ b/docs/examples/anyauthput.c @@ -147,13 +147,13 @@ int main(int argc, char **argv) curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); /* which file to upload */ - curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&hd); + curl_easy_setopt(curl, CURLOPT_READDATA, (void*)&hd); /* set the ioctl function */ curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl); /* pass the file descriptor to the ioctl callback as well */ - curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void *)&hd); + curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)&hd); /* enable "uploading" (which means PUT when doing HTTP) */ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); diff --git a/docs/examples/asiohiper.cpp b/docs/examples/asiohiper.cpp index ced4ef419..d6065e554 100644 --- a/docs/examples/asiohiper.cpp +++ b/docs/examples/asiohiper.cpp @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 2012 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 2012 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -40,8 +40,8 @@ * Note: * For the sake of simplicity, URL is hard coded to "www.google.com" * - * This is purely a demo app, all retrieved data is simply discarded by the - * write callback. + * This is purely a demo app, all retrieved data is simply discarded by the write + * callback. */ @@ -85,12 +85,14 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) /* cancel running timer */ timer.cancel(); - if(timeout_ms > 0) { + if(timeout_ms > 0) + { /* update timer */ timer.expires_from_now(boost::posix_time::millisec(timeout_ms)); timer.async_wait(boost::bind(&timer_cb, _1, g)); } - else { + else + { /* call timeout function immediately */ boost::system::error_code error; /*success*/ timer_cb(error, g); @@ -102,9 +104,11 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) /* Die if we get a bad CURLMcode somewhere */ static void mcode_or_die(const char *where, CURLMcode code) { - if(CURLM_OK != code) { + if(CURLM_OK != code) + { const char *s; - switch(code) { + switch(code) + { case CURLM_CALL_MULTI_PERFORM: s = "CURLM_CALL_MULTI_PERFORM"; break; @@ -154,8 +158,10 @@ static void check_multi_info(GlobalInfo *g) fprintf(MSG_OUT, "\nREMAINING: %d", g->still_running); - while((msg = curl_multi_info_read(g->multi, &msgs_left))) { - if(msg->msg == CURLMSG_DONE) { + while((msg = curl_multi_info_read(g->multi, &msgs_left))) + { + if(msg->msg == CURLMSG_DONE) + { easy = msg->easy_handle; res = msg->data.result; curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn); @@ -171,55 +177,33 @@ static void check_multi_info(GlobalInfo *g) /* Called by asio when there is an action on a socket */ static void event_cb(GlobalInfo *g, boost::asio::ip::tcp::socket *tcp_socket, - int action, const boost::system::error_code & error, - int *fdp) + int action) { fprintf(MSG_OUT, "\nevent_cb: action=%d", action); - /* make sure the event matches what are wanted */ - if(*fdp == action || *fdp == CURL_POLL_INOUT) { - curl_socket_t s = tcp_socket->native_handle(); - CURLMcode rc; - if(error) - action = CURL_CSELECT_ERR; - rc = curl_multi_socket_action(g->multi, s, action, &g->still_running); - - mcode_or_die("event_cb: curl_multi_socket_action", rc); - check_multi_info(g); + CURLMcode rc; + rc = curl_multi_socket_action(g->multi, tcp_socket->native_handle(), action, + &g->still_running); - if(g->still_running <= 0) { - fprintf(MSG_OUT, "\nlast transfer done, kill timeout"); - timer.cancel(); - } + mcode_or_die("event_cb: curl_multi_socket_action", rc); + check_multi_info(g); - /* keep on watching. - * the socket may have been closed and/or fdp may have been changed - * in curl_multi_socket_action(), so check them both */ - if(!error && socket_map.find(s) != socket_map.end() && - (*fdp == action || *fdp == CURL_POLL_INOUT)) { - if(action == CURL_POLL_IN) { - tcp_socket->async_read_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - action, _1, fdp)); - } - if(action == CURL_POLL_OUT) { - tcp_socket->async_write_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - action, _1, fdp)); - } - } + if(g->still_running <= 0) + { + fprintf(MSG_OUT, "\nlast transfer done, kill timeout"); + timer.cancel(); } } /* Called by asio when our timeout expires */ static void timer_cb(const boost::system::error_code & error, GlobalInfo *g) { - if(!error) { + if(!error) + { fprintf(MSG_OUT, "\ntimer_cb: "); CURLMcode rc; - rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, - &g->still_running); + rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running); mcode_or_die("timer_cb: curl_multi_socket_action", rc); check_multi_info(g); @@ -231,21 +215,22 @@ static void remsock(int *f, GlobalInfo *g) { fprintf(MSG_OUT, "\nremsock: "); - if(f) { + if(f) + { free(f); } } -static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact, - GlobalInfo *g) +static void setsock(int *fdp, curl_socket_t s, CURL*e, int act, GlobalInfo*g) { fprintf(MSG_OUT, "\nsetsock: socket=%d, act=%d, fdp=%p", s, act, fdp); - std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = - socket_map.find(s); + std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = socket_map.find(s); - if(it == socket_map.end()) { + if(it == socket_map.end()) + { fprintf(MSG_OUT, "\nsocket %d is a c-ares socket, ignoring", s); + return; } @@ -253,34 +238,29 @@ static void setsock(int *fdp, curl_socket_t s, CURL *e, int act, int oldact, *fdp = act; - if(act == CURL_POLL_IN) { + if(act == CURL_POLL_IN) + { fprintf(MSG_OUT, "\nwatching for socket to become readable"); - if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) { - tcp_socket->async_read_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_IN, _1, fdp)); - } + + tcp_socket->async_read_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); } - else if(act == CURL_POLL_OUT) { + else if (act == CURL_POLL_OUT) + { fprintf(MSG_OUT, "\nwatching for socket to become writable"); - if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) { - tcp_socket->async_write_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_OUT, _1, fdp)); - } + + tcp_socket->async_write_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); } - else if(act == CURL_POLL_INOUT) { + else if(act == CURL_POLL_INOUT) + { fprintf(MSG_OUT, "\nwatching for socket to become readable & writable"); - if(oldact != CURL_POLL_IN && oldact != CURL_POLL_INOUT) { - tcp_socket->async_read_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_IN, _1, fdp)); - } - if(oldact != CURL_POLL_OUT && oldact != CURL_POLL_INOUT) { - tcp_socket->async_write_some(boost::asio::null_buffers(), - boost::bind(&event_cb, g, tcp_socket, - CURL_POLL_OUT, _1, fdp)); - } + + tcp_socket->async_read_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); + + tcp_socket->async_write_some(boost::asio::null_buffers(), + boost::bind(&event_cb, g, tcp_socket, act)); } } @@ -289,7 +269,7 @@ static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g) /* fdp is used to store current action */ int *fdp = (int *) calloc(sizeof(int), 1); - setsock(fdp, s, easy, action, 0, g); + setsock(fdp, s, easy, action, g); curl_multi_assign(g->multi, s, fdp); } @@ -305,20 +285,24 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) fprintf(MSG_OUT, "\nsocket callback: s=%d e=%p what=%s ", s, e, whatstr[what]); - if(what == CURL_POLL_REMOVE) { + if(what == CURL_POLL_REMOVE) + { fprintf(MSG_OUT, "\n"); remsock(actionp, g); } - else { - if(!actionp) { + else + { + if(!actionp) + { fprintf(MSG_OUT, "\nAdding data: %s", whatstr[what]); addsock(s, e, what, g); } - else { + else + { fprintf(MSG_OUT, "\nChanging action from %s to %s", whatstr[*actionp], whatstr[what]); - setsock(actionp, s, e, what, *actionp, g); + setsock(actionp, s, e, what, g); } } @@ -328,8 +312,9 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) /* CURLOPT_WRITEFUNCTION */ static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) { + size_t written = size * nmemb; - char *pBuffer = (char *)malloc(written + 1); + char* pBuffer = (char *) malloc(written + 1); strncpy(pBuffer, (const char *)ptr, written); pBuffer[written] = '\0'; @@ -365,28 +350,28 @@ static curl_socket_t opensocket(void *clientp, curlsocktype purpose, curl_socket_t sockfd = CURL_SOCKET_BAD; /* restrict to IPv4 */ - if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) { + if(purpose == CURLSOCKTYPE_IPCXN && address->family == AF_INET) + { /* create a tcp socket object */ - boost::asio::ip::tcp::socket *tcp_socket = - new boost::asio::ip::tcp::socket(io_service); + boost::asio::ip::tcp::socket *tcp_socket = new boost::asio::ip::tcp::socket(io_service); /* open it and get the native handle*/ boost::system::error_code ec; tcp_socket->open(boost::asio::ip::tcp::v4(), ec); - if(ec) { + if(ec) + { /* An error occurred */ - std::cout << std::endl << "Couldn't open socket [" << ec << "][" << - ec.message() << "]"; + std::cout << std::endl << "Couldn't open socket [" << ec << "][" << ec.message() << "]"; fprintf(MSG_OUT, "\nERROR: Returning CURL_SOCKET_BAD to signal error"); } - else { + else + { sockfd = tcp_socket->native_handle(); fprintf(MSG_OUT, "\nOpened socket %d", sockfd); /* save it for monitoring */ - socket_map.insert(std::pair<curl_socket_t, - boost::asio::ip::tcp::socket *>(sockfd, tcp_socket)); + socket_map.insert(std::pair<curl_socket_t, boost::asio::ip::tcp::socket *>(sockfd, tcp_socket)); } } @@ -398,10 +383,10 @@ static int close_socket(void *clientp, curl_socket_t item) { fprintf(MSG_OUT, "\nclose_socket : %d", item); - std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = - socket_map.find(item); + std::map<curl_socket_t, boost::asio::ip::tcp::socket *>::iterator it = socket_map.find(item); - if(it != socket_map.end()) { + if(it != socket_map.end()) + { delete it->second; socket_map.erase(it); } @@ -418,8 +403,10 @@ static void new_conn(char *url, GlobalInfo *g) conn = (ConnInfo *) calloc(1, sizeof(ConnInfo)); conn->easy = curl_easy_init(); - if(!conn->easy) { + if(!conn->easy) + { fprintf(MSG_OUT, "\ncurl_easy_init() failed, exiting!"); + exit(2); } diff --git a/docs/examples/cacertinmem.c b/docs/examples/cacertinmem.c index ace58e48e..bba8c722e 100644 --- a/docs/examples/cacertinmem.c +++ b/docs/examples/cacertinmem.c @@ -34,12 +34,12 @@ size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream) return (nmemb*size); } -static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm) +static CURLcode sslctx_function(CURL * curl, void * sslctx, void * parm) { - X509_STORE *store; - X509 *cert=NULL; - BIO *bio; - char *mypem = /* www.cacert.org */ + X509_STORE * store; + X509 * cert=NULL; + BIO * bio; + char * mypem = /* www.cacert.org */ "-----BEGIN CERTIFICATE-----\n"\ "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\ "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\ @@ -107,7 +107,7 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm) int main(void) { - CURL *ch; + CURL * ch; CURLcode rv; rv=curl_global_init(CURL_GLOBAL_ALL); diff --git a/docs/examples/curlx.c b/docs/examples/curlx.c index cd1677af3..155da2371 100644 --- a/docs/examples/curlx.c +++ b/docs/examples/curlx.c @@ -133,14 +133,14 @@ static const char *curlx_usage[]={ /* This is a context that we pass to all callbacks */ typedef struct sslctxparm_st { - unsigned char *p12file; - const char *pst; - PKCS12 *p12; - EVP_PKEY *pkey; - X509 *usercert; + unsigned char * p12file; + const char * pst; + PKCS12 * p12; + EVP_PKEY * pkey; + X509 * usercert; STACK_OF(X509) * ca; - CURL *curl; - BIO *errorbio; + CURL * curl; + BIO * errorbio; int accesstype; int verbose; @@ -196,12 +196,11 @@ static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg) BIO_printf(p->errorbio, "entering ssl_app_verify_callback\n"); if((ok= X509_verify_cert(ctx)) && ctx->cert) { - unsigned char *accessinfo; + unsigned char * accessinfo; if(p->verbose > 1) X509_print_ex(p->errorbio, ctx->cert, 0, 0); - accessinfo = my_get_ext(ctx->cert, p->accesstype, NID_sinfo_access); - if(accessinfo) { + if(accessinfo = my_get_ext(ctx->cert, p->accesstype, NID_sinfo_access)) { if(p->verbose) BIO_printf(p->errorbio, "Setting URL from SIA to: %s\n", accessinfo); @@ -229,10 +228,10 @@ static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg) - an application verification callback (the function above) */ -static CURLcode sslctxfun(CURL *curl, void *sslctx, void *parm) +static CURLcode sslctxfun(CURL * curl, void * sslctx, void * parm) { - sslctxparm *p = (sslctxparm *) parm; - SSL_CTX *ctx = (SSL_CTX *) sslctx; + sslctxparm * p = (sslctxparm *) parm; + SSL_CTX * ctx = (SSL_CTX *) sslctx; if(!SSL_CTX_use_certificate(ctx, p->usercert)) { BIO_printf(p->errorbio, "SSL_CTX_use_certificate problem\n"); @@ -271,30 +270,30 @@ int main(int argc, char **argv) BIO* in=NULL; BIO* out=NULL; - char *outfile = NULL; - char *infile = NULL; + char * outfile = NULL; + char * infile = NULL; int tabLength=100; char *binaryptr; - char *mimetype; - char *mimetypeaccept=NULL; - char *contenttype; - const char **pp; - unsigned char *hostporturl = NULL; - BIO *p12bio; + char* mimetype; + char* mimetypeaccept=NULL; + char* contenttype; + const char** pp; + unsigned char* hostporturl = NULL; + BIO * p12bio; char **args = argv + 1; - unsigned char *serverurl; + unsigned char * serverurl; sslctxparm p; char *response; CURLcode res; - struct curl_slist *headers=NULL; + struct curl_slist * headers=NULL; int badarg=0; binaryptr = malloc(tabLength); p.verbose = 0; - p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE); + p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE); curl_global_init(CURL_GLOBAL_DEFAULT); @@ -356,8 +355,7 @@ int main(int argc, char **argv) } else if(strcmp(*args, "-accesstype") == 0) { if(args[1]) { - p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args, 0)); - if(p.accesstype == 0) + if((p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args, 0))) == 0) badarg=1; } else @@ -410,21 +408,18 @@ int main(int argc, char **argv) } - p.errorbio = BIO_new_fp(stderr, BIO_NOCLOSE); + p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE); - p.curl = curl_easy_init(); - if(!p.curl) { + if(!(p.curl = curl_easy_init())) { BIO_printf(p.errorbio, "Cannot init curl lib\n"); goto err; } - p12bio = BIO_new_file(p.p12file, "rb"); - if(!p12bio) { + if(!(p12bio = BIO_new_file(p.p12file, "rb"))) { BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file); goto err; } - p.p12 = d2i_PKCS12_bio(p12bio, NULL); - if(!p.p12) { + if(!(p.p12 = d2i_PKCS12_bio (p12bio, NULL))) { BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file); goto err; } @@ -452,19 +447,16 @@ int main(int argc, char **argv) } else if(p.accesstype != 0) { /* see whether we can find an AIA or SIA for a given access type */ - serverurl = my_get_ext(p.usercert, p.accesstype, NID_info_access); - if(!serverurl) { + if(!(serverurl = my_get_ext(p.usercert, p.accesstype, NID_info_access))) { int j=0; BIO_printf(p.errorbio, "no service URL in user cert " "cherching in others certificats\n"); for(j=0; j<sk_X509_num(p.ca); j++) { - serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype, - NID_info_access); - if(serverurl) + if((serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype, + NID_info_access))) break; - serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype, - NID_sinfo_access); - if(serverurl) + if((serverurl = my_get_ext(sk_X509_value(p.ca, j), p.accesstype, + NID_sinfo_access))) break; } } @@ -513,7 +505,7 @@ int main(int argc, char **argv) { int lu; int i=0; - while((lu = BIO_read(in, &binaryptr[i], tabLength-i)) >0) { + while((lu = BIO_read (in, &binaryptr[i], tabLength-i)) >0) { i+=lu; if(i== tabLength) { tabLength+=100; diff --git a/docs/examples/debug.c b/docs/examples/debug.c index cbf1c1106..f5d58bf19 100644 --- a/docs/examples/debug.c +++ b/docs/examples/debug.c @@ -88,7 +88,7 @@ int my_trace(CURL *handle, curl_infotype type, const char *text; (void)handle; /* prevent compiler warning */ - switch(type) { + switch (type) { case CURLINFO_TEXT: fprintf(stderr, "== Info: %s", data); default: /* in case a new one is introduced to shock us */ diff --git a/docs/examples/evhiperfifo.c b/docs/examples/evhiperfifo.c index efe42475a..118f152ee 100644 --- a/docs/examples/evhiperfifo.c +++ b/docs/examples/evhiperfifo.c @@ -86,7 +86,7 @@ typedef struct _GlobalInfo struct ev_timer timer_event; CURLM *multi; int still_running; - FILE *input; + FILE* input; } GlobalInfo; @@ -134,7 +134,7 @@ static void mcode_or_die(const char *where, CURLMcode code) { if(CURLM_OK != code) { const char *s; - switch(code) { + switch (code) { case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break; @@ -243,8 +243,7 @@ static void remsock(SockInfo *f, GlobalInfo *g) /* Assign information to a SockInfo structure */ -static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act, - GlobalInfo *g) +static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g) { printf("%s \n", __PRETTY_FUNCTION__); @@ -317,8 +316,8 @@ static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) /* CURLOPT_PROGRESSFUNCTION */ -static int prog_cb(void *p, double dltotal, double dlnow, double ult, - double uln) +static int prog_cb (void *p, double dltotal, double dlnow, double ult, + double uln) { ConnInfo *conn = (ConnInfo *)p; (void)ult; @@ -388,7 +387,7 @@ static void fifo_cb(EV_P_ struct ev_io *w, int revents) } /* Create a named pipe and tell libevent to monitor it */ -static int init_fifo(GlobalInfo *g) +static int init_fifo (GlobalInfo *g) { struct stat st; static const char *fifo = "hiper.fifo"; @@ -399,18 +398,18 @@ static int init_fifo(GlobalInfo *g) if((st.st_mode & S_IFMT) == S_IFREG) { errno = EEXIST; perror("lstat"); - exit(1); + exit (1); } } unlink(fifo); if(mkfifo (fifo, 0600) == -1) { perror("mkfifo"); - exit(1); + exit (1); } sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0); if(sockfd == -1) { perror("open"); - exit(1); + exit (1); } g->input = fdopen(sockfd, "r"); diff --git a/docs/examples/externalsocket.c b/docs/examples/externalsocket.c index 918f08218..9b144b42d 100644 --- a/docs/examples/externalsocket.c +++ b/docs/examples/externalsocket.c @@ -90,8 +90,9 @@ int main(void) #ifdef WIN32 WSADATA wsaData; - int initwsa = WSAStartup(MAKEWORD(2, 0), &wsaData); - if(initwsa != 0) { + int initwsa; + + if((initwsa = WSAStartup(MAKEWORD(2, 0), &wsaData)) != 0) { printf("WSAStartup failed: %d\n", initwsa); return 1; } @@ -106,8 +107,7 @@ int main(void) curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999"); /* Create the socket "manually" */ - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if(sockfd == CURL_SOCKET_BAD) { + if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == CURL_SOCKET_BAD) { printf("Error creating listening socket.\n"); return 3; } @@ -116,8 +116,7 @@ int main(void) servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORTNUM); - servaddr.sin_addr.s_addr = inet_addr(IPADDR); - if(INADDR_NONE == servaddr.sin_addr.s_addr) + if(INADDR_NONE == (servaddr.sin_addr.s_addr = inet_addr(IPADDR))) return 2; if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == diff --git a/docs/examples/fileupload.c b/docs/examples/fileupload.c index 6b05c4cec..363fae694 100644 --- a/docs/examples/fileupload.c +++ b/docs/examples/fileupload.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -37,12 +37,16 @@ int main(void) FILE *fd; fd = fopen("debugit", "rb"); /* open file to upload */ - if(!fd) + if(!fd) { + return 1; /* can't continue */ + } /* to get the file size */ - if(fstat(fileno(fd), &file_info) != 0) + if(fstat(fileno(fd), &file_info) != 0) { + return 1; /* can't continue */ + } curl = curl_easy_init(); if(curl) { @@ -82,6 +86,5 @@ int main(void) /* always cleanup */ curl_easy_cleanup(curl); } - fclose(fd); return 0; } diff --git a/docs/examples/fopen.c b/docs/examples/fopen.c index 7435264a7..71be178ef 100644 --- a/docs/examples/fopen.c +++ b/docs/examples/fopen.c @@ -84,7 +84,7 @@ URL_FILE *url_fopen(const char *url, const char *operation); int url_fclose(URL_FILE *file); int url_feof(URL_FILE *file); size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file); -char *url_fgets(char *ptr, size_t size, URL_FILE *file); +char * url_fgets(char *ptr, size_t size, URL_FILE *file); void url_rewind(URL_FILE *file); /* we use a global one for convenience */ diff --git a/docs/examples/ftpupload.c b/docs/examples/ftpupload.c index 41e2d807c..bba0c4d53 100644 --- a/docs/examples/ftpupload.c +++ b/docs/examples/ftpupload.c @@ -127,7 +127,7 @@ int main(void) curl_easy_strerror(res)); /* clean up the FTP commands list */ - curl_slist_free_all(headerlist); + curl_slist_free_all (headerlist); /* always cleanup */ curl_easy_cleanup(curl); diff --git a/docs/examples/ftpuploadresume.c b/docs/examples/ftpuploadresume.c index 8f7f45dae..641563404 100644 --- a/docs/examples/ftpuploadresume.c +++ b/docs/examples/ftpuploadresume.c @@ -36,8 +36,8 @@ /* The MinGW headers are missing a few Win32 function definitions, you shouldn't need this if you use VC++ */ #if defined(__MINGW32__) && !defined(__MINGW64__) -int __cdecl _snscanf(const char *input, size_t length, - const char *format, ...); +int __cdecl _snscanf(const char * input, size_t length, + const char * format, ...); #endif @@ -77,7 +77,7 @@ size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream) } -int upload(CURL *curlhandle, const char *remotepath, const char *localpath, +int upload(CURL *curlhandle, const char * remotepath, const char * localpath, long timeout, long tries) { FILE *f; diff --git a/docs/examples/ghiper.c b/docs/examples/ghiper.c index a1af4c5a3..7317a63d8 100644 --- a/docs/examples/ghiper.c +++ b/docs/examples/ghiper.c @@ -94,11 +94,10 @@ typedef struct _SockInfo { } SockInfo; /* Die if we get a bad CURLMcode somewhere */ -static void mcode_or_die(const char *where, CURLMcode code) -{ +static void mcode_or_die(const char *where, CURLMcode code) { if(CURLM_OK != code) { const char *s; - switch(code) { + switch (code) { case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break; case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break; case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break; @@ -207,8 +206,7 @@ static void remsock(SockInfo *f) } /* Assign information to a SockInfo structure */ -static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act, - GlobalInfo *g) +static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g) { GIOCondition kind = (act&CURL_POLL_IN?G_IO_IN:0)|(act&CURL_POLL_OUT?G_IO_OUT:0); @@ -272,8 +270,8 @@ static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) } /* CURLOPT_PROGRESSFUNCTION */ -static int prog_cb(void *p, double dltotal, double dlnow, double ult, - double uln) +static int prog_cb (void *p, double dltotal, double dlnow, double ult, + double uln) { ConnInfo *conn = (ConnInfo *)p; MSG_OUT("Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal); @@ -318,7 +316,7 @@ static void new_conn(char *url, GlobalInfo *g) } /* This gets called by glib whenever data is received from the fifo */ -static gboolean fifo_cb(GIOChannel *ch, GIOCondition condition, gpointer data) +static gboolean fifo_cb (GIOChannel *ch, GIOCondition condition, gpointer data) { #define BUF_SIZE 1024 gsize len, tp; @@ -380,21 +378,21 @@ int init_fifo(void) if((st.st_mode & S_IFMT) == S_IFREG) { errno = EEXIST; perror("lstat"); - exit(1); + exit (1); } } - unlink(fifo); + unlink (fifo); if(mkfifo (fifo, 0600) == -1) { perror("mkfifo"); - exit(1); + exit (1); } - socket = open(fifo, O_RDWR | O_NONBLOCK, 0); + socket = open (fifo, O_RDWR | O_NONBLOCK, 0); if(socket == -1) { perror("open"); - exit(1); + exit (1); } MSG_OUT("Now, pipe some URL's into > %s\n", fifo); diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index f08fcda99..98bcafe6d 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -82,7 +82,7 @@ typedef struct _GlobalInfo struct event *timer_event; CURLM *multi; int still_running; - FILE *input; + FILE* input; } GlobalInfo; @@ -128,7 +128,7 @@ static void mcode_or_die(const char *where, CURLMcode code) { if(CURLM_OK != code) { const char *s; - switch(code) { + switch (code) { case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break; case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break; case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break; @@ -230,8 +230,7 @@ static void remsock(SockInfo *f) /* Assign information to a SockInfo structure */ -static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act, - GlobalInfo *g) +static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g) { int kind = (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST; @@ -300,8 +299,8 @@ static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) /* CURLOPT_PROGRESSFUNCTION */ -static int prog_cb(void *p, double dltotal, double dlnow, double ult, - double uln) +static int prog_cb (void *p, double dltotal, double dlnow, double ult, + double uln) { ConnInfo *conn = (ConnInfo *)p; (void)ult; @@ -371,7 +370,7 @@ static void fifo_cb(int fd, short event, void *arg) /* Create a named pipe and tell libevent to monitor it */ static const char *fifo = "hiper.fifo"; -static int init_fifo(GlobalInfo *g) +static int init_fifo (GlobalInfo *g) { struct stat st; curl_socket_t sockfd; @@ -381,18 +380,18 @@ static int init_fifo(GlobalInfo *g) if((st.st_mode & S_IFMT) == S_IFREG) { errno = EEXIST; perror("lstat"); - exit(1); + exit (1); } } unlink(fifo); if(mkfifo (fifo, 0600) == -1) { perror("mkfifo"); - exit(1); + exit (1); } sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0); if(sockfd == -1) { perror("open"); - exit(1); + exit (1); } g->input = fdopen(sockfd, "r"); diff --git a/docs/examples/htmltitle.cpp b/docs/examples/htmltitle.cpp index 8148888a4..5e6b4a003 100644 --- a/docs/examples/htmltitle.cpp +++ b/docs/examples/htmltitle.cpp @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -22,14 +22,14 @@ /* <DESC> * Get a web page, extract the title with libxml. * </DESC> + */ +// Written by Lars Nilsson +// +// GNU C++ compile command line suggestion (edit paths accordingly): +// +// g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \ +// -o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2 - Written by Lars Nilsson - - GNU C++ compile command line suggestion (edit paths accordingly): - - g++ -Wall -I/opt/curl/include -I/opt/libxml/include/libxml2 htmltitle.cpp \ - -o htmltitle -L/opt/curl/lib -L/opt/libxml/lib -lcurl -lxml2 -*/ #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -72,7 +72,7 @@ static std::string buffer; static int writer(char *data, size_t size, size_t nmemb, std::string *writerData) { - if(writerData == NULL) + if (writerData == NULL) return 0; writerData->append(data, size*nmemb); @@ -90,38 +90,50 @@ static bool init(CURL *&conn, char *url) conn = curl_easy_init(); - if(conn == NULL) { + if (conn == NULL) + { fprintf(stderr, "Failed to create CURL connection\n"); + exit(EXIT_FAILURE); } code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, errorBuffer); - if(code != CURLE_OK) { + if (code != CURLE_OK) + { fprintf(stderr, "Failed to set error buffer [%d]\n", code); + return false; } code = curl_easy_setopt(conn, CURLOPT_URL, url); - if(code != CURLE_OK) { + if (code != CURLE_OK) + { fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer); + return false; } code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1L); - if(code != CURLE_OK) { + if (code != CURLE_OK) + { fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer); + return false; } code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer); - if(code != CURLE_OK) { + if (code != CURLE_OK) + { fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer); + return false; } code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, &buffer); - if(code != CURLE_OK) { + if (code != CURLE_OK) + { fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer); + return false; } @@ -138,7 +150,8 @@ static void StartElement(void *voidContext, { Context *context = (Context *)voidContext; - if(COMPARE((char *)name, "TITLE")) { + if (COMPARE((char *)name, "TITLE")) + { context->title = ""; context->addTitle = true; } @@ -154,7 +167,7 @@ static void EndElement(void *voidContext, { Context *context = (Context *)voidContext; - if(COMPARE((char *)name, "TITLE")) + if (COMPARE((char *)name, "TITLE")) context->addTitle = false; } @@ -166,7 +179,7 @@ static void handleCharacters(Context *context, const xmlChar *chars, int length) { - if(context->addTitle) + if (context->addTitle) context->title.append((char *)chars, length); } @@ -260,8 +273,10 @@ int main(int argc, char *argv[]) // Ensure one argument is given - if(argc != 2) { + if (argc != 2) + { fprintf(stderr, "Usage: %s <url>\n", argv[0]); + exit(EXIT_FAILURE); } @@ -269,8 +284,10 @@ int main(int argc, char *argv[]) // Initialize CURL connection - if(!init(conn, argv[1])) { + if (!init(conn, argv[1])) + { fprintf(stderr, "Connection initializion failed\n"); + exit(EXIT_FAILURE); } @@ -279,15 +296,19 @@ int main(int argc, char *argv[]) code = curl_easy_perform(conn); curl_easy_cleanup(conn); - if(code != CURLE_OK) { + if (code != CURLE_OK) + { fprintf(stderr, "Failed to get '%s' [%s]\n", argv[1], errorBuffer); + exit(EXIT_FAILURE); } // Parse the (assumed) HTML code + parseHtml(buffer, title); // Display the extracted title + printf("Title: %s\n", title.c_str()); return EXIT_SUCCESS; diff --git a/docs/examples/http2-download.c b/docs/examples/http2-download.c index c82fed285..9e87a064e 100644 --- a/docs/examples/http2-download.c +++ b/docs/examples/http2-download.c @@ -114,7 +114,7 @@ int my_trace(CURL *handle, curl_infotype type, int num = hnd2num(handle); (void)handle; /* prevent compiler warning */ (void)userp; - switch(type) { + switch (type) { case CURLINFO_TEXT: fprintf(stderr, "== %d Info: %s", num, data); default: /* in case a new one is introduced to shock us */ diff --git a/docs/examples/http2-serverpush.c b/docs/examples/http2-serverpush.c index ecd9cc5d4..1a8c69b7d 100644 --- a/docs/examples/http2-serverpush.c +++ b/docs/examples/http2-serverpush.c @@ -93,7 +93,7 @@ int my_trace(CURL *handle, curl_infotype type, const char *text; (void)handle; /* prevent compiler warning */ (void)userp; - switch(type) { + switch (type) { case CURLINFO_TEXT: fprintf(stderr, "== Info: %s", data); default: /* in case a new one is introduced to shock us */ diff --git a/docs/examples/http2-upload.c b/docs/examples/http2-upload.c index 67e17f34b..b6353713e 100644 --- a/docs/examples/http2-upload.c +++ b/docs/examples/http2-upload.c @@ -133,7 +133,7 @@ int my_trace(CURL *handle, curl_infotype type, snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld", now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec); - switch(type) { + switch (type) { case CURLINFO_TEXT: fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data); default: /* in case a new one is introduced to shock us */ diff --git a/docs/examples/imap-append.c b/docs/examples/imap-append.c index bbf9fe436..3f832897d 100644 --- a/docs/examples/imap-append.c +++ b/docs/examples/imap-append.c @@ -85,8 +85,6 @@ int main(void) { CURL *curl; CURLcode res = CURLE_OK; - const char **p; - long infilesize; struct upload_status upload_ctx; upload_ctx.lines_read = 0; @@ -109,12 +107,6 @@ int main(void) curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); - infilesize = 0; - for(p = payload_text; *p; ++p) { - infilesize += (long)strlen(*p); - } - curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize); - /* Perform the append */ res = curl_easy_perform(curl); diff --git a/docs/examples/multi-app.c b/docs/examples/multi-app.c index 9a8ecfeb8..f8447930a 100644 --- a/docs/examples/multi-app.c +++ b/docs/examples/multi-app.c @@ -156,7 +156,7 @@ int main(void) break; } - switch(idx) { + switch (idx) { case HTTP_HANDLE: printf("HTTP transfer completed with status %d\n", msg->data.result); break; diff --git a/docs/examples/multi-debugcallback.c b/docs/examples/multi-debugcallback.c index be241fc72..d07cc3bb9 100644 --- a/docs/examples/multi-debugcallback.c +++ b/docs/examples/multi-debugcallback.c @@ -96,7 +96,7 @@ int my_trace(CURL *handle, curl_infotype type, (void)userp; (void)handle; /* prevent compiler warning */ - switch(type) { + switch (type) { case CURLINFO_TEXT: fprintf(stderr, "== Info: %s", data); default: /* in case a new one is introduced to shock us */ diff --git a/docs/examples/multi-post.c b/docs/examples/multi-post.c index cc2ca18e9..5d9abc12f 100644 --- a/docs/examples/multi-post.c +++ b/docs/examples/multi-post.c @@ -165,7 +165,7 @@ int main(void) curl_formfree(formpost); /* free slist */ - curl_slist_free_all(headerlist); + curl_slist_free_all (headerlist); } return 0; } diff --git a/docs/examples/multi-uv.c b/docs/examples/multi-uv.c index 37e4f4781..51526c894 100644 --- a/docs/examples/multi-uv.c +++ b/docs/examples/multi-uv.c @@ -24,12 +24,18 @@ * multi_socket API using libuv * </DESC> */ -/* Example application using the multi socket interface to download multiple - files in parallel, powered by libuv. +/* Example application code using the multi socket interface to download + multiple files at once, but instead of using curl_multi_perform and + curl_multi_wait, which uses select(), we use libuv. + It supports epoll, kqueue, etc. on unixes and fast IO completion ports on + Windows, which means, it should be very fast on all platforms.. + + Written by Clemens Gruber, based on an outdated example from uvbook and + some tests from libuv. Requires libuv and (of course) libcurl. - See https://nikhilm.github.com/uvbook/ for more information on libuv. + See http://nikhilm.github.com/uvbook/ for more information on libuv. */ #include <stdio.h> @@ -46,7 +52,7 @@ typedef struct curl_context_s { curl_socket_t sockfd; } curl_context_t; -static curl_context_t* create_curl_context(curl_socket_t sockfd) +curl_context_t* create_curl_context(curl_socket_t sockfd) { curl_context_t *context; @@ -60,18 +66,19 @@ static curl_context_t* create_curl_context(curl_socket_t sockfd) return context; } -static void curl_close_cb(uv_handle_t *handle) +void curl_close_cb(uv_handle_t *handle) { curl_context_t *context = (curl_context_t *) handle->data; free(context); } -static void destroy_curl_context(curl_context_t *context) +void destroy_curl_context(curl_context_t *context) { uv_close((uv_handle_t *) &context->poll_handle, curl_close_cb); } -static void add_download(const char *url, int num) + +void add_download(const char *url, int num) { char filename[50]; FILE *file; @@ -95,28 +102,22 @@ static void add_download(const char *url, int num) static void check_multi_info(void) { + int running_handles; char *done_url; CURLMsg *message; int pending; - CURL *easy_handle; FILE *file; while((message = curl_multi_info_read(curl_handle, &pending))) { switch(message->msg) { case CURLMSG_DONE: - /* Do not use message data after calling curl_multi_remove_handle() and - curl_easy_cleanup(). As per curl_multi_info_read() docs: - "WARNING: The data the returned pointer points to will not survive - calling curl_multi_cleanup, curl_multi_remove_handle or - curl_easy_cleanup." */ - easy_handle = message->easy_handle; - - curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &done_url); - curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &file); + curl_easy_getinfo(message->easy_handle, CURLINFO_EFFECTIVE_URL, + &done_url); + curl_easy_getinfo(message->easy_handle, CURLINFO_PRIVATE, &file); printf("%s DONE\n", done_url); - curl_multi_remove_handle(curl_handle, easy_handle); - curl_easy_cleanup(easy_handle); + curl_multi_remove_handle(curl_handle, message->easy_handle); + curl_easy_cleanup(message->easy_handle); if(file) { fclose(file); } @@ -129,18 +130,23 @@ static void check_multi_info(void) } } -static void curl_perform(uv_poll_t *req, int status, int events) +void curl_perform(uv_poll_t *req, int status, int events) { int running_handles; int flags = 0; curl_context_t *context; + char *done_url; + CURLMsg *message; + int pending; + + uv_timer_stop(&timeout); if(events & UV_READABLE) flags |= CURL_CSELECT_IN; if(events & UV_WRITABLE) flags |= CURL_CSELECT_OUT; - context = (curl_context_t *) req->data; + context = (curl_context_t *) req; curl_multi_socket_action(curl_handle, context->sockfd, flags, &running_handles); @@ -148,7 +154,7 @@ static void curl_perform(uv_poll_t *req, int status, int events) check_multi_info(); } -static void on_timeout(uv_timer_t *req, int status) +void on_timeout(uv_timer_t *req, int status) { int running_handles; curl_multi_socket_action(curl_handle, CURL_SOCKET_TIMEOUT, 0, @@ -156,41 +162,34 @@ static void on_timeout(uv_timer_t *req, int status) check_multi_info(); } -static int start_timeout(CURLM *multi, long timeout_ms, void *userp) +void start_timeout(CURLM *multi, long timeout_ms, void *userp) { - if(timeout_ms < 0) { - uv_timer_stop(&timeout); - } - else { - if(timeout_ms == 0) - timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it - in a bit */ - uv_timer_start(&timeout, on_timeout, timeout_ms, 0); - } - return 0; + if(timeout_ms <= 0) + timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it in + a bit */ + uv_timer_start(&timeout, on_timeout, timeout_ms, 0); } -static int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, +int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, void *socketp) { curl_context_t *curl_context; - int events = 0; + if(action == CURL_POLL_IN || action == CURL_POLL_OUT) { + if(socketp) { + curl_context = (curl_context_t *) socketp; + } + else { + curl_context = create_curl_context(s); + } + curl_multi_assign(curl_handle, s, (void *) curl_context); + } switch(action) { case CURL_POLL_IN: + uv_poll_start(&curl_context->poll_handle, UV_READABLE, curl_perform); + break; case CURL_POLL_OUT: - case CURL_POLL_INOUT: - curl_context = socketp ? - (curl_context_t *) socketp : create_curl_context(s); - - curl_multi_assign(curl_handle, s, (void *) curl_context); - - if(action != CURL_POLL_IN) - events |= UV_WRITABLE; - if(action != CURL_POLL_OUT) - events |= UV_READABLE; - - uv_poll_start(&curl_context->poll_handle, events, curl_perform); + uv_poll_start(&curl_context->poll_handle, UV_WRITABLE, curl_perform); break; case CURL_POLL_REMOVE: if(socketp) { @@ -214,7 +213,7 @@ int main(int argc, char **argv) return 0; if(curl_global_init(CURL_GLOBAL_ALL)) { - fprintf(stderr, "Could not init curl\n"); + fprintf(stderr, "Could not init cURL\n"); return 1; } diff --git a/docs/examples/multithread.c b/docs/examples/multithread.c index 26c40f5a1..475e8fcfc 100644 --- a/docs/examples/multithread.c +++ b/docs/examples/multithread.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -33,10 +33,10 @@ /* List of URLs to fetch. - If you intend to use a SSL-based protocol here you might need to setup TLS - library mutex callbacks as described here: + If you intend to use a SSL-based protocol here you MUST setup the OpenSSL + callback functions as described here: - https://curl.haxx.se/libcurl/c/threadsafe.html + https://www.openssl.org/docs/crypto/threads.html#DESCRIPTION */ const char * const urls[NUMT]= { diff --git a/docs/examples/opensslthreadlock.c b/docs/examples/opensslthreadlock.c index 6f86c7f70..eebc42ee2 100644 --- a/docs/examples/opensslthreadlock.c +++ b/docs/examples/opensslthreadlock.c @@ -52,7 +52,7 @@ void handle_error(const char *file, int lineno, const char *msg) /* This array will store all of the mutexes available to OpenSSL. */ static MUTEX_TYPE *mutex_buf= NULL; -static void locking_function(int mode, int n, const char *file, int line) +static void locking_function(int mode, int n, const char * file, int line) { if(mode & CRYPTO_LOCK) MUTEX_LOCK(mutex_buf[n]); diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index ef50a66d9..67e685f1e 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -101,7 +101,7 @@ int main(int argc, char *argv[]) /* then cleanup the formpost chain */ curl_formfree(formpost); /* free slist */ - curl_slist_free_all(headerlist); + curl_slist_free_all (headerlist); } return 0; } diff --git a/docs/examples/rtsp.c b/docs/examples/rtsp.c index 5c66aa6e5..63c46e151 100644 --- a/docs/examples/rtsp.c +++ b/docs/examples/rtsp.c @@ -61,15 +61,13 @@ static int _getch(void) #define VERSION_STR "V1.0" /* error handling macros */ -#define my_curl_easy_setopt(A, B, C) \ - res = curl_easy_setopt((A), (B), (C)); \ - if(!res) \ +#define my_curl_easy_setopt(A, B, C) \ + if((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \ fprintf(stderr, "curl_easy_setopt(%s, %s, %s) failed: %d\n", \ #A, #B, #C, res); -#define my_curl_easy_perform(A) \ - res = curl_easy_perform(A); \ - if(!res) \ +#define my_curl_easy_perform(A) \ + if((res = curl_easy_perform((A))) != CURLE_OK) \ fprintf(stderr, "curl_easy_perform(%s) failed: %d\n", #A, res); @@ -190,7 +188,7 @@ int main(int argc, char * const argv[]) printf("\nRTSP request %s\n", VERSION_STR); printf(" Project web site: http://code.google.com/p/rtsprequest/\n"); - printf(" Requires curl V7.20 or greater\n\n"); + printf(" Requires cURL V7.20 or greater\n\n"); /* check command line */ if((argc != 2) && (argc != 3)) { @@ -228,7 +226,7 @@ int main(int argc, char * const argv[]) if(res == CURLE_OK) { curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); CURL *curl; - fprintf(stderr, " curl V%s loaded\n", data->version); + fprintf(stderr, " cURL V%s loaded\n", data->version); /* initialize this curl session */ curl = curl_easy_init(); diff --git a/docs/examples/sendrecv.c b/docs/examples/sendrecv.c index 662323487..41e283cdc 100644 --- a/docs/examples/sendrecv.c +++ b/docs/examples/sendrecv.c @@ -62,9 +62,10 @@ int main(void) CURLcode res; /* Minimalistic http request */ const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n"; - size_t request_len = strlen(request); - curl_socket_t sockfd; - size_t nsent_total = 0; + curl_socket_t sockfd; /* socket */ + long sockextr; + size_t iolen; + curl_off_t nread; /* A general note of caution here: if you're using curl_easy_recv() or curl_easy_send() to implement HTTP or _any_ other protocol libcurl @@ -81,76 +82,54 @@ int main(void) curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); res = curl_easy_perform(curl); - if(res != CURLE_OK) { - printf("Error: %s\n", curl_easy_strerror(res)); + if(CURLE_OK != res) { + printf("Error: %s\n", strerror(res)); return 1; } - /* Extract the socket from the curl handle - we'll need it for waiting. */ - res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd); + /* Extract the socket from the curl handle - we'll need it for waiting. + * Note that this API takes a pointer to a 'long' while we use + * curl_socket_t for sockets otherwise. + */ + res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr); - if(res != CURLE_OK) { + if(CURLE_OK != res) { printf("Error: %s\n", curl_easy_strerror(res)); return 1; } - printf("Sending request.\n"); - - do { - /* Warning: This example program may loop indefinitely. - * A production-quality program must define a timeout and exit this loop - * as soon as the timeout has expired. */ - size_t nsent; - do { - nsent = 0; - res = curl_easy_send(curl, request + nsent_total, - request_len - nsent_total, &nsent); - nsent_total += nsent; - - if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) { - printf("Error: timeout.\n"); - return 1; - } - } while(res == CURLE_AGAIN); + sockfd = (curl_socket_t)sockextr; - if(res != CURLE_OK) { - printf("Error: %s\n", curl_easy_strerror(res)); - return 1; - } - - printf("Sent %" CURL_FORMAT_CURL_OFF_T " bytes.\n", - (curl_off_t)nsent); + /* wait for the socket to become ready for sending */ + if(!wait_on_socket(sockfd, 0, 60000L)) { + printf("Error: timeout.\n"); + return 1; + } - } while(nsent_total < request_len); + puts("Sending request."); + /* Send the request. Real applications should check the iolen + * to see if all the request has been sent */ + res = curl_easy_send(curl, request, strlen(request), &iolen); - printf("Reading response.\n"); + if(CURLE_OK != res) { + printf("Error: %s\n", curl_easy_strerror(res)); + return 1; + } + puts("Reading response."); + /* read the response */ for(;;) { - /* Warning: This example program may loop indefinitely (see above). */ char buf[1024]; - size_t nread; - do { - nread = 0; - res = curl_easy_recv(curl, buf, sizeof(buf), &nread); - - if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) { - printf("Error: timeout.\n"); - return 1; - } - } while(res == CURLE_AGAIN); - - if(res != CURLE_OK) { - printf("Error: %s\n", curl_easy_strerror(res)); - break; - } - if(nread == 0) { - /* end of the response */ + wait_on_socket(sockfd, 1, 60000L); + res = curl_easy_recv(curl, buf, 1024, &iolen); + + if(CURLE_OK != res) break; - } - printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", - (curl_off_t)nread); + nread = (curl_off_t)iolen; + + printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", nread); } /* always cleanup */ diff --git a/docs/examples/sessioninfo.c b/docs/examples/sessioninfo.c index 024a0e12d..11c87cd1b 100644 --- a/docs/examples/sessioninfo.c +++ b/docs/examples/sessioninfo.c @@ -24,7 +24,7 @@ * </DESC> */ -/* Note that this example currently requires curl to be linked against +/* Note that this example currently requires cURL to be linked against GnuTLS (and this program must also be linked against -lgnutls). */ #include <stdio.h> diff --git a/docs/examples/smooth-gtk-thread.c b/docs/examples/smooth-gtk-thread.c index 713fcc6c3..547cc0f30 100644 --- a/docs/examples/smooth-gtk-thread.c +++ b/docs/examples/smooth-gtk-thread.c @@ -105,7 +105,7 @@ void *pull_one_url(void *NaN) curl_easy_cleanup(curl); } - g_free(http); + g_free (http); /* Adds more latency, testing the mutex.*/ sleep(1); @@ -118,7 +118,7 @@ void *pull_one_url(void *NaN) gboolean pulse_bar(gpointer data) { gdk_threads_enter(); - gtk_progress_bar_pulse(GTK_PROGRESS_BAR (data)); + gtk_progress_bar_pulse (GTK_PROGRESS_BAR (data)); gdk_threads_leave(); /* Return true so the function will be called again; @@ -182,8 +182,8 @@ int main(int argc, char **argv) /* Init thread */ g_thread_init(NULL); - gdk_threads_init(); - gdk_threads_enter(); + gdk_threads_init (); + gdk_threads_enter (); gtk_init(&argc, &argv); @@ -203,9 +203,9 @@ int main(int argc, char **argv) /* Progress bar */ progress_bar = gtk_progress_bar_new(); - gtk_progress_bar_pulse(GTK_PROGRESS_BAR (progress_bar)); + gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progress_bar)); /* Make uniform pulsing */ - gint pulse_ref = g_timeout_add(300, pulse_bar, progress_bar); + gint pulse_ref = g_timeout_add (300, pulse_bar, progress_bar); g_object_set_data(G_OBJECT(progress_bar), "pulse_id", GINT_TO_POINTER(pulse_ref)); gtk_container_add(GTK_CONTAINER(inside_frame), progress_bar); diff --git a/docs/examples/synctime.c b/docs/examples/synctime.c index 48377f527..1c787bb91 100644 --- a/docs/examples/synctime.c +++ b/docs/examples/synctime.c @@ -152,9 +152,9 @@ size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb, TmpStr1 & 2? */ AutoSyncTime = 0; else { - RetVal = sscanf((char *)(ptr), "Date: %s %hu %s %hu %hu:%hu:%hu", - TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear, - &SYSTime.wHour, &SYSTime.wMinute, &SYSTime.wSecond); + RetVal = sscanf ((char *)(ptr), "Date: %s %hu %s %hu %hu:%hu:%hu", + TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear, + &SYSTime.wHour, &SYSTime.wMinute, &SYSTime.wSecond); if(RetVal == 7) { SYSTime.wMilliseconds = 500; /* adjust to midpoint, 0.5 sec */ diff --git a/docs/examples/usercertinmem.c b/docs/examples/usercertinmem.c index 77fde911c..47573ebbd 100644 --- a/docs/examples/usercertinmem.c +++ b/docs/examples/usercertinmem.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 2013 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 2013 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -20,7 +20,8 @@ * ***************************************************************************/ /* <DESC> - * Use an in-memory user certificate and RSA key and retrieve an https page. +/* Example using an in memory PEM user certificate and RSA key to retrieve an + * https page. * </DESC> */ /* Written by Ishan SinghLevett, based on Theo Borm's cacertinmem.c. |