Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdio.h>
00027 #include <curl/curl.h>
00028
00029 struct data {
00030 char trace_ascii;
00031 };
00032
00033 static
00034 void dump(const char *text,
00035 FILE *stream, unsigned char *ptr, size_t size,
00036 char nohex)
00037 {
00038 size_t i;
00039 size_t c;
00040
00041 unsigned int width=0x10;
00042
00043 if(nohex)
00044
00045 width = 0x40;
00046
00047 fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
00048 text, (long)size, (long)size);
00049
00050 for(i=0; i<size; i+= width) {
00051
00052 fprintf(stream, "%4.4lx: ", (long)i);
00053
00054 if(!nohex) {
00055
00056 for(c = 0; c < width; c++)
00057 if(i+c < size)
00058 fprintf(stream, "%02x ", ptr[i+c]);
00059 else
00060 fputs(" ", stream);
00061 }
00062
00063 for(c = 0; (c < width) && (i+c < size); c++) {
00064
00065 if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
00066 i+=(c+2-width);
00067 break;
00068 }
00069 fprintf(stream, "%c",
00070 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
00071
00072 if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
00073 i+=(c+3-width);
00074 break;
00075 }
00076 }
00077 fputc('\n', stream);
00078 }
00079 fflush(stream);
00080 }
00081
00082 static
00083 int my_trace(CURL *handle, curl_infotype type,
00084 char *data, size_t size,
00085 void *userp)
00086 {
00087 struct data *config = (struct data *)userp;
00088 const char *text;
00089 (void)handle;
00090
00091 switch(type) {
00092 case CURLINFO_TEXT:
00093 fprintf(stderr, "== Info: %s", data);
00094 default:
00095 return 0;
00096
00097 case CURLINFO_HEADER_OUT:
00098 text = "=> Send header";
00099 break;
00100 case CURLINFO_DATA_OUT:
00101 text = "=> Send data";
00102 break;
00103 case CURLINFO_SSL_DATA_OUT:
00104 text = "=> Send SSL data";
00105 break;
00106 case CURLINFO_HEADER_IN:
00107 text = "<= Recv header";
00108 break;
00109 case CURLINFO_DATA_IN:
00110 text = "<= Recv data";
00111 break;
00112 case CURLINFO_SSL_DATA_IN:
00113 text = "<= Recv SSL data";
00114 break;
00115 }
00116
00117 dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
00118 return 0;
00119 }
00120
00121 int main(void)
00122 {
00123 CURL *curl;
00124 CURLcode res;
00125 struct data config;
00126
00127 config.trace_ascii = 1;
00128
00129 curl = curl_easy_init();
00130 if(curl) {
00131 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
00132 curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
00133
00134
00135 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00136
00137
00138 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
00139
00140 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
00141 res = curl_easy_perform(curl);
00142
00143 if(res != CURLE_OK)
00144 fprintf(stderr, "curl_easy_perform() failed: %s\n",
00145 curl_easy_strerror(res));
00146
00147
00148 curl_easy_cleanup(curl);
00149 }
00150 return 0;
00151 }