Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib540.c
blobf7e61cb2aad7c02d74e64badc6f0220ff66460f1
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib540.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
10 * This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
11 * mailing list on 10 Jul 2007, converted to a test case.
13 * argv1 = URL
14 * argv2 = proxy
15 * argv3 = proxyuser:password
16 * argv4 = host name to use for the custom Host: header
19 #include "test.h"
21 #define PROXY libtest_arg2
22 #define PROXYUSERPWD libtest_arg3
23 #define HOST test_argv[4]
25 static void init(CURLM *cm, const char* url, const char* userpwd,
26 struct curl_slist *headers)
28 CURL *eh = curl_easy_init();
30 curl_easy_setopt(eh, CURLOPT_URL, url);
31 curl_easy_setopt(eh, CURLOPT_PROXY, PROXY);
32 curl_easy_setopt(eh, CURLOPT_PROXYUSERPWD, userpwd);
33 curl_easy_setopt(eh, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
34 curl_easy_setopt(eh, CURLOPT_VERBOSE, 1L);
35 curl_easy_setopt(eh, CURLOPT_HEADER, 1L);
36 curl_easy_setopt(eh, CURLOPT_HTTPHEADER, headers); /* custom Host: */
38 curl_multi_add_handle(cm, eh);
41 static int loop(CURLM *cm, const char* url, const char* userpwd,
42 struct curl_slist *headers)
44 CURLMsg *msg;
45 long L;
46 int M, Q, U = -1;
47 fd_set R, W, E;
48 struct timeval T;
50 init(cm, url, userpwd, headers);
52 while (U) {
53 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(cm, &U));
55 if (U) {
56 FD_ZERO(&R);
57 FD_ZERO(&W);
58 FD_ZERO(&E);
60 if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
61 fprintf(stderr, "E: curl_multi_fdset\n");
62 return EXIT_FAILURE;
65 /* In a real-world program you OF COURSE check the return that maxfd is
66 bigger than -1 so that the call to select() below makes sense! */
68 if (curl_multi_timeout(cm, &L)) {
69 fprintf(stderr, "E: curl_multi_timeout\n");
70 return EXIT_FAILURE;
73 if(L != -1) {
74 T.tv_sec = L/1000;
75 T.tv_usec = (L%1000)*1000;
77 else {
78 T.tv_sec = 5;
79 T.tv_usec = 0;
82 if (0 > select(M+1, &R, &W, &E, &T)) {
83 fprintf(stderr, "E: select\n");
84 return EXIT_FAILURE;
88 while ((msg = curl_multi_info_read(cm, &Q))) {
89 if (msg->msg == CURLMSG_DONE) {
90 CURL *e = msg->easy_handle;
91 fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
92 curl_easy_strerror(msg->data.result));
93 curl_multi_remove_handle(cm, e);
94 curl_easy_cleanup(e);
96 else {
97 fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
102 return 1;
105 int test(char *URL)
107 CURLM *cm;
108 struct curl_slist *headers = NULL;
109 char buffer[246]; /* naively fixed-size */
111 if(test_argc < 4)
112 return 99;
114 sprintf(buffer, "Host: %s", HOST);
116 /* now add a custom Host: header */
117 headers = curl_slist_append(headers, buffer);
119 curl_global_init(CURL_GLOBAL_ALL);
121 cm = curl_multi_init();
122 loop(cm, URL, PROXYUSERPWD, headers);
124 fprintf(stderr, "lib540: now we do the request again\n");
125 loop(cm, URL, PROXYUSERPWD, headers);
127 curl_multi_cleanup(cm);
129 curl_global_cleanup();
131 curl_slist_free_all(headers);
133 return EXIT_SUCCESS;