[579] | 1 | /* |
---|
[1] | 2 | * ulinet.c |
---|
| 3 | * |
---|
| 4 | * Author : Gérald FENOY |
---|
| 5 | * |
---|
[579] | 6 | * Copyright (c) 2008-2015 GeoLabs SARL |
---|
[1] | 7 | * |
---|
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 9 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 10 | * in the Software without restriction, including without limitation the rights |
---|
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 12 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 13 | * furnished to do so, subject to the following conditions: |
---|
| 14 | * |
---|
| 15 | * The above copyright notice and this permission notice shall be included in |
---|
| 16 | * all copies or substantial portions of the Software. |
---|
| 17 | * |
---|
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 24 | * THE SOFTWARE. |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #define _ULINET |
---|
[490] | 29 | #define MAX_WAIT_MSECS 180*1000 /* Wait max. 180 seconds */ |
---|
[1] | 30 | #include "ulinet.h" |
---|
| 31 | #include <assert.h> |
---|
[817] | 32 | #include <ctype.h> |
---|
[579] | 33 | |
---|
| 34 | /** |
---|
| 35 | * Write the downloaded content to a _HINTERNET structure |
---|
| 36 | * |
---|
| 37 | * @param buffer the buffer to read |
---|
| 38 | * @param size size of each member |
---|
| 39 | * @param nmemb number of element to read |
---|
| 40 | * @param data the _HINTERNET structure to write in |
---|
| 41 | * @return the size red, -1 if buffer is NULL |
---|
| 42 | */ |
---|
[1] | 43 | size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
| 44 | size_t realsize = size * nmemb; |
---|
[492] | 45 | _HINTERNET *psInternet; |
---|
[93] | 46 | if(buffer==NULL){ |
---|
[1] | 47 | buffer=NULL; |
---|
| 48 | return -1; |
---|
| 49 | } |
---|
[492] | 50 | psInternet=(_HINTERNET *)data; |
---|
[1] | 51 | if(psInternet->pabyData){ |
---|
[446] | 52 | psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1); |
---|
[1] | 53 | psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1; |
---|
| 54 | } |
---|
| 55 | else{ |
---|
[446] | 56 | psInternet->pabyData=(unsigned char*)malloc(psInternet->nDataLen+realsize+1); |
---|
[1] | 57 | psInternet->nDataAlloc=realsize+1; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | if (psInternet->pabyData) { |
---|
| 61 | memcpy( psInternet->pabyData + psInternet->nDataLen, buffer, realsize); |
---|
| 62 | psInternet->nDataLen += realsize; |
---|
| 63 | psInternet->pabyData[psInternet->nDataLen] = 0; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | buffer=NULL; |
---|
| 67 | return realsize; |
---|
| 68 | } |
---|
| 69 | |
---|
[579] | 70 | /** |
---|
| 71 | * In case of presence of "Set-Cookie" in the headers red, store the cookie |
---|
[834] | 72 | * identifier in cookie |
---|
[579] | 73 | * |
---|
| 74 | * @param buffer the buffer to read |
---|
| 75 | * @param size size of each member |
---|
| 76 | * @param nmemb number of element to read |
---|
| 77 | * @param data the _HINTERNET structure to write in |
---|
| 78 | * @return the size red, -1 if buffer is NULL |
---|
[834] | 79 | * @see cookie |
---|
[579] | 80 | */ |
---|
[1] | 81 | size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
[509] | 82 | if(strncmp("Set-Cookie: ",(char*)buffer,12)==0){ |
---|
[1] | 83 | int i; |
---|
[492] | 84 | char* tmp; |
---|
[841] | 85 | int cnt; |
---|
| 86 | _HINTERNET *psInternet; |
---|
[1] | 87 | for(i=0;i<12;i++) |
---|
| 88 | #ifndef WIN32 |
---|
| 89 | buffer++; |
---|
| 90 | #else |
---|
| 91 | ; |
---|
| 92 | #endif |
---|
[834] | 93 | tmp=strtok(buffer,";"); |
---|
[841] | 94 | cnt=0; |
---|
| 95 | psInternet=(_HINTERNET *)data; |
---|
[834] | 96 | if(tmp!=NULL && psInternet!=NULL){ |
---|
| 97 | psInternet->cookie=(char*)malloc(sizeof(char)*(strlen(tmp)+1)); |
---|
| 98 | sprintf(psInternet->cookie,"%s",tmp); |
---|
[825] | 99 | } |
---|
[1] | 100 | } |
---|
| 101 | return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER); |
---|
| 102 | }; |
---|
| 103 | |
---|
[579] | 104 | /** |
---|
| 105 | * Define the proxy to use for a CURL handler |
---|
| 106 | * |
---|
| 107 | * @param handle the CURL handler |
---|
| 108 | * @param host the proxy host (including http://) |
---|
| 109 | * @param port the proxy port |
---|
| 110 | */ |
---|
[1] | 111 | void setProxy(CURL* handle,char* host,long port){ |
---|
[579] | 112 | char* proxyDef=(char*)malloc((strlen(host)+10+2)*sizeof(char)); |
---|
| 113 | sprintf(proxyDef,"%s:%ld",host,port); |
---|
| 114 | curl_easy_setopt(handle,CURLOPT_PROXY,proxyDef); |
---|
| 115 | free(proxyDef); |
---|
[1] | 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * MACOSX |
---|
| 120 | */ |
---|
| 121 | #if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | char* CFStringToCString(CFStringRef dest,char *buffer){ |
---|
| 125 | CFStringEncoding encoding = kCFStringEncodingUTF8; |
---|
| 126 | Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding); |
---|
| 127 | if(bool2){ |
---|
| 128 | printf("Loaded into local_buffer"); |
---|
| 129 | return buffer; |
---|
| 130 | } |
---|
| 131 | return NULL; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | OSStatus setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
| 135 | OSStatus err; |
---|
| 136 | CFDictionaryRef proxyDict; |
---|
| 137 | CFArrayRef proxies; |
---|
| 138 | |
---|
[446] | 139 | CFStringRef key_enabled = NULL; |
---|
| 140 | CFStringRef key_host = NULL; |
---|
| 141 | CFStringRef key_port = NULL; |
---|
[1] | 142 | |
---|
| 143 | bool proxy_enabled; |
---|
| 144 | char *proxy_host; |
---|
| 145 | long proxy_port; |
---|
| 146 | |
---|
| 147 | proxyDict = NULL; |
---|
| 148 | proxies = NULL; |
---|
| 149 | |
---|
| 150 | err = noErr; |
---|
| 151 | proxyDict = SCDynamicStoreCopyProxies(NULL); |
---|
| 152 | |
---|
[446] | 153 | if(strncmp(proto,"http",4)==0){ |
---|
[1] | 154 | key_enabled=kSCPropNetProxiesHTTPEnable; |
---|
| 155 | key_host=kSCPropNetProxiesHTTPProxy; |
---|
| 156 | key_port=kSCPropNetProxiesHTTPPort; |
---|
| 157 | } |
---|
| 158 | else |
---|
[446] | 159 | if(strncmp(proto,"https",5)==0){ |
---|
[1] | 160 | key_enabled=kSCPropNetProxiesHTTPSEnable; |
---|
| 161 | key_host=kSCPropNetProxiesHTTPSProxy; |
---|
| 162 | key_port=kSCPropNetProxiesHTTPSPort; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled); |
---|
| 166 | if(proxy_enabled){ |
---|
| 167 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port); |
---|
| 168 | char buffer[1024]; |
---|
| 169 | CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer); |
---|
| 170 | proxy_host=buffer; |
---|
| 171 | |
---|
| 172 | #ifdef MSG_LAF_VERBOSE |
---|
| 173 | printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer); |
---|
| 174 | #endif |
---|
| 175 | |
---|
| 176 | if (proxyDict == NULL) { |
---|
| 177 | err = coreFoundationUnknownErr; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | setProxy(handle,proxy_host,proxy_port); |
---|
| 181 | } |
---|
| 182 | return err; |
---|
| 183 | } |
---|
| 184 | #else |
---|
| 185 | /** |
---|
[579] | 186 | * Should autodetect the proxy configuration (do nothing on linux) |
---|
| 187 | * |
---|
| 188 | * @param handle a CURL handle |
---|
| 189 | * @param proto the protocol requiring the use of a proxy |
---|
[1] | 190 | */ |
---|
[828] | 191 | int setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
[1] | 192 | #ifdef MSG_LAF_VERBOSE |
---|
| 193 | fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" ); |
---|
| 194 | #endif |
---|
[820] | 195 | return 0; |
---|
[1] | 196 | } |
---|
| 197 | #endif |
---|
| 198 | |
---|
[579] | 199 | /** |
---|
| 200 | * Create a HINTERNET |
---|
| 201 | * |
---|
| 202 | * @param lpszAgent the HTPP User-Agent to use to send requests |
---|
| 203 | * @param dwAccessType type of access required |
---|
| 204 | * @param lpszProxyName the name of the proxy server(s) to use |
---|
| 205 | * @param lpszProxyBypass ip address or host names which should not be routed |
---|
| 206 | * through the proxy |
---|
| 207 | * @param dwFlags Options (INTERNET_FLAG_ASYNC,INTERNET_FLAG_FROM_CACHE,INTERNET_FLAG_OFFLINE) |
---|
| 208 | * @return the created HINTERNET |
---|
| 209 | */ |
---|
[492] | 210 | HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ |
---|
[1] | 211 | HINTERNET ret; |
---|
[490] | 212 | ret.handle=curl_multi_init(); |
---|
| 213 | ret.agent=strdup(lpszAgent); |
---|
[492] | 214 | ret.nb=0; |
---|
| 215 | ret.ihandle[ret.nb].header=NULL; |
---|
[1] | 216 | return ret; |
---|
| 217 | } |
---|
| 218 | |
---|
[579] | 219 | /** |
---|
[817] | 220 | * Add missing headers to an existing _HINTERNET |
---|
| 221 | * |
---|
| 222 | * |
---|
| 223 | * @param handle the _HINTERNET pointer |
---|
| 224 | * @param key the header parameter name |
---|
| 225 | * @param value the header parameter value |
---|
| 226 | * @return 0 if the operation succeeded, -1 in other case. |
---|
| 227 | */ |
---|
| 228 | int AddMissingHeaderEntry(_HINTERNET* handle,const char* key,const char* value){ |
---|
| 229 | int length=strlen(key)+strlen(value)+3; |
---|
| 230 | char *entry=(char*)malloc((length)*sizeof(char)); |
---|
| 231 | if(entry==NULL) |
---|
| 232 | return -1; |
---|
| 233 | snprintf (entry, length, "%s: %s", key, value); |
---|
| 234 | handle->header = curl_slist_append (handle->header, entry); |
---|
| 235 | free(entry); |
---|
| 236 | return 0; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Verify if a host is protected (appear in [security] > hosts) |
---|
| 241 | * |
---|
| 242 | * @param protectedHosts string containing all the protected hosts (coma separated) |
---|
| 243 | * @param url string used to extract the host from |
---|
| 244 | * @return 1 if the host is listed as protected, 0 in other case |
---|
| 245 | */ |
---|
| 246 | int isProtectedHost(const char* protectedHosts,const char* url){ |
---|
| 247 | char *token, *saveptr; |
---|
[841] | 248 | int cnt; |
---|
| 249 | char* host; |
---|
[817] | 250 | token = strtok_r (url, "//", &saveptr); |
---|
[841] | 251 | cnt=0; |
---|
[817] | 252 | while(token!=NULL && cnt<=1){ |
---|
| 253 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,token); |
---|
| 254 | if(cnt==1) |
---|
| 255 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,strstr(protectedHosts,token)); |
---|
| 256 | fflush(stderr); |
---|
| 257 | if(cnt==1 && strstr(protectedHosts,token)!=NULL){ |
---|
| 258 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,strstr(protectedHosts,token)); |
---|
| 259 | return 1; |
---|
| 260 | } |
---|
| 261 | token = strtok_r (NULL, "/", &saveptr); |
---|
| 262 | cnt+=1; |
---|
| 263 | } |
---|
| 264 | return 0; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /** |
---|
| 268 | * Add headers defined in [security] > attributes to an existing HINTERNET |
---|
| 269 | * @see isProtectedHost, AddMissingHeaderEntry |
---|
| 270 | * |
---|
| 271 | * @param handle the _HINTERNET pointer |
---|
| 272 | * @param conf the header parameter name |
---|
| 273 | * @param value the header parameter value |
---|
| 274 | * @return 0 if the operation succeeded, -1 in other case. |
---|
| 275 | */ |
---|
| 276 | void AddHeaderEntries(HINTERNET* handle,maps* conf){ |
---|
| 277 | map* passThrough=getMapFromMaps(conf,"security","attributes"); |
---|
| 278 | map* targetHosts=getMapFromMaps(conf,"security","hosts"); |
---|
| 279 | char* passedHeader[10]; |
---|
| 280 | int cnt=0; |
---|
| 281 | if(passThrough!=NULL && targetHosts!=NULL){ |
---|
| 282 | char *tmp=zStrdup(passThrough->value); |
---|
| 283 | char *token, *saveptr; |
---|
[841] | 284 | int i; |
---|
[817] | 285 | token = strtok_r (tmp, ",", &saveptr); |
---|
[820] | 286 | for(i=0;i<handle->nb;i++){ |
---|
[817] | 287 | if(targetHosts->value[0]=='*' || isProtectedHost(targetHosts->value,handle->ihandle[i].url)==1){ |
---|
| 288 | while (token != NULL){ |
---|
[841] | 289 | int j; |
---|
| 290 | map* tmpMap; |
---|
[817] | 291 | int length=strlen(token)+6; |
---|
| 292 | char* tmp1=(char*)malloc(length*sizeof(char)); |
---|
| 293 | snprintf(tmp1,6,"HTTP_"); |
---|
[820] | 294 | for(j=0;token[j]!='\0';j++){ |
---|
| 295 | if(token[j]!='-') |
---|
| 296 | tmp1[5+j]=toupper(token[i]); |
---|
[817] | 297 | else |
---|
[820] | 298 | tmp1[5+j]='_'; |
---|
| 299 | tmp1[5+j+1]='\0'; |
---|
[817] | 300 | } |
---|
| 301 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,tmp1); |
---|
[841] | 302 | tmpMap = getMapFromMaps(conf,"renv",tmp1); |
---|
[817] | 303 | if(tmpMap!=NULL) |
---|
| 304 | AddMissingHeaderEntry(&handle->ihandle[i],token,tmpMap->value); |
---|
| 305 | free(tmp1); |
---|
| 306 | if(handle->ihandle[i].header!=NULL) |
---|
| 307 | curl_easy_setopt(handle->ihandle[i].handle,CURLOPT_HTTPHEADER,handle->ihandle[i].header); |
---|
| 308 | cnt+=1; |
---|
| 309 | token = strtok_r (NULL, ",", &saveptr); |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | free(tmp); |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | /** |
---|
[781] | 318 | * Close a HINTERNET connection and free allocated resources |
---|
[579] | 319 | * |
---|
| 320 | * @param handle0 the HINTERNET connection to close |
---|
| 321 | */ |
---|
[492] | 322 | void InternetCloseHandle(HINTERNET* handle0){ |
---|
| 323 | int i=0; |
---|
| 324 | for(i=0;i<handle0->nb;i++){ |
---|
| 325 | _HINTERNET handle=handle0->ihandle[i]; |
---|
| 326 | if(handle.hasCacheFile>0){ |
---|
| 327 | fclose(handle.file); |
---|
| 328 | unlink(handle.filename); |
---|
| 329 | } |
---|
| 330 | else{ |
---|
| 331 | handle.pabyData = NULL; |
---|
| 332 | handle.nDataAlloc = handle.nDataLen = 0; |
---|
| 333 | } |
---|
| 334 | if(handle0->ihandle[i].header!=NULL){ |
---|
| 335 | curl_slist_free_all(handle0->ihandle[i].header); |
---|
| 336 | handle0->ihandle[i].header=NULL; |
---|
| 337 | } |
---|
[621] | 338 | if(handle.post!=NULL) |
---|
| 339 | free(handle.post); |
---|
[817] | 340 | if(handle.url!=NULL) |
---|
| 341 | free(handle.url); |
---|
[834] | 342 | if(handle.mimeType!=NULL) |
---|
| 343 | free(handle.mimeType); |
---|
| 344 | if(handle.cookie!=NULL) |
---|
| 345 | free(handle.cookie); |
---|
[1] | 346 | } |
---|
[492] | 347 | if(handle0->handle) |
---|
| 348 | curl_multi_cleanup(handle0->handle); |
---|
| 349 | free(handle0->agent); |
---|
| 350 | for(i=handle0->nb-1;i>=0;i--){ |
---|
| 351 | free(handle0->waitingRequests[i]); |
---|
[1] | 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
[579] | 355 | /** |
---|
| 356 | * Create a new element in the download queue |
---|
| 357 | * |
---|
| 358 | * @param hInternet the HINTERNET connection to add the download link |
---|
| 359 | * @param lpszUrl the url to download |
---|
| 360 | * @param lpszHeaders the additional headers to be sent to the HTTP server |
---|
| 361 | * @param dwHeadersLength the size of the additional headers |
---|
| 362 | * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file) |
---|
| 363 | * @param dwContext not used |
---|
| 364 | */ |
---|
[492] | 365 | HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){ |
---|
[1] | 366 | |
---|
| 367 | char filename[255]; |
---|
[490] | 368 | struct MemoryStruct header; |
---|
| 369 | |
---|
[492] | 370 | hInternet->ihandle[hInternet->nb].handle=curl_easy_init( ); |
---|
| 371 | hInternet->ihandle[hInternet->nb].hasCacheFile=0; |
---|
| 372 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
[817] | 373 | hInternet->ihandle[hInternet->nb].url = NULL; |
---|
[492] | 374 | hInternet->ihandle[hInternet->nb].mimeType = NULL; |
---|
[834] | 375 | hInternet->ihandle[hInternet->nb].cookie = NULL; |
---|
[492] | 376 | hInternet->ihandle[hInternet->nb].nDataLen = 0; |
---|
| 377 | hInternet->ihandle[hInternet->nb].id = hInternet->nb; |
---|
| 378 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
| 379 | hInternet->ihandle[hInternet->nb].pabyData = NULL; |
---|
[621] | 380 | hInternet->ihandle[hInternet->nb].post = NULL; |
---|
[834] | 381 | |
---|
[492] | 382 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL"); |
---|
[490] | 383 | #ifndef TIGER |
---|
[492] | 384 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
[490] | 385 | #endif |
---|
[492] | 386 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent); |
---|
[490] | 387 | |
---|
[492] | 388 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1); |
---|
| 389 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3); |
---|
[490] | 390 | |
---|
| 391 | header.memory=NULL; |
---|
| 392 | header.size = 0; |
---|
| 393 | |
---|
[492] | 394 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data); |
---|
| 395 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header); |
---|
[490] | 396 | |
---|
| 397 | #ifdef MSG_LAF_VERBOSE |
---|
[492] | 398 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1); |
---|
[490] | 399 | #endif |
---|
| 400 | |
---|
[1] | 401 | |
---|
| 402 | switch(dwFlags) |
---|
| 403 | { |
---|
[492] | 404 | case INTERNET_FLAG_NO_CACHE_WRITE: |
---|
| 405 | hInternet->ihandle[hInternet->nb].hasCacheFile=-1; |
---|
| 406 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into); |
---|
| 407 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); |
---|
[1] | 408 | break; |
---|
| 409 | default: |
---|
[492] | 410 | sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL)); |
---|
| 411 | hInternet->ihandle[hInternet->nb].filename[24]=0; |
---|
[1] | 412 | #ifdef MSG_LAF_VERBOSE |
---|
[492] | 413 | fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename); |
---|
[1] | 414 | #endif |
---|
[492] | 415 | hInternet->ihandle[hInternet->nb].filename=filename; |
---|
| 416 | hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+"); |
---|
[1] | 417 | |
---|
[492] | 418 | hInternet->ihandle[hInternet->nb].hasCacheFile=1; |
---|
| 419 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL); |
---|
| 420 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file); |
---|
| 421 | hInternet->ihandle[hInternet->nb].nDataLen=0; |
---|
[1] | 422 | break; |
---|
| 423 | } |
---|
| 424 | #ifdef ULINET_DEBUG |
---|
| 425 | fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders); |
---|
| 426 | #endif |
---|
| 427 | if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){ |
---|
| 428 | #ifdef MSG_LAF_VERBOSE |
---|
| 429 | fprintf(stderr,"FROM ULINET !!"); |
---|
[621] | 430 | fprintf(stderr,"HEADER : [%s] %d\n",lpszHeaders,dwHeadersLength); |
---|
[1] | 431 | #endif |
---|
[492] | 432 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1); |
---|
[1] | 433 | #ifdef ULINET_DEBUG |
---|
| 434 | fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength); |
---|
[492] | 435 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1); |
---|
[1] | 436 | #endif |
---|
[621] | 437 | hInternet->ihandle[hInternet->nb].post=strdup(lpszHeaders); |
---|
| 438 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,hInternet->ihandle[hInternet->nb].post); |
---|
| 439 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDSIZE,(long)dwHeadersLength); |
---|
[1] | 440 | } |
---|
[607] | 441 | if(hInternet->ihandle[hInternet->nb].header!=NULL) |
---|
| 442 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header); |
---|
[1] | 443 | |
---|
[492] | 444 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl); |
---|
[817] | 445 | hInternet->ihandle[hInternet->nb].url = zStrdup(lpszUrl); |
---|
[490] | 446 | |
---|
[492] | 447 | curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle); |
---|
[490] | 448 | |
---|
[621] | 449 | hInternet->ihandle[hInternet->nb].header=NULL; |
---|
[492] | 450 | ++hInternet->nb; |
---|
[490] | 451 | |
---|
[446] | 452 | #ifdef ULINET_DEBUG |
---|
| 453 | fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType); |
---|
[478] | 454 | fflush(stderr); |
---|
[446] | 455 | #endif |
---|
[492] | 456 | return *hInternet; |
---|
[1] | 457 | }; |
---|
| 458 | |
---|
[579] | 459 | /** |
---|
| 460 | * Download all opened urls in the queue |
---|
| 461 | * |
---|
| 462 | * @param hInternet the HINTERNET structure containing the queue |
---|
| 463 | * @return 0 |
---|
| 464 | */ |
---|
[492] | 465 | int processDownloads(HINTERNET* hInternet){ |
---|
| 466 | int still_running=0; |
---|
| 467 | int msgs_left=0; |
---|
| 468 | int i=0; |
---|
| 469 | do{ |
---|
| 470 | curl_multi_perform(hInternet->handle, &still_running); |
---|
| 471 | }while(still_running); |
---|
| 472 | for(i=0;i<hInternet->nb;i++){ |
---|
[534] | 473 | char *tmp; |
---|
| 474 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp); |
---|
[539] | 475 | if(tmp!=NULL) |
---|
| 476 | hInternet->ihandle[i].mimeType=strdup(tmp); |
---|
[607] | 477 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_RESPONSE_CODE,&hInternet->ihandle[i].code); |
---|
[492] | 478 | curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle); |
---|
| 479 | curl_easy_cleanup(hInternet->ihandle[i].handle); |
---|
| 480 | } |
---|
[509] | 481 | return 0; |
---|
[492] | 482 | } |
---|
| 483 | |
---|
[579] | 484 | /** |
---|
[834] | 485 | * Initialize the cookie for a specific index (hInternet.nb) |
---|
[579] | 486 | * |
---|
[834] | 487 | * @param hInternet the HINTERNET structure to know the cookie index to reset |
---|
[579] | 488 | * @return 1 |
---|
| 489 | * @see HINTERNET |
---|
| 490 | */ |
---|
[1] | 491 | int freeCookieList(HINTERNET hInternet){ |
---|
[834] | 492 | if(hInternet.ihandle[hInternet.nb].cookie) |
---|
| 493 | free(hInternet.ihandle[hInternet.nb].cookie); |
---|
| 494 | hInternet.ihandle[hInternet.nb].cookie=NULL; |
---|
[1] | 495 | #ifndef TIGER |
---|
[492] | 496 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
[1] | 497 | #endif |
---|
| 498 | return 1; |
---|
| 499 | } |
---|
| 500 | |
---|
[579] | 501 | /** |
---|
| 502 | * Copy a downloaded content |
---|
| 503 | * |
---|
| 504 | * @param hInternet the _HINTERNET structure |
---|
| 505 | * @param lpBuffer the memory space to copy the downloaded content |
---|
| 506 | * @param dwNumberOfBytesToRead the size of lpBuffer |
---|
| 507 | * @param lpdwNumberOfBytesRead number of bytes red |
---|
| 508 | * @return 1 on success, 0 if failure |
---|
| 509 | */ |
---|
[492] | 510 | int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){ |
---|
[1] | 511 | int dwDataSize; |
---|
| 512 | |
---|
| 513 | if(hInternet.hasCacheFile>0){ |
---|
| 514 | fseek (hInternet.file , 0 , SEEK_END); |
---|
| 515 | dwDataSize=ftell(hInternet.file); //taille du ficher |
---|
| 516 | rewind (hInternet.file); |
---|
| 517 | } |
---|
| 518 | else{ |
---|
| 519 | memset(lpBuffer,0,hInternet.nDataLen+1); |
---|
[375] | 520 | memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen ); |
---|
[1] | 521 | dwDataSize=hInternet.nDataLen; |
---|
| 522 | free( hInternet.pabyData ); |
---|
| 523 | hInternet.pabyData=NULL; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ) |
---|
| 527 | return 0; |
---|
| 528 | |
---|
| 529 | #ifdef MSG_LAF_VERBOSE |
---|
| 530 | printf("\nfile size : %dko\n",dwDataSize/1024); |
---|
| 531 | #endif |
---|
| 532 | |
---|
| 533 | if(hInternet.hasCacheFile>0){ |
---|
| 534 | *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); |
---|
| 535 | } |
---|
| 536 | else{ |
---|
| 537 | *lpdwNumberOfBytesRead = hInternet.nDataLen; |
---|
| 538 | free( hInternet.pabyData ); |
---|
| 539 | hInternet.pabyData = NULL; |
---|
| 540 | hInternet.nDataAlloc = hInternet.nDataLen = 0; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | if( *lpdwNumberOfBytesRead < dwDataSize ) |
---|
| 544 | return 0; |
---|
| 545 | else |
---|
| 546 | return 1; // TRUE |
---|
| 547 | } |
---|
| 548 | |
---|
[607] | 549 | |
---|
| 550 | /** |
---|
[781] | 551 | * Use basic authentication for accessing a resource |
---|
[607] | 552 | * |
---|
| 553 | * @param hInternet the _HINTERNET structure |
---|
| 554 | * @param login the login to use to authenticate |
---|
| 555 | * @param passwd the password to use to authenticate |
---|
| 556 | */ |
---|
| 557 | int setBasicAuth(HINTERNET hInternet,char* login,char* passwd){ |
---|
| 558 | char *tmp; |
---|
| 559 | tmp=(char*)malloc((strlen(login)+strlen(passwd)+2)*sizeof(char)); |
---|
| 560 | sprintf(tmp,"%s:%s",login,passwd); |
---|
| 561 | if(curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle,CURLOPT_USERPWD,tmp)==CURLE_OUT_OF_MEMORY){ |
---|
| 562 | free(tmp); |
---|
| 563 | return -1; |
---|
| 564 | } |
---|
| 565 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_HTTPAUTH,CURLAUTH_ANY); |
---|
| 566 | free(tmp); |
---|
| 567 | return 0; |
---|
| 568 | } |
---|