Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added libcurl examples - http://curl.haxx.se/libcurl/c/example.html

  • Loading branch information...
commit 1a14cbb0f4f51b3f9e788e2f6d7afefc259869c9 1 parent 3e0630d
@azawawi authored
Showing with 9,040 additions and 0 deletions.
  1. +197 −0 examples/10-at-a-time.c
  2. +177 −0 examples/anyauthput.c
  3. +151 −0 examples/cacertinmem.c
  4. +81 −0 examples/certinfo.c
  5. +176 −0 examples/chkspeed.c
  6. +124 −0 examples/cookie_interface.c
  7. +106 −0 examples/curlgtk.c
  8. +513 −0 examples/curlx.c
  9. +144 −0 examples/debug.c
  10. +442 −0 examples/evhiperfifo.c
  11. +149 −0 examples/externalsocket.c
  12. +86 −0 examples/fileupload.c
  13. +527 −0 examples/fopen.c
  14. +148 −0 examples/ftp-wildcard.c
  15. +94 −0 examples/ftpget.c
  16. +87 −0 examples/ftpgetinfo.c
  17. +76 −0 examples/ftpgetresp.c
  18. +101 −0 examples/ftpsget.c
  19. +140 −0 examples/ftpupload.c
  20. +174 −0 examples/ftpuploadresume.c
  21. +53 −0 examples/getinfo.c
  22. +112 −0 examples/getinmemory.c
  23. +452 −0 examples/ghiper.c
  24. +418 −0 examples/hiperfifo.c
  25. +86 −0 examples/href_extractor.c
  26. +130 −0 examples/htmltidy.c
  27. +55 −0 examples/http-post.c
  28. +61 −0 examples/httpcustomheader.c
  29. +125 −0 examples/httpput.c
  30. +74 −0 examples/https.c
  31. +44 −0 examples/imap.c
  32. +153 −0 examples/multi-app.c
  33. +205 −0 examples/multi-debugcallback.c
  34. +119 −0 examples/multi-double.c
  35. +148 −0 examples/multi-post.c
  36. +116 −0 examples/multi-single.c
  37. +93 −0 examples/multithread.c
  38. +94 −0 examples/opensslthreadlock.c
  39. +64 −0 examples/persistant.c
  40. +73 −0 examples/pop3s.c
  41. +73 −0 examples/pop3slist.c
  42. +143 −0 examples/post-callback.c
  43. +103 −0 examples/postit2.c
  44. +84 −0 examples/progressfunc.c
  45. +51 −0 examples/resolve.c
  46. +271 −0 examples/rtsp.c
  47. +107 −0 examples/sampleconv.c
  48. +135 −0 examples/sendrecv.c
  49. +85 −0 examples/sepheaders.c
  50. +106 −0 examples/sftpget.c
  51. +45 −0 examples/simple.c
  52. +53 −0 examples/simplepost.c
  53. +87 −0 examples/simplesmtp.c
  54. +137 −0 examples/simplessl.c
  55. +228 −0 examples/smooth-gtk-thread.c
  56. +203 −0 examples/smtp-multi.c
  57. +152 −0 examples/smtp-tls.c
  58. +366 −0 examples/synctime.c
  59. +162 −0 examples/threaded-ssl.c
  60. +81 −0 examples/url2file.c
