aboutsummaryrefslogtreecommitdiff
path: root/docs/examples
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2008-02-03 12:28:48 +0000
committerDaniel Stenberg <daniel@haxx.se>2008-02-03 12:28:48 +0000
commit454e840590eef557c114b32edb14c113f165272a (patch)
treec06b8c573f98ecbb90f3e57d2255f8b11e847ae9 /docs/examples
parented0a413711f890b1a4e95e26ecccb71cef9b793c (diff)
threaded-ssl.c is a little example that does multi-threaded downloads from
HTTPS sites with OpenSSL-enabled libcurl (and pthreads) and thus do the thread-locking and things openssl-style.
Diffstat (limited to 'docs/examples')
-rw-r--r--docs/examples/Makefile.am2
-rw-r--r--docs/examples/threaded-ssl.c124
2 files changed, 125 insertions, 1 deletions
diff --git a/docs/examples/Makefile.am b/docs/examples/Makefile.am
index 73383131e..16f3152b5 100644
--- a/docs/examples/Makefile.am
+++ b/docs/examples/Makefile.am
@@ -33,5 +33,5 @@ noinst_PROGRAMS = 10-at-a-time anyauthput cookie_interface \
COMPLICATED_EXAMPLES = \
curlgtk.c curlx.c htmltitle.cc cacertinmem.c ftpuploadresume.c \
ghiper.c hiperfifo.c htmltidy.c multithread.c \
- opensslthreadlock.c sampleconv.c synctime.c
+ opensslthreadlock.c sampleconv.c synctime.c threaded-ssl.c
diff --git a/docs/examples/threaded-ssl.c b/docs/examples/threaded-ssl.c
new file mode 100644
index 000000000..e49443d40
--- /dev/null
+++ b/docs/examples/threaded-ssl.c
@@ -0,0 +1,124 @@
+/*****************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * $Id$
+ *
+ * A multi-threaded example that uses pthreads and fetches 4 remote files at
+ * once over HTTPS. The lock callbacks and stuff assume OpenSSL so far.
+ * Should be expanded to do optional GnuTLS style locking.
+ *
+ * OpenSSL docs for this: http://www.openssl.org/docs/crypto/threads.html
+ */
+
+#include <stdio.h>
+#include <pthread.h>
+#include <curl/curl.h>
+#include <openssl/crypto.h>
+
+/* we have this global to let the callback get easy access to it */
+static pthread_mutex_t *lockarray;
+
+static void lock_callback(int mode, int type, char *file, int line)
+{
+ (void)file;
+ (void)line;
+ if (mode & CRYPTO_LOCK) {
+ pthread_mutex_lock(&(lockarray[type]));
+ }
+ else {
+ pthread_mutex_unlock(&(lockarray[type]));
+ }
+}
+
+static unsigned long thread_id(void)
+{
+ unsigned long ret;
+
+ ret=(unsigned long)pthread_self();
+ return(ret);
+}
+
+static void init_locks(void)
+{
+ int i;
+
+ lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
+ sizeof(pthread_mutex_t));
+ for (i=0; i<CRYPTO_num_locks(); i++) {
+ pthread_mutex_init(&(lockarray[i]),NULL);
+ }
+
+ CRYPTO_set_id_callback((unsigned long (*)())thread_id);
+ CRYPTO_set_locking_callback((void (*)())lock_callback);
+}
+
+static void kill_locks(void)
+{
+ int i;
+
+ CRYPTO_set_locking_callback(NULL);
+ for (i=0; i<CRYPTO_num_locks(); i++)
+ pthread_mutex_destroy(&(lockarray[i]));
+
+ OPENSSL_free(lockarray);
+}
+
+/* List of URLs to fetch.*/
+const char *urls[]= {
+ "https://www.sf.net/",
+ "https://www.openssl.org/",
+ "https://www.sf.net/",
+ "https://www.openssl.org/",
+};
+
+static void *pull_one_url(void *url)
+{
+ CURL *curl;
+
+ curl = curl_easy_init();
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ /* this example doesn't verify the server's certificate, which means we
+ might be downloading stuff from an impostor */
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
+ curl_easy_perform(curl); /* ignores error */
+ curl_easy_cleanup(curl);
+
+ return NULL;
+}
+
+int main(int argc, char **argv)
+{
+ pthread_t tid[4];
+ int i;
+ int error;
+ (void)argc; /* we don't use any arguments in this example */
+ (void)argv;
+
+ init_locks();
+
+ for(i=0; i< 4; i++) {
+ error = pthread_create(&tid[i],
+ NULL, /* default attributes please */
+ pull_one_url,
+ (void *)urls[i]);
+ if(0 != error)
+ fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
+ else
+ fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
+ }
+
+ /* now wait for all threads to terminate */
+ for(i=0; i< 4; i++) {
+ error = pthread_join(tid[i], NULL);
+ fprintf(stderr, "Thread %d terminated\n", i);
+ }
+
+ kill_locks();
+
+ return 0;
+}