xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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 with embedded user+password |
| 26 | */ |
| 27 | |
| 28 | #include "test.h" |
| 29 | |
| 30 | #include "warnless.h" |
| 31 | #include "memdebug.h" |
| 32 | |
| 33 | struct data { |
| 34 | char trace_ascii; /* 1 or 0 */ |
| 35 | }; |
| 36 | |
| 37 | static |
| 38 | void dump(const char *text, |
| 39 | FILE *stream, unsigned char *ptr, size_t size, |
| 40 | char nohex) |
| 41 | { |
| 42 | size_t i; |
| 43 | size_t c; |
| 44 | |
| 45 | unsigned int width = 0x10; |
| 46 | |
| 47 | if(nohex) |
| 48 | /* without the hex output, we can fit more on screen */ |
| 49 | width = 0x40; |
| 50 | |
| 51 | fprintf(stream, "%s, %zu bytes (0x%zx)\n", text, size, size); |
| 52 | |
| 53 | for(i = 0; i<size; i += width) { |
| 54 | |
| 55 | fprintf(stream, "%04zx: ", i); |
| 56 | |
| 57 | if(!nohex) { |
| 58 | /* hex not disabled, show it */ |
| 59 | for(c = 0; c < width; c++) |
| 60 | if(i + c < size) |
| 61 | fprintf(stream, "%02x ", ptr[i + c]); |
| 62 | else |
| 63 | fputs(" ", stream); |
| 64 | } |
| 65 | |
| 66 | for(c = 0; (c < width) && (i + c < size); c++) { |
| 67 | /* check for 0D0A; if found, skip past and start a new line of output */ |
| 68 | if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D && |
| 69 | ptr[i + c + 1] == 0x0A) { |
| 70 | i += (c + 2 - width); |
| 71 | break; |
| 72 | } |
| 73 | fprintf(stream, "%c", |
| 74 | (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)? ptr[i + c] : '.'); |
| 75 | /* check again for 0D0A, to avoid an extra \n if it's at width */ |
| 76 | if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D && |
| 77 | ptr[i + c + 2] == 0x0A) { |
| 78 | i += (c + 3 - width); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | fputc('\n', stream); /* newline */ |
| 83 | } |
| 84 | fflush(stream); |
| 85 | } |
| 86 | |
| 87 | static |
| 88 | int my_trace(CURL *handle, curl_infotype type, |
| 89 | char *data, size_t size, |
| 90 | void *userp) |
| 91 | { |
| 92 | struct data *config = (struct data *)userp; |
| 93 | const char *text; |
| 94 | (void)handle; /* prevent compiler warning */ |
| 95 | |
| 96 | switch(type) { |
| 97 | case CURLINFO_TEXT: |
| 98 | fprintf(stderr, "== Info: %s", (char *)data); |
| 99 | /* FALLTHROUGH */ |
| 100 | default: /* in case a new one is introduced to shock us */ |
| 101 | return 0; |
| 102 | |
| 103 | case CURLINFO_HEADER_OUT: |
| 104 | text = "=> Send header"; |
| 105 | break; |
| 106 | case CURLINFO_DATA_OUT: |
| 107 | text = "=> Send data"; |
| 108 | break; |
| 109 | case CURLINFO_SSL_DATA_OUT: |
| 110 | text = "=> Send SSL data"; |
| 111 | break; |
| 112 | case CURLINFO_HEADER_IN: |
| 113 | text = "<= Recv header"; |
| 114 | break; |
| 115 | case CURLINFO_DATA_IN: |
| 116 | text = "<= Recv data"; |
| 117 | break; |
| 118 | case CURLINFO_SSL_DATA_IN: |
| 119 | text = "<= Recv SSL data"; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | dump(text, stderr, (unsigned char *)data, size, config->trace_ascii); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static size_t current_offset = 0; |
| 129 | static char databuf[70000]; /* MUST be more than 64k OR |
| 130 | MAX_INITIAL_POST_SIZE */ |
| 131 | |
| 132 | static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream) |
| 133 | { |
| 134 | size_t amount = nmemb * size; /* Total bytes curl wants */ |
| 135 | size_t available = sizeof(databuf) - current_offset; /* What we have to |
| 136 | give */ |
| 137 | size_t given = amount < available ? amount : available; /* What is given */ |
| 138 | (void)stream; |
| 139 | memcpy(ptr, databuf + current_offset, given); |
| 140 | current_offset += given; |
| 141 | return given; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static size_t write_callback(void *ptr, size_t size, size_t nmemb, |
| 146 | void *stream) |
| 147 | { |
| 148 | int amount = curlx_uztosi(size * nmemb); |
| 149 | printf("%.*s", amount, (char *)ptr); |
| 150 | (void)stream; |
| 151 | return size * nmemb; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | static curlioerr ioctl_callback(CURL *handle, int cmd, void *clientp) |
| 156 | { |
| 157 | (void)clientp; |
| 158 | if(cmd == CURLIOCMD_RESTARTREAD) { |
| 159 | printf("APPLICATION received a CURLIOCMD_RESTARTREAD request\n"); |
| 160 | printf("APPLICATION ** REWINDING! **\n"); |
| 161 | current_offset = 0; |
| 162 | return CURLIOE_OK; |
| 163 | } |
| 164 | (void)handle; |
| 165 | return CURLIOE_UNKNOWNCMD; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | int test(char *URL) |
| 171 | { |
| 172 | CURL *curl; |
| 173 | CURLcode res = CURLE_OK; |
| 174 | struct data config; |
| 175 | size_t i; |
| 176 | static const char fill[] = "test data"; |
| 177 | |
| 178 | config.trace_ascii = 1; /* enable ascii tracing */ |
| 179 | |
| 180 | global_init(CURL_GLOBAL_ALL); |
| 181 | easy_init(curl); |
| 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 | /* 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 |
| 210 | might work too, not NTLM */ |
| 211 | test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY); |
| 212 | |
| 213 | res = curl_easy_perform(curl); |
| 214 | fprintf(stderr, "curl_easy_perform = %d\n", (int)res); |
| 215 | |
| 216 | test_cleanup: |
| 217 | |
| 218 | curl_easy_cleanup(curl); |
| 219 | curl_global_cleanup(); |
| 220 | return (int)res; |
| 221 | } |