xref: /curl/tests/libtest/lib510.c (revision 71cf0d1f)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "test.h"
25 
26 #include "memdebug.h"
27 
28 static const char * const testpost[]={
29   "one",
30   "two",
31   "three",
32   "and a final longer crap: four",
33   NULL
34 };
35 
36 
37 struct WriteThis {
38   int counter;
39 };
40 
read_callback(char * ptr,size_t size,size_t nmemb,void * userp)41 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
42 {
43   struct WriteThis *pooh = (struct WriteThis *)userp;
44   const char *data;
45 
46   if(size*nmemb < 1)
47     return 0;
48 
49   data = testpost[pooh->counter];
50 
51   if(data) {
52     size_t len = strlen(data);
53     if(size*nmemb < len) {
54       fprintf(stderr, "read buffer is too small to run test\n");
55       return 0;
56     }
57     memcpy(ptr, data, len);
58     pooh->counter++; /* advance pointer */
59     return len;
60   }
61   return 0;                         /* no more data left to deliver */
62 }
63 
test(char * URL)64 CURLcode test(char *URL)
65 {
66   CURL *curl;
67   CURLcode res = CURLE_OK;
68   struct curl_slist *slist = NULL;
69   struct WriteThis pooh;
70   pooh.counter = 0;
71 
72   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
73     fprintf(stderr, "curl_global_init() failed\n");
74     return TEST_ERR_MAJOR_BAD;
75   }
76 
77   curl = curl_easy_init();
78   if(!curl) {
79     fprintf(stderr, "curl_easy_init() failed\n");
80     curl_global_cleanup();
81     return TEST_ERR_MAJOR_BAD;
82   }
83 
84   slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
85   if(!slist) {
86     fprintf(stderr, "curl_slist_append() failed\n");
87     curl_easy_cleanup(curl);
88     curl_global_cleanup();
89     return TEST_ERR_MAJOR_BAD;
90   }
91 
92   /* First set the URL that is about to receive our POST. */
93   test_setopt(curl, CURLOPT_URL, URL);
94 
95   /* Now specify we want to POST data */
96   test_setopt(curl, CURLOPT_POST, 1L);
97 
98   /* we want to use our own read function */
99   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
100 
101   /* pointer to pass to our read function */
102   test_setopt(curl, CURLOPT_READDATA, &pooh);
103 
104   /* get verbose debug output please */
105   test_setopt(curl, CURLOPT_VERBOSE, 1L);
106 
107   /* include headers in the output */
108   test_setopt(curl, CURLOPT_HEADER, 1L);
109 
110   /* enforce chunked transfer by setting the header */
111   test_setopt(curl, CURLOPT_HTTPHEADER, slist);
112 
113 #ifdef LIB565
114   test_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
115   test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
116 #endif
117 
118   /* Perform the request, res will get the return code */
119   res = curl_easy_perform(curl);
120 
121 test_cleanup:
122 
123   /* clean up the headers list */
124   if(slist)
125     curl_slist_free_all(slist);
126 
127   /* always cleanup */
128   curl_easy_cleanup(curl);
129   curl_global_cleanup();
130 
131   return res;
132 }
133