Changeset 281 for trunk/zoo-kernel
- Timestamp:
- Jul 27, 2011, 2:28:25 AM (13 years ago)
- Location:
- trunk/zoo-kernel
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-kernel/service.h
r254 r281 203 203 } 204 204 205 205 206 static map* getLastMap(map* m){ 206 207 map* tmp=m; … … 497 498 } 498 499 500 static map* getMapOrEmpty(map* m,const char *key){ 501 map* tmp=m; 502 map* tmpMap=getMap(tmp,key); 503 if(tmpMap==NULL){ 504 addToMap(tmp,key,""); 505 tmpMap=getMap(tmp,key); 506 } 507 return tmpMap; 508 } 509 499 510 static bool contains(map* m,map* i){ 500 511 while(i!=NULL){ -
trunk/zoo-kernel/service_internal.c
r280 r281 2299 2299 } 2300 2300 2301 2301 2302 /** 2302 2303 * Cache a file for a given request … … 2372 2373 return NULL; 2373 2374 } 2375 2376 /** 2377 * loadRemoteFile: 2378 * Try to load file from cache or download a remote file if not in cache 2379 */ 2380 void loadRemoteFile(maps* m,map* content,HINTERNET hInternet,char *url){ 2381 HINTERNET res; 2382 char* fcontent; 2383 char* cached=isInCache(m,url); 2384 int fsize; 2385 if(cached!=NULL){ 2386 fprintf(stderr,"Use cached file: %s\n",cached); 2387 struct stat f_status; 2388 int s=stat(cached, &f_status); 2389 if(s==0){ 2390 fprintf(stderr,"Use cached file: %s\n",cached); 2391 fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 2392 FILE* f=fopen(cached,"r"); 2393 fread(fcontent,sizeof(char),f_status.st_size,f); 2394 fsize=f_status.st_size; 2395 } 2396 }else{ 2397 res=InternetOpenUrl(hInternet,url,NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0); 2398 fcontent=(char*)calloc((res.nDataLen+1),sizeof(char)); 2399 if(fcontent == NULL){ 2400 return errorException(m, _("Unable to allocate memory."), "InternalError"); 2401 } 2402 size_t dwRead; 2403 InternetReadFile(res, (LPVOID)fcontent, res.nDataLen, &dwRead); 2404 fcontent[res.nDataLen]=0; 2405 fsize=res.nDataLen; 2406 } 2407 map* tmpMap=getMapOrEmpty(content,"value"); 2408 free(tmpMap->value); 2409 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 2410 memcpy(tmpMap->value,fcontent,(fsize)*sizeof(char)); 2411 char ltmp1[256]; 2412 sprintf(ltmp1,"%d",fsize); 2413 addToMap(content,"size",ltmp1); 2414 if(cached==NULL) 2415 addToCache(m,url,fcontent,fsize); 2416 dumpMap(content); 2417 free(fcontent); 2418 } 2419 -
trunk/zoo-kernel/service_internal.h
r280 r281 59 59 60 60 #include "cgic.h" 61 #include "ulinet.h" 61 62 62 63 extern int getServiceFromFile(const char*,service**); … … 127 128 void addToCache(maps*,char*,char*,int); 128 129 char* isInCache(maps*,char*); 130 void loadRemoteFile(maps*,map*,HINTERNET,char*); 129 131 130 132 #ifdef __cplusplus -
trunk/zoo-kernel/zoo_service_loader.c
r280 r281 1053 1053 #endif 1054 1054 { 1055 char* cached=isInCache(m,tmpv1+1); 1056 if(cached!=NULL){ 1057 fprintf(stderr,"Use cached file: %s\n",cached); 1058 struct stat f_status; 1059 int s=stat(cached, &f_status); 1060 if(s==0){ 1061 map* tmpMap=getMap(tmpmaps->content,"value"); 1062 char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 1063 FILE* f=fopen(cached,"r"); 1064 fread(fcontent,sizeof(char),f_status.st_size,f); 1065 free(tmpMap->value); 1066 tmpMap->value=(char*)malloc((f_status.st_size+1)*sizeof(char)); 1067 memmove(tmpMap->value,fcontent,(f_status.st_size)*sizeof(char)); 1068 free(fcontent); 1069 } 1070 }else{ 1071 res=InternetOpenUrl(hInternet,tmpv1+1,NULL,0, 1072 INTERNET_FLAG_NO_CACHE_WRITE,0); 1073 #ifdef DEBUG 1074 fprintf(stderr,"(%s) content-length : %d,,res.nDataAlloc %d \n", 1075 tmpv1+1,res.nDataAlloc,res.nDataLen); 1076 #endif 1077 char* tmpContent=(char*)calloc((res.nDataLen+1),sizeof(char)); 1078 if(tmpContent == NULL){ 1079 return errorException(m, _("Unable to allocate memory."), "InternalError"); 1080 } 1081 size_t dwRead; 1082 InternetReadFile(res, (LPVOID)tmpContent,res.nDataLen, &dwRead); 1083 map* tmpMap=getMap(tmpmaps->content,"value"); 1084 if(tmpMap!=NULL){ 1085 free(tmpMap->value); 1086 tmpMap->value=(char*)malloc((res.nDataLen+1)*sizeof(char)); 1087 memmove(tmpMap->value,tmpContent,(res.nDataLen)*sizeof(char)); 1088 tmpMap->value[res.nDataLen]=0; 1089 if(strlen(tmpContent)!=res.nDataLen){ 1090 char tmp[256]; 1091 sprintf(tmp,"%d",res.nDataLen*sizeof(char)); 1092 addToMap(tmpmaps->content,"size",tmp); 1093 } 1094 addToCache(m,tmpv1+1,tmpContent,res.nDataLen); 1095 } 1096 free(tmpContent); 1097 } 1055 loadRemoteFile(m,tmpmaps->content,hInternet,tmpv1+1); 1098 1056 } 1099 1057 char *tmpx1=url_encode(tmpv1+1); … … 1237 1195 if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0) 1238 1196 && CHECK_INET_HANDLE(hInternet)){ 1239 res=InternetOpenUrl(hInternet,(char*)val,NULL,0, 1240 INTERNET_FLAG_NO_CACHE_WRITE,0); 1241 char* tmpContent= 1242 (char*)calloc((res.nDataLen+1),sizeof(char)); 1243 if(tmpContent == NULL){ 1244 return errorException(m, _("Unable to allocate memory."), "InternalError"); 1245 } 1246 size_t dwRead; 1247 InternetReadFile(res, (LPVOID)tmpContent, 1248 res.nDataLen, &dwRead); 1249 tmpContent[res.nDataLen]=0; 1250 addToMap(tmpmaps->content,"value",tmpContent); 1197 loadRemoteFile(m,tmpmaps->content,hInternet,(char*)val); 1251 1198 } 1252 1199 }
Note: See TracChangeset
for help on using the changeset viewer.