xref: /curl/tests/libtest/lib547.c (revision 25cbc2f7)
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 /* argv1 = URL
25  * argv2 = proxy
26  * argv3 = proxyuser:password
27  */
28 
29 #include "test.h"
30 
31 #include "memdebug.h"
32 
33 #define UPLOADTHIS "this is the blurb we want to upload\n"
34 
35 #ifndef LIB548
readcallback(char * ptr,size_t size,size_t nmemb,void * clientp)36 static size_t readcallback(char  *ptr,
37                            size_t size,
38                            size_t nmemb,
39                            void *clientp)
40 {
41   int *counter = (int *)clientp;
42 
43   if(*counter) {
44     /* only do this once and then require a clearing of this */
45     fprintf(stderr, "READ ALREADY DONE!\n");
46     return 0;
47   }
48   (*counter)++; /* bump */
49 
50   if(size * nmemb >= strlen(UPLOADTHIS)) {
51     fprintf(stderr, "READ!\n");
52     strcpy(ptr, UPLOADTHIS);
53     return strlen(UPLOADTHIS);
54   }
55   fprintf(stderr, "READ NOT FINE!\n");
56   return 0;
57 }
ioctlcallback(CURL * handle,int cmd,void * clientp)58 static curlioerr ioctlcallback(CURL *handle,
59                                int cmd,
60                                void *clientp)
61 {
62   int *counter = (int *)clientp;
63   (void)handle; /* unused */
64   if(cmd == CURLIOCMD_RESTARTREAD) {
65     fprintf(stderr, "REWIND!\n");
66     *counter = 0; /* clear counter to make the read callback restart */
67   }
68   return CURLIOE_OK;
69 }
70 
71 
72 
73 #endif
74 
test(char * URL)75 CURLcode test(char *URL)
76 {
77   CURLcode res;
78   CURL *curl;
79 #ifndef LIB548
80   int counter = 0;
81 #endif
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   curl = curl_easy_init();
89   if(!curl) {
90     fprintf(stderr, "curl_easy_init() failed\n");
91     curl_global_cleanup();
92     return TEST_ERR_MAJOR_BAD;
93   }
94 
95   test_setopt(curl, CURLOPT_URL, URL);
96   test_setopt(curl, CURLOPT_VERBOSE, 1L);
97   test_setopt(curl, CURLOPT_HEADER, 1L);
98 #ifdef LIB548
99   /* set the data to POST with a mere pointer to a null-terminated string */
100   test_setopt(curl, CURLOPT_POSTFIELDS, UPLOADTHIS);
101 #else
102   /* 547 style, which means reading the POST data from a callback */
103   CURL_IGNORE_DEPRECATION(
104     test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctlcallback);
105     test_setopt(curl, CURLOPT_IOCTLDATA, &counter);
106   )
107   test_setopt(curl, CURLOPT_READFUNCTION, readcallback);
108   test_setopt(curl, CURLOPT_READDATA, &counter);
109   /* We CANNOT do the POST fine without setting the size (or choose
110      chunked)! */
111   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(UPLOADTHIS));
112 #endif
113   test_setopt(curl, CURLOPT_POST, 1L);
114   test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
115   test_setopt(curl, CURLOPT_PROXYUSERPWD, libtest_arg3);
116   test_setopt(curl, CURLOPT_PROXYAUTH,
117                    (long) (CURLAUTH_NTLM | CURLAUTH_DIGEST | CURLAUTH_BASIC) );
118 
119   res = curl_easy_perform(curl);
120 
121 test_cleanup:
122 
123   curl_easy_cleanup(curl);
124   curl_global_cleanup();
125 
126   return res;
127 }
128