etlegacy-libs/curl/tests/libtest/lib552.c

225 lines
6 KiB
C
Raw Normal View History

2012-06-01 15:23:17 +00:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2018-07-15 15:57:46 +00:00
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
2012-06-01 15:23:17 +00:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2016-09-16 04:29:17 +00:00
* are also available at https://curl.haxx.se/docs/copyright.html.
2012-06-01 15:23:17 +00:00
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
/* argv1 = URL
* argv2 = proxy with embedded user+password
*/
#include "test.h"
#include "warnless.h"
#include "memdebug.h"
struct data {
char trace_ascii; /* 1 or 0 */
};
static
void dump(const char *text,
FILE *stream, unsigned char *ptr, size_t size,
char nohex)
{
size_t i;
size_t c;
2018-02-04 17:52:34 +00:00
unsigned int width = 0x10;
2012-06-01 15:23:17 +00:00
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
2018-07-15 15:57:46 +00:00
fprintf(stream, "%s, %zu bytes (0x%zx)\n", text, size, size);
2012-06-01 15:23:17 +00:00
2018-02-04 17:52:34 +00:00
for(i = 0; i<size; i += width) {
2012-06-01 15:23:17 +00:00
2018-07-15 15:57:46 +00:00
fprintf(stream, "%04zx: ", i);
2012-06-01 15:23:17 +00:00
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
2018-02-04 17:52:34 +00:00
if(i + c < size)
fprintf(stream, "%02x ", ptr[i + c]);
2012-06-01 15:23:17 +00:00
else
fputs(" ", stream);
}
2018-02-04 17:52:34 +00:00
for(c = 0; (c < width) && (i + c < size); c++) {
2012-06-01 15:23:17 +00:00
/* check for 0D0A; if found, skip past and start a new line of output */
2018-02-04 17:52:34 +00:00
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
2012-06-01 15:23:17 +00:00
break;
}
fprintf(stream, "%c",
2018-02-04 17:52:34 +00:00
(ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)? ptr[i + c] : '.');
2012-06-01 15:23:17 +00:00
/* check again for 0D0A, to avoid an extra \n if it's at width */
2018-02-04 17:52:34 +00:00
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
2012-06-01 15:23:17 +00:00
break;
}
}
fputc('\n', stream); /* newline */
}
fflush(stream);
}
static
int my_trace(CURL *handle, curl_infotype type,
char *data, size_t size,
void *userp)
{
struct data *config = (struct data *)userp;
const char *text;
(void)handle; /* prevent compiler warning */
2017-03-02 22:46:25 +00:00
switch(type) {
2012-06-01 15:23:17 +00:00
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", (char *)data);
2017-07-01 14:06:24 +00:00
/* FALLTHROUGH */
2012-06-01 15:23:17 +00:00
default: /* in case a new one is introduced to shock us */
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
}
dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
return 0;
}
static size_t current_offset = 0;
2016-09-16 04:29:17 +00:00
static char databuf[70000]; /* MUST be more than 64k OR
MAX_INITIAL_POST_SIZE */
2012-06-01 15:23:17 +00:00
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t amount = nmemb * size; /* Total bytes curl wants */
2016-09-16 04:29:17 +00:00
size_t available = sizeof(databuf) - current_offset; /* What we have to
give */
2012-06-01 15:23:17 +00:00
size_t given = amount < available ? amount : available; /* What is given */
(void)stream;
memcpy(ptr, databuf + current_offset, given);
current_offset += given;
return given;
}
2016-09-16 04:29:17 +00:00
static size_t write_callback(void *ptr, size_t size, size_t nmemb,
void *stream)
2012-06-01 15:23:17 +00:00
{
int amount = curlx_uztosi(size * nmemb);
printf("%.*s", amount, (char *)ptr);
(void)stream;
return size * nmemb;
}
2017-03-02 22:46:25 +00:00
static curlioerr ioctl_callback(CURL *handle, int cmd, void *clientp)
2012-06-01 15:23:17 +00:00
{
(void)clientp;
2016-09-16 04:29:17 +00:00
if(cmd == CURLIOCMD_RESTARTREAD) {
2017-03-02 22:46:25 +00:00
printf("APPLICATION: received a CURLIOCMD_RESTARTREAD request\n");
2012-06-01 15:23:17 +00:00
printf("APPLICATION: ** REWINDING! **\n");
current_offset = 0;
return CURLIOE_OK;
}
(void)handle;
return CURLIOE_UNKNOWNCMD;
}
int test(char *URL)
{
CURL *curl;
2018-02-04 17:52:34 +00:00
CURLcode res = CURLE_OK;
2012-06-01 15:23:17 +00:00
struct data config;
size_t i;
static const char fill[] = "test data";
config.trace_ascii = 1; /* enable ascii tracing */
2018-02-04 17:52:34 +00:00
global_init(CURL_GLOBAL_ALL);
easy_init(curl);
2012-06-01 15:23:17 +00:00
test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
test_setopt(curl, CURLOPT_DEBUGDATA, &config);
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
test_setopt(curl, CURLOPT_VERBOSE, 1L);
/* setup repeated data string */
2018-02-04 17:52:34 +00:00
for(i = 0; i < sizeof(databuf); ++i)
2018-07-15 15:57:46 +00:00
databuf[i] = fill[i % sizeof(fill)];
2012-06-01 15:23:17 +00:00
/* Post */
test_setopt(curl, CURLOPT_POST, 1L);
#ifdef CURL_DOES_CONVERSIONS
/* Convert the POST data to ASCII */
test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
#endif
/* Setup read callback */
test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* Write callback */
test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
/* Ioctl function */
test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
test_setopt(curl, CURLOPT_URL, URL);
2016-09-16 04:29:17 +00:00
/* Accept any auth. But for this bug configure proxy with DIGEST, basic
might work too, not NTLM */
2012-06-01 15:23:17 +00:00
test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
res = curl_easy_perform(curl);
fprintf(stderr, "curl_easy_perform = %d\n", (int)res);
test_cleanup:
curl_easy_cleanup(curl);
curl_global_cleanup();
return (int)res;
}