Git init
[external/curl.git] / tests / libtest / lib547.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * argv1 = URL
10  * argv2 = proxy
11  * argv3 = proxyuser:password
12  */
13
14 #include "test.h"
15
16 #include "memdebug.h"
17
18 #ifdef CURL_DOES_CONVERSIONS
19    /* ASCII representation with escape sequences for non-ASCII platforms */
20 #  define UPLOADTHIS "\x74\x68\x69\x73\x20\x69\x73\x20\x74\x68\x65\x20\x62" \
21                      "\x6c\x75\x72\x62\x20\x77\x65\x20\x77\x61\x6e\x74\x20" \
22                      "\x74\x6f\x20\x75\x70\x6c\x6f\x61\x64\x0a"
23 #else
24 #  define UPLOADTHIS "this is the blurb we want to upload\n"
25 #endif
26
27 #ifndef LIB548
28 static size_t readcallback(void  *ptr,
29                            size_t size,
30                            size_t nmemb,
31                            void *clientp)
32 {
33   int *counter = (int *)clientp;
34
35   if(*counter) {
36     /* only do this once and then require a clearing of this */
37     fprintf(stderr, "READ ALREADY DONE!\n");
38     return 0;
39   }
40   (*counter)++; /* bump */
41
42   if(size * nmemb > strlen(UPLOADTHIS)) {
43     fprintf(stderr, "READ!\n");
44     strcpy(ptr, UPLOADTHIS);
45     return strlen(UPLOADTHIS);
46   }
47   fprintf(stderr, "READ NOT FINE!\n");
48   return 0;
49 }
50 static curlioerr ioctlcallback(CURL *handle,
51                                int cmd,
52                                void *clientp)
53 {
54   int *counter = (int *)clientp;
55   (void)handle; /* unused */
56   if(cmd == CURLIOCMD_RESTARTREAD) {
57     fprintf(stderr, "REWIND!\n");
58     *counter = 0; /* clear counter to make the read callback restart */
59   }
60   return CURLIOE_OK;
61 }
62
63
64
65 #endif
66
67 int test(char *URL)
68 {
69   CURLcode res;
70   CURL *curl;
71 #ifndef LIB548
72   int counter=0;
73 #endif
74
75   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
76     fprintf(stderr, "curl_global_init() failed\n");
77     return TEST_ERR_MAJOR_BAD;
78   }
79
80   if ((curl = curl_easy_init()) == NULL) {
81     fprintf(stderr, "curl_easy_init() failed\n");
82     curl_global_cleanup();
83     return TEST_ERR_MAJOR_BAD;
84   }
85
86   test_setopt(curl, CURLOPT_URL, URL);
87   test_setopt(curl, CURLOPT_VERBOSE, 1L);
88   test_setopt(curl, CURLOPT_HEADER, 1L);
89 #ifdef LIB548
90   /* set the data to POST with a mere pointer to a zero-terminated string */
91   test_setopt(curl, CURLOPT_POSTFIELDS, UPLOADTHIS);
92 #else
93   /* 547 style, which means reading the POST data from a callback */
94   test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctlcallback);
95   test_setopt(curl, CURLOPT_IOCTLDATA, &counter);
96   test_setopt(curl, CURLOPT_READFUNCTION, readcallback);
97   test_setopt(curl, CURLOPT_READDATA, &counter);
98   /* We CANNOT do the POST fine without setting the size (or choose chunked)! */
99   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(UPLOADTHIS));
100 #endif
101   test_setopt(curl, CURLOPT_POST, 1L);
102   test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
103   test_setopt(curl, CURLOPT_PROXYUSERPWD, libtest_arg3);
104   test_setopt(curl, CURLOPT_PROXYAUTH,
105                    (long) (CURLAUTH_NTLM | CURLAUTH_DIGEST | CURLAUTH_BASIC) );
106
107   res = curl_easy_perform(curl);
108
109 test_cleanup:
110
111   curl_easy_cleanup(curl);
112   curl_global_cleanup();
113
114   return (int)res;
115 }
116