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 |
---|
40 | * @warning make sure to free resources returned by this function |
---|
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 | /** |
---|
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 | /** |
---|
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 | */ |
---|
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 | */ |
---|
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){ |
---|
226 | char* myRequest=getFilenameForRequest(conf,request); |
---|
227 | char* md5str=getMd5(myRequest); |
---|
228 | free(myRequest); |
---|
229 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
230 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
231 | zooLock* lck=lockFile(conf,fname,'w'); |
---|
232 | if(lck!=NULL){ |
---|
233 | #ifdef DEBUG |
---|
234 | fprintf(stderr,"Cache list : %s\n",fname); |
---|
235 | fflush(stderr); |
---|
236 | #endif |
---|
237 | FILE* fo=fopen(fname,"w+"); |
---|
238 | if(fo==NULL){ |
---|
239 | #ifdef DEBUG |
---|
240 | fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); |
---|
241 | #endif |
---|
242 | filepath = NULL; |
---|
243 | unlockFile(conf,lck); |
---|
244 | return; |
---|
245 | } |
---|
246 | fwrite(content,sizeof(char),length,fo); |
---|
247 | unlockFile(conf,lck); |
---|
248 | fclose(fo); |
---|
249 | |
---|
250 | if (filepath != NULL) { |
---|
251 | strncpy(filepath, fname, max_path); |
---|
252 | } |
---|
253 | |
---|
254 | sprintf(fname,"%s/%s.zcm",tmp->value,md5str); |
---|
255 | fo=fopen(fname,"w+"); |
---|
256 | #ifdef DEBUG |
---|
257 | fprintf(stderr,"MIMETYPE: %s\n",mimeType); |
---|
258 | #endif |
---|
259 | fwrite(mimeType,sizeof(char),strlen(mimeType),fo); |
---|
260 | fclose(fo); |
---|
261 | |
---|
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 | } |
---|
274 | } |
---|
275 | else { |
---|
276 | filepath = NULL; |
---|
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 |
---|
286 | * @warning make sure to free resources returned by this function (if not NULL) |
---|
287 | */ |
---|
288 | char* isInCache(maps* conf,char* request){ |
---|
289 | map* tmpM=getMapFromMaps(conf,"main","cacheDir"); |
---|
290 | if(tmpM!=NULL){ |
---|
291 | char* myRequest=getFilenameForRequest(conf,request); |
---|
292 | char* md5str=getMd5(myRequest); |
---|
293 | free(myRequest); |
---|
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 | /** |
---|
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 |
---|
318 | * @param error the error map pointer |
---|
319 | * @return 0 in case of success, -1 in case of failure |
---|
320 | */ |
---|
321 | int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){ |
---|
322 | |
---|
323 | int shouldClean=-1; |
---|
324 | map* tmp1; |
---|
325 | char sindex[5]; |
---|
326 | maps* content=*in; |
---|
327 | map* length=getMap(content->content,"length"); |
---|
328 | map* memUse=getMapFromMaps(*m,"main","memory"); |
---|
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]; |
---|
344 | char bname[8]; |
---|
345 | char hname[11]; |
---|
346 | char oname[12]; |
---|
347 | char ufile[12]; |
---|
348 | if(*index>0) |
---|
349 | sprintf(vname1,"value_%d",*index); |
---|
350 | else |
---|
351 | sprintf(vname1,"value"); |
---|
352 | |
---|
353 | if(i>0){ |
---|
354 | sprintf(cname,"cache_file_%d",i); |
---|
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); |
---|
361 | sprintf(bname,"body_%d",i); |
---|
362 | sprintf(hname,"headers_%d",i); |
---|
363 | sprintf(oname,"Order_%d",i); |
---|
364 | sprintf(ufile,"use_file_%d",i); |
---|
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"); |
---|
372 | sprintf(bname,"body"); |
---|
373 | sprintf(hname,"headers"); |
---|
374 | sprintf(oname,"Order"); |
---|
375 | sprintf(ufile,"use_file"); |
---|
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){ |
---|
381 | |
---|
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; |
---|
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,""); |
---|
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"); |
---|
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 | } |
---|
431 | |
---|
432 | char ltmp1[256]; |
---|
433 | sprintf(ltmp1,"%d",fsize); |
---|
434 | map* tmp=getMapFromMaps(*m,"main","cacheDir"); |
---|
435 | char *request=NULL; |
---|
436 | if(tmp!=NULL){ |
---|
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); |
---|
444 | free(tmpStr); |
---|
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{ |
---|
453 | char *myRequest=getFilenameForRequest(*m,tmp1->value); |
---|
454 | md5str=getMd5(myRequest); |
---|
455 | request=zStrdup(tmp1->value); |
---|
456 | free(myRequest); |
---|
457 | } |
---|
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); |
---|
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 | } |
---|
472 | free(mimeType); |
---|
473 | free(request); |
---|
474 | (*index)++; |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | if(shouldClean>0){ |
---|
479 | freeMap(&length); |
---|
480 | free(length); |
---|
481 | } |
---|
482 | return 0; |
---|
483 | } |
---|
484 | |
---|
485 | /** |
---|
486 | * Effectively run all the HTTP requests in the queue |
---|
487 | * |
---|
488 | * @param m the maps containing the settings of the main.cfg file |
---|
489 | * @param inputs the maps containing the inputs (defined in the requests+added |
---|
490 | * per default based on the zcfg file) |
---|
491 | * @param hInternet the HINTERNET pointer |
---|
492 | * @param error the error map pointer |
---|
493 | * @return 0 on success, -1 on failure |
---|
494 | */ |
---|
495 | int runHttpRequests(maps** m,maps** inputs,HINTERNET* hInternet,map** error){ |
---|
496 | int hasAFailure=0; |
---|
497 | if(hInternet!=NULL && hInternet->nb>0){ |
---|
498 | AddHeaderEntries(hInternet,*m); |
---|
499 | processDownloads(hInternet); |
---|
500 | maps* content=*inputs; |
---|
501 | int index=0; |
---|
502 | while(content!=NULL){ |
---|
503 | if(content->child!=NULL){ |
---|
504 | maps* cursor=content->child; |
---|
505 | while(cursor!=NULL){ |
---|
506 | int red=readCurrentInput(m,&cursor,&index,hInternet,error); |
---|
507 | if(red<0) |
---|
508 | hasAFailure=red; |
---|
509 | cursor=cursor->next; |
---|
510 | } |
---|
511 | } |
---|
512 | else{ |
---|
513 | int red=readCurrentInput(m,&content,&index,hInternet,error); |
---|
514 | if(red<0) |
---|
515 | hasAFailure=red; |
---|
516 | } |
---|
517 | content=content->next; |
---|
518 | } |
---|
519 | } |
---|
520 | return hasAFailure; |
---|
521 | } |
---|
522 | |
---|
523 | /** |
---|
524 | * Add a request in the download queue |
---|
525 | * |
---|
526 | * @param m the maps containing the settings of the main.cfg file |
---|
527 | * @param url the url to add to the queue |
---|
528 | */ |
---|
529 | void addRequestToQueue(maps** m,HINTERNET* hInternet,const char* url,bool req){ |
---|
530 | hInternet->waitingRequests[hInternet->nb]=strdup(url); |
---|
531 | if(req) |
---|
532 | InternetOpenUrl(hInternet,hInternet->waitingRequests[hInternet->nb],NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0,*m); |
---|
533 | maps *oreq=getMaps(*m,"orequests"); |
---|
534 | if(oreq==NULL){ |
---|
535 | oreq=createMaps("orequests"); |
---|
536 | oreq->content=createMap("value",url); |
---|
537 | addMapsToMaps(m,oreq); |
---|
538 | freeMaps(&oreq); |
---|
539 | free(oreq); |
---|
540 | }else{ |
---|
541 | setMapArray(oreq->content,"value",hInternet->nb,url); |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | /** |
---|
546 | * Try to load file from cache or download a remote file if not in cache |
---|
547 | * |
---|
548 | * @param m the maps containing the settings of the main.cfg file |
---|
549 | * @param content the map to update |
---|
550 | * @param hInternet the HINTERNET pointer |
---|
551 | * @param url the url to fetch |
---|
552 | * @return 0 |
---|
553 | */ |
---|
554 | int loadRemoteFile(maps** m,map** content,HINTERNET* hInternet,char *url){ |
---|
555 | char* fcontent = NULL; |
---|
556 | char* cached=isInCache(*m,url); |
---|
557 | char *mimeType=NULL; |
---|
558 | int fsize=0; |
---|
559 | map* memUse=getMapFromMaps(*m,"main","memory"); |
---|
560 | |
---|
561 | map* t=getMap(*content,"xlink:href"); |
---|
562 | if(t==NULL){ |
---|
563 | t=getMap((*content),"href"); |
---|
564 | addToMap(*content,"xlink:href",url); |
---|
565 | } |
---|
566 | |
---|
567 | if(cached!=NULL){ |
---|
568 | struct stat f_status; |
---|
569 | int s=stat(cached, &f_status); |
---|
570 | if(s==0){ |
---|
571 | zooLock* lck=lockFile(*m,cached,'r'); |
---|
572 | if(lck==NULL) |
---|
573 | return -1; |
---|
574 | fsize=f_status.st_size; |
---|
575 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
576 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
577 | FILE* f=fopen(cached,"rb"); |
---|
578 | if(f!=NULL){ |
---|
579 | fread(fcontent,f_status.st_size,1,f); |
---|
580 | fcontent[fsize]=0; |
---|
581 | fclose(f); |
---|
582 | } |
---|
583 | } |
---|
584 | addToMap(*content,"cache_file",cached); |
---|
585 | unlockFile(*m,lck); |
---|
586 | } |
---|
587 | cached[strlen(cached)-1]='m'; |
---|
588 | s=stat(cached, &f_status); |
---|
589 | if(s==0){ |
---|
590 | zooLock* lck=lockFile(*m,cached,'r'); |
---|
591 | if(lck==NULL) |
---|
592 | return -1; |
---|
593 | mimeType=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
594 | FILE* f=fopen(cached,"rb"); |
---|
595 | fread(mimeType,f_status.st_size,1,f); |
---|
596 | mimeType[f_status.st_size]=0; |
---|
597 | fclose(f); |
---|
598 | unlockFile(*m,lck); |
---|
599 | } |
---|
600 | }else{ |
---|
601 | addRequestToQueue(m,hInternet,url,true); |
---|
602 | return 0; |
---|
603 | } |
---|
604 | if(fsize==0){ |
---|
605 | return errorException(*m, _("Unable to download the file."), "InternalError",NULL); |
---|
606 | } |
---|
607 | if(mimeType!=NULL){ |
---|
608 | addToMap(*content,"fmimeType",mimeType); |
---|
609 | } |
---|
610 | |
---|
611 | map* tmpMap=getMapOrFill(content,"value",""); |
---|
612 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
613 | free(tmpMap->value); |
---|
614 | tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); |
---|
615 | if(tmpMap->value==NULL || fcontent == NULL) |
---|
616 | return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); |
---|
617 | memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); |
---|
618 | } |
---|
619 | |
---|
620 | char ltmp1[256]; |
---|
621 | sprintf(ltmp1,"%d",fsize); |
---|
622 | addToMap(*content,"size",ltmp1); |
---|
623 | if(cached==NULL){ |
---|
624 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0) |
---|
625 | addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); |
---|
626 | else |
---|
627 | cacheFile(*m,url,mimeType,fsize,hInternet->ihandle[hInternet->nb-1].filename); |
---|
628 | } |
---|
629 | else{ |
---|
630 | addToMap(*content,"isCached","true"); |
---|
631 | map* tmp=getMapFromMaps(*m,"main","cacheDir"); |
---|
632 | if(tmp!=NULL){ |
---|
633 | map *c=getMap((*content),"xlink:href"); |
---|
634 | char *myRequest=getFilenameForRequest(*m,c->value); |
---|
635 | char* md5str=getMd5(myRequest); |
---|
636 | free(myRequest); |
---|
637 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
638 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
639 | addToMap(*content,"cache_file",fname); |
---|
640 | free(fname); |
---|
641 | free(md5str); |
---|
642 | } |
---|
643 | } |
---|
644 | free(fcontent); |
---|
645 | free(mimeType); |
---|
646 | free(cached); |
---|
647 | return 0; |
---|
648 | } |
---|