Changeset 492 for trunk/zoo-project/zoo-kernel/ulinet.c
- Timestamp:
- Jul 11, 2014, 10:14:03 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/ulinet.c
r490 r492 30 30 #include "ulinet.h" 31 31 #include <assert.h> 32 32 33 33 size_t write_data_into(void *buffer, size_t size, size_t nmemb, void *data){ 34 34 size_t realsize = size * nmemb; 35 HINTERNET *psInternet;35 _HINTERNET *psInternet; 36 36 if(buffer==NULL){ 37 37 buffer=NULL; 38 38 return -1; 39 39 } 40 psInternet=( HINTERNET *)data;40 psInternet=(_HINTERNET *)data; 41 41 if(psInternet->pabyData){ 42 42 psInternet->pabyData=(unsigned char*)realloc(psInternet->pabyData,psInternet->nDataLen+realsize+1); … … 64 64 char path[1024]; 65 65 char domain[1024]; 66 66 char* tmp; 67 67 for(i=0;i<12;i++) 68 68 #ifndef WIN32 … … 163 163 #endif 164 164 165 HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ 165 HINTERNET InternetOpen(char* lpszAgent,int dwAccessType,char* lpszProxyName,char* lpszProxyBypass,int dwFlags){ 166 166 HINTERNET ret; 167 167 ret.handle=curl_multi_init(); 168 ret.ihandle=NULL;169 ret.hasCacheFile=0;170 ret.nDataAlloc = 0;171 ret.mimeType = NULL;172 168 ret.agent=strdup(lpszAgent); 169 ret.nb=0; 170 ret.ihandle[ret.nb].header=NULL; 173 171 return ret; 174 172 } 175 173 176 void InternetCloseHandle(HINTERNET handle){ 177 if(handle.hasCacheFile>0){ 178 fclose(handle.file); 179 unlink(handle.filename); 180 handle.mimeType = NULL; 181 } 182 else{ 183 handle.pabyData = NULL; 184 handle.mimeType = NULL; 185 handle.nDataAlloc = handle.nDataLen = 0; 186 } 187 if(handle.ihandle!=NULL) 188 curl_easy_cleanup(handle.ihandle); 189 if(handle.handle) 190 curl_multi_cleanup(handle.handle); 191 free(handle.agent); 192 } 193 194 HINTERNET InternetOpenUrl(HINTERNET hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){ 174 void InternetCloseHandle(HINTERNET* handle0){ 175 int i=0; 176 for(i=0;i<handle0->nb;i++){ 177 _HINTERNET handle=handle0->ihandle[i]; 178 if(handle.hasCacheFile>0){ 179 fclose(handle.file); 180 unlink(handle.filename); 181 handle.mimeType = NULL; 182 } 183 else{ 184 handle.pabyData = NULL; 185 handle.mimeType = NULL; 186 handle.nDataAlloc = handle.nDataLen = 0; 187 } 188 if(handle0->ihandle[i].header!=NULL){ 189 curl_slist_free_all(handle0->ihandle[i].header); 190 handle0->ihandle[i].header=NULL; 191 } 192 free(handle.mimeType); 193 } 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]); 199 } 200 } 201 202 HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){ 195 203 196 204 char filename[255]; 197 205 struct MemoryStruct header; 198 206 199 hInternet.nDataLen = 0; 200 201 hInternet.nDataAlloc = 0; 202 hInternet.pabyData= NULL; 203 204 if(hInternet.ihandle!=NULL) 205 curl_easy_cleanup(hInternet.ihandle); 206 hInternet.ihandle=curl_easy_init( ); 207 curl_easy_setopt(hInternet.ihandle, CURLOPT_COOKIEFILE, "ALL"); 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; 215 216 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL"); 208 217 #ifndef TIGER 209 curl_easy_setopt(hInternet .ihandle, CURLOPT_COOKIELIST, "ALL");210 #endif 211 curl_easy_setopt(hInternet .ihandle, CURLOPT_USERAGENT, hInternet.agent);212 213 curl_easy_setopt(hInternet .ihandle,CURLOPT_FOLLOWLOCATION,1);214 curl_easy_setopt(hInternet .ihandle,CURLOPT_MAXREDIRS,3);218 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIELIST, "ALL"); 219 #endif 220 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_USERAGENT, hInternet->agent); 221 222 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_FOLLOWLOCATION,1); 223 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_MAXREDIRS,3); 215 224 216 225 header.memory=NULL; 217 226 header.size = 0; 218 227 219 curl_easy_setopt(hInternet .ihandle, CURLOPT_HEADERFUNCTION, header_write_data);220 curl_easy_setopt(hInternet .ihandle, CURLOPT_WRITEHEADER, (void *)&header);221 222 #ifdef MSG_LAF_VERBOSE 223 curl_easy_setopt(hInternet .ihandle, CURLOPT_VERBOSE, 1);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); 230 231 #ifdef MSG_LAF_VERBOSE 232 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1); 224 233 #endif 225 234 … … 227 236 switch(dwFlags) 228 237 { 229 case INTERNET_FLAG_NO_CACHE_WRITE: 230 hInternet .hasCacheFile=-1;231 curl_easy_setopt(hInternet .ihandle, CURLOPT_WRITEFUNCTION, write_data_into);232 curl_easy_setopt(hInternet .ihandle, CURLOPT_WRITEDATA, &hInternet);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]); 233 242 break; 234 243 default: 235 sprintf( filename,"/tmp/ZOO_Cache%d",(int)time(NULL));236 filename[24]=0;237 #ifdef MSG_LAF_VERBOSE 238 fprintf(stderr,"file=%s", filename);239 #endif 240 hInternet .filename=filename;241 hInternet .file=fopen(hInternet.filename,"w+");244 sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL)); 245 hInternet->ihandle[hInternet->nb].filename[24]=0; 246 #ifdef MSG_LAF_VERBOSE 247 fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename); 248 #endif 249 hInternet->ihandle[hInternet->nb].filename=filename; 250 hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+"); 242 251 243 hInternet .hasCacheFile=1;244 curl_easy_setopt(hInternet .ihandle, CURLOPT_WRITEFUNCTION, NULL);245 curl_easy_setopt(hInternet .ihandle, CURLOPT_WRITEDATA, hInternet.file);246 hInternet .nDataLen=0;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; 247 256 break; 248 257 } … … 255 264 fprintf(stderr,"HEADER : %s\n",lpszHeaders); 256 265 #endif 257 //curl_easy_setopt(hInternet.handle,CURLOPT_COOKIE,lpszHeaders); 258 curl_easy_setopt(hInternet.ihandle,CURLOPT_POST,1); 266 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_POST,1); 259 267 #ifdef ULINET_DEBUG 260 268 fprintf(stderr,"** (%s) %d **\n",lpszHeaders,dwHeadersLength); 261 curl_easy_setopt(hInternet.ihandle,CURLOPT_VERBOSE,1); 262 #endif 263 curl_easy_setopt(hInternet.ihandle,CURLOPT_POSTFIELDS,lpszHeaders); 264 //curl_easy_setopt(hInternet.handle,CURLOPT_POSTFIELDSIZE,dwHeadersLength+1); 265 if(hInternet.header!=NULL) 266 curl_easy_setopt(hInternet.ihandle,CURLOPT_HTTPHEADER,hInternet.header); 267 } 268 269 curl_easy_setopt(hInternet.ihandle,CURLOPT_URL,lpszUrl); 270 271 curl_multi_add_handle(hInternet.handle,hInternet.ihandle); 272 273 int still_running=0; 274 int msgs_left=0; 275 do{ 276 curl_multi_perform(hInternet.handle, &still_running); 277 }while(still_running); 278 279 CURLMsg *msg=NULL; 280 while ((msg = curl_multi_info_read(hInternet.handle, &msgs_left))) { 281 if (msg->msg == CURLMSG_DONE) { 282 CURL *eh=NULL; 283 eh = msg->easy_handle; 284 curl_easy_getinfo(eh,CURLINFO_CONTENT_TYPE,&hInternet.mimeType); 285 } 286 else { 287 fprintf(stderr, "error: after curl_multi_info_read(), CURLMsg=%d\n", msg->msg); 288 } 289 } 290 curl_multi_remove_handle(hInternet.handle, hInternet.ihandle); 269 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_VERBOSE,1); 270 #endif 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); 275 } 276 277 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle,CURLOPT_URL,lpszUrl); 278 279 curl_multi_add_handle(hInternet->handle,hInternet->ihandle[hInternet->nb].handle); 280 281 ++hInternet->nb; 282 hInternet->ihandle[hInternet->nb].header=NULL; 291 283 292 284 #ifdef ULINET_DEBUG … … 294 286 fflush(stderr); 295 287 #endif 296 return hInternet;288 return *hInternet; 297 289 }; 298 290 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++){ 299 curl_easy_getinfo(hInternet->ihandle[i].handle,CURLINFO_CONTENT_TYPE,&hInternet->ihandle[i].mimeType); 300 curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle); 301 curl_easy_cleanup(hInternet->ihandle[i].handle); 302 } 303 } 304 299 305 int freeCookieList(HINTERNET hInternet){ 300 memset(&CCookie[ 0],0,1024);306 memset(&CCookie[hInternet.nb][0],0,1024); 301 307 #ifndef TIGER 302 curl_easy_setopt(hInternet.ihandle , CURLOPT_COOKIELIST, "ALL");308 curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL"); 303 309 #endif 304 310 return 1; 305 311 } 306 312 307 int InternetReadFile( HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){313 int InternetReadFile(_HINTERNET hInternet,LPVOID lpBuffer,int dwNumberOfBytesToRead, size_t *lpdwNumberOfBytesRead){ 308 314 int dwDataSize; 309 315 … … 338 344 } 339 345 340 CCookie[ 0]=0;346 CCookie[hInternet.id][0]=0; 341 347 342 348 if( *lpdwNumberOfBytesRead < dwDataSize ) … … 346 352 } 347 353 348 bool InternetGetCookie(LPCTSTR lpszUrl,LPCTSTR lpszCookieName,LPTSTR lpszCookieData,LPDWORD lpdwSize){349 350 bool ret=1;351 int count=0;352 int hasCookie=-1;353 char TMP[1024];354 int j;355 int tmpC=0;356 lpszUrl=NULL;357 358 for(j=0;j<strlen(CCookie);j++){359 if(lpszCookieName[count]==CCookie[j]){360 hasCookie=1;361 count++;362 if(count==strlen(lpszCookieName))363 break;364 continue;365 }366 }367 368 if(hasCookie>0){369 if(CCookie[count]=='='){370 int i=0;371 count++;372 for(i=count;i<strlen(CCookie);i++){373 if(CCookie[i]!=';'){374 TMP[tmpC]=CCookie[i];375 tmpC++;376 }377 else{378 break;379 }380 }381 }382 }383 else384 return -1;385 386 TMP[tmpC]=0;387 strncpy(lpszCookieData,TMP,strlen(TMP)+1);388 lpdwSize=(size_t*) strlen(lpszCookieData);389 390 #ifdef MSG_LAF_VERBOSE391 printf("Cookie returned : (%s)",(char*)lpszCookieData);392 #endif393 394 return ret;395 396 }397
Note: See TracChangeset
for help on using the changeset viewer.