[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 |
---|
| 72 | * identifier in CCookie |
---|
| 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 |
---|
| 79 | * @see CCookie |
---|
| 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; |
---|
[508] | 84 | char env[256]; |
---|
| 85 | char path[256]; |
---|
| 86 | char domain[256]; |
---|
[492] | 87 | char* tmp; |
---|
[1] | 88 | for(i=0;i<12;i++) |
---|
| 89 | #ifndef WIN32 |
---|
| 90 | buffer++; |
---|
| 91 | #else |
---|
| 92 | ; |
---|
| 93 | #endif |
---|
[509] | 94 | sscanf((char*)buffer,"%s; path=%s; domain=%s",env,path,domain); |
---|
[797] | 95 | _HINTERNET *psInternet=(_HINTERNET *)data; |
---|
| 96 | tmp=strcat(env,CCookie[psInternet->id][0]); |
---|
[1] | 97 | #ifdef MSG_LAF_OUT |
---|
| 98 | printf("\n**Cookie env : [%s] , path : [%s], domain : [%s]**\n",env,path,domain); |
---|
| 99 | printf("buffer : %d (%s) (%s) (%s)\n",(buffer==NULL),buffer,tmp,CCookie); |
---|
| 100 | #endif |
---|
[797] | 101 | strcpy(CCookie[psInternet->id][0],tmp); |
---|
[1] | 102 | } |
---|
| 103 | return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER); |
---|
| 104 | }; |
---|
| 105 | |
---|
[579] | 106 | /** |
---|
| 107 | * Define the proxy to use for a CURL handler |
---|
| 108 | * |
---|
| 109 | * @param handle the CURL handler |
---|
| 110 | * @param host the proxy host (including http://) |
---|
| 111 | * @param port the proxy port |
---|
| 112 | */ |
---|
[1] | 113 | void setProxy(CURL* handle,char* host,long port){ |
---|
[579] | 114 | char* proxyDef=(char*)malloc((strlen(host)+10+2)*sizeof(char)); |
---|
| 115 | sprintf(proxyDef,"%s:%ld",host,port); |
---|
| 116 | curl_easy_setopt(handle,CURLOPT_PROXY,proxyDef); |
---|
| 117 | free(proxyDef); |
---|
[1] | 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * MACOSX |
---|
| 122 | */ |
---|
| 123 | #if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | char* CFStringToCString(CFStringRef dest,char *buffer){ |
---|
| 127 | CFStringEncoding encoding = kCFStringEncodingUTF8; |
---|
| 128 | Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding); |
---|
| 129 | if(bool2){ |
---|
| 130 | printf("Loaded into local_buffer"); |
---|
| 131 | return buffer; |
---|
| 132 | } |
---|
| 133 | return NULL; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | OSStatus setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
| 137 | OSStatus err; |
---|
| 138 | CFDictionaryRef proxyDict; |
---|
| 139 | CFArrayRef proxies; |
---|
| 140 | |
---|
[446] | 141 | CFStringRef key_enabled = NULL; |
---|
| 142 | CFStringRef key_host = NULL; |
---|
| 143 | CFStringRef key_port = NULL; |
---|
[1] | 144 | |
---|
| 145 | bool proxy_enabled; |
---|
| 146 | char *proxy_host; |
---|
| 147 | long proxy_port; |
---|
| 148 | |
---|
| 149 | proxyDict = NULL; |
---|
| 150 | proxies = NULL; |
---|
| 151 | |
---|
| 152 | err = noErr; |
---|
| 153 | proxyDict = SCDynamicStoreCopyProxies(NULL); |
---|
| 154 | |
---|
[446] | 155 | if(strncmp(proto,"http",4)==0){ |
---|
[1] | 156 | key_enabled=kSCPropNetProxiesHTTPEnable; |
---|
| 157 | key_host=kSCPropNetProxiesHTTPProxy; |
---|
| 158 | key_port=kSCPropNetProxiesHTTPPort; |
---|
| 159 | } |
---|
| 160 | else |
---|
[446] | 161 | if(strncmp(proto,"https",5)==0){ |
---|
[1] | 162 | key_enabled=kSCPropNetProxiesHTTPSEnable; |
---|
| 163 | key_host=kSCPropNetProxiesHTTPSProxy; |
---|
| 164 | key_port=kSCPropNetProxiesHTTPSPort; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled); |
---|
| 168 | if(proxy_enabled){ |
---|
| 169 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port); |
---|
| 170 | char buffer[1024]; |
---|
| 171 | CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer); |
---|
| 172 | proxy_host=buffer; |
---|
| 173 | |
---|
| 174 | #ifdef MSG_LAF_VERBOSE |
---|
| 175 | printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer); |
---|
| 176 | #endif |
---|
| 177 | |
---|
| 178 | if (proxyDict == NULL) { |
---|
| 179 | err = coreFoundationUnknownErr; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | setProxy(handle,proxy_host,proxy_port); |
---|
| 183 | } |
---|
| 184 | return err; |
---|
| 185 | } |
---|
| 186 | #else |
---|
| 187 | /** |
---|
[579] | 188 | * Should autodetect the proxy configuration (do nothing on linux) |
---|
| 189 | * |
---|
| 190 | * @param handle a CURL handle |
---|
| 191 | * @param proto the protocol requiring the use of a proxy |
---|
[1] | 192 | */ |
---|
| 193 | bool setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
| 194 | #ifdef MSG_LAF_VERBOSE |
---|
| 195 | fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" ); |
---|
| 196 | #endif |
---|
[509] | 197 | return true; |
---|
[1] | 198 | } |
---|
| 199 | #endif |
---|
| 200 | |
---|
[579] | 201 | /** |
---|
| 202 | * Create a HINTERNET |
---|
| 203 | * |
---|
| 204 | * @param lpszAgent the HTPP User-Agent to use to send requests |
---|
| 205 | * @param dwAccessType type of access required |
---|
| 206 | * @param lpszProxyName the name of the proxy server(s) to use |
---|
| 207 | * @param lpszProxyBypass ip address or host names which should not be routed |
---|
| 208 | * through the proxy |
---|
| 209 | * @param dwFlags Options (INTERNET_FLAG_ASYNC,INTERNET_FLAG_FROM_CACHE,INTERNET_FLAG_OFFLINE) |
---|
| 210 | * @return the created HINTERNET |
---|
| 211 | */ |
---|
[492] | 212 | HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ |
---|
[1] | 213 | HINTERNET ret; |
---|
[490] | 214 | ret.handle=curl_multi_init(); |
---|
| 215 | ret.agent=strdup(lpszAgent); |
---|
[492] | 216 | ret.nb=0; |
---|
| 217 | ret.ihandle[ret.nb].header=NULL; |
---|
[1] | 218 | return ret; |
---|
| 219 | } |
---|
| 220 | |
---|
[579] | 221 | /** |
---|
[817] | 222 | * Add missing headers to an existing _HINTERNET |
---|
| 223 | * |
---|
| 224 | * |
---|
| 225 | * @param handle the _HINTERNET pointer |
---|
| 226 | * @param key the header parameter name |
---|
| 227 | * @param value the header parameter value |
---|
| 228 | * @return 0 if the operation succeeded, -1 in other case. |
---|
| 229 | */ |
---|
| 230 | int AddMissingHeaderEntry(_HINTERNET* handle,const char* key,const char* value){ |
---|
| 231 | int length=strlen(key)+strlen(value)+3; |
---|
| 232 | char *entry=(char*)malloc((length)*sizeof(char)); |
---|
| 233 | if(entry==NULL) |
---|
| 234 | return -1; |
---|
| 235 | snprintf (entry, length, "%s: %s", key, value); |
---|
| 236 | handle->header = curl_slist_append (handle->header, entry); |
---|
| 237 | free(entry); |
---|
| 238 | return 0; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /** |
---|
| 242 | * Verify if a host is protected (appear in [security] > hosts) |
---|
| 243 | * |
---|
| 244 | * @param protectedHosts string containing all the protected hosts (coma separated) |
---|
| 245 | * @param url string used to extract the host from |
---|
| 246 | * @return 1 if the host is listed as protected, 0 in other case |
---|
| 247 | */ |
---|
| 248 | int isProtectedHost(const char* protectedHosts,const char* url){ |
---|
| 249 | char *token, *saveptr; |
---|
| 250 | token = strtok_r (url, "//", &saveptr); |
---|
| 251 | int cnt=0; |
---|
| 252 | char* host; |
---|
| 253 | while(token!=NULL && cnt<=1){ |
---|
| 254 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,token); |
---|
| 255 | if(cnt==1) |
---|
| 256 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,strstr(protectedHosts,token)); |
---|
| 257 | fflush(stderr); |
---|
| 258 | if(cnt==1 && strstr(protectedHosts,token)!=NULL){ |
---|
| 259 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,strstr(protectedHosts,token)); |
---|
| 260 | return 1; |
---|
| 261 | } |
---|
| 262 | token = strtok_r (NULL, "/", &saveptr); |
---|
| 263 | cnt+=1; |
---|
| 264 | } |
---|
| 265 | return 0; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | /** |
---|
| 269 | * Add headers defined in [security] > attributes to an existing HINTERNET |
---|
| 270 | * @see isProtectedHost, AddMissingHeaderEntry |
---|
| 271 | * |
---|
| 272 | * @param handle the _HINTERNET pointer |
---|
| 273 | * @param conf the header parameter name |
---|
| 274 | * @param value the header parameter value |
---|
| 275 | * @return 0 if the operation succeeded, -1 in other case. |
---|
| 276 | */ |
---|
| 277 | void AddHeaderEntries(HINTERNET* handle,maps* conf){ |
---|
| 278 | map* passThrough=getMapFromMaps(conf,"security","attributes"); |
---|
| 279 | map* targetHosts=getMapFromMaps(conf,"security","hosts"); |
---|
| 280 | char* passedHeader[10]; |
---|
| 281 | int cnt=0; |
---|
| 282 | if(passThrough!=NULL && targetHosts!=NULL){ |
---|
| 283 | char *tmp=zStrdup(passThrough->value); |
---|
| 284 | char *token, *saveptr; |
---|
| 285 | token = strtok_r (tmp, ",", &saveptr); |
---|
| 286 | for(int i=0;i<handle->nb;i++){ |
---|
| 287 | if(targetHosts->value[0]=='*' || isProtectedHost(targetHosts->value,handle->ihandle[i].url)==1){ |
---|
| 288 | while (token != NULL){ |
---|
| 289 | int length=strlen(token)+6; |
---|
| 290 | char* tmp1=(char*)malloc(length*sizeof(char)); |
---|
| 291 | snprintf(tmp1,6,"HTTP_"); |
---|
| 292 | for(int i=0;token[i]!='\0';i++){ |
---|
| 293 | if(token[i]!='-') |
---|
| 294 | tmp1[5+i]=toupper(token[i]); |
---|
| 295 | else |
---|
| 296 | tmp1[5+i]='_'; |
---|
| 297 | tmp1[5+i+1]='\0'; |
---|
| 298 | } |
---|
| 299 | fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,tmp1); |
---|
| 300 | map* tmpMap = getMapFromMaps(conf,"renv",tmp1); |
---|
| 301 | if(tmpMap!=NULL) |
---|
| 302 | AddMissingHeaderEntry(&handle->ihandle[i],token,tmpMap->value); |
---|
| 303 | free(tmp1); |
---|
| 304 | if(handle->ihandle[i].header!=NULL) |
---|
| 305 | curl_easy_setopt(handle->ihandle[i].handle,CURLOPT_HTTPHEADER,handle->ihandle[i].header); |
---|
| 306 | cnt+=1; |
---|
| 307 | token = strtok_r (NULL, ",", &saveptr); |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | free(tmp); |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | /** |
---|
[781] | 316 | * Close a HINTERNET connection and free allocated resources |
---|
[579] | 317 | * |
---|
| 318 | * @param handle0 the HINTERNET connection to close |
---|
| 319 | */ |
---|
[492] | 320 | void InternetCloseHandle(HINTERNET* handle0){ |
---|
| 321 | int i=0; |
---|
| 322 | for(i=0;i<handle0->nb;i++){ |
---|
| 323 | _HINTERNET handle=handle0->ihandle[i]; |
---|
| 324 | if(handle.hasCacheFile>0){ |
---|
| 325 | fclose(handle.file); |
---|
| 326 | unlink(handle.filename); |
---|
| 327 | } |
---|
| 328 | else{ |
---|
| 329 | handle.pabyData = NULL; |
---|
| 330 | handle.nDataAlloc = handle.nDataLen = 0; |
---|
| 331 | } |
---|
| 332 | if(handle0->ihandle[i].header!=NULL){ |
---|
| 333 | curl_slist_free_all(handle0->ihandle[i].header); |
---|
| 334 | handle0->ihandle[i].header=NULL; |
---|
| 335 | } |
---|
[621] | 336 | if(handle.post!=NULL) |
---|
| 337 | free(handle.post); |
---|
[817] | 338 | if(handle.url!=NULL) |
---|
| 339 | free(handle.url); |
---|
[492] | 340 | free(handle.mimeType); |
---|
[534] | 341 | handle.mimeType = NULL; |
---|
[1] | 342 | } |
---|
[492] | 343 | if(handle0->handle) |
---|
| 344 | curl_multi_cleanup(handle0->handle); |
---|
| 345 | free(handle0->agent); |
---|
| 346 | for(i=handle0->nb-1;i>=0;i--){ |
---|
| 347 | free(handle0->waitingRequests[i]); |
---|
[1] | 348 | } |
---|
| 349 | } |
---|
| 350 | |
---|
[579] | 351 | /** |
---|
| 352 | * Create a new element in the download queue |
---|
| 353 | * |
---|
| 354 | * @param hInternet the HINTERNET connection to add the download link |
---|
| 355 | * @param lpszUrl the url to download |
---|
| 356 | * @param lpszHeaders the additional headers to be sent to the HTTP server |
---|
| 357 | * @param dwHeadersLength the size of the additional headers |
---|
| 358 | * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file) |
---|
| 359 | * @param dwContext not used |
---|
| 360 | */ |
---|
[492] | 361 | HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){ |
---|
[1] | 362 | |
---|
| 363 | char filename[255]; |
---|
[490] | 364 | struct MemoryStruct header; |
---|
| 365 | |
---|
[492] | 366 | hInternet->ihandle[hInternet->nb].handle=curl_easy_init( ); |
---|
| 367 | hInternet->ihandle[hInternet->nb].hasCacheFile=0; |
---|
| 368 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
[817] | 369 | hInternet->ihandle[hInternet->nb].url = NULL; |
---|
[492] | 370 | hInternet->ihandle[hInternet->nb].mimeType = NULL; |
---|
| 371 | hInternet->ihandle[hInternet->nb].nDataLen = 0; |
---|
| 372 | hInternet->ihandle[hInternet->nb].id = hInternet->nb; |
---|
| 373 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
| 374 | hInternet->ihandle[hInternet->nb].pabyData = NULL; |
---|
[621] | 375 | hInternet->ihandle[hInternet->nb].post = NULL; |
---|
[1] | 376 | |
---|
[492] | 377 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL"); |
---|
[490] | 378 | #ifndef TIGER |
---|
[492] | 379 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
[490] | 380 | #endif |
---|
[492] | 381 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent); |
---|
[490] | 382 | |
---|
[492] | 383 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1); |
---|
| 384 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3); |
---|
[490] | 385 | |
---|
| 386 | header.memory=NULL; |
---|
| 387 | header.size = 0; |
---|
| 388 | |
---|
[492] | 389 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data); |
---|
| 390 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header); |
---|
[490] | 391 | |
---|
| 392 | #ifdef MSG_LAF_VERBOSE |
---|
[492] | 393 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1); |
---|
[490] | 394 | #endif |
---|
| 395 | |
---|
[1] | 396 | |
---|
| 397 | switch(dwFlags) |
---|
| 398 | { |
---|
[492] | 399 | case INTERNET_FLAG_NO_CACHE_WRITE: |
---|
| 400 | hInternet->ihandle[hInternet->nb].hasCacheFile=-1; |
---|
| 401 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into); |
---|
| 402 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); |
---|
[1] | 403 | break; |
---|
| 404 | default: |
---|
[492] | 405 | sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL)); |
---|
| 406 | hInternet->ihandle[hInternet->nb].filename[24]=0; |
---|
[1] | 407 | #ifdef MSG_LAF_VERBOSE |
---|
[492] | 408 | fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename); |
---|
[1] | 409 | #endif |
---|
[492] | 410 | hInternet->ihandle[hInternet->nb].filename=filename; |
---|
| 411 | hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+"); |
---|
[1] | 412 | |
---|
[492] | 413 | hInternet->ihandle[hInternet->nb].hasCacheFile=1; |
---|
| 414 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL); |
---|
| 415 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file); |
---|
| 416 | hInternet->ihandle[hInternet->nb].nDataLen=0; |
---|
[1] | 417 | break; |
---|
| 418 | } |
---|
| 419 | #ifdef ULINET_DEBUG |
---|
| 420 | fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders); |
---|
| 421 | #endif |
---|
| 422 | if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){ |
---|
| 423 | #ifdef MSG_LAF_VERBOSE |
---|
| 424 | fprintf(stderr,"FROM ULINET !!"); |
---|
[621] | 425 | fprintf(stderr,"HEADER : [%s] %d\n",lpszHeaders,dwHeadersLength); |
---|
[1] | 426 | #endif |
---|
[492] | 427 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1); |
---|
[1] | 428 | #ifdef ULINET_DEBUG |
---|
| 429 | fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength); |
---|
[492] | 430 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1); |
---|
[1] | 431 | #endif |
---|
[621] | 432 | hInternet->ihandle[hInternet->nb].post=strdup(lpszHeaders); |
---|
| 433 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,hInternet->ihandle[hInternet->nb].post); |
---|
| 434 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDSIZE,(long)dwHeadersLength); |
---|
[1] | 435 | } |
---|
[607] | 436 | if(hInternet->ihandle[hInternet->nb].header!=NULL) |
---|
| 437 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header); |
---|
[1] | 438 | |
---|
[492] | 439 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl); |
---|
[817] | 440 | hInternet->ihandle[hInternet->nb].url = zStrdup(lpszUrl); |
---|
[490] | 441 | |
---|
[492] | 442 | curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle); |
---|
[490] | 443 | |
---|
[621] | 444 | hInternet->ihandle[hInternet->nb].header=NULL; |
---|
[492] | 445 | ++hInternet->nb; |
---|
[490] | 446 | |
---|
[446] | 447 | #ifdef ULINET_DEBUG |
---|
| 448 | fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType); |
---|
[478] | 449 | fflush(stderr); |
---|
[446] | 450 | #endif |
---|
[492] | 451 | return *hInternet; |
---|
[1] | 452 | }; |
---|
| 453 | |
---|
[579] | 454 | /** |
---|
| 455 | * Download all opened urls in the queue |
---|
| 456 | * |
---|
| 457 | * @param hInternet the HINTERNET structure containing the queue |
---|
| 458 | * @return 0 |
---|
| 459 | */ |
---|
[492] | 460 | int processDownloads(HINTERNET* hInternet){ |
---|
| 461 | int still_running=0; |
---|
| 462 | int msgs_left=0; |
---|
| 463 | int i=0; |
---|
| 464 | do{ |
---|
| 465 | curl_multi_perform(hInternet->handle, &still_running); |
---|
| 466 | }while(still_running); |
---|
| 467 | for(i=0;i<hInternet->nb;i++){ |
---|
[534] | 468 | char *tmp; |
---|
| 469 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp); |
---|
[539] | 470 | if(tmp!=NULL) |
---|
| 471 | hInternet->ihandle[i].mimeType=strdup(tmp); |
---|
[607] | 472 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_RESPONSE_CODE,&hInternet->ihandle[i].code); |
---|
[492] | 473 | curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle); |
---|
| 474 | curl_easy_cleanup(hInternet->ihandle[i].handle); |
---|
| 475 | } |
---|
[509] | 476 | return 0; |
---|
[492] | 477 | } |
---|
| 478 | |
---|
[579] | 479 | /** |
---|
| 480 | * Initialize the CCookie for a specific index (hInternet.nb) |
---|
| 481 | * |
---|
| 482 | * @param hInternet the HINTERNET structure to know the CCookie index to reset |
---|
| 483 | * @return 1 |
---|
| 484 | * @see HINTERNET |
---|
| 485 | */ |
---|
[1] | 486 | int freeCookieList(HINTERNET hInternet){ |
---|
[492] | 487 | memset(&CCookie[hInternet.nb][0],0,1024); |
---|
[1] | 488 | #ifndef TIGER |
---|
[492] | 489 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
[1] | 490 | #endif |
---|
| 491 | return 1; |
---|
| 492 | } |
---|
| 493 | |
---|
[579] | 494 | /** |
---|
| 495 | * Copy a downloaded content |
---|
| 496 | * |
---|
| 497 | * @param hInternet the _HINTERNET structure |
---|
| 498 | * @param lpBuffer the memory space to copy the downloaded content |
---|
| 499 | * @param dwNumberOfBytesToRead the size of lpBuffer |
---|
| 500 | * @param lpdwNumberOfBytesRead number of bytes red |
---|
| 501 | * @return 1 on success, 0 if failure |
---|
| 502 | */ |
---|
[492] | 503 | int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){ |
---|
[1] | 504 | int dwDataSize; |
---|
| 505 | |
---|
| 506 | if(hInternet.hasCacheFile>0){ |
---|
| 507 | fseek (hInternet.file , 0 , SEEK_END); |
---|
| 508 | dwDataSize=ftell(hInternet.file); //taille du ficher |
---|
| 509 | rewind (hInternet.file); |
---|
| 510 | } |
---|
| 511 | else{ |
---|
| 512 | memset(lpBuffer,0,hInternet.nDataLen+1); |
---|
[375] | 513 | memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen ); |
---|
[1] | 514 | dwDataSize=hInternet.nDataLen; |
---|
| 515 | free( hInternet.pabyData ); |
---|
| 516 | hInternet.pabyData=NULL; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ) |
---|
| 520 | return 0; |
---|
| 521 | |
---|
| 522 | #ifdef MSG_LAF_VERBOSE |
---|
| 523 | printf("\nfile size : %dko\n",dwDataSize/1024); |
---|
| 524 | #endif |
---|
| 525 | |
---|
| 526 | if(hInternet.hasCacheFile>0){ |
---|
| 527 | *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); |
---|
| 528 | } |
---|
| 529 | else{ |
---|
| 530 | *lpdwNumberOfBytesRead = hInternet.nDataLen; |
---|
| 531 | free( hInternet.pabyData ); |
---|
| 532 | hInternet.pabyData = NULL; |
---|
| 533 | hInternet.nDataAlloc = hInternet.nDataLen = 0; |
---|
| 534 | } |
---|
| 535 | |
---|
[492] | 536 | CCookie[hInternet.id][0]=0; |
---|
[1] | 537 | |
---|
| 538 | if( *lpdwNumberOfBytesRead < dwDataSize ) |
---|
| 539 | return 0; |
---|
| 540 | else |
---|
| 541 | return 1; // TRUE |
---|
| 542 | } |
---|
| 543 | |
---|
[607] | 544 | |
---|
| 545 | /** |
---|
[781] | 546 | * Use basic authentication for accessing a resource |
---|
[607] | 547 | * |
---|
| 548 | * @param hInternet the _HINTERNET structure |
---|
| 549 | * @param login the login to use to authenticate |
---|
| 550 | * @param passwd the password to use to authenticate |
---|
| 551 | */ |
---|
| 552 | int setBasicAuth(HINTERNET hInternet,char* login,char* passwd){ |
---|
| 553 | char *tmp; |
---|
| 554 | tmp=(char*)malloc((strlen(login)+strlen(passwd)+2)*sizeof(char)); |
---|
| 555 | sprintf(tmp,"%s:%s",login,passwd); |
---|
| 556 | if(curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle,CURLOPT_USERPWD,tmp)==CURLE_OUT_OF_MEMORY){ |
---|
| 557 | free(tmp); |
---|
| 558 | return -1; |
---|
| 559 | } |
---|
| 560 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_HTTPAUTH,CURLAUTH_ANY); |
---|
| 561 | free(tmp); |
---|
| 562 | return 0; |
---|
| 563 | } |
---|