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