View
197 examples/10-at-a-time.c
@@ -0,0 +1,197 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+/* Example application source code using the multi interface to download many
+ * files, but with a capped maximum amount of simultaneous transfers.
+ *
+ * Written by Michael Wallner
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#ifndef WIN32
+# include <unistd.h>
+#endif
+#include <curl/multi.h>
+
+static const char *urls[] = {
+ "http://www.microsoft.com",
+ "http://www.opensource.org",
+ "http://www.google.com",
+ "http://www.yahoo.com",
+ "http://www.ibm.com",
+ "http://www.mysql.com",
+ "http://www.oracle.com",
+ "http://www.ripe.net",
+ "http://www.iana.org",
+ "http://www.amazon.com",
+ "http://www.netcraft.com",
+ "http://www.heise.de",
+ "http://www.chip.de",
+ "http://www.ca.com",
+ "http://www.cnet.com",
+ "http://www.news.com",
+ "http://www.cnn.com",
+ "http://www.wikipedia.org",
+ "http://www.dell.com",
+ "http://www.hp.com",
+ "http://www.cert.org",
+ "http://www.mit.edu",
+ "http://www.nist.gov",
+ "http://www.ebay.com",
+ "http://www.playstation.com",
+ "http://www.uefa.com",
+ "http://www.ieee.org",
+ "http://www.apple.com",
+ "http://www.sony.com",
+ "http://www.symantec.com",
+ "http://www.zdnet.com",
+ "http://www.fujitsu.com",
+ "http://www.supermicro.com",
+ "http://www.hotmail.com",
+ "http://www.ecma.com",
+ "http://www.bbc.co.uk",
+ "http://news.google.com",
+ "http://www.foxnews.com",
+ "http://www.msn.com",
+ "http://www.wired.com",
+ "http://www.sky.com",
+ "http://www.usatoday.com",
+ "http://www.cbs.com",
+ "http://www.nbc.com",
+ "http://slashdot.org",
+ "http://www.bloglines.com",
+ "http://www.techweb.com",
+ "http://www.newslink.org",
+ "http://www.un.org",
+};
+
+#define MAX 10 /* number of simultaneous transfers */
+#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)
+{
+ /* take care of the data here, ignored in this example */
+ (void)d;
+ (void)p;
+ return n*l;
+}
+
+static void init(CURLM *cm, int i)
+{
+ CURL *eh = curl_easy_init();
+
+ curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
+ curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
+ curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
+ curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
+ curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
+
+ curl_multi_add_handle(cm, eh);
+}
+
+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;
+}
View
177 examples/anyauthput.c
@@ -0,0 +1,177 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2012, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include <stdio.h>
+#include <fcntl.h>
+#ifdef WIN32
+# include <io.h>
+#else
+# ifdef __VMS
+ typedef int intptr_t;
+# endif
+# include <stdint.h>
+# include <unistd.h>
+#endif
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef _MSC_VER
+# ifdef _WIN64
+ typedef __int64 intptr_t;
+# else
+ typedef int intptr_t;
+# endif
+#endif
+
+#include <curl/curl.h>
+
+#if LIBCURL_VERSION_NUM < 0x070c03
+#error "upgrade your libcurl to no less than 7.12.3"
+#endif
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+/*
+ * This example shows a HTTP PUT operation with authentiction using "any"
+ * type. It PUTs a file given as a command line argument to the URL also given
+ * on the command line.
+ *
+ * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl
+ * function.
+ *
+ * This example also uses its own read callback.
+ */
+
+/* ioctl callback function */
+static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp)
+{
+ intptr_t fd = (intptr_t)userp;
+
+ (void)handle; /* not used in here */
+
+ switch(cmd) {
+ case CURLIOCMD_RESTARTREAD:
+ /* mr libcurl kindly asks as to rewind the read data stream to start */
+ if(-1 == lseek(fd, 0, SEEK_SET))
+ /* couldn't rewind */
+ return CURLIOE_FAILRESTART;
+
+ break;
+
+ default: /* ignore unknown commands */
+ return CURLIOE_UNKNOWNCMD;
+ }
+ return CURLIOE_OK; /* success! */
+}
+
+/* read callback function, fread() look alike */
+static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
+{
+ size_t retcode;
+ curl_off_t nread;
+
+ intptr_t fd = (intptr_t)stream;
+
+ retcode = read(fd, ptr, size * nmemb);
+
+ nread = (curl_off_t)retcode;
+
+ fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
+ " bytes from file\n", nread);
+
+ return retcode;
+}
+
+int main(int argc, char **argv)
+{
+ CURL *curl;
+ CURLcode res;
+ intptr_t hd ;
+ struct stat file_info;
+
+ char *file;
+ char *url;
+
+ if(argc < 3)
+ return 1;
+
+ file= argv[1];
+ url = argv[2];
+
+ /* get the file size of the local file */
+ hd = open(file, O_RDONLY) ;
+ fstat(hd, &file_info);
+
+ /* In windows, this will init the winsock stuff */
+ curl_global_init(CURL_GLOBAL_ALL);
+
+ /* get a curl handle */
+ curl = curl_easy_init();
+ if(curl) {
+ /* we want to use our own read function */
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
+
+ /* which file to upload */
+ 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);
+
+ /* enable "uploading" (which means PUT when doing HTTP) */
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
+
+ /* specify target URL, and note that this URL should also include a file
+ name, not only a directory (as you can do with GTP uploads) */
+ curl_easy_setopt(curl,CURLOPT_URL, url);
+
+ /* and give the size of the upload, this supports large file sizes
+ on systems that have general support for it */
+ curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
+ (curl_off_t)file_info.st_size);
+
+ /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
+ also costs one extra round-trip and possibly sending of all the PUT
+ data twice!!! */
+ curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
+
+ /* set user name and password for the authentication */
+ curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
+
+ /* Now run off and do what you've been told! */
+ 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);
+ }
+ close(hd); /* close the local file */
+
+ curl_global_cleanup();
+ return 0;
+}
View
151 examples/cacertinmem.c
@@ -0,0 +1,151 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+/* Example using a "in core" PEM certificate to retrieve a https page.
+ * Written by Theo Borm
+ */
+
+/* on a netBSD system with OPENSSL& LIBCURL installed from
+ * pkgsrc (using default paths) this program can be compiled using:
+ * gcc -I/usr/pkg/include -L/usr/pkg/lib -lcurl -Wl,-R/usr/pkg/lib -lssl
+ * -lcrypto -lz -o curlcacerttest curlcacerttest.c
+ * on other operating systems you may want to change paths to headers
+ * and libraries
+*/
+#include <openssl/ssl.h>
+#include <curl/curl.h>
+#include <stdio.h>
+
+size_t writefunction( void *ptr, size_t size, size_t nmemb, void *stream)
+{
+ fwrite(ptr,size,nmemb,stream);
+ return(nmemb*size);
+}
+
+static CURLcode sslctx_function(CURL * curl, void * sslctx, void * parm)
+{
+ X509_STORE * store;
+ X509 * cert=NULL;
+ BIO * bio;
+ char * mypem = /* www.cacert.org */
+ "-----BEGIN CERTIFICATE-----\n"\
+ "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
+ "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"\
+ "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"\
+ "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"\
+ "BgNVBAoTB1Jvb3QgQ0ExHjAcBgNVBAsTFWh0dHA6Ly93d3cuY2FjZXJ0Lm9yZzEi\n"\
+ "MCAGA1UEAxMZQ0EgQ2VydCBTaWduaW5nIEF1dGhvcml0eTEhMB8GCSqGSIb3DQEJ\n"\
+ "ARYSc3VwcG9ydEBjYWNlcnQub3JnMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC\n"\
+ "CgKCAgEAziLA4kZ97DYoB1CW8qAzQIxL8TtmPzHlawI229Z89vGIj053NgVBlfkJ\n"\
+ "8BLPRoZzYLdufujAWGSuzbCtRRcMY/pnCujW0r8+55jE8Ez64AO7NV1sId6eINm6\n"\
+ "zWYyN3L69wj1x81YyY7nDl7qPv4coRQKFWyGhFtkZip6qUtTefWIonvuLwphK42y\n"\
+ "fk1WpRPs6tqSnqxEQR5YYGUFZvjARL3LlPdCfgv3ZWiYUQXw8wWRBB0bF4LsyFe7\n"\
+ "w2t6iPGwcswlWyCR7BYCEo8y6RcYSNDHBS4CMEK4JZwFaz+qOqfrU0j36NK2B5jc\n"\
+ "G8Y0f3/JHIJ6BVgrCFvzOKKrF11myZjXnhCLotLddJr3cQxyYN/Nb5gznZY0dj4k\n"\
+ "epKwDpUeb+agRThHqtdB7Uq3EvbXG4OKDy7YCbZZ16oE/9KTfWgu3YtLq1i6L43q\n"\
+ "laegw1SJpfvbi1EinbLDvhG+LJGGi5Z4rSDTii8aP8bQUWWHIbEZAWV/RRyH9XzQ\n"\
+ "QUxPKZgh/TMfdQwEUfoZd9vUFBzugcMd9Zi3aQaRIt0AUMyBMawSB3s42mhb5ivU\n"\
+ "fslfrejrckzzAeVLIL+aplfKkQABi6F1ITe1Yw1nPkZPcCBnzsXWWdsC4PDSy826\n"\
+ "YreQQejdIOQpvGQpQsgi3Hia/0PsmBsJUUtaWsJx8cTLc6nloQsCAwEAAaOCAc4w\n"\
+ "ggHKMB0GA1UdDgQWBBQWtTIb1Mfz4OaO873SsDrusjkY0TCBowYDVR0jBIGbMIGY\n"\
+ "gBQWtTIb1Mfz4OaO873SsDrusjkY0aF9pHsweTEQMA4GA1UEChMHUm9vdCBDQTEe\n"\
+ "MBwGA1UECxMVaHR0cDovL3d3dy5jYWNlcnQub3JnMSIwIAYDVQQDExlDQSBDZXJ0\n"\
+ "IFNpZ25pbmcgQXV0aG9yaXR5MSEwHwYJKoZIhvcNAQkBFhJzdXBwb3J0QGNhY2Vy\n"\
+ "dC5vcmeCAQAwDwYDVR0TAQH/BAUwAwEB/zAyBgNVHR8EKzApMCegJaAjhiFodHRw\n"\
+ "czovL3d3dy5jYWNlcnQub3JnL3Jldm9rZS5jcmwwMAYJYIZIAYb4QgEEBCMWIWh0\n"\
+ "dHBzOi8vd3d3LmNhY2VydC5vcmcvcmV2b2tlLmNybDA0BglghkgBhvhCAQgEJxYl\n"\
+ "aHR0cDovL3d3dy5jYWNlcnQub3JnL2luZGV4LnBocD9pZD0xMDBWBglghkgBhvhC\n"\
+ "AQ0ESRZHVG8gZ2V0IHlvdXIgb3duIGNlcnRpZmljYXRlIGZvciBGUkVFIGhlYWQg\n"\
+ "b3ZlciB0byBodHRwOi8vd3d3LmNhY2VydC5vcmcwDQYJKoZIhvcNAQEEBQADggIB\n"\
+ "ACjH7pyCArpcgBLKNQodgW+JapnM8mgPf6fhjViVPr3yBsOQWqy1YPaZQwGjiHCc\n"\
+ "nWKdpIevZ1gNMDY75q1I08t0AoZxPuIrA2jxNGJARjtT6ij0rPtmlVOKTV39O9lg\n"\
+ "18p5aTuxZZKmxoGCXJzN600BiqXfEVWqFcofN8CCmHBh22p8lqOOLlQ+TyGpkO/c\n"\
+ "gr/c6EWtTZBzCDyUZbAEmXZ/4rzCahWqlwQ3JNgelE5tDlG+1sSPypZt90Pf6DBl\n"\
+ "Jzt7u0NDY8RD97LsaMzhGY4i+5jhe1o+ATc7iwiwovOVThrLm82asduycPAtStvY\n"\
+ "sONvRUgzEv/+PDIqVPfE94rwiCPCR/5kenHA0R6mY7AHfqQv0wGP3J8rtsYIqQ+T\n"\
+ "SCX8Ev2fQtzzxD72V7DX3WnRBnc0CkvSyqD/HMaMyRa+xMwyN2hzXwj7UfdJUzYF\n"\
+ "CpUCTPJ5GhD22Dp1nPMd8aINcGeGG7MW9S/lpOt5hvk9C8JzC6WZrG/8Z7jlLwum\n"\
+ "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"\
+ "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"\
+ "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"\
+ "-----END CERTIFICATE-----\n";
+ /* get a BIO */
+ bio=BIO_new_mem_buf(mypem, -1);
+ /* use it to read the PEM formatted certificate from memory into an X509
+ * structure that SSL can use
+ */
+ PEM_read_bio_X509(bio, &cert, 0, NULL);
+ if (cert == NULL)
+ printf("PEM_read_bio_X509 failed...\n");
+
+ /* get a pointer to the X509 certificate store (which may be empty!) */
+ store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
+
+ /* add our certificate to this store */
+ if (X509_STORE_add_cert(store, cert)==0)
+ printf("error adding certificate\n");
+
+ /* all set to go */
+ return CURLE_OK ;
+}
+
+int main(void)
+{
+ CURL * ch;
+ CURLcode rv;
+
+ rv=curl_global_init(CURL_GLOBAL_ALL);
+ ch=curl_easy_init();
+ rv=curl_easy_setopt(ch,CURLOPT_VERBOSE, 0L);
+ rv=curl_easy_setopt(ch,CURLOPT_HEADER, 0L);
+ rv=curl_easy_setopt(ch,CURLOPT_NOPROGRESS, 1L);
+ rv=curl_easy_setopt(ch,CURLOPT_NOSIGNAL, 1L);
+ rv=curl_easy_setopt(ch,CURLOPT_WRITEFUNCTION, *writefunction);
+ rv=curl_easy_setopt(ch,CURLOPT_WRITEDATA, stdout);
+ rv=curl_easy_setopt(ch,CURLOPT_HEADERFUNCTION, *writefunction);
+ rv=curl_easy_setopt(ch,CURLOPT_WRITEHEADER, stderr);
+ rv=curl_easy_setopt(ch,CURLOPT_SSLCERTTYPE,"PEM");
+ rv=curl_easy_setopt(ch,CURLOPT_SSL_VERIFYPEER,1L);
+ rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
+
+ /* first try: retrieve page without cacerts' certificate -> will fail
+ */
+ rv=curl_easy_perform(ch);
+ if (rv==CURLE_OK)
+ printf("*** transfer succeeded ***\n");
+ else
+ printf("*** transfer failed ***\n");
+
+ /* second try: retrieve page using cacerts' certificate -> will succeed
+ * load the certificate by installing a function doing the nescessary
+ * "modifications" to the SSL CONTEXT just before link init
+ */
+ rv=curl_easy_setopt(ch,CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
+ rv=curl_easy_perform(ch);
+ if (rv==CURLE_OK)
+ printf("*** transfer succeeded ***\n");
+ else
+ printf("*** transfer failed ***\n");
+
+ curl_easy_cleanup(ch);
+ curl_global_cleanup();
+ return rv;
+}
View
81 examples/certinfo.c
@@ -0,0 +1,81 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include <stdio.h>
+
+#include <curl/curl.h>
+
+static size_t wrfu(void *ptr, size_t size, size_t nmemb, void *stream)
+{
+ (void)stream;
+ (void)ptr;
+ return size * nmemb;
+}
+
+int main(void)
+{
+ CURL *curl;
+ CURLcode res;
+
+ curl_global_init(CURL_GLOBAL_DEFAULT);
+
+ curl = curl_easy_init();
+ if(curl) {
+ curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
+
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wrfu);
+
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
+
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
+ curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
+
+ res = curl_easy_perform(curl);
+
+ if(!res) {
+ struct curl_certinfo *ci = NULL;
+
+ res = curl_easy_getinfo(curl, CURLINFO_CERTINFO, &ci);
+
+ if(!res && ci) {
+ int i;
+ printf("%d certs!\n", ci->num_of_certs);
+
+ for(i=0; i<ci->num_of_certs; i++) {
+ struct curl_slist *slist;
+
+ for(slist = ci->certinfo[i]; slist; slist = slist->next)
+ printf("%s\n", slist->data);
+
+ }
+ }
+
+ }
+
+
+ curl_easy_cleanup(curl);
+ }
+
+ curl_global_cleanup();
+
+ return 0;
+}
View
176 examples/chkspeed.c
@@ -0,0 +1,176 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+/* Example source code to show how the callback function can be used to
+ * download data into a chunk of memory instead of storing it in a file.
+ * After successful download we use curl_easy_getinfo() calls to get the
+ * amount of downloaded bytes, the time used for the whole download, and
+ * the average download speed.
+ * On Linux you can create the download test files with:
+ * dd if=/dev/urandom of=file_1M.bin bs=1M count=1
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include <curl/curl.h>
+
+#define URL_BASE "http://speedtest.your.domain/"
+#define URL_1M URL_BASE "file_1M.bin"
+#define URL_2M URL_BASE "file_2M.bin"
+#define URL_5M URL_BASE "file_5M.bin"
+#define URL_10M URL_BASE "file_10M.bin"
+#define URL_20M URL_BASE "file_20M.bin"
+#define URL_50M URL_BASE "file_50M.bin"
+#define URL_100M URL_BASE "file_100M.bin"
+
+#define CHKSPEED_VERSION "1.0"
+
+static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
+{
+ /* we are not interested in the downloaded bytes itself,
+ so we only return the size we would have saved ... */
+ (void)ptr; /* unused */
+ (void)data; /* unused */
+ return (size_t)(size * nmemb);
+}
+
+int main(int argc, char *argv[])
+{
+ CURL *curl_handle;
+ CURLcode res;
+ int prtsep = 0, prttime = 0;
+ const char *url = URL_1M;
+ char *appname = argv[0];
+
+ if (argc > 1) {
+ /* parse input parameters */
+ for (argc--, argv++; *argv; argc--, argv++) {
+ if (strncasecmp(*argv, "-", 1) == 0) {
+ if (strncasecmp(*argv, "-H", 2) == 0) {
+ fprintf(stderr,
+ "\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
+ appname);
+ exit(1);
+ } else if (strncasecmp(*argv, "-V", 2) == 0) {
+ fprintf(stderr, "\r%s %s - %s\n",
+ appname, CHKSPEED_VERSION, curl_version());
+ exit(1);
+ } else if (strncasecmp(*argv, "-X", 2) == 0) {
+ prtsep = 1;
+ } else if (strncasecmp(*argv, "-T", 2) == 0) {
+ prttime = 1;
+ } else if (strncasecmp(*argv, "-M=", 3) == 0) {
+ long m = strtol((*argv)+3, NULL, 10);
+ switch(m) {
+ case 1: url = URL_1M;
+ break;
+ case 2: url = URL_2M;
+ break;
+ case 5: url = URL_5M;
+ break;
+ case 10: url = URL_10M;
+ break;
+ case 20: url = URL_20M;
+ break;
+ case 50: url = URL_50M;
+ break;
+ case 100: url = URL_100M;
+ break;
+ default: fprintf(stderr, "\r%s: invalid parameter %s\n",
+ appname, *argv + 3);
+ exit(1);
+ }
+ } else {
+ fprintf(stderr, "\r%s: invalid or unknown option %s\n",
+ appname, *argv);
+ exit(1);
+ }
+ } else {
+ url = *argv;
+ }
+ }
+ }
+
+ /* print separator line */
+ if (prtsep) {
+ printf("-------------------------------------------------\n");
+ }
+ /* print localtime */
+ if (prttime) {
+ time_t t = time(NULL);
+ printf("Localtime: %s", ctime(&t));
+ }
+
+ /* init libcurl */
+ curl_global_init(CURL_GLOBAL_ALL);
+
+ /* init the curl session */
+ curl_handle = curl_easy_init();
+
+ /* specify URL to get */
+ curl_easy_setopt(curl_handle, CURLOPT_URL, url);
+
+ /* send all data to this function */
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
+
+ /* some servers don't like requests that are made without a user-agent
+ field, so we provide one */
+ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
+ "libcurl-speedchecker/" CHKSPEED_VERSION);
+
+ /* get it! */
+ res = curl_easy_perform(curl_handle);
+
+ if(CURLE_OK == res) {
+ double val;
+
+ /* check for bytes downloaded */
+ res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &val);
+ if((CURLE_OK == res) && (val>0))
+ printf("Data downloaded: %0.0f bytes.\n", val);
+
+ /* check for total download time */
+ res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val);
+ if((CURLE_OK == res) && (val>0))
+ printf("Total download time: %0.3f sec.\n", val);
+
+ /* check for average download speed */
+ res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
+ if((CURLE_OK == res) && (val>0))
+ printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
+
+ } else {
+ fprintf(stderr, "Error while fetching '%s' : %s\n",
+ url, curl_easy_strerror(res));
+ }
+
+ /* cleanup curl stuff */
+ curl_easy_cleanup(curl_handle);
+
+ /* we're done with libcurl, so clean it up */
+ curl_global_cleanup();
+
+ return 0;
+}
View
124 examples/cookie_interface.c
@@ -0,0 +1,124 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+/* This example shows usage of simple cookie interface. */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <time.h>
+
+#include <curl/curl.h>
+
+static void
+print_cookies(CURL *curl)
+{
+ CURLcode res;
+ struct curl_slist *cookies;
+ struct curl_slist *nc;
+ int i;
+
+ printf("Cookies, curl knows:\n");
+ res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
+ if (res != CURLE_OK) {
+ fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
+ exit(1);
+ }
+ nc = cookies, i = 1;
+ while (nc) {
+ printf("[%d]: %s\n", i, nc->data);
+ nc = nc->next;
+ i++;
+ }
+ if (i == 1) {
+ printf("(none)\n");
+ }
+ curl_slist_free_all(cookies);
+}
+
+int
+main(void)
+{
+ CURL *curl;
+ CURLcode res;
+
+ curl_global_init(CURL_GLOBAL_ALL);
+ curl = curl_easy_init();
+ if (curl) {
+ char nline[256];
+
+ curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+ curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
+ res = curl_easy_perform(curl);
+ if (res != CURLE_OK) {
+ fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
+ return 1;
+ }
+
+ print_cookies(curl);
+
+ printf("Erasing curl's knowledge of cookies!\n");
+ curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
+
+ print_cookies(curl);
+
+ printf("-----------------------------------------------\n"
+ "Setting a cookie \"PREF\" via cookie interface:\n");
+#ifdef WIN32
+#define snprintf _snprintf
+#endif
+ /* Netscape format cookie */
+ snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
+ ".google.com", "TRUE", "/", "FALSE", time(NULL) + 31337, "PREF", "hello google, i like you very much!");
+ res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
+ if (res != CURLE_OK) {
+ fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
+ return 1;
+ }
+
+ /* HTTP-header style cookie */
+ snprintf(nline, sizeof(nline),
+ "Set-Cookie: OLD_PREF=3d141414bf4209321; "
+ "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
+ res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
+ if (res != CURLE_OK) {
+ fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
+ return 1;
+ }
+
+ print_cookies(curl);
+
+ res = curl_easy_perform(curl);
+ if (res != CURLE_OK) {
+ fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
+ return 1;
+ }
+ }
+ else {
+ fprintf(stderr, "Curl init failed!\n");
+ return 1;
+ }
+
+ curl_global_cleanup();
+ return 0;
+}
View
106 examples/curlgtk.c
@@ -0,0 +1,106 @@
+/*****************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ */
+/* Copyright (c) 2000 David Odin (aka DindinX) for MandrakeSoft */
+/* an attempt to use the curl library in concert with a gtk-threaded application */
+
+#include <stdio.h>
+#include <gtk/gtk.h>
+
+#include <curl/curl.h>
+
+GtkWidget *Bar;
+
+size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ return fwrite(ptr, size, nmemb, stream);
+}
+
+size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ return fread(ptr, size, nmemb, stream);
+}
+
+int my_progress_func(GtkWidget *bar,
+ double t, /* dltotal */
+ double d, /* dlnow */
+ double ultotal,
+ double ulnow)
+{
+/* printf("%d / %d (%g %%)\n", d, t, d*100.0/t);*/
+ gdk_threads_enter();
+ gtk_progress_set_value(GTK_PROGRESS(bar), d*100.0/t);
+ gdk_threads_leave();
+ return 0;
+}
+
+void *my_thread(void *ptr)
+{
+ CURL *curl;
+ CURLcode res;
+ FILE *outfile;
+ gchar *url = ptr;
+
+ curl = curl_easy_init();
+ if(curl)
+ {
+ outfile = fopen("test.curl", "w");
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
+ curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
+ curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
+ curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
+
+ res = curl_easy_perform(curl);
+
+ fclose(outfile);
+ /* always cleanup */
+ curl_easy_cleanup(curl);
+ }
+
+ return NULL;
+}
+
+int main(int argc, char **argv)
+{
+ GtkWidget *Window, *Frame, *Frame2;
+ GtkAdjustment *adj;
+
+ /* Must initialize libcurl before any threads are started */
+ curl_global_init(CURL_GLOBAL_ALL);
+
+ /* Init thread */
+ g_thread_init(NULL);
+
+ gtk_init(&argc, &argv);
+ Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ Frame = gtk_frame_new(NULL);
+ gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
+ gtk_container_add(GTK_CONTAINER(Window), Frame);
+ Frame2 = gtk_frame_new(NULL);
+ gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
+ gtk_container_add(GTK_CONTAINER(Frame), Frame2);
+ gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
+ adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
+ Bar = gtk_progress_bar_new_with_adjustment(adj);
+ gtk_container_add(GTK_CONTAINER(Frame2), Bar);
+ gtk_widget_show_all(Window);
+
+ if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
+ g_warning("can't create the thread");
+
+
+ gdk_threads_enter();
+ gtk_main();
+ gdk_threads_leave();
+ return 0;
+}
+
View
513 examples/curlx.c
@@ -0,0 +1,513 @@
+/*
+ curlx.c Authors: Peter Sylvester, Jean-Paul Merlin
+
+ This is a little program to demonstrate the usage of
+
+ - an ssl initialisation callback setting a user key and trustbases
+ coming from a pkcs12 file
+ - using an ssl application callback to find a URI in the
+ certificate presented during ssl session establishment.
+
+*/
+
+
+/*
+ * Copyright (c) 2003 The OpenEvidence Project. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions, the following disclaimer,
+ * and the original OpenSSL and SSLeay Licences below.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions, the following disclaimer
+ * and the original OpenSSL and SSLeay Licences below in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ * software must display the following acknowledgments:
+ * "This product includes software developed by the Openevidence Project
+ * for use in the OpenEvidence Toolkit. (http://www.openevidence.org/)"
+ * This product includes software developed by the OpenSSL Project
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com)."
+ *
+ * 4. The names "OpenEvidence Toolkit" and "OpenEvidence Project" must not be
+ * used to endorse or promote products derived from this software without
+ * prior written permission. For written permission, please contact
+ * openevidence-core@openevidence.org.
+ *
+ * 5. Products derived from this software may not be called "OpenEvidence"
+ * nor may "OpenEvidence" appear in their names without prior written
+ * permission of the OpenEvidence Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ * acknowledgments:
+ * "This product includes software developed by the OpenEvidence Project
+ * for use in the OpenEvidence Toolkit (http://www.openevidence.org/)
+ * This product includes software developed by the OpenSSL Project
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com)."
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenEvidence PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenEvidence PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes software developed by the OpenSSL Project
+ * for use in the OpenSSL Toolkit (http://www.openssl.org/)
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <curl/curl.h>
+#include <openssl/x509v3.h>
+#include <openssl/x509_vfy.h>
+#include <openssl/crypto.h>
+#include <openssl/lhash.h>
+#include <openssl/objects.h>
+#include <openssl/err.h>
+#include <openssl/evp.h>
+#include <openssl/x509.h>
+#include <openssl/pkcs12.h>
+#include <openssl/bio.h>
+#include <openssl/ssl.h>
+
+static const char *curlx_usage[]={
+ "usage: curlx args\n",
+ " -p12 arg - tia file ",
+ " -envpass arg - environement variable which content the tia private key password",
+ " -out arg - output file (response)- default stdout",
+ " -in arg - input file (request)- default stdin",
+ " -connect arg - URL of the server for the connection ex: www.openevidence.org",
+ " -mimetype arg - MIME type for data in ex : application/timestamp-query or application/dvcs -default application/timestamp-query",
+ " -acceptmime arg - MIME type acceptable for the response ex : application/timestamp-response or application/dvcs -default none",
+ " -accesstype arg - an Object identifier in an AIA/SIA method, e.g. AD_DVCS or ad_timestamping",
+ NULL
+};
+
+/*
+
+./curlx -p12 psy.p12 -envpass XX -in request -verbose -accesstype AD_DVCS
+-mimetype application/dvcs -acceptmime application/dvcs -out response
+
+*/
+
+/*
+ * We use this ZERO_NULL to avoid picky compiler warnings,
+ * when assigning a NULL pointer to a function pointer var.
+ */
+
+#define ZERO_NULL 0
+
+/* 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 ;
+ STACK_OF(X509) * ca ;
+ CURL * curl;
+ BIO * errorbio;
+ int accesstype ;
+ int verbose;
+
+} sslctxparm;
+
+/* some helper function. */
+
+static char *i2s_ASN1_IA5STRING( ASN1_IA5STRING *ia5)
+{
+ char *tmp;
+ if(!ia5 || !ia5->length)
+ return NULL;
+ tmp = OPENSSL_malloc(ia5->length + 1);
+ memcpy(tmp, ia5->data, ia5->length);
+ tmp[ia5->length] = 0;
+ return tmp;
+}
+
+/* A conveniance routine to get an access URI. */
+
+static unsigned char *my_get_ext(X509 * cert, const int type, int extensiontype) {
+
+ int i;
+ STACK_OF(ACCESS_DESCRIPTION) * accessinfo ;
+ accessinfo = X509_get_ext_d2i(cert, extensiontype, NULL, NULL) ;
+
+ if (!sk_ACCESS_DESCRIPTION_num(accessinfo))
+ return NULL;
+ for (i = 0; i < sk_ACCESS_DESCRIPTION_num(accessinfo); i++) {
+ ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(accessinfo, i);
+ if (OBJ_obj2nid(ad->method) == type) {
+ if (ad->location->type == GEN_URI) {
+ return i2s_ASN1_IA5STRING(ad->location->d.ia5);
+ }
+ return NULL;
+ }
+ }
+ return NULL;
+}
+
+/* This is an application verification call back, it does not
+ perform any addition verification but tries to find a URL
+ in the presented certificat. If found, this will become
+ the URL to be used in the POST.
+*/
+
+static int ssl_app_verify_callback(X509_STORE_CTX *ctx, void *arg)
+{
+ sslctxparm * p = (sslctxparm *) arg;
+ int ok;
+
+ if (p->verbose > 2)
+ BIO_printf(p->errorbio,"entering ssl_app_verify_callback\n");
+
+ if ((ok= X509_verify_cert(ctx)) && ctx->cert) {
+ unsigned char * accessinfo ;
+ if (p->verbose > 1)
+ X509_print_ex(p->errorbio,ctx->cert,0,0);
+
+ 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);
+
+ curl_easy_setopt(p->curl, CURLOPT_URL,accessinfo);
+ }
+ else if (accessinfo = my_get_ext(ctx->cert,p->accesstype,
+ NID_info_access)) {
+ if (p->verbose)
+ BIO_printf(p->errorbio,"Setting URL from AIA to: %s\n", accessinfo);
+
+ curl_easy_setopt(p->curl, CURLOPT_URL,accessinfo);
+ }
+ }
+ if (p->verbose > 2)
+ BIO_printf(p->errorbio,"leaving ssl_app_verify_callback with %d\n", ok);
+ return(ok);
+}
+
+
+/* This is an example of an curl SSL initialisation call back. The callback sets:
+ - a private key and certificate
+ - a trusted ca certificate
+ - a preferred cipherlist
+ - an application verification callback (the function above)
+*/
+
+static CURLcode sslctxfun(CURL * curl, void * sslctx, void * parm) {
+
+ 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"); goto err;
+ }
+ if (!SSL_CTX_use_PrivateKey(ctx,p->pkey)) {
+ BIO_printf(p->errorbio, "SSL_CTX_use_PrivateKey\n"); goto err;
+ }
+
+ if (!SSL_CTX_check_private_key(ctx)) {
+ BIO_printf(p->errorbio, "SSL_CTX_check_private_key\n"); goto err;
+ }
+
+ SSL_CTX_set_quiet_shutdown(ctx,1);
+ SSL_CTX_set_cipher_list(ctx,"RC4-MD5");
+ SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
+
+ X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx), sk_X509_value(p->ca, sk_X509_num(p->ca)-1));
+
+ SSL_CTX_set_verify_depth(ctx,2);
+
+ SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,ZERO_NULL);
+
+ SSL_CTX_set_cert_verify_callback(ctx, ssl_app_verify_callback, parm);
+
+
+ return CURLE_OK ;
+ err:
+ ERR_print_errors(p->errorbio);
+ return CURLE_SSL_CERTPROBLEM;
+
+}
+
+int main(int argc, char **argv) {
+
+ BIO* in=NULL;
+ BIO* out=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 **args = argv + 1;
+ unsigned char * serverurl;
+ sslctxparm p;
+ char *response;
+
+ CURLcode res;
+ struct curl_slist * headers=NULL;
+ int badarg=0;
+
+ binaryptr = malloc(tabLength);
+
+ p.verbose = 0;
+ p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
+
+ curl_global_init(CURL_GLOBAL_DEFAULT);
+
+ /* we need some more for the P12 decoding */
+
+ OpenSSL_add_all_ciphers();
+ OpenSSL_add_all_digests();
+ ERR_load_crypto_strings();
+
+
+
+ while (*args && *args[0] == '-') {
+ if (!strcmp (*args, "-in")) {
+ if (args[1]) {
+ infile=*(++args);
+ } else badarg=1;
+ } else if (!strcmp (*args, "-out")) {
+ if (args[1]) {
+ outfile=*(++args);
+ } else badarg=1;
+ } else if (!strcmp (*args, "-p12")) {
+ if (args[1]) {
+ p.p12file = *(++args);
+ } else badarg=1;
+ } else if (strcmp(*args,"-envpass") == 0) {
+ if (args[1]) {
+ p.pst = getenv(*(++args));
+ } else badarg=1;
+ } else if (strcmp(*args,"-connect") == 0) {
+ if (args[1]) {
+ hostporturl = *(++args);
+ } else badarg=1;
+ } else if (strcmp(*args,"-mimetype") == 0) {
+ if (args[1]) {
+ mimetype = *(++args);
+ } else badarg=1;
+ } else if (strcmp(*args,"-acceptmime") == 0) {
+ if (args[1]) {
+ mimetypeaccept = *(++args);
+ } else badarg=1;
+ } else if (strcmp(*args,"-accesstype") == 0) {
+ if (args[1]) {
+ if ((p.accesstype = OBJ_obj2nid(OBJ_txt2obj(*++args,0))) == 0) badarg=1;
+ } else badarg=1;
+ } else if (strcmp(*args,"-verbose") == 0) {
+ p.verbose++;
+ } else badarg=1;
+ args++;
+ }
+
+ if (mimetype==NULL || mimetypeaccept == NULL) badarg = 1;
+
+ if (badarg) {
+ for (pp=curlx_usage; (*pp != NULL); pp++)
+ BIO_printf(p.errorbio,"%s\n",*pp);
+ BIO_printf(p.errorbio,"\n");
+ goto err;
+ }
+
+
+
+ /* set input */
+
+ if ((in=BIO_new(BIO_s_file())) == NULL) {
+ BIO_printf(p.errorbio, "Error setting input bio\n");
+ goto err;
+ } else if (infile == NULL)
+ BIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);
+ else if (BIO_read_filename(in,infile) <= 0) {
+ BIO_printf(p.errorbio, "Error opening input file %s\n", infile);
+ BIO_free(in);
+ goto err;
+ }
+
+ /* set output */
+
+ if ((out=BIO_new(BIO_s_file())) == NULL) {
+ BIO_printf(p.errorbio, "Error setting output bio.\n");
+ goto err;
+ } else if (outfile == NULL)
+ BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
+ else if (BIO_write_filename(out,outfile) <= 0) {
+ BIO_printf(p.errorbio, "Error opening output file %s\n", outfile);
+ BIO_free(out);
+ goto err;
+ }
+
+
+ p.errorbio = BIO_new_fp (stderr, BIO_NOCLOSE);
+
+ if (!(p.curl = curl_easy_init())) {
+ BIO_printf(p.errorbio, "Cannot init curl lib\n");
+ goto err;
+ }
+
+
+
+ if (!(p12bio = BIO_new_file(p.p12file , "rb"))) {
+ BIO_printf(p.errorbio, "Error opening P12 file %s\n", p.p12file); goto err;
+ }
+ if (!(p.p12 = d2i_PKCS12_bio (p12bio, NULL))) {
+ BIO_printf(p.errorbio, "Cannot decode P12 structure %s\n", p.p12file); goto err;
+ }
+
+ p.ca= NULL;
+ if (!(PKCS12_parse (p.p12, p.pst, &(p.pkey), &(p.usercert), &(p.ca) ) )) {
+ BIO_printf(p.errorbio,"Invalid P12 structure in %s\n", p.p12file); goto err;
+ }
+
+ if (sk_X509_num(p.ca) <= 0) {
+ BIO_printf(p.errorbio,"No trustworthy CA given.%s\n", p.p12file); goto err;
+ }
+
+ if (p.verbose > 1)
+ X509_print_ex(p.errorbio,p.usercert,0,0);
+
+ /* determine URL to go */
+
+ if (hostporturl) {
+ serverurl = malloc(9+strlen(hostporturl));
+ sprintf(serverurl,"https://%s",hostporturl);
+ }
+ else if (p.accesstype != 0) { /* see whether we can find an AIA or SIA for a given access type */
+ 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++) {
+ if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,
+ NID_info_access)))
+ break;
+ if ((serverurl = my_get_ext(sk_X509_value(p.ca,j),p.accesstype,
+ NID_sinfo_access)))
+ break;
+ }
+ }
+ }
+
+ if (!serverurl) {
+ BIO_printf(p.errorbio, "no service URL in certificats,"
+ " check '-accesstype (AD_DVCS | ad_timestamping)'"
+ " or use '-connect'\n");
+ goto err;
+ }
+
+ if (p.verbose)
+ BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
+
+ curl_easy_setopt(p.curl, CURLOPT_URL, serverurl);
+
+ /* Now specify the POST binary data */
+
+ curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
+ curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,(long)tabLength);
+
+ /* pass our list of custom made headers */
+
+ contenttype = malloc(15+strlen(mimetype));
+ sprintf(contenttype,"Content-type: %s",mimetype);
+ headers = curl_slist_append(headers,contenttype);
+ curl_easy_setopt(p.curl, CURLOPT_HTTPHEADER, headers);
+
+ if (p.verbose)
+ BIO_printf(p.errorbio, "Service URL: <%s>\n", serverurl);
+
+ {
+ FILE *outfp;
+ BIO_get_fp(out,&outfp);
+ curl_easy_setopt(p.curl, CURLOPT_WRITEDATA, outfp);
+ }
+
+ res = curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun) ;
+
+ if (res != CURLE_OK)
+ BIO_printf(p.errorbio,"%d %s=%d %d\n", __LINE__, "CURLOPT_SSL_CTX_FUNCTION",CURLOPT_SSL_CTX_FUNCTION,res);
+
+ curl_easy_setopt(p.curl, CURLOPT_SSL_CTX_DATA, &p);
+
+ {
+ int lu; int i=0;
+ while ((lu = BIO_read (in,&binaryptr[i],tabLength-i)) >0 ) {
+ i+=lu;
+ if (i== tabLength) {
+ tabLength+=100;
+ binaryptr=realloc(binaryptr,tabLength); /* should be more careful */
+ }
+ }
+ tabLength = i;
+ }
+ /* Now specify the POST binary data */
+
+ curl_easy_setopt(p.curl, CURLOPT_POSTFIELDS, binaryptr);
+ curl_easy_setopt(p.curl, CURLOPT_POSTFIELDSIZE,(long)tabLength);
+
+
+ /* Perform the request, res will get the return code */
+
+ BIO_printf(p.errorbio,"%d %s %d\n", __LINE__, "curl_easy_perform",
+ res = curl_easy_perform(p.curl));
+ {
+ int result =curl_easy_getinfo(p.curl,CURLINFO_CONTENT_TYPE,&response);
+ if( mimetypeaccept && p.verbose)
+ if(!strcmp(mimetypeaccept,response))
+ BIO_printf(p.errorbio,"the response has a correct mimetype : %s\n",
+ response);
+ else
+ BIO_printf(p.errorbio,"the reponse doesn\'t has an acceptable "
+ "mime type, it is %s instead of %s\n",
+ response,mimetypeaccept);
+ }
+
+ /*** code d'erreur si accept mime ***, egalement code return HTTP != 200 ***/
+
+/* free the header list*/
+
+ curl_slist_free_all(headers);
+
+ /* always cleanup */
+ curl_easy_cleanup(p.curl);
+
+ BIO_free(in);
+ BIO_free(out);
+ return (EXIT_SUCCESS);
+
+ err: BIO_printf(p.errorbio,"error");
+ exit(1);
+}
View
144 examples/debug.c
@@ -0,0 +1,144 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include <stdio.h>
+#include <curl/curl.h>
+
+struct data {
+ char trace_ascii; /* 1 or 0 */
+};
+
+static
+void dump(const char *text,
+ FILE *stream, unsigned char *ptr, size_t size,
+ char nohex)
+{
+ size_t i;
+ size_t c;
+
+ unsigned int width=0x10;
+
+ if(nohex)
+ /* without the hex output, we can fit more on screen */
+ width = 0x40;
+
+ fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
+ text, (long)size, (long)size);
+
+ for(i=0; i<size; i+= width) {
+
+ fprintf(stream, "%4.4lx: ", (long)i);
+
+ if(!nohex) {
+ /* hex not disabled, show it */
+ for(c = 0; c < width; c++)
+ if(i+c < size)
+ fprintf(stream, "%02x ", ptr[i+c]);
+ else
+ fputs(" ", stream);
+ }
+
+ for(c = 0; (c < width) && (i+c < size); c++) {
+ /* check for 0D0A; if found, skip past and start a new line of output */
+ if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
+ i+=(c+2-width);
+ break;
+ }
+ fprintf(stream, "%c",
+ (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
+ /* check again for 0D0A, to avoid an extra \n if it's at width */
+ if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
+ i+=(c+3-width);
+ break;
+ }
+ }
+ fputc('\n', stream); /* newline */
+ }
+ fflush(stream);
+}
+
+static
+int my_trace(CURL *handle, curl_infotype type,
+ char *data, size_t size,
+ void *userp)
+{
+ struct data *config = (struct data *)userp;
+ const char *text;
+ (void)handle; /* prevent compiler warning */
+
+ switch (type) {
+ case CURLINFO_TEXT:
+ fprintf(stderr, "== Info: %s", data);
+ default: /* in case a new one is introduced to shock us */
+ return 0;
+
+ case CURLINFO_HEADER_OUT:
+ text = "=> Send header";
+ break;
+ case CURLINFO_DATA_OUT:
+ text = "=> Send data";
+ break;
+ case CURLINFO_SSL_DATA_OUT:
+ text = "=> Send SSL data";
+ break;
+ case CURLINFO_HEADER_IN:
+ text = "<= Recv header";
+ break;
+ case CURLINFO_DATA_IN:
+ text = "<= Recv data";
+ break;
+ case CURLINFO_SSL_DATA_IN:
+ text = "<= Recv SSL data";
+ break;
+ }
+
+ dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
+ return 0;
+}
+
+int main(void)
+{
+ CURL *curl;
+ CURLcode res;
+ struct data config;
+
+ config.trace_ascii = 1; /* enable ascii tracing */
+
+ curl = curl_easy_init();
+ if(curl) {
+ curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
+ curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
+
+ /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+
+ curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
+ 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);
+ }
+ return 0;
+}
View
442 examples/evhiperfifo.c
@@ -0,0 +1,442 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2012, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+/* Example application source code using the multi socket interface to
+ * download many files at once.
+ *
+ * This example features the same basic functionality as hiperfifo.c does,
+ * but this uses libev instead of libevent.
+ *
+ * Written by Jeff Pohlmeyer, converted to use libev by Markus Koetter
+
+Requires libev and a (POSIX?) system that has mkfifo().
+
+This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
+sample programs.
+
+When running, the program creates the named pipe "hiper.fifo"
+
+Whenever there is input into the fifo, the program reads the input as a list
+of URL's and creates some new easy handles to fetch each URL via the
+curl_multi "hiper" API.
+
+
+Thus, you can try a single URL:
+ % echo http://www.yahoo.com > hiper.fifo
+
+Or a whole bunch of them:
+ % cat my-url-list > hiper.fifo
+
+The fifo buffer is handled almost instantly, so you can even add more URL's
+while the previous requests are still being downloaded.
+
+Note:
+ For the sake of simplicity, URL length is limited to 1023 char's !
+
+This is purely a demo app, all retrieved data is simply discarded by the write
+callback.
+
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/poll.h>
+#include <curl/curl.h>
+#include <ev.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#define DPRINT(x...) printf(x)
+
+#define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
+
+
+/* Global information, common to all connections */
+typedef struct _GlobalInfo
+{
+ struct ev_loop *loop;
+ struct ev_io fifo_event;
+ struct ev_timer timer_event;
+ CURLM *multi;
+ int still_running;
+ FILE* input;
+} GlobalInfo;
+
+
+/* Information associated with a specific easy handle */
+typedef struct _ConnInfo
+{
+ CURL *easy;
+ char *url;
+ GlobalInfo *global;
+ char error[CURL_ERROR_SIZE];
+} ConnInfo;
+
+
+/* Information associated with a specific socket */
+typedef struct _SockInfo
+{
+ curl_socket_t sockfd;
+ CURL *easy;
+ int action;
+ long timeout;
+ struct ev_io ev;
+ int evset;
+ GlobalInfo *global;
+} SockInfo;
+
+static void timer_cb(EV_P_ struct ev_timer *w, int revents);
+
+/* Update the event timer after curl_multi library calls */
+static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
+{
+ DPRINT("%s %li\n", __PRETTY_FUNCTION__, timeout_ms);
+ ev_timer_stop(g->loop, &g->timer_event);
+ if (timeout_ms > 0)
+ {
+ double t = timeout_ms / 1000;
+ ev_timer_init(&g->timer_event, timer_cb, t, 0.);
+ ev_timer_start(g->loop, &g->timer_event);
+ }else
+ timer_cb(g->loop, &g->timer_event, 0);
+ return 0;
+}
+
+/* Die if we get a bad CURLMcode somewhere */
+static void mcode_or_die(const char *where, CURLMcode code)
+{
+ if ( CURLM_OK != code )
+ {
+ const char *s;
+ switch ( code )
+ {
+ case CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break;
+ 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;
+ case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break;
+ case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break;
+ case CURLM_LAST: s="CURLM_LAST"; break;
+ default: s="CURLM_unknown";
+ break;
+ case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET";
+ fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
+ /* ignore this error */
+ return;
+ }
+ fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
+ exit(code);
+ }
+}
+
+
+
+/* Check for completed transfers, and remove their easy handles */
+static void check_multi_info(GlobalInfo *g)
+{
+ char *eff_url;
+ CURLMsg *msg;
+ int msgs_left;
+ ConnInfo *conn;
+ CURL *easy;
+ CURLcode res;
+
+ fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
+ 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);
+ curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
+ fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
+ curl_multi_remove_handle(g->multi, easy);
+ free(conn->url);
+ curl_easy_cleanup(easy);
+ free(conn);
+ }
+ }
+}
+
+
+
+/* Called by libevent when we get action on a multi socket */
+static void event_cb(EV_P_ struct ev_io *w, int revents)
+{
+ DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
+ GlobalInfo *g = (GlobalInfo*) w->data;
+ CURLMcode rc;
+
+ int action = (revents&EV_READ?CURL_POLL_IN:0)|
+ (revents&EV_WRITE?CURL_POLL_OUT:0);
+ rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running);
+ mcode_or_die("event_cb: curl_multi_socket_action", rc);
+ check_multi_info(g);
+ if ( g->still_running <= 0 )
+ {
+ fprintf(MSG_OUT, "last transfer done, kill timeout\n");
+ ev_timer_stop(g->loop, &g->timer_event);
+ }
+}
+
+/* Called by libevent when our timeout expires */
+static void timer_cb(EV_P_ struct ev_timer *w, int revents)
+{
+ DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
+
+ GlobalInfo *g = (GlobalInfo *)w->data;
+ CURLMcode rc;
+
+ 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);
+}
+
+/* Clean up the SockInfo structure */
+static void remsock(SockInfo *f, GlobalInfo *g)
+{
+ printf("%s \n", __PRETTY_FUNCTION__);
+ if ( f )
+ {
+ if ( f->evset )
+ ev_io_stop(g->loop, &f->ev);
+ free(f);
+ }
+}
+
+
+
+/* Assign information to a SockInfo structure */
+static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g)
+{
+ printf("%s \n", __PRETTY_FUNCTION__);
+
+ int kind = (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0);
+
+ f->sockfd = s;
+ f->action = act;
+ f->easy = e;
+ if ( f->evset )
+ ev_io_stop(g->loop, &f->ev);
+ ev_io_init(&f->ev, event_cb, f->sockfd, kind);
+ f->ev.data = g;
+ f->evset=1;
+ ev_io_start(g->loop, &f->ev);
+}
+
+
+
+/* Initialize a new SockInfo structure */
+static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
+{
+ SockInfo *fdp = calloc(sizeof(SockInfo), 1);
+
+ fdp->global = g;
+ setsock(fdp, s, easy, action, g);
+ curl_multi_assign(g->multi, s, fdp);
+}
+
+/* CURLMOPT_SOCKETFUNCTION */
+static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
+{
+ DPRINT("%s e %p s %i what %i cbp %p sockp %p\n",
+ __PRETTY_FUNCTION__, e, s, what, cbp, sockp);
+
+ GlobalInfo *g = (GlobalInfo*) cbp;
+ SockInfo *fdp = (SockInfo*) sockp;
+ const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE"};
+
+ fprintf(MSG_OUT,
+ "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
+ if ( what == CURL_POLL_REMOVE )
+ {
+ fprintf(MSG_OUT, "\n");
+ remsock(fdp, g);
+ } else
+ {
+ if ( !fdp )
+ {
+ fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
+ addsock(s, e, what, g);
+ } else
+ {
+ fprintf(MSG_OUT,
+ "Changing action from %s to %s\n",
+ whatstr[fdp->action], whatstr[what]);
+ setsock(fdp, s, e, what, g);
+ }
+ }
+ return 0;
+}
+
+
+/* CURLOPT_WRITEFUNCTION */
+static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
+{
+ size_t realsize = size * nmemb;
+ ConnInfo *conn = (ConnInfo*) data;
+ (void)ptr;
+ (void)conn;
+ return realsize;
+}
+
+
+/* CURLOPT_PROGRESSFUNCTION */
+static int prog_cb (void *p, double dltotal, double dlnow, double ult,
+ double uln)
+{
+ ConnInfo *conn = (ConnInfo *)p;
+ (void)ult;
+ (void)uln;
+
+ fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
+ return 0;
+}
+
+
+/* Create a new easy handle, and add it to the global curl_multi */
+static void new_conn(char *url, GlobalInfo *g )
+{
+ ConnInfo *conn;
+ CURLMcode rc;
+
+ conn = calloc(1, sizeof(ConnInfo));
+ memset(conn, 0, sizeof(ConnInfo));
+ conn->error[0]='\0';
+
+ conn->easy = curl_easy_init();
+ if ( !conn->easy )
+ {
+ fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
+ exit(2);
+ }
+ conn->global = g;
+ conn->url = strdup(url);
+ curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
+ curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
+ curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, conn);
+ curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
+ curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
+ curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
+ curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
+ curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
+ curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
+ curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_TIME, 3L);
+ curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_LIMIT, 10L);
+
+ fprintf(MSG_OUT,
+ "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
+ rc = curl_multi_add_handle(g->multi, conn->easy);
+ mcode_or_die("new_conn: curl_multi_add_handle", rc);
+
+ /* note that the add_handle() will set a time-out to trigger very soon so
+ that the necessary socket_action() call will be called by this app */
+}
+
+/* This gets called whenever data is received from the fifo */
+static void fifo_cb(EV_P_ struct ev_io *w, int revents)
+{
+ char s[1024];
+ long int rv=0;
+ int n=0;
+ GlobalInfo *g = (GlobalInfo *)w->data;
+
+ do
+ {
+ s[0]='\0';
+ rv=fscanf(g->input, "%1023s%n", s, &n);
+ s[n]='\0';
+ if ( n && s[0] )
+ {
+ new_conn(s,g); /* if we read a URL, go get it! */
+ } else break;
+ } while ( rv != EOF );
+}
+
+/* Create a named pipe and tell libevent to monitor it */
+static int init_fifo (GlobalInfo *g)
+{
+ struct stat st;
+ static const char *fifo = "hiper.fifo";
+ curl_socket_t sockfd;
+
+ fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
+ if ( lstat (fifo, &st) == 0 )
+ {
+ if ( (st.st_mode & S_IFMT) == S_IFREG )
+ {
+ errno = EEXIST;
+ perror("lstat");
+ exit (1);
+ }
+ }
+ unlink(fifo);
+ if ( mkfifo (fifo, 0600) == -1 )
+ {
+ perror("mkfifo");
+ exit (1);
+ }
+ sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
+ if ( sockfd == -1 )
+ {
+ perror("open");
+ exit (1);
+ }
+ g->input = fdopen(sockfd, "r");
+
+ fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
+ ev_io_init(&g->fifo_event, fifo_cb, sockfd, EV_READ);
+ ev_io_start(g->loop, &g->fifo_event);
+ return(0);
+}
+
+int main(int argc, char **argv)
+{
+ GlobalInfo g;
+ CURLMcode rc;
+ (void)argc;
+ (void)argv;
+
+ memset(&g, 0, sizeof(GlobalInfo));
+ g.loop = ev_default_loop(0);
+
+ init_fifo(&g);
+ g.multi = curl_multi_init();
+
+ ev_timer_init(&g.timer_event, timer_cb, 0., 0.);
+ g.timer_event.data = &g;
+ g.fifo_event.data = &g;
+ curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
+ curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
+ curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
+ curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
+
+ /* we don't call any curl_multi_socket*() function yet as we have no handles
+ added! */
+
+ ev_loop(g.loop, 0);
+ curl_multi_cleanup(g.multi);
+ return 0;
+}
View
149 examples/externalsocket.c
@@ -0,0 +1,149 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2012, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+/*
+ * This is an example demonstrating how an application can pass in a custom
+ * socket to libcurl to use. This example also handles the connect itself.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <curl/curl.h>
+
+#ifdef WIN32
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#define close closesocket
+#else
+#include <sys/types.h> /* socket types */
+#include <sys/socket.h> /* socket definitions */
+#include <netinet/in.h>
+#include <arpa/inet.h> /* inet (3) funtions */
+#include <unistd.h> /* misc. UNIX functions */
+#endif
+
+#include <errno.h>
+
+/* The IP address and port number to connect to */
+#define IPADDR "127.0.0.1"
+#define PORTNUM 80
+
+static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
+{
+ int written = fwrite(ptr, size, nmemb, (FILE *)stream);
+ return written;
+}
+
+static curl_socket_t opensocket(void *clientp,
+ curlsocktype purpose,
+ struct curl_sockaddr *address)
+{
+ curl_socket_t sockfd;
+ (void)purpose;
+ (void)address;
+ sockfd = *(curl_socket_t *)clientp;
+ /* the actual externally set socket is passed in via the OPENSOCKETDATA
+ option */
+ return sockfd;
+}
+
+static int sockopt_callback(void *clientp, curl_socket_t curlfd,
+ curlsocktype purpose)
+{
+ (void)clientp;
+ (void)curlfd;
+ (void)purpose;
+ /* This return code was added in libcurl 7.21.5 */
+ return CURL_SOCKOPT_ALREADY_CONNECTED;
+}
+
+int main(void)
+{
+ CURL *curl;
+ CURLcode res;
+ struct sockaddr_in servaddr; /* socket address structure */
+ curl_socket_t sockfd;
+
+#ifdef WIN32
+ WSADATA wsaData;
+ int initwsa;
+
+ if((initwsa = WSAStartup(MAKEWORD(2,0), &wsaData)) != 0) {
+ printf("WSAStartup failed: %d\n", initwsa);
+ return 1;
+ }
+#endif
+
+ curl = curl_easy_init();
+ if(curl) {
+ /*
+ * Note that libcurl will internally think that you connect to the host
+ * and port that you specify in the URL option.
+ */
+ curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
+
+ /* Create the socket "manually" */
+ if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == CURL_SOCKET_BAD ) {
+ printf("Error creating listening socket.\n");
+ return 3;
+ }
+
+ memset(&servaddr, 0, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_port = htons(PORTNUM);
+
+ if (INADDR_NONE == (servaddr.sin_addr.s_addr = inet_addr(IPADDR)))
+ return 2;
+
+ if(connect(sockfd,(struct sockaddr *) &servaddr, sizeof(servaddr)) ==
+ -1) {
+ close(sockfd);
+ printf("client error: connect: %s\n", strerror(errno));
+ return 1;
+ }
+
+ /* no progress meter please */
+ curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
+
+ /* send all data to this function */
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
+
+ /* call this function to get a socket */
+ curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
+ curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
+
+ /* call this function to set options for the socket */
+ curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
+
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
+
+ res = curl_easy_perform(curl);
+
+ curl_easy_cleanup(curl);
+
+ if(res) {
+ printf("libcurl error: %d\n", res);
+ return 4;
+ }
+ }
+ return 0;
+}
View
86 examples/fileupload.c
@@ -0,0 +1,86 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2011, 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
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include <stdio.h>
+#include <curl/curl.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int main(void)
+{
+ CURL *curl;
+ CURLcode res;
+ struct stat file_info;
+ double speed_upload, total_time;
+ FILE *fd;
+
+ fd = fopen("debugit", "rb"); /* open file to upload */
+ if(!fd) {
+
+ return 1; /* can't continue */
+ }
+
+ /* to get the file size */
+ if(fstat(fileno(fd), &file_info) != 0) {
+
+ return 1; /* can't continue */
+ }
+
+ curl = curl_easy_init();
+ if(curl) {
+ /* upload to this place */
+ curl_easy_setopt(curl, CURLOPT_URL,
+ "file:///home/dast/src/curl/debug/new");
+
+ /* tell it to "upload" to the URL */
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+
+ /* set where to read from (on Windows you need to use READFUNCTION too) */
+ curl_easy_setopt(curl, CURLOPT_READDATA, fd);
+
+ /* and give the size of the upload (optional) */
+ curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
+ (curl_off_t)file_info.st_size);
+
+ /* enable verbose for easier tracing */
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+
+ res = curl_easy_perform(curl);
+ /* Check for errors */
+ if(res != CURLE_OK) {
+ fprintf(stderr, "curl_easy_perform() failed: %s\n",
+ curl_easy_strerror(res));
+
+ }
+ else {
+ /* now extract transfer info */
+ curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
+ curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
+
+ fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
+ speed_upload, total_time);
+
+ }
+ /* always cleanup */
+ curl_easy_cleanup(curl);
+ }
+ return 0;
+}
View
527 examples/fopen.c
@@ -0,0 +1,527 @@
+/*****************************************************************************
+ *
+ * This example source code introduces a c library buffered I/O interface to
+ * URL reads it supports fopen(), fread(), fgets(), feof(), fclose(),
+ * rewind(). Supported functions have identical prototypes to their normal c
+ * lib namesakes and are preceaded by url_ .
+ *
+ * Using this code you can replace your program's fopen() with url_fopen()
+ * and fread() with url_fread() and it become possible to read remote streams
+ * instead of (only) local files. Local files (ie those that can be directly
+ * fopened) will drop back to using the underlying clib implementations
+ *
+ * See the main() function at the bottom that shows an app that retrives from a
+ * specified url using fgets() and fread() and saves as two output files.
+ *
+ * Copyright (c) 2003 Simtec Electronics
+ *
+ * Re-implemented by Vincent Sanders <vince@kyllikki.org> with extensive
+ * reference to original curl example code
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR <