[642] | 1 | /* |
---|
| 2 | * Author : Gérald Fenoy |
---|
| 3 | * |
---|
| 4 | * Copyright 2008-2015 GeoLabs SARL. All rights reserved. |
---|
| 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "caching.h" |
---|
| 26 | #include "service.h" |
---|
| 27 | #include "service_internal.h" |
---|
| 28 | #include "response_print.h" |
---|
| 29 | #include <openssl/md5.h> |
---|
| 30 | #include <openssl/hmac.h> |
---|
| 31 | #include <openssl/evp.h> |
---|
| 32 | #include <openssl/bio.h> |
---|
| 33 | #include <openssl/buffer.h> |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Compute md5 |
---|
| 37 | * |
---|
| 38 | * @param url the char* |
---|
| 39 | * @return a char* representing the md5 of the url |
---|
[781] | 40 | * @warning make sure to free resources returned by this function |
---|
[642] | 41 | */ |
---|
| 42 | char* getMd5(char* url){ |
---|
| 43 | EVP_MD_CTX md5ctx; |
---|
| 44 | char* fresult=(char*)malloc((EVP_MAX_MD_SIZE+1)*sizeof(char)); |
---|
| 45 | unsigned char result[EVP_MAX_MD_SIZE]; |
---|
| 46 | unsigned int len; |
---|
| 47 | EVP_DigestInit(&md5ctx, EVP_md5()); |
---|
| 48 | EVP_DigestUpdate(&md5ctx, url, strlen(url)); |
---|
| 49 | EVP_DigestFinal_ex(&md5ctx,result,&len); |
---|
| 50 | EVP_MD_CTX_cleanup(&md5ctx); |
---|
| 51 | int i; |
---|
| 52 | for(i = 0; i < len; i++){ |
---|
| 53 | if(i>0){ |
---|
| 54 | char *tmp=strdup(fresult); |
---|
| 55 | sprintf(fresult,"%s%02x", tmp,result[i]); |
---|
| 56 | free(tmp); |
---|
| 57 | } |
---|
| 58 | else |
---|
| 59 | sprintf(fresult,"%02x",result[i]); |
---|
| 60 | } |
---|
| 61 | return fresult; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | /** |
---|
[854] | 66 | * Create a URL by appending every request header listed in the security |
---|
| 67 | * section.This imply that the URL will contain any authentication |
---|
| 68 | * informations that should be fowarded to the server from which de input |
---|
| 69 | * was download. |
---|
| 70 | * @param conf the main configuration maps |
---|
| 71 | * @param request the URL to transform. |
---|
| 72 | * @return a char* that contain the original URL plus potential header (only for |
---|
| 73 | * hosts that are not shared). |
---|
| 74 | * @warning Be sure to free the memory returned by this function. |
---|
| 75 | */ |
---|
| 76 | char* getFilenameForRequest(maps* conf, const char* request){ |
---|
| 77 | map* passThrough=getMapFromMaps(conf,"security","attributes"); |
---|
| 78 | map* targetHosts=getMapFromMaps(conf,"security","hosts"); |
---|
| 79 | char* passedHeader[10]; |
---|
| 80 | int cnt=0; |
---|
| 81 | char *res=zStrdup(request); |
---|
| 82 | char *toAppend=NULL; |
---|
| 83 | if(passThrough!=NULL && targetHosts!=NULL){ |
---|
| 84 | char *tmp=zStrdup(passThrough->value); |
---|
| 85 | char *token, *saveptr; |
---|
| 86 | token = strtok_r (tmp, ",", &saveptr); |
---|
| 87 | int i; |
---|
| 88 | if((strstr(targetHosts->value,"*")!=NULL || isProtectedHost(targetHosts->value,request)==1) && strncasecmp(getProvenance(conf,request),"SHARED",6)!=0){ |
---|
| 89 | while (token != NULL){ |
---|
| 90 | int length=strlen(token)+6; |
---|
| 91 | char* tmp1=(char*)malloc(length*sizeof(char)); |
---|
| 92 | map* tmpMap; |
---|
| 93 | snprintf(tmp1,6,"HTTP_"); |
---|
| 94 | int j; |
---|
| 95 | for(j=0;token[j]!='\0';j++){ |
---|
| 96 | if(token[j]!='-') |
---|
| 97 | tmp1[5+j]=toupper(token[j]); |
---|
| 98 | else |
---|
| 99 | tmp1[5+j]='_'; |
---|
| 100 | tmp1[5+j+1]='\0'; |
---|
| 101 | } |
---|
| 102 | tmpMap = getMapFromMaps(conf,"renv",tmp1); |
---|
| 103 | if(tmpMap!=NULL){ |
---|
| 104 | if(toAppend==NULL){ |
---|
| 105 | toAppend=(char*)malloc((strlen(tmpMap->value)+1)*sizeof(char)); |
---|
| 106 | sprintf(toAppend,"%s",tmpMap->value); |
---|
| 107 | }else{ |
---|
| 108 | char *tmp3=zStrdup(toAppend); |
---|
| 109 | toAppend=(char*)realloc(toAppend,(strlen(tmpMap->value)+strlen(tmp3)+2)*sizeof(char)); |
---|
| 110 | sprintf(toAppend,"%s,%s",tmp3,tmpMap->value); |
---|
| 111 | free(tmp3); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | free(tmp1); |
---|
| 115 | cnt+=1; |
---|
| 116 | token = strtok_r (NULL, ",", &saveptr); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | free(tmp); |
---|
| 120 | } |
---|
| 121 | if(toAppend!=NULL){ |
---|
| 122 | char *tmp3=zStrdup(res); |
---|
| 123 | res=(char*)realloc(res,(strlen(tmp3)+strlen(toAppend)+1)*sizeof(char)); |
---|
| 124 | sprintf(res,"%s%s",tmp3,toAppend); |
---|
| 125 | free(tmp3); |
---|
| 126 | free(toAppend); |
---|
| 127 | } |
---|
| 128 | return res; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
[642] | 132 | * Cache a file for a given request. |
---|
| 133 | * For each cached file, the are two files stored, a .zca and a .zcm containing |
---|
| 134 | * the downloaded content and the mimeType respectively. |
---|
| 135 | * |
---|
| 136 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 137 | * @param request the url used too fetch the content |
---|
| 138 | * @param content the downloaded content |
---|
| 139 | * @param mimeType the content mimeType |
---|
| 140 | * @param length the content size |
---|
| 141 | * @param filepath a buffer for storing the path of the cached file; may be NULL |
---|
| 142 | * @param max_path the size of the allocated filepath buffer |
---|
| 143 | */ |
---|
[863] | 144 | void cacheFile(maps* conf,char* request,char* mimeType,int length,char* filename){ |
---|
| 145 | map* tmp=getMapFromMaps(conf,"main","cacheDir"); |
---|
| 146 | char contentr[4096]; |
---|
| 147 | int cred=0; |
---|
| 148 | if(tmp!=NULL){ |
---|
| 149 | char* myRequest=getFilenameForRequest(conf,request); |
---|
| 150 | char* md5str=getMd5(myRequest); |
---|
| 151 | free(myRequest); |
---|
| 152 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 153 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
| 154 | zooLock* lck=lockFile(conf,fname,'w'); |
---|
| 155 | if(lck!=NULL){ |
---|
| 156 | #ifdef DEBUG |
---|
| 157 | fprintf(stderr,"Cache list : %s\n",fname); |
---|
| 158 | fflush(stderr); |
---|
| 159 | #endif |
---|
| 160 | FILE* fi=fopen(filename,"rb"); |
---|
| 161 | FILE* fo=fopen(fname,"w+"); |
---|
| 162 | if(fo==NULL){ |
---|
| 163 | #ifdef DEBUG |
---|
| 164 | fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); |
---|
| 165 | #endif |
---|
| 166 | unlockFile(conf,lck); |
---|
| 167 | return; |
---|
| 168 | } |
---|
| 169 | if(fi==NULL){ |
---|
| 170 | #ifdef DEBUG |
---|
| 171 | fprintf (stderr, "Failed to open %s for reading: %s\n",filename, strerror(errno)); |
---|
| 172 | #endif |
---|
| 173 | unlockFile(conf,lck); |
---|
| 174 | return; |
---|
| 175 | } |
---|
| 176 | memset(contentr,0,4096); |
---|
| 177 | while((cred=fread(contentr,sizeof(char),4096,fi))>0){ |
---|
| 178 | fwrite(contentr,sizeof(char),cred,fo); |
---|
| 179 | fflush(fo); |
---|
| 180 | memset(contentr,0,4096); |
---|
| 181 | } |
---|
| 182 | unlockFile(conf,lck); |
---|
| 183 | fclose(fo); |
---|
| 184 | fclose(fi); |
---|
| 185 | |
---|
| 186 | sprintf(fname,"%s/%s.zcm",tmp->value,md5str); |
---|
| 187 | fo=fopen(fname,"w+"); |
---|
| 188 | #ifdef DEBUG |
---|
| 189 | fprintf(stderr,"MIMETYPE: %s\n",mimeType); |
---|
| 190 | #endif |
---|
| 191 | fwrite(mimeType,sizeof(char),strlen(mimeType),fo); |
---|
| 192 | fclose(fo); |
---|
| 193 | |
---|
| 194 | sprintf(fname,"%s/%s.zcp",tmp->value,md5str); |
---|
| 195 | fo=fopen(fname,"w+"); |
---|
| 196 | char* origin=getProvenance(conf,request); |
---|
| 197 | #ifdef DEBUG |
---|
| 198 | fprintf(stderr,"ORIGIN: %s\n",mimeType); |
---|
| 199 | #endif |
---|
| 200 | fwrite(origin,sizeof(char),strlen(origin),fo); |
---|
| 201 | fclose(fo); |
---|
| 202 | |
---|
| 203 | free(md5str); |
---|
| 204 | free(fname); |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * Cache a file for a given request. |
---|
| 211 | * For each cached file, the are two files stored, a .zca and a .zcm containing |
---|
| 212 | * the downloaded content and the mimeType respectively. |
---|
| 213 | * |
---|
| 214 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 215 | * @param request the url used too fetch the content |
---|
| 216 | * @param content the downloaded content |
---|
| 217 | * @param mimeType the content mimeType |
---|
| 218 | * @param length the content size |
---|
| 219 | * @param filepath a buffer for storing the path of the cached file; may be NULL |
---|
| 220 | * @param max_path the size of the allocated filepath buffer |
---|
| 221 | */ |
---|
[642] | 222 | void addToCache(maps* conf,char* request,char* content,char* mimeType,int length, |
---|
| 223 | char* filepath, size_t max_path){ |
---|
| 224 | map* tmp=getMapFromMaps(conf,"main","cacheDir"); |
---|
| 225 | if(tmp!=NULL){ |
---|
[854] | 226 | char* myRequest=getFilenameForRequest(conf,request); |
---|
| 227 | char* md5str=getMd5(myRequest); |
---|
| 228 | free(myRequest); |
---|
[642] | 229 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 230 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
[854] | 231 | zooLock* lck=lockFile(conf,fname,'w'); |
---|
| 232 | if(lck!=NULL){ |
---|
[642] | 233 | #ifdef DEBUG |
---|
[854] | 234 | fprintf(stderr,"Cache list : %s\n",fname); |
---|
| 235 | fflush(stderr); |
---|
[642] | 236 | #endif |
---|
[854] | 237 | FILE* fo=fopen(fname,"w+"); |
---|
| 238 | if(fo==NULL){ |
---|
[642] | 239 | #ifdef DEBUG |
---|
[854] | 240 | fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); |
---|
[642] | 241 | #endif |
---|
[854] | 242 | filepath = NULL; |
---|
| 243 | unlockFile(conf,lck); |
---|
| 244 | return; |
---|
| 245 | } |
---|
| 246 | fwrite(content,sizeof(char),length,fo); |
---|
| 247 | unlockFile(conf,lck); |
---|
| 248 | fclose(fo); |
---|
[642] | 249 | |
---|
[854] | 250 | if (filepath != NULL) { |
---|
| 251 | strncpy(filepath, fname, max_path); |
---|
| 252 | } |
---|
[642] | 253 | |
---|
[854] | 254 | sprintf(fname,"%s/%s.zcm",tmp->value,md5str); |
---|
| 255 | fo=fopen(fname,"w+"); |
---|
[642] | 256 | #ifdef DEBUG |
---|
[854] | 257 | fprintf(stderr,"MIMETYPE: %s\n",mimeType); |
---|
[642] | 258 | #endif |
---|
[854] | 259 | fwrite(mimeType,sizeof(char),strlen(mimeType),fo); |
---|
| 260 | fclose(fo); |
---|
[642] | 261 | |
---|
[854] | 262 | sprintf(fname,"%s/%s.zcp",tmp->value,md5str); |
---|
| 263 | fo=fopen(fname,"w+"); |
---|
| 264 | char* origin=getProvenance(conf,request); |
---|
| 265 | #ifdef DEBUG |
---|
| 266 | fprintf(stderr,"ORIGIN: %s\n",mimeType); |
---|
| 267 | #endif |
---|
| 268 | fwrite(origin,sizeof(char),strlen(origin),fo); |
---|
| 269 | fclose(fo); |
---|
| 270 | |
---|
| 271 | free(md5str); |
---|
| 272 | free(fname); |
---|
| 273 | } |
---|
[642] | 274 | } |
---|
| 275 | else { |
---|
[854] | 276 | filepath = NULL; |
---|
[642] | 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Verify if a url is available in the cache |
---|
| 282 | * |
---|
| 283 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 284 | * @param request the url |
---|
| 285 | * @return the full name of the cached file if any, NULL in other case |
---|
[781] | 286 | * @warning make sure to free resources returned by this function (if not NULL) |
---|
[642] | 287 | */ |
---|
| 288 | char* isInCache(maps* conf,char* request){ |
---|
| 289 | map* tmpM=getMapFromMaps(conf,"main","cacheDir"); |
---|
| 290 | if(tmpM!=NULL){ |
---|
[854] | 291 | char* myRequest=getFilenameForRequest(conf,request); |
---|
| 292 | char* md5str=getMd5(myRequest); |
---|
| 293 | free(myRequest); |
---|
[642] | 294 | #ifdef DEBUG |
---|
| 295 | fprintf(stderr,"MD5STR : (%s)\n\n",md5str); |
---|
| 296 | #endif |
---|
| 297 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmpM->value)+strlen(md5str)+6)); |
---|
| 298 | sprintf(fname,"%s/%s.zca",tmpM->value,md5str); |
---|
| 299 | struct stat f_status; |
---|
| 300 | int s=stat(fname, &f_status); |
---|
| 301 | if(s==0 && f_status.st_size>0){ |
---|
| 302 | free(md5str); |
---|
| 303 | return fname; |
---|
| 304 | } |
---|
| 305 | free(md5str); |
---|
| 306 | free(fname); |
---|
| 307 | } |
---|
| 308 | return NULL; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | /** |
---|
[790] | 312 | * Read the downloaded file for a specific input |
---|
| 313 | * |
---|
| 314 | * @param m the maps containing the settings of the main.cfg file |
---|
| 315 | * @param in the input |
---|
| 316 | * @param index the input index |
---|
| 317 | * @param hInternet the internet connection |
---|
[797] | 318 | * @param error the error map pointer |
---|
| 319 | * @return 0 in case of success, -1 in case of failure |
---|
[790] | 320 | */ |
---|
[797] | 321 | int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){ |
---|
[839] | 322 | |
---|
[863] | 323 | int shouldClean=-1; |
---|
[790] | 324 | map* tmp1; |
---|
| 325 | char sindex[5]; |
---|
| 326 | maps* content=*in; |
---|
| 327 | map* length=getMap(content->content,"length"); |
---|
[863] | 328 | map* memUse=getMapFromMaps(*m,"main","memory"); |
---|
[790] | 329 | if(length==NULL){ |
---|
| 330 | length=createMap("length","1"); |
---|
| 331 | shouldClean=1; |
---|
| 332 | } |
---|
| 333 | for(int i=0;i<atoi(length->value);i++){ |
---|
| 334 | char* fcontent; |
---|
| 335 | char *mimeType=NULL; |
---|
| 336 | int fsize=0; |
---|
| 337 | char cname[15]; |
---|
| 338 | char vname[11]; |
---|
| 339 | char vname1[11]; |
---|
| 340 | char sname[9]; |
---|
| 341 | char mname[15]; |
---|
| 342 | char icname[14]; |
---|
| 343 | char xname[16]; |
---|
[822] | 344 | char bname[8]; |
---|
| 345 | char hname[11]; |
---|
[790] | 346 | char oname[12]; |
---|
[863] | 347 | char ufile[12]; |
---|
[790] | 348 | if(*index>0) |
---|
| 349 | sprintf(vname1,"value_%d",*index); |
---|
| 350 | else |
---|
| 351 | sprintf(vname1,"value"); |
---|
| 352 | |
---|
| 353 | if(i>0){ |
---|
[860] | 354 | sprintf(cname,"cache_file_%d",i); |
---|
[790] | 355 | tmp1=getMap(content->content,cname); |
---|
| 356 | sprintf(vname,"value_%d",i); |
---|
| 357 | sprintf(sname,"size_%d",i); |
---|
| 358 | sprintf(mname,"mimeType_%d",i); |
---|
| 359 | sprintf(icname,"isCached_%d",i); |
---|
| 360 | sprintf(xname,"Reference_%d",i); |
---|
[822] | 361 | sprintf(bname,"body_%d",i); |
---|
| 362 | sprintf(hname,"headers_%d",i); |
---|
[790] | 363 | sprintf(oname,"Order_%d",i); |
---|
[863] | 364 | sprintf(ufile,"use_file_%d",i); |
---|
[790] | 365 | }else{ |
---|
| 366 | sprintf(cname,"cache_file"); |
---|
| 367 | sprintf(vname,"value"); |
---|
| 368 | sprintf(sname,"size"); |
---|
| 369 | sprintf(mname,"mimeType"); |
---|
| 370 | sprintf(icname,"isCached"); |
---|
| 371 | sprintf(xname,"Reference"); |
---|
[822] | 372 | sprintf(bname,"body"); |
---|
[854] | 373 | sprintf(hname,"headers"); |
---|
[790] | 374 | sprintf(oname,"Order"); |
---|
[863] | 375 | sprintf(ufile,"use_file"); |
---|
[790] | 376 | } |
---|
| 377 | |
---|
| 378 | map* tmap=getMap(content->content,oname); |
---|
| 379 | sprintf(sindex,"%d",*index+1); |
---|
| 380 | if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){ |
---|
[839] | 381 | |
---|
[863] | 382 | if(getMap(content->content,icname)==NULL) { |
---|
| 383 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 384 | fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); |
---|
| 385 | if(fcontent == NULL){ |
---|
| 386 | errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); |
---|
| 387 | return -1; |
---|
| 388 | } |
---|
| 389 | size_t dwRead; |
---|
| 390 | InternetReadFile(hInternet->ihandle[*index], |
---|
| 391 | (LPVOID)fcontent, |
---|
| 392 | hInternet->ihandle[*index].nDataLen, |
---|
| 393 | &dwRead); |
---|
| 394 | fcontent[hInternet->ihandle[*index].nDataLen]=0; |
---|
[790] | 395 | } |
---|
| 396 | fsize=hInternet->ihandle[*index].nDataLen; |
---|
| 397 | if(hInternet->ihandle[*index].mimeType==NULL) |
---|
| 398 | mimeType=zStrdup("none"); |
---|
| 399 | else |
---|
| 400 | mimeType=zStrdup(hInternet->ihandle[*index].mimeType); |
---|
| 401 | |
---|
| 402 | map* tmpMap=getMapOrFill(&(*in)->content,vname,""); |
---|
[863] | 403 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 404 | free(tmpMap->value); |
---|
| 405 | tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); |
---|
| 406 | if(tmpMap->value==NULL){ |
---|
| 407 | return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); |
---|
| 408 | } |
---|
| 409 | memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); |
---|
| 410 | }else |
---|
| 411 | addToMap((*in)->content,ufile,"true"); |
---|
[797] | 412 | if(hInternet->ihandle[*index].code!=200){ |
---|
| 413 | char *error_rep_str=_("Unable to download the file for the input <%s>, response code was : %d."); |
---|
| 414 | char *error_msg=(char*)malloc((strlen(error_rep_str)+strlen(content->name)+4)*sizeof(char)); |
---|
| 415 | sprintf(error_msg,error_rep_str,content->name,hInternet->ihandle[*index].code); |
---|
| 416 | if(*error==NULL){ |
---|
| 417 | *error=createMap("text",error_msg); |
---|
| 418 | addToMap(*error,"locator",content->name); |
---|
| 419 | addToMap(*error,"code","InvalidParameterValue"); |
---|
| 420 | }else{ |
---|
| 421 | int nb=1; |
---|
| 422 | map* tmpMap=getMap(*error,"length"); |
---|
| 423 | if(tmpMap!=NULL) |
---|
| 424 | nb=atoi(tmpMap->value); |
---|
| 425 | setMapArray(*error,"text",nb,error_msg); |
---|
| 426 | setMapArray(*error,"locator",nb,content->name); |
---|
| 427 | setMapArray(*error,"code",nb,"InvalidParameterValue"); |
---|
| 428 | } |
---|
| 429 | return -1; |
---|
| 430 | } |
---|
[790] | 431 | |
---|
| 432 | char ltmp1[256]; |
---|
| 433 | sprintf(ltmp1,"%d",fsize); |
---|
| 434 | map* tmp=getMapFromMaps(*m,"main","cacheDir"); |
---|
[822] | 435 | char *request=NULL; |
---|
[790] | 436 | if(tmp!=NULL){ |
---|
[822] | 437 | map* tmp2; |
---|
| 438 | char* md5str=NULL; |
---|
| 439 | if((tmp2=getMap(content->content,bname))!=NULL){ |
---|
| 440 | char *tmpStr=(char*)malloc((strlen(tmp1->value)+strlen(tmp2->value)+1)*sizeof(char)); |
---|
| 441 | sprintf(tmpStr,"%s%s",tmp1->value,tmp2->value); |
---|
| 442 | if((tmp2=getMap(content->content,"headers"))!=NULL){ |
---|
| 443 | char *tmpStr2=zStrdup(tmpStr); |
---|
[839] | 444 | free(tmpStr); |
---|
[822] | 445 | tmpStr=(char*)malloc((strlen(tmpStr2)+strlen(tmp2->value)+1)*sizeof(char)); |
---|
| 446 | sprintf(tmpStr,"%s%s",tmpStr2,tmp2->value); |
---|
| 447 | free(tmpStr2); |
---|
| 448 | } |
---|
| 449 | md5str=getMd5(tmpStr); |
---|
| 450 | request=zStrdup(tmpStr); |
---|
| 451 | free(tmpStr); |
---|
| 452 | }else{ |
---|
[854] | 453 | char *myRequest=getFilenameForRequest(*m,tmp1->value); |
---|
| 454 | md5str=getMd5(myRequest); |
---|
[822] | 455 | request=zStrdup(tmp1->value); |
---|
[854] | 456 | free(myRequest); |
---|
[822] | 457 | } |
---|
[790] | 458 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 459 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
| 460 | addToMap((*in)->content,cname,fname); |
---|
| 461 | free(fname); |
---|
| 462 | } |
---|
| 463 | addToMap((*in)->content,sname,ltmp1); |
---|
| 464 | addToMap((*in)->content,mname,mimeType); |
---|
[863] | 465 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 466 | addToCache(*m,request,fcontent,mimeType,fsize, NULL, 0); |
---|
| 467 | free(fcontent); |
---|
| 468 | }else{ |
---|
| 469 | addToMap((*in)->content,ufile,"true"); |
---|
| 470 | cacheFile(*m,request,mimeType,fsize,hInternet->ihandle[*index].filename); |
---|
| 471 | } |
---|
[790] | 472 | free(mimeType); |
---|
[822] | 473 | free(request); |
---|
[839] | 474 | (*index)++; |
---|
[790] | 475 | } |
---|
| 476 | } |
---|
| 477 | } |
---|
| 478 | if(shouldClean>0){ |
---|
| 479 | freeMap(&length); |
---|
| 480 | free(length); |
---|
| 481 | } |
---|
[863] | 482 | dumpMaps(*in); |
---|
[790] | 483 | return 0; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | /** |
---|
[642] | 487 | * Effectively run all the HTTP requests in the queue |
---|
| 488 | * |
---|
| 489 | * @param m the maps containing the settings of the main.cfg file |
---|
| 490 | * @param inputs the maps containing the inputs (defined in the requests+added |
---|
| 491 | * per default based on the zcfg file) |
---|
| 492 | * @param hInternet the HINTERNET pointer |
---|
[797] | 493 | * @param error the error map pointer |
---|
| 494 | * @return 0 on success, -1 on failure |
---|
[642] | 495 | */ |
---|
[797] | 496 | int runHttpRequests(maps** m,maps** inputs,HINTERNET* hInternet,map** error){ |
---|
| 497 | int hasAFailure=0; |
---|
[790] | 498 | if(hInternet!=NULL && hInternet->nb>0){ |
---|
[817] | 499 | AddHeaderEntries(hInternet,*m); |
---|
[642] | 500 | processDownloads(hInternet); |
---|
| 501 | maps* content=*inputs; |
---|
| 502 | int index=0; |
---|
| 503 | while(content!=NULL){ |
---|
[790] | 504 | if(content->child!=NULL){ |
---|
| 505 | maps* cursor=content->child; |
---|
| 506 | while(cursor!=NULL){ |
---|
[797] | 507 | int red=readCurrentInput(m,&cursor,&index,hInternet,error); |
---|
| 508 | if(red<0) |
---|
| 509 | hasAFailure=red; |
---|
[790] | 510 | cursor=cursor->next; |
---|
[642] | 511 | } |
---|
| 512 | } |
---|
[797] | 513 | else{ |
---|
| 514 | int red=readCurrentInput(m,&content,&index,hInternet,error); |
---|
| 515 | if(red<0) |
---|
| 516 | hasAFailure=red; |
---|
| 517 | } |
---|
[642] | 518 | content=content->next; |
---|
[797] | 519 | } |
---|
[642] | 520 | } |
---|
[797] | 521 | return hasAFailure; |
---|
[642] | 522 | } |
---|
| 523 | |
---|
| 524 | /** |
---|
| 525 | * Add a request in the download queue |
---|
| 526 | * |
---|
| 527 | * @param m the maps containing the settings of the main.cfg file |
---|
| 528 | * @param url the url to add to the queue |
---|
| 529 | */ |
---|
| 530 | void addRequestToQueue(maps** m,HINTERNET* hInternet,const char* url,bool req){ |
---|
| 531 | hInternet->waitingRequests[hInternet->nb]=strdup(url); |
---|
| 532 | if(req) |
---|
[863] | 533 | InternetOpenUrl(hInternet,hInternet->waitingRequests[hInternet->nb],NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0,*m); |
---|
[642] | 534 | maps *oreq=getMaps(*m,"orequests"); |
---|
| 535 | if(oreq==NULL){ |
---|
[790] | 536 | oreq=createMaps("orequests"); |
---|
[642] | 537 | oreq->content=createMap("value",url); |
---|
| 538 | addMapsToMaps(m,oreq); |
---|
| 539 | freeMaps(&oreq); |
---|
| 540 | free(oreq); |
---|
| 541 | }else{ |
---|
[839] | 542 | setMapArray(oreq->content,"value",hInternet->nb,url); |
---|
[642] | 543 | } |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | /** |
---|
| 547 | * Try to load file from cache or download a remote file if not in cache |
---|
| 548 | * |
---|
| 549 | * @param m the maps containing the settings of the main.cfg file |
---|
| 550 | * @param content the map to update |
---|
| 551 | * @param hInternet the HINTERNET pointer |
---|
| 552 | * @param url the url to fetch |
---|
| 553 | * @return 0 |
---|
| 554 | */ |
---|
| 555 | int loadRemoteFile(maps** m,map** content,HINTERNET* hInternet,char *url){ |
---|
[738] | 556 | char* fcontent = NULL; |
---|
[642] | 557 | char* cached=isInCache(*m,url); |
---|
| 558 | char *mimeType=NULL; |
---|
| 559 | int fsize=0; |
---|
[863] | 560 | map* memUse=getMapFromMaps(*m,"main","memory"); |
---|
[642] | 561 | |
---|
| 562 | map* t=getMap(*content,"xlink:href"); |
---|
| 563 | if(t==NULL){ |
---|
| 564 | t=getMap((*content),"href"); |
---|
| 565 | addToMap(*content,"xlink:href",url); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | if(cached!=NULL){ |
---|
| 569 | struct stat f_status; |
---|
| 570 | int s=stat(cached, &f_status); |
---|
| 571 | if(s==0){ |
---|
[854] | 572 | zooLock* lck=lockFile(*m,cached,'r'); |
---|
| 573 | if(lck==NULL) |
---|
| 574 | return -1; |
---|
[642] | 575 | fsize=f_status.st_size; |
---|
[863] | 576 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 577 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 578 | FILE* f=fopen(cached,"rb"); |
---|
| 579 | if(f!=NULL){ |
---|
| 580 | fread(fcontent,f_status.st_size,1,f); |
---|
| 581 | fcontent[fsize]=0; |
---|
| 582 | fclose(f); |
---|
| 583 | } |
---|
| 584 | } |
---|
[642] | 585 | addToMap(*content,"cache_file",cached); |
---|
[854] | 586 | unlockFile(*m,lck); |
---|
[642] | 587 | } |
---|
| 588 | cached[strlen(cached)-1]='m'; |
---|
| 589 | s=stat(cached, &f_status); |
---|
| 590 | if(s==0){ |
---|
[854] | 591 | zooLock* lck=lockFile(*m,cached,'r'); |
---|
| 592 | if(lck==NULL) |
---|
| 593 | return -1; |
---|
[642] | 594 | mimeType=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 595 | FILE* f=fopen(cached,"rb"); |
---|
| 596 | fread(mimeType,f_status.st_size,1,f); |
---|
| 597 | mimeType[f_status.st_size]=0; |
---|
| 598 | fclose(f); |
---|
[854] | 599 | unlockFile(*m,lck); |
---|
[642] | 600 | } |
---|
| 601 | }else{ |
---|
| 602 | addRequestToQueue(m,hInternet,url,true); |
---|
| 603 | return 0; |
---|
| 604 | } |
---|
| 605 | if(fsize==0){ |
---|
| 606 | return errorException(*m, _("Unable to download the file."), "InternalError",NULL); |
---|
| 607 | } |
---|
| 608 | if(mimeType!=NULL){ |
---|
| 609 | addToMap(*content,"fmimeType",mimeType); |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | map* tmpMap=getMapOrFill(content,"value",""); |
---|
[863] | 613 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 614 | free(tmpMap->value); |
---|
| 615 | tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); |
---|
| 616 | if(tmpMap->value==NULL || fcontent == NULL) |
---|
| 617 | return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); |
---|
| 618 | memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); |
---|
| 619 | } |
---|
| 620 | |
---|
[642] | 621 | char ltmp1[256]; |
---|
| 622 | sprintf(ltmp1,"%d",fsize); |
---|
| 623 | addToMap(*content,"size",ltmp1); |
---|
| 624 | if(cached==NULL){ |
---|
[863] | 625 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0) |
---|
| 626 | addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); |
---|
| 627 | else |
---|
| 628 | cacheFile(*m,url,mimeType,fsize,hInternet->ihandle[hInternet->nb-1].filename); |
---|
[642] | 629 | } |
---|
| 630 | else{ |
---|
| 631 | addToMap(*content,"isCached","true"); |
---|
| 632 | map* tmp=getMapFromMaps(*m,"main","cacheDir"); |
---|
| 633 | if(tmp!=NULL){ |
---|
| 634 | map *c=getMap((*content),"xlink:href"); |
---|
[854] | 635 | char *myRequest=getFilenameForRequest(*m,c->value); |
---|
| 636 | char* md5str=getMd5(myRequest); |
---|
| 637 | free(myRequest); |
---|
[642] | 638 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 639 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
| 640 | addToMap(*content,"cache_file",fname); |
---|
| 641 | free(fname); |
---|
[839] | 642 | free(md5str); |
---|
[642] | 643 | } |
---|
| 644 | } |
---|
| 645 | free(fcontent); |
---|
| 646 | free(mimeType); |
---|
| 647 | free(cached); |
---|
| 648 | return 0; |
---|
| 649 | } |
---|