1 /*****************************************************************************
3 * Project ___| | | | _ \| |
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.
15 * argv3 = proxyuser:password
16 * argv4 = host name to use for the custom Host: header
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
)
50 init(cm
, url
, userpwd
, headers
);
53 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(cm
, &U
));
60 if (curl_multi_fdset(cm
, &R
, &W
, &E
, &M
)) {
61 fprintf(stderr
, "E: curl_multi_fdset\n");
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");
75 T
.tv_usec
= (L
%1000)*1000;
82 if (0 > select(M
+1, &R
, &W
, &E
, &T
)) {
83 fprintf(stderr
, "E: select\n");
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
);
97 fprintf(stderr
, "E: CURLMsg (%d)\n", (int)msg
->msg
);
108 struct curl_slist
*headers
= NULL
;
109 char buffer
[246]; /* naively fixed-size */
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
);