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