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 "test.h"
00027
00028 #include "warnless.h"
00029 #include "memdebug.h"
00030
00031 struct data {
00032 char trace_ascii;
00033 };
00034
00035 static
00036 void dump(const char *text,
00037 FILE *stream, unsigned char *ptr, size_t size,
00038 char nohex)
00039 {
00040 size_t i;
00041 size_t c;
00042
00043 unsigned int width=0x10;
00044
00045 if(nohex)
00046
00047 width = 0x40;
00048
00049 fprintf(stream, "%s, %d bytes (0x%x)\n", text, (int)size, (int)size);
00050
00051 for(i=0; i<size; i+= width) {
00052
00053 fprintf(stream, "%04x: ", (int)i);
00054
00055 if(!nohex) {
00056
00057 for(c = 0; c < width; c++)
00058 if(i+c < size)
00059 fprintf(stream, "%02x ", ptr[i+c]);
00060 else
00061 fputs(" ", stream);
00062 }
00063
00064 for(c = 0; (c < width) && (i+c < size); c++) {
00065
00066 if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
00067 i+=(c+2-width);
00068 break;
00069 }
00070 fprintf(stream, "%c",
00071 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
00072
00073 if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
00074 i+=(c+3-width);
00075 break;
00076 }
00077 }
00078 fputc('\n', stream);
00079 }
00080 fflush(stream);
00081 }
00082
00083 static
00084 int my_trace(CURL *handle, curl_infotype type,
00085 char *data, size_t size,
00086 void *userp)
00087 {
00088 struct data *config = (struct data *)userp;
00089 const char *text;
00090 (void)handle;
00091
00092 switch(type) {
00093 case CURLINFO_TEXT:
00094 fprintf(stderr, "== Info: %s", (char *)data);
00095 default:
00096 return 0;
00097
00098 case CURLINFO_HEADER_OUT:
00099 text = "=> Send header";
00100 break;
00101 case CURLINFO_DATA_OUT:
00102 text = "=> Send data";
00103 break;
00104 case CURLINFO_SSL_DATA_OUT:
00105 text = "=> Send SSL data";
00106 break;
00107 case CURLINFO_HEADER_IN:
00108 text = "<= Recv header";
00109 break;
00110 case CURLINFO_DATA_IN:
00111 text = "<= Recv data";
00112 break;
00113 case CURLINFO_SSL_DATA_IN:
00114 text = "<= Recv SSL data";
00115 break;
00116 }
00117
00118 dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
00119 return 0;
00120 }
00121
00122
00123 static size_t current_offset = 0;
00124 static char databuf[70000];
00125
00126
00127 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
00128 {
00129 size_t amount = nmemb * size;
00130 size_t available = sizeof(databuf) - current_offset;
00131
00132 size_t given = amount < available ? amount : available;
00133 (void)stream;
00134 memcpy(ptr, databuf + current_offset, given);
00135 current_offset += given;
00136 return given;
00137 }
00138
00139
00140 static size_t write_callback(void *ptr, size_t size, size_t nmemb,
00141 void *stream)
00142 {
00143 int amount = curlx_uztosi(size * nmemb);
00144 printf("%.*s", amount, (char *)ptr);
00145 (void)stream;
00146 return size * nmemb;
00147 }
00148
00149
00150 static curlioerr ioctl_callback(CURL *handle, int cmd, void *clientp)
00151 {
00152 (void)clientp;
00153 if(cmd == CURLIOCMD_RESTARTREAD) {
00154 printf("APPLICATION: recieved a CURLIOCMD_RESTARTREAD request\n");
00155 printf("APPLICATION: ** REWINDING! **\n");
00156 current_offset = 0;
00157 return CURLIOE_OK;
00158 }
00159 (void)handle;
00160 return CURLIOE_UNKNOWNCMD;
00161 }
00162
00163
00164
00165 int test(char *URL)
00166 {
00167 CURL *curl;
00168 CURLcode res = CURLE_OUT_OF_MEMORY;
00169 struct data config;
00170 size_t i;
00171 static const char fill[] = "test data";
00172
00173 config.trace_ascii = 1;
00174
00175 curl = curl_easy_init();
00176 if(!curl) {
00177 fprintf(stderr, "curl_easy_init() failed\n");
00178 curl_global_cleanup();
00179 return TEST_ERR_MAJOR_BAD;
00180 }
00181
00182 test_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
00183 test_setopt(curl, CURLOPT_DEBUGDATA, &config);
00184
00185 test_setopt(curl, CURLOPT_VERBOSE, 1L);
00186
00187
00188 for(i=0; i < sizeof(databuf); ++i)
00189 databuf[i] = fill[i % sizeof fill];
00190
00191
00192 test_setopt(curl, CURLOPT_POST, 1L);
00193
00194 #ifdef CURL_DOES_CONVERSIONS
00195
00196 test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
00197 #endif
00198
00199
00200 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) sizeof(databuf));
00201 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00202
00203
00204 test_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
00205
00206
00207 test_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_callback);
00208
00209 test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
00210
00211 test_setopt(curl, CURLOPT_URL, URL);
00212
00213
00214
00215 test_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
00216
00217 res = curl_easy_perform(curl);
00218 fprintf(stderr, "curl_easy_perform = %d\n", (int)res);
00219
00220 test_cleanup:
00221
00222 curl_easy_cleanup(curl);
00223 curl_global_cleanup();
00224 return (int)res;
00225 }