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
00027 #include <stdio.h>
00028 #include <string.h>
00029
00030
00031 #include <sys/time.h>
00032 #include <unistd.h>
00033
00034
00035 #include <curl/curl.h>
00036
00037 typedef char bool;
00038 #define TRUE 1
00039
00040 static
00041 void dump(const char *text,
00042 FILE *stream, unsigned char *ptr, size_t size,
00043 bool nohex)
00044 {
00045 size_t i;
00046 size_t c;
00047
00048 unsigned int width=0x10;
00049
00050 if(nohex)
00051
00052 width = 0x40;
00053
00054 fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
00055 text, (long)size, (long)size);
00056
00057 for(i=0; i<size; i+= width) {
00058
00059 fprintf(stream, "%4.4lx: ", (long)i);
00060
00061 if(!nohex) {
00062
00063 for(c = 0; c < width; c++)
00064 if(i+c < size)
00065 fprintf(stream, "%02x ", ptr[i+c]);
00066 else
00067 fputs(" ", stream);
00068 }
00069
00070 for(c = 0; (c < width) && (i+c < size); c++) {
00071
00072 if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
00073 i+=(c+2-width);
00074 break;
00075 }
00076 fprintf(stream, "%c",
00077 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
00078
00079 if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
00080 i+=(c+3-width);
00081 break;
00082 }
00083 }
00084 fputc('\n', stream);
00085 }
00086 fflush(stream);
00087 }
00088
00089 static
00090 int my_trace(CURL *handle, curl_infotype type,
00091 unsigned char *data, size_t size,
00092 void *userp)
00093 {
00094 const char *text;
00095
00096 (void)userp;
00097 (void)handle;
00098
00099 switch(type) {
00100 case CURLINFO_TEXT:
00101 fprintf(stderr, "== Info: %s", data);
00102 default:
00103 return 0;
00104
00105 case CURLINFO_HEADER_OUT:
00106 text = "=> Send header";
00107 break;
00108 case CURLINFO_DATA_OUT:
00109 text = "=> Send data";
00110 break;
00111 case CURLINFO_HEADER_IN:
00112 text = "<= Recv header";
00113 break;
00114 case CURLINFO_DATA_IN:
00115 text = "<= Recv data";
00116 break;
00117 }
00118
00119 dump(text, stderr, data, size, TRUE);
00120 return 0;
00121 }
00122
00123
00124
00125
00126 int main(void)
00127 {
00128 CURL *http_handle;
00129 CURLM *multi_handle;
00130
00131 int still_running;
00132
00133 http_handle = curl_easy_init();
00134
00135
00136 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
00137
00138 curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
00139 curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
00140
00141
00142 multi_handle = curl_multi_init();
00143
00144
00145 curl_multi_add_handle(multi_handle, http_handle);
00146
00147
00148 curl_multi_perform(multi_handle, &still_running);
00149
00150 do {
00151 struct timeval timeout;
00152 int rc;
00153 CURLMcode mc;
00154
00155 fd_set fdread;
00156 fd_set fdwrite;
00157 fd_set fdexcep;
00158 int maxfd = -1;
00159
00160 long curl_timeo = -1;
00161
00162 FD_ZERO(&fdread);
00163 FD_ZERO(&fdwrite);
00164 FD_ZERO(&fdexcep);
00165
00166
00167 timeout.tv_sec = 1;
00168 timeout.tv_usec = 0;
00169
00170 curl_multi_timeout(multi_handle, &curl_timeo);
00171 if(curl_timeo >= 0) {
00172 timeout.tv_sec = curl_timeo / 1000;
00173 if(timeout.tv_sec > 1)
00174 timeout.tv_sec = 1;
00175 else
00176 timeout.tv_usec = (curl_timeo % 1000) * 1000;
00177 }
00178
00179
00180 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
00181
00182 if(mc != CURLM_OK) {
00183 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00184 break;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 if(maxfd == -1) {
00194 #ifdef _WIN32
00195 Sleep(100);
00196 rc = 0;
00197 #else
00198
00199 struct timeval wait = { 0, 100 * 1000 };
00200 rc = select(0, NULL, NULL, NULL, &wait);
00201 #endif
00202 }
00203 else {
00204
00205
00206 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00207 }
00208
00209 switch(rc) {
00210 case -1:
00211
00212 still_running = 0;
00213 printf("select() returns error, this is badness\n");
00214 break;
00215 case 0:
00216 default:
00217
00218 curl_multi_perform(multi_handle, &still_running);
00219 break;
00220 }
00221 } while(still_running);
00222
00223 curl_multi_cleanup(multi_handle);
00224
00225 curl_easy_cleanup(http_handle);
00226
00227 return 0;
00228 }