1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, 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 http://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
31 struct data {
32 char trace_ascii; /* 1 or 0 */
33 };
34
35 static
dump(const char * text,FILE * stream,unsigned char * ptr,size_t size,char nohex)36 void 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
83 static
my_trace(CURL * handle,curl_infotype type,char * data,size_t size,void * userp)84 int 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 default: /* in case a new one is introduced to shock us */
96 return 0;
97
98 case CURLINFO_HEADER_OUT:
99 text = "=> Send header";
100 break;
101 case CURLINFO_DATA_OUT:
102 text = "=> Send data";
103 break;
104 case CURLINFO_SSL_DATA_OUT:
105 text = "=> Send SSL data";
106 break;
107 case CURLINFO_HEADER_IN:
108 text = "<= Recv header";
109 break;
110 case CURLINFO_DATA_IN:
111 text = "<= Recv data";
112 break;
113 case CURLINFO_SSL_DATA_IN:
114 text = "<= Recv SSL data";
115 break;
116 }
117
118 dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
119 return 0;
120 }
121
122
123 static size_t current_offset = 0;
124 static char databuf[70000]; /* MUST be more than 64k OR MAX_INITIAL_POST_SIZE */
125
read_callback(void * ptr,size_t size,size_t nmemb,void * stream)126 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
127 {
128 size_t amount = nmemb * size; /* Total bytes curl wants */
129 size_t available = sizeof(databuf) - current_offset; /* What we have to give */
130 size_t given = amount < available ? amount : available; /* What is given */
131 (void)stream;
132 memcpy(ptr, databuf + current_offset, given);
133 current_offset += given;
134 return given;
135 }
136
137
write_callback(void * ptr,size_t size,size_t nmemb,void * stream)138 static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
139 {
140 int amount = curlx_uztosi(size * nmemb);
141 printf("%.*s", amount, (char *)ptr);
142 (void)stream;
143 return size * nmemb;
144 }
145
146
ioctl_callback(CURL * handle,int cmd,void * clientp)147 static curlioerr ioctl_callback(CURL * handle, int cmd, void *clientp)
148 {
149 (void)clientp;
150 if (cmd == CURLIOCMD_RESTARTREAD ) {
151 printf("APPLICATION: recieved a CURLIOCMD_RESTARTREAD request\n");
152 printf("APPLICATION: ** REWINDING! **\n");
153 current_offset = 0;
154 return CURLIOE_OK;
155 }
156 (void)handle;
157 return CURLIOE_UNKNOWNCMD;
158 }
159
160
161
test(char * URL)162 int test(char *URL)
163 {
164 CURL *curl;
165 CURLcode res = CURLE_OUT_OF_MEMORY;
166 struct data config;
167 size_t i;
168 static const char fill[] = "test data";
169
170 config.trace_ascii = 1; /* enable ascii tracing */
171
172 if((curl = curl_easy_init()) == NULL) {
173 fprintf(stderr, "curl_easy_init() failed\n");
174 curl_global_cleanup();
175 return TEST_ERR_MAJOR_BAD;
176 }
177
178 test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
179 test_setopt(curl, CURLOPT_DEBUGDATA, &config);
180 /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
181 test_setopt(curl, CURLOPT_VERBOSE, 1L);
182
183 /* setup repeated data string */
184 for (i=0; i < sizeof(databuf); ++i)
185 databuf[i] = fill[i % sizeof fill];
186
187 /* Post */
188 test_setopt(curl, CURLOPT_POST, 1L);
189
190 #ifdef CURL_DOES_CONVERSIONS
191 /* Convert the POST data to ASCII */
192 test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
193 #endif
194
195 /* Setup read callback */
196 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
197 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
198
199 /* Write callback */
200 test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
201
202 /* Ioctl function */
203 test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
204
205 test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
206
207 test_setopt(curl, CURLOPT_URL, URL);
208
209 /* Accept any auth. But for this bug configure proxy with DIGEST, basic might work too, not NTLM */
210 test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
211
212 res = curl_easy_perform(curl);
213 fprintf(stderr, "curl_easy_perform = %d\n", (int)res);
214
215 test_cleanup:
216
217 curl_easy_cleanup(curl);
218 curl_global_cleanup();
219 return (int)res;
220 }
221