blob: 3a93dafafa3b2b35ae5fe696f7f3f800063cc99e [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2016, 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.haxx.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 ***************************************************************************/
22/* argv1 = URL
23 * argv2 = proxy with embedded user+password
24 */
25
26#include "test.h"
27
28#include "warnless.h"
29#include "memdebug.h"
30
31struct data {
32 char trace_ascii; /* 1 or 0 */
33};
34
35static
36void dump(const char *text,
37 FILE *stream, unsigned char *ptr, size_t size,
38 char nohex)
39{
40 size_t i;
41 size_t c;
42
43 unsigned int width=0x10;
44
45 if(nohex)
46 /* without the hex output, we can fit more on screen */
47 width = 0x40;
48
49 fprintf(stream, "%s, %d bytes (0x%x)\n", text, (int)size, (int)size);
50
51 for(i=0; i<size; i+= width) {
52
53 fprintf(stream, "%04x: ", (int)i);
54
55 if(!nohex) {
56 /* hex not disabled, show it */
57 for(c = 0; c < width; c++)
58 if(i+c < size)
59 fprintf(stream, "%02x ", ptr[i+c]);
60 else
61 fputs(" ", stream);
62 }
63
64 for(c = 0; (c < width) && (i+c < size); c++) {
65 /* check for 0D0A; if found, skip past and start a new line of output */
66 if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
67 i+=(c+2-width);
68 break;
69 }
70 fprintf(stream, "%c",
71 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
72 /* check again for 0D0A, to avoid an extra \n if it's at width */
73 if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
74 i+=(c+3-width);
75 break;
76 }
77 }
78 fputc('\n', stream); /* newline */
79 }
80 fflush(stream);
81}
82
83static
84int my_trace(CURL *handle, curl_infotype type,
85 char *data, size_t size,
86 void *userp)
87{
88 struct data *config = (struct data *)userp;
89 const char *text;
90 (void)handle; /* prevent compiler warning */
91
92 switch(type) {
93 case CURLINFO_TEXT:
94 fprintf(stderr, "== Info: %s", (char *)data);
95 /* FALLTHROUGH */
96 default: /* in case a new one is introduced to shock us */
97 return 0;
98
99 case CURLINFO_HEADER_OUT:
100 text = "=> Send header";
101 break;
102 case CURLINFO_DATA_OUT:
103 text = "=> Send data";
104 break;
105 case CURLINFO_SSL_DATA_OUT:
106 text = "=> Send SSL data";
107 break;
108 case CURLINFO_HEADER_IN:
109 text = "<= Recv header";
110 break;
111 case CURLINFO_DATA_IN:
112 text = "<= Recv data";
113 break;
114 case CURLINFO_SSL_DATA_IN:
115 text = "<= Recv SSL data";
116 break;
117 }
118
119 dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
120 return 0;
121}
122
123
124static size_t current_offset = 0;
125static char databuf[70000]; /* MUST be more than 64k OR
126 MAX_INITIAL_POST_SIZE */
127
128static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
129{
130 size_t amount = nmemb * size; /* Total bytes curl wants */
131 size_t available = sizeof(databuf) - current_offset; /* What we have to
132 give */
133 size_t given = amount < available ? amount : available; /* What is given */
134 (void)stream;
135 memcpy(ptr, databuf + current_offset, given);
136 current_offset += given;
137 return given;
138}
139
140
141static size_t write_callback(void *ptr, size_t size, size_t nmemb,
142 void *stream)
143{
144 int amount = curlx_uztosi(size * nmemb);
145 printf("%.*s", amount, (char *)ptr);
146 (void)stream;
147 return size * nmemb;
148}
149
150
151static curlioerr ioctl_callback(CURL *handle, int cmd, void *clientp)
152{
153 (void)clientp;
154 if(cmd == CURLIOCMD_RESTARTREAD) {
155 printf("APPLICATION: received a CURLIOCMD_RESTARTREAD request\n");
156 printf("APPLICATION: ** REWINDING! **\n");
157 current_offset = 0;
158 return CURLIOE_OK;
159 }
160 (void)handle;
161 return CURLIOE_UNKNOWNCMD;
162}
163
164
165
166int test(char *URL)
167{
168 CURL *curl;
169 CURLcode res = CURLE_OUT_OF_MEMORY;
170 struct data config;
171 size_t i;
172 static const char fill[] = "test data";
173
174 config.trace_ascii = 1; /* enable ascii tracing */
175
176 curl = curl_easy_init();
177 if(!curl) {
178 fprintf(stderr, "curl_easy_init() failed\n");
179 curl_global_cleanup();
180 return TEST_ERR_MAJOR_BAD;
181 }
182
183 test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
184 test_setopt(curl, CURLOPT_DEBUGDATA, &config);
185 /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
186 test_setopt(curl, CURLOPT_VERBOSE, 1L);
187
188 /* setup repeated data string */
189 for(i=0; i < sizeof(databuf); ++i)
190 databuf[i] = fill[i % sizeof fill];
191
192 /* Post */
193 test_setopt(curl, CURLOPT_POST, 1L);
194
195#ifdef CURL_DOES_CONVERSIONS
196 /* Convert the POST data to ASCII */
197 test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
198#endif
199
200 /* Setup read callback */
201 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
202 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
203
204 /* Write callback */
205 test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
206
207 /* Ioctl function */
208 test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
209
210 test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
211
212 test_setopt(curl, CURLOPT_URL, URL);
213
214 /* Accept any auth. But for this bug configure proxy with DIGEST, basic
215 might work too, not NTLM */
216 test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
217
218 res = curl_easy_perform(curl);
219 fprintf(stderr, "curl_easy_perform = %d\n", (int)res);
220
221test_cleanup:
222
223 curl_easy_cleanup(curl);
224 curl_global_cleanup();
225 return (int)res;
226}