Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * |
| 9 | * This is a very simple example using the multi interface and the debug |
| 10 | * callback. |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | /* somewhat unix-specific */ |
| 17 | #include <sys/time.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | /* curl stuff */ |
| 21 | #include <curl/curl.h> |
| 22 | |
| 23 | typedef char bool; |
| 24 | #define TRUE 1 |
| 25 | |
| 26 | static |
| 27 | void dump(const char *text, |
| 28 | FILE *stream, unsigned char *ptr, size_t size, |
| 29 | bool nohex) |
| 30 | { |
| 31 | size_t i; |
| 32 | size_t c; |
| 33 | |
| 34 | unsigned int width=0x10; |
| 35 | |
| 36 | if(nohex) |
| 37 | /* without the hex output, we can fit more on screen */ |
| 38 | width = 0x40; |
| 39 | |
| 40 | fprintf(stream, "%s, %010.10ld bytes (0x%08.8lx)\n", |
| 41 | text, (long)size, (long)size); |
| 42 | |
| 43 | for(i=0; i<size; i+= width) { |
| 44 | |
| 45 | fprintf(stream, "%04.4lx: ", (long)i); |
| 46 | |
| 47 | if(!nohex) { |
| 48 | /* hex not disabled, show it */ |
| 49 | for(c = 0; c < width; c++) |
| 50 | if(i+c < size) |
| 51 | fprintf(stream, "%02x ", ptr[i+c]); |
| 52 | else |
| 53 | fputs(" ", stream); |
| 54 | } |
| 55 | |
| 56 | for(c = 0; (c < width) && (i+c < size); c++) { |
| 57 | /* check for 0D0A; if found, skip past and start a new line of output */ |
| 58 | if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) { |
| 59 | i+=(c+2-width); |
| 60 | break; |
| 61 | } |
| 62 | fprintf(stream, "%c", |
| 63 | (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.'); |
| 64 | /* check again for 0D0A, to avoid an extra \n if it's at width */ |
| 65 | if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) { |
| 66 | i+=(c+3-width); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | fputc('\n', stream); /* newline */ |
| 71 | } |
| 72 | fflush(stream); |
| 73 | } |
| 74 | |
| 75 | static |
| 76 | int my_trace(CURL *handle, curl_infotype type, |
| 77 | unsigned char *data, size_t size, |
| 78 | void *userp) |
| 79 | { |
| 80 | const char *text; |
| 81 | |
| 82 | (void)handle; /* prevent compiler warning */ |
| 83 | |
| 84 | switch (type) { |
| 85 | case CURLINFO_TEXT: |
| 86 | fprintf(stderr, "== Info: %s", data); |
| 87 | default: /* in case a new one is introduced to shock us */ |
| 88 | return 0; |
| 89 | |
| 90 | case CURLINFO_HEADER_OUT: |
| 91 | text = "=> Send header"; |
| 92 | break; |
| 93 | case CURLINFO_DATA_OUT: |
| 94 | text = "=> Send data"; |
| 95 | break; |
| 96 | case CURLINFO_HEADER_IN: |
| 97 | text = "<= Recv header"; |
| 98 | break; |
| 99 | case CURLINFO_DATA_IN: |
| 100 | text = "<= Recv data"; |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | dump(text, stderr, data, size, TRUE); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Simply download a HTTP file. |
| 110 | */ |
| 111 | int main(int argc, char **argv) |
| 112 | { |
| 113 | CURL *http_handle; |
| 114 | CURLM *multi_handle; |
| 115 | |
| 116 | int still_running; /* keep number of running handles */ |
| 117 | |
| 118 | http_handle = curl_easy_init(); |
| 119 | |
| 120 | /* set the options (I left out a few, you'll get the point anyway) */ |
| 121 | curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/"); |
| 122 | |
| 123 | curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace); |
| 124 | curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L); |
| 125 | |
| 126 | /* init a multi stack */ |
| 127 | multi_handle = curl_multi_init(); |
| 128 | |
| 129 | /* add the individual transfers */ |
| 130 | curl_multi_add_handle(multi_handle, http_handle); |
| 131 | |
| 132 | /* we start some action by calling perform right away */ |
| 133 | curl_multi_perform(multi_handle, &still_running); |
| 134 | |
| 135 | while(still_running) { |
| 136 | struct timeval timeout; |
| 137 | int rc; /* select() return code */ |
| 138 | |
| 139 | fd_set fdread; |
| 140 | fd_set fdwrite; |
| 141 | fd_set fdexcep; |
| 142 | int maxfd = -1; |
| 143 | |
| 144 | long curl_timeo = -1; |
| 145 | |
| 146 | FD_ZERO(&fdread); |
| 147 | FD_ZERO(&fdwrite); |
| 148 | FD_ZERO(&fdexcep); |
| 149 | |
| 150 | /* set a suitable timeout to play around with */ |
| 151 | timeout.tv_sec = 1; |
| 152 | timeout.tv_usec = 0; |
| 153 | |
| 154 | curl_multi_timeout(multi_handle, &curl_timeo); |
| 155 | if(curl_timeo >= 0) { |
| 156 | timeout.tv_sec = curl_timeo / 1000; |
| 157 | if(timeout.tv_sec > 1) |
| 158 | timeout.tv_sec = 1; |
| 159 | else |
| 160 | timeout.tv_usec = (curl_timeo % 1000) * 1000; |
| 161 | } |
| 162 | |
| 163 | /* get file descriptors from the transfers */ |
| 164 | curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); |
| 165 | |
| 166 | /* In a real-world program you OF COURSE check the return code of the |
| 167 | function calls. On success, the value of maxfd is guaranteed to be |
| 168 | greater or equal than -1. We call select(maxfd + 1, ...), specially in |
| 169 | case of (maxfd == -1), we call select(0, ...), which is basically equal |
| 170 | to sleep. */ |
| 171 | |
| 172 | rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); |
| 173 | |
| 174 | switch(rc) { |
| 175 | case -1: |
| 176 | /* select error */ |
| 177 | still_running = 0; |
| 178 | printf("select() returns error, this is badness\n"); |
| 179 | break; |
| 180 | case 0: |
| 181 | default: |
| 182 | /* timeout or readable/writable sockets */ |
| 183 | curl_multi_perform(multi_handle, &still_running); |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | curl_multi_cleanup(multi_handle); |
| 189 | |
| 190 | curl_easy_cleanup(http_handle); |
| 191 | |
| 192 | return 0; |
| 193 | } |