[1] | 1 | /** |
---|
| 2 | * ulinet.c |
---|
| 3 | * |
---|
| 4 | * Author : Gérald FENOY |
---|
| 5 | * |
---|
| 6 | * Copyright (c) 2008-2010 GeoLabs SARL |
---|
| 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> |
---|
[492] | 32 | |
---|
[1] | 33 | size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
| 34 | size_t realsize = size * nmemb; |
---|
[492] | 35 | _HINTERNET *psInternet; |
---|
[93] | 36 | if(buffer==NULL){ |
---|
[1] | 37 | buffer=NULL; |
---|
| 38 | return -1; |
---|
| 39 | } |
---|
[492] | 40 | psInternet=(_HINTERNET *)data; |
---|
[1] | 41 | if(psInternet->pabyData){ |
---|
[446] | 42 | psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1); |
---|
[1] | 43 | psInternet->nDataAlloc+=psInternet->nDataLen+realsize+1; |
---|
| 44 | } |
---|
| 45 | else{ |
---|
[446] | 46 | psInternet->pabyData=(unsigned char*)malloc(psInternet->nDataLen+realsize+1); |
---|
[1] | 47 | psInternet->nDataAlloc=realsize+1; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | if (psInternet->pabyData) { |
---|
| 51 | memcpy( psInternet->pabyData + psInternet->nDataLen, buffer, realsize); |
---|
| 52 | psInternet->nDataLen += realsize; |
---|
| 53 | psInternet->pabyData[psInternet->nDataLen] = 0; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | buffer=NULL; |
---|
| 57 | return realsize; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){ |
---|
[509] | 61 | if(strncmp("Set-Cookie: ",(char*)buffer,12)==0){ |
---|
[1] | 62 | int i; |
---|
[508] | 63 | char env[256]; |
---|
| 64 | char path[256]; |
---|
| 65 | char domain[256]; |
---|
[492] | 66 | char* tmp; |
---|
[1] | 67 | for(i=0;i<12;i++) |
---|
| 68 | #ifndef WIN32 |
---|
| 69 | buffer++; |
---|
| 70 | #else |
---|
| 71 | ; |
---|
| 72 | #endif |
---|
[509] | 73 | sscanf((char*)buffer,"%s; path=%s; domain=%s",env,path,domain); |
---|
| 74 | tmp=strcat(env,CCookie[0]); |
---|
[1] | 75 | #ifdef MSG_LAF_OUT |
---|
| 76 | printf("\n**Cookie env : [%s] , path : [%s], domain : [%s]**\n",env,path,domain); |
---|
| 77 | printf("buffer : %d (%s) (%s) (%s)\n",(buffer==NULL),buffer,tmp,CCookie); |
---|
| 78 | #endif |
---|
[509] | 79 | strcpy(CCookie[0],tmp); |
---|
[1] | 80 | } |
---|
| 81 | return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER); |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | void setProxy(CURL* handle,char* host,long port){ |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * MACOSX |
---|
| 90 | */ |
---|
| 91 | #if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | char* CFStringToCString(CFStringRef dest,char *buffer){ |
---|
| 95 | CFStringEncoding encoding = kCFStringEncodingUTF8; |
---|
| 96 | Boolean bool2 = CFStringGetCString(dest,buffer,1024,encoding); |
---|
| 97 | if(bool2){ |
---|
| 98 | printf("Loaded into local_buffer"); |
---|
| 99 | return buffer; |
---|
| 100 | } |
---|
| 101 | return NULL; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | OSStatus setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
| 105 | OSStatus err; |
---|
| 106 | CFDictionaryRef proxyDict; |
---|
| 107 | CFArrayRef proxies; |
---|
| 108 | |
---|
[446] | 109 | CFStringRef key_enabled = NULL; |
---|
| 110 | CFStringRef key_host = NULL; |
---|
| 111 | CFStringRef key_port = NULL; |
---|
[1] | 112 | |
---|
| 113 | bool proxy_enabled; |
---|
| 114 | char *proxy_host; |
---|
| 115 | long proxy_port; |
---|
| 116 | |
---|
| 117 | proxyDict = NULL; |
---|
| 118 | proxies = NULL; |
---|
| 119 | |
---|
| 120 | err = noErr; |
---|
| 121 | proxyDict = SCDynamicStoreCopyProxies(NULL); |
---|
| 122 | |
---|
[446] | 123 | if(strncmp(proto,"http",4)==0){ |
---|
[1] | 124 | key_enabled=kSCPropNetProxiesHTTPEnable; |
---|
| 125 | key_host=kSCPropNetProxiesHTTPProxy; |
---|
| 126 | key_port=kSCPropNetProxiesHTTPPort; |
---|
| 127 | } |
---|
| 128 | else |
---|
[446] | 129 | if(strncmp(proto,"https",5)==0){ |
---|
[1] | 130 | key_enabled=kSCPropNetProxiesHTTPSEnable; |
---|
| 131 | key_host=kSCPropNetProxiesHTTPSProxy; |
---|
| 132 | key_port=kSCPropNetProxiesHTTPSPort; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_enabled),kCFNumberIntType,&proxy_enabled); |
---|
| 136 | if(proxy_enabled){ |
---|
| 137 | CFNumberGetValue(CFDictionaryGetValue(proxyDict,key_port),CFNumberGetType(CFDictionaryGetValue(proxyDict,key_port)),&proxy_port); |
---|
| 138 | char buffer[1024]; |
---|
| 139 | CFStringToCString(CFDictionaryGetValue(proxyDict,key_host),buffer); |
---|
| 140 | proxy_host=buffer; |
---|
| 141 | |
---|
| 142 | #ifdef MSG_LAF_VERBOSE |
---|
| 143 | printf("\n**[PROXY SETTINGS DETECTION %s (%d) %s:%li (%s)]**\n",proto,proxy_enabled,(char*)proxy_host,proxy_port,buffer); |
---|
| 144 | #endif |
---|
| 145 | |
---|
| 146 | if (proxyDict == NULL) { |
---|
| 147 | err = coreFoundationUnknownErr; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | setProxy(handle,proxy_host,proxy_port); |
---|
| 151 | } |
---|
| 152 | return err; |
---|
| 153 | } |
---|
| 154 | #else |
---|
| 155 | /** |
---|
| 156 | * Linux (Gnome) |
---|
| 157 | */ |
---|
| 158 | bool setProxiesForProtcol(CURL* handle,const char *proto){ |
---|
| 159 | #ifdef MSG_LAF_VERBOSE |
---|
| 160 | fprintf( stderr, "setProxiesForProtocol (do nothing) ...\n" ); |
---|
| 161 | #endif |
---|
[509] | 162 | return true; |
---|
[1] | 163 | } |
---|
| 164 | #endif |
---|
| 165 | |
---|
[492] | 166 | HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ |
---|
[1] | 167 | HINTERNET ret; |
---|
[490] | 168 | ret.handle=curl_multi_init(); |
---|
| 169 | ret.agent=strdup(lpszAgent); |
---|
[492] | 170 | ret.nb=0; |
---|
| 171 | ret.ihandle[ret.nb].header=NULL; |
---|
[1] | 172 | return ret; |
---|
| 173 | } |
---|
| 174 | |
---|
[492] | 175 | void InternetCloseHandle(HINTERNET* handle0){ |
---|
| 176 | int i=0; |
---|
| 177 | for(i=0;i<handle0->nb;i++){ |
---|
| 178 | _HINTERNET handle=handle0->ihandle[i]; |
---|
| 179 | if(handle.hasCacheFile>0){ |
---|
| 180 | fclose(handle.file); |
---|
| 181 | unlink(handle.filename); |
---|
| 182 | } |
---|
| 183 | else{ |
---|
| 184 | handle.pabyData = NULL; |
---|
| 185 | handle.nDataAlloc = handle.nDataLen = 0; |
---|
| 186 | } |
---|
| 187 | if(handle0->ihandle[i].header!=NULL){ |
---|
| 188 | curl_slist_free_all(handle0->ihandle[i].header); |
---|
| 189 | handle0->ihandle[i].header=NULL; |
---|
| 190 | } |
---|
| 191 | free(handle.mimeType); |
---|
[534] | 192 | handle.mimeType = NULL; |
---|
[1] | 193 | } |
---|
[492] | 194 | if(handle0->handle) |
---|
| 195 | curl_multi_cleanup(handle0->handle); |
---|
| 196 | free(handle0->agent); |
---|
| 197 | for(i=handle0->nb-1;i>=0;i--){ |
---|
| 198 | free(handle0->waitingRequests[i]); |
---|
[1] | 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[492] | 202 | HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){ |
---|
[1] | 203 | |
---|
| 204 | char filename[255]; |
---|
[490] | 205 | struct MemoryStruct header; |
---|
| 206 | |
---|
[492] | 207 | hInternet->ihandle[hInternet->nb].handle=curl_easy_init( ); |
---|
| 208 | hInternet->ihandle[hInternet->nb].hasCacheFile=0; |
---|
| 209 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
| 210 | hInternet->ihandle[hInternet->nb].mimeType = NULL; |
---|
| 211 | hInternet->ihandle[hInternet->nb].nDataLen = 0; |
---|
| 212 | hInternet->ihandle[hInternet->nb].id = hInternet->nb; |
---|
| 213 | hInternet->ihandle[hInternet->nb].nDataAlloc = 0; |
---|
| 214 | hInternet->ihandle[hInternet->nb].pabyData = NULL; |
---|
[1] | 215 | |
---|
[492] | 216 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL"); |
---|
[490] | 217 | #ifndef TIGER |
---|
[492] | 218 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
[490] | 219 | #endif |
---|
[492] | 220 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent); |
---|
[490] | 221 | |
---|
[492] | 222 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1); |
---|
| 223 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3); |
---|
[490] | 224 | |
---|
| 225 | header.memory=NULL; |
---|
| 226 | header.size = 0; |
---|
| 227 | |
---|
[492] | 228 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_HEADERFUNCTION, header_write_data); |
---|
| 229 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEHEADER, (void *)&header); |
---|
[490] | 230 | |
---|
| 231 | #ifdef MSG_LAF_VERBOSE |
---|
[492] | 232 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1); |
---|
[490] | 233 | #endif |
---|
| 234 | |
---|
[1] | 235 | |
---|
| 236 | switch(dwFlags) |
---|
| 237 | { |
---|
[492] | 238 | case INTERNET_FLAG_NO_CACHE_WRITE: |
---|
| 239 | hInternet->ihandle[hInternet->nb].hasCacheFile=-1; |
---|
| 240 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into); |
---|
| 241 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); |
---|
[1] | 242 | break; |
---|
| 243 | default: |
---|
[492] | 244 | sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL)); |
---|
| 245 | hInternet->ihandle[hInternet->nb].filename[24]=0; |
---|
[1] | 246 | #ifdef MSG_LAF_VERBOSE |
---|
[492] | 247 | fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename); |
---|
[1] | 248 | #endif |
---|
[492] | 249 | hInternet->ihandle[hInternet->nb].filename=filename; |
---|
| 250 | hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+"); |
---|
[1] | 251 | |
---|
[492] | 252 | hInternet->ihandle[hInternet->nb].hasCacheFile=1; |
---|
| 253 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL); |
---|
| 254 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file); |
---|
| 255 | hInternet->ihandle[hInternet->nb].nDataLen=0; |
---|
[1] | 256 | break; |
---|
| 257 | } |
---|
| 258 | #ifdef ULINET_DEBUG |
---|
| 259 | fprintf(stderr,"URL (%s)\nBODY (%s)\n",lpszUrl,lpszHeaders); |
---|
| 260 | #endif |
---|
| 261 | if(lpszHeaders!=NULL && strlen(lpszHeaders)>0){ |
---|
| 262 | #ifdef MSG_LAF_VERBOSE |
---|
| 263 | fprintf(stderr,"FROM ULINET !!"); |
---|
| 264 | fprintf(stderr,"HEADER : %s\n",lpszHeaders); |
---|
| 265 | #endif |
---|
[492] | 266 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1); |
---|
[1] | 267 | #ifdef ULINET_DEBUG |
---|
| 268 | fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength); |
---|
[492] | 269 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1); |
---|
[1] | 270 | #endif |
---|
[492] | 271 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POSTFIELDS,lpszHeaders); |
---|
| 272 | //curl_easy_setopt(hInternet->handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1); |
---|
| 273 | if(hInternet->ihandle[hInternet->nb].header!=NULL) |
---|
| 274 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_HTTPHEADER,hInternet->ihandle[hInternet->nb].header); |
---|
[1] | 275 | } |
---|
| 276 | |
---|
[492] | 277 | curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl); |
---|
[490] | 278 | |
---|
[492] | 279 | curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle); |
---|
[490] | 280 | |
---|
[492] | 281 | ++hInternet->nb; |
---|
| 282 | hInternet->ihandle[hInternet->nb].header=NULL; |
---|
[490] | 283 | |
---|
[446] | 284 | #ifdef ULINET_DEBUG |
---|
| 285 | fprintf(stderr,"DEBUG MIMETYPE: %s\n",hInternet.mimeType); |
---|
[478] | 286 | fflush(stderr); |
---|
[446] | 287 | #endif |
---|
[492] | 288 | return *hInternet; |
---|
[1] | 289 | }; |
---|
| 290 | |
---|
[492] | 291 | int processDownloads(HINTERNET* hInternet){ |
---|
| 292 | int still_running=0; |
---|
| 293 | int msgs_left=0; |
---|
| 294 | int i=0; |
---|
| 295 | do{ |
---|
| 296 | curl_multi_perform(hInternet->handle, &still_running); |
---|
| 297 | }while(still_running); |
---|
| 298 | for(i=0;i<hInternet->nb;i++){ |
---|
[534] | 299 | char *tmp; |
---|
| 300 | curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&tmp); |
---|
[539] | 301 | if(tmp!=NULL) |
---|
| 302 | hInternet->ihandle[i].mimeType=strdup(tmp); |
---|
[492] | 303 | curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle); |
---|
| 304 | curl_easy_cleanup(hInternet->ihandle[i].handle); |
---|
| 305 | } |
---|
[509] | 306 | return 0; |
---|
[492] | 307 | } |
---|
| 308 | |
---|
[1] | 309 | int freeCookieList(HINTERNET hInternet){ |
---|
[492] | 310 | memset(&CCookie[hInternet.nb][0],0,1024); |
---|
[1] | 311 | #ifndef TIGER |
---|
[492] | 312 | curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL"); |
---|
[1] | 313 | #endif |
---|
| 314 | return 1; |
---|
| 315 | } |
---|
| 316 | |
---|
[492] | 317 | int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){ |
---|
[1] | 318 | int dwDataSize; |
---|
| 319 | |
---|
| 320 | if(hInternet.hasCacheFile>0){ |
---|
| 321 | fseek (hInternet.file , 0 , SEEK_END); |
---|
| 322 | dwDataSize=ftell(hInternet.file); //taille du ficher |
---|
| 323 | rewind (hInternet.file); |
---|
| 324 | } |
---|
| 325 | else{ |
---|
| 326 | memset(lpBuffer,0,hInternet.nDataLen+1); |
---|
[375] | 327 | memcpy(lpBuffer, hInternet.pabyData, hInternet.nDataLen ); |
---|
[1] | 328 | dwDataSize=hInternet.nDataLen; |
---|
| 329 | free( hInternet.pabyData ); |
---|
| 330 | hInternet.pabyData=NULL; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ) |
---|
| 334 | return 0; |
---|
| 335 | |
---|
| 336 | #ifdef MSG_LAF_VERBOSE |
---|
| 337 | printf("\nfile size : %dko\n",dwDataSize/1024); |
---|
| 338 | #endif |
---|
| 339 | |
---|
| 340 | if(hInternet.hasCacheFile>0){ |
---|
| 341 | *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); |
---|
| 342 | } |
---|
| 343 | else{ |
---|
| 344 | *lpdwNumberOfBytesRead = hInternet.nDataLen; |
---|
| 345 | free( hInternet.pabyData ); |
---|
| 346 | hInternet.pabyData = NULL; |
---|
| 347 | hInternet.nDataAlloc = hInternet.nDataLen = 0; |
---|
| 348 | } |
---|
| 349 | |
---|
[492] | 350 | CCookie[hInternet.id][0]=0; |
---|
[1] | 351 | |
---|
| 352 | if( *lpdwNumberOfBytesRead < dwDataSize ) |
---|
| 353 | return 0; |
---|
| 354 | else |
---|
| 355 | return 1; // TRUE |
---|
| 356 | } |
---|
| 357 | |
---|