Git init
[external/curl.git] / tests / libtest / lib579.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #include "memdebug.h"
13
14 static const char * const post[]={
15   "one",
16   "two",
17   "three",
18   "and a final longer crap: four",
19   NULL
20 };
21
22
23 struct WriteThis {
24   int counter;
25 };
26
27 static int progress_callback(void *clientp, double dltotal, double dlnow,
28                              double ultotal, double ulnow)
29 {
30   FILE *moo;
31   static int prev_ultotal = -1;
32   static int prev_ulnow = -1;
33   (void)clientp; /* UNUSED */
34   (void)dltotal; /* UNUSED */
35   (void)dlnow; /* UNUSED */
36
37   /* to avoid depending on timing, which will cause this progress function to
38      get called a different number of times depending on circumstances, we
39      only log these lines if the numbers are different from the previous
40      invoke */
41   if((prev_ultotal != (int)ultotal) ||
42      (prev_ulnow != (int)ulnow)) {
43
44     moo = fopen(libtest_arg2, "ab");
45     if(moo) {
46       fprintf(moo, "Progress callback called with UL %d out of %d\n",
47               (int)ulnow, (int)ultotal);
48       fclose(moo);
49     }
50     prev_ulnow = (int) ulnow;
51     prev_ultotal = (int) ultotal;
52   }
53   return 0;
54 }
55
56 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
57 {
58   struct WriteThis *pooh = (struct WriteThis *)userp;
59   const char *data;
60
61   if(size*nmemb < 1)
62     return 0;
63
64   data = post[pooh->counter];
65
66   if(data) {
67     size_t len = strlen(data);
68     memcpy(ptr, data, len);
69     pooh->counter++; /* advance pointer */
70     return len;
71   }
72   return 0;                         /* no more data left to deliver */
73 }
74
75 int test(char *URL)
76 {
77   CURL *curl;
78   CURLcode res=CURLE_OK;
79   struct curl_slist *slist = NULL;
80   struct WriteThis pooh;
81   pooh.counter = 0;
82
83   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
84     fprintf(stderr, "curl_global_init() failed\n");
85     return TEST_ERR_MAJOR_BAD;
86   }
87
88   if ((curl = curl_easy_init()) == NULL) {
89     fprintf(stderr, "curl_easy_init() failed\n");
90     curl_global_cleanup();
91     return TEST_ERR_MAJOR_BAD;
92   }
93
94   slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
95   if (slist == NULL) {
96     fprintf(stderr, "curl_slist_append() failed\n");
97     curl_easy_cleanup(curl);
98     curl_global_cleanup();
99     return TEST_ERR_MAJOR_BAD;
100   }
101
102   /* First set the URL that is about to receive our POST. */
103   test_setopt(curl, CURLOPT_URL, URL);
104
105   /* Now specify we want to POST data */
106   test_setopt(curl, CURLOPT_POST, 1L);
107
108 #ifdef CURL_DOES_CONVERSIONS
109   /* Convert the POST data to ASCII */
110   test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
111 #endif
112
113   /* we want to use our own read function */
114   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
115
116   /* pointer to pass to our read function */
117   test_setopt(curl, CURLOPT_INFILE, &pooh);
118
119   /* get verbose debug output please */
120   test_setopt(curl, CURLOPT_VERBOSE, 1L);
121
122   /* include headers in the output */
123   test_setopt(curl, CURLOPT_HEADER, 1L);
124
125   /* enforce chunked transfer by setting the header */
126   test_setopt(curl, CURLOPT_HTTPHEADER, slist);
127
128   test_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
129   test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
130
131   /* we want to use our own progress function */
132   test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
133   test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
134
135   /* Perform the request, res will get the return code */
136   res = curl_easy_perform(curl);
137
138 test_cleanup:
139
140   /* clean up the headers list */
141   if(slist)
142     curl_slist_free_all(slist);
143
144   /* always cleanup */
145   curl_easy_cleanup(curl);
146   curl_global_cleanup();
147
148   return res;
149 }