2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
2004-08-23 22:22:26 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-10 18:48:02 +08: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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2011-03-10 18:48:02 +08:00
|
|
|
***************************************************************************/
|
2015-06-18 16:17:02 +08:00
|
|
|
/* <DESC>
|
|
|
|
* Show how CURLOPT_DEBUGFUNCTION can be used.
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2004-08-23 22:22:26 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <curl/curl.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;
|
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
unsigned int width = 0x10;
|
2004-08-23 22:22:26 +08:00
|
|
|
|
|
|
|
if(nohex)
|
|
|
|
/* without the hex output, we can fit more on screen */
|
|
|
|
width = 0x40;
|
|
|
|
|
2018-05-07 01:44:03 +08:00
|
|
|
fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
|
2018-07-08 22:44:32 +08:00
|
|
|
text, (unsigned long)size, (unsigned long)size);
|
2004-08-23 22:22:26 +08:00
|
|
|
|
2017-09-10 05:55:08 +08:00
|
|
|
for(i = 0; i<size; i += width) {
|
2004-08-23 22:22:26 +08:00
|
|
|
|
2018-07-08 22:44:32 +08:00
|
|
|
fprintf(stream, "%4.4lx: ", (unsigned long)i);
|
2004-08-23 22:22:26 +08:00
|
|
|
|
|
|
|
if(!nohex) {
|
|
|
|
/* hex not disabled, show it */
|
|
|
|
for(c = 0; c < width; c++)
|
2017-09-10 05:55:08 +08:00
|
|
|
if(i + c < size)
|
|
|
|
fprintf(stream, "%02x ", ptr[i + c]);
|
2004-08-23 22:22:26 +08:00
|
|
|
else
|
|
|
|
fputs(" ", stream);
|
|
|
|
}
|
|
|
|
|
2017-09-10 05:55:08 +08:00
|
|
|
for(c = 0; (c < width) && (i + c < size); c++) {
|
2004-08-23 22:22:26 +08:00
|
|
|
/* check for 0D0A; if found, skip past and start a new line of output */
|
2017-09-10 05:55:08 +08:00
|
|
|
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
|
|
|
|
ptr[i + c + 1] == 0x0A) {
|
|
|
|
i += (c + 2 - width);
|
2004-08-23 22:22:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(stream, "%c",
|
2017-09-10 05:55:08 +08:00
|
|
|
(ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
|
2004-08-23 22:22:26 +08:00
|
|
|
/* check again for 0D0A, to avoid an extra \n if it's at width */
|
2017-09-10 05:55:08 +08:00
|
|
|
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
|
|
|
|
ptr[i + c + 2] == 0x0A) {
|
|
|
|
i += (c + 3 - width);
|
2004-08-23 22:22:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputc('\n', stream); /* newline */
|
|
|
|
}
|
|
|
|
fflush(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int my_trace(CURL *handle, curl_infotype type,
|
2008-02-27 17:06:15 +08:00
|
|
|
char *data, size_t size,
|
2004-08-23 22:22:26 +08:00
|
|
|
void *userp)
|
|
|
|
{
|
|
|
|
struct data *config = (struct data *)userp;
|
|
|
|
const char *text;
|
|
|
|
(void)handle; /* prevent compiler warning */
|
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(type) {
|
2004-08-23 22:22:26 +08:00
|
|
|
case CURLINFO_TEXT:
|
|
|
|
fprintf(stderr, "== Info: %s", data);
|
2017-05-24 17:56:38 +08:00
|
|
|
/* FALLTHROUGH */
|
2004-08-23 22:22:26 +08: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;
|
2006-10-21 05:26:10 +08:00
|
|
|
case CURLINFO_SSL_DATA_OUT:
|
|
|
|
text = "=> Send SSL data";
|
|
|
|
break;
|
2004-08-23 22:22:26 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-27 17:06:15 +08:00
|
|
|
dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
|
2004-08-23 22:22:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
|
|
|
struct data config;
|
|
|
|
|
|
|
|
config.trace_ascii = 1; /* enable ascii tracing */
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if(curl) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
|
|
|
|
|
|
|
|
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
|
2008-05-23 05:20:07 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
2004-08-23 22:22:26 +08:00
|
|
|
|
2013-01-29 20:04:41 +08:00
|
|
|
/* example.com is redirected, so we tell libcurl to follow redirection */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
|
2018-09-23 17:20:26 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
|
2004-08-23 22:22:26 +08:00
|
|
|
res = curl_easy_perform(curl);
|
2012-07-04 23:03:52 +08:00
|
|
|
/* Check for errors */
|
|
|
|
if(res != CURLE_OK)
|
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
2004-08-23 22:22:26 +08:00
|
|
|
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|