1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2008-2011 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 | #define MALLOC_CHECK_ 0 |
---|
26 | #define MALLOC_CHECK 0 |
---|
27 | |
---|
28 | /** |
---|
29 | * Specific includes |
---|
30 | */ |
---|
31 | #ifndef WIN32 |
---|
32 | /* |
---|
33 | #include "fcgio.h" |
---|
34 | #include "fcgi_config.h" |
---|
35 | */ |
---|
36 | //#include <stdio.h> |
---|
37 | #include <unistd.h> |
---|
38 | #include <fcgiapp.h> |
---|
39 | #endif |
---|
40 | #include <sys/wait.h> |
---|
41 | #include <pthread.h> |
---|
42 | #include <sys/types.h> |
---|
43 | #include <unistd.h> |
---|
44 | #include "service_internal.h" |
---|
45 | #ifdef WIN32 |
---|
46 | #include "windows.h" |
---|
47 | #define strtok_r strtok_s |
---|
48 | #endif |
---|
49 | |
---|
50 | extern "C" |
---|
51 | { |
---|
52 | #include "cgic.h" |
---|
53 | #include <libxml/tree.h> |
---|
54 | #include <libxml/xmlmemory.h> |
---|
55 | #include <libxml/parser.h> |
---|
56 | #include <libxml/xpath.h> |
---|
57 | #include <libxml/xpathInternals.h> |
---|
58 | |
---|
59 | #include <string.h> |
---|
60 | |
---|
61 | #include <stdlib.h> |
---|
62 | #include <glib.h> |
---|
63 | #include <sys/stat.h> |
---|
64 | #include <ctype.h> |
---|
65 | |
---|
66 | } |
---|
67 | #include "response_print.h" |
---|
68 | #include "zoo_zcfg.h" |
---|
69 | #include "zoo_json.h" |
---|
70 | #include "zoo_amqp.h" |
---|
71 | //#include "zoo_sql.h" |
---|
72 | #include "server_internal.h" |
---|
73 | #include "request_parser.h" |
---|
74 | void |
---|
75 | loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs, |
---|
76 | maps ** inputs, maps ** ioutputs, int *eres,FCGX_Stream *out, FCGX_Stream *err); |
---|
77 | |
---|
78 | xmlXPathObjectPtr extractFromDoc (xmlDocPtr, const char *); |
---|
79 | int runRequest (map **,struct cgi_env **,FCGX_Request *); |
---|
80 | |
---|
81 | using namespace std; |
---|
82 | |
---|
83 | #ifndef TRUE |
---|
84 | #define TRUE 1 |
---|
85 | #endif |
---|
86 | #ifndef FALSE |
---|
87 | #define FALSE -1 |
---|
88 | #endif |
---|
89 | |
---|
90 | |
---|
91 | static void PrintEnv(FCGX_Stream *out, char *label, char **envp) |
---|
92 | { |
---|
93 | FCGX_FPrintF(out, "%s:<br>\n<pre>\n", label); |
---|
94 | for( ; *envp != NULL; envp++) { |
---|
95 | FCGX_FPrintF(out, "%s\n", *envp); |
---|
96 | } |
---|
97 | FCGX_FPrintF(out, "</pre><p>\n"); |
---|
98 | } |
---|
99 | |
---|
100 | #define PATH_SOCKET "/tmp/zoo.sock" |
---|
101 | #define THREAD_COUNT 50 |
---|
102 | static int counts[THREAD_COUNT]; |
---|
103 | |
---|
104 | |
---|
105 | int process(FCGX_Request *request){ |
---|
106 | |
---|
107 | int pid = getpid(); |
---|
108 | struct cgi_env *cgi; |
---|
109 | cgi = (struct cgi_env*)malloc(sizeof(struct cgi_env)); |
---|
110 | cgiMain_init (NULL, NULL,&cgi,request); |
---|
111 | char *strQuery = NULL; |
---|
112 | if (cgi->cgiQueryString != NULL) |
---|
113 | strQuery = zStrdup (cgi->cgiQueryString); |
---|
114 | map *tmpMap = NULL; |
---|
115 | |
---|
116 | if (strncmp (cgi->cgiContentType, "text/xml", 8) == 0 || |
---|
117 | strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0) |
---|
118 | { |
---|
119 | if (cgi->cgiContentLength == 0) |
---|
120 | { |
---|
121 | char *post_data = NULL; |
---|
122 | int i = 0; |
---|
123 | int ch = FCGX_GetChar(request->in); |
---|
124 | while (ch != -1){ |
---|
125 | { |
---|
126 | i++; |
---|
127 | if (post_data == NULL) |
---|
128 | { |
---|
129 | post_data=(char*)malloc(sizeof(char)); |
---|
130 | post_data[i-1] = (char) ch; |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | post_data=(char*)realloc(post_data,i*sizeof(char)); |
---|
135 | post_data[i-1] = (char) ch; |
---|
136 | } |
---|
137 | ch = FCGX_GetChar(request->in); |
---|
138 | if (ch == -1 ){ |
---|
139 | post_data=(char*)realloc(post_data,(i + 1)*sizeof(char)); |
---|
140 | post_data[i] = '\0'; |
---|
141 | } |
---|
142 | } |
---|
143 | cgi->cgiContentLength = i; |
---|
144 | if (post_data == NULL && (strQuery == NULL || strlen (strQuery) == 0)) |
---|
145 | { |
---|
146 | return errorException (NULL, |
---|
147 | "ZOO-Kernel failed to process your request cause the request was emtpty.", |
---|
148 | "InternalError", NULL,request->out); |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | if (strQuery == NULL || strlen (strQuery) == 0) |
---|
153 | tmpMap = createMap ("request", post_data); |
---|
154 | } |
---|
155 | if (post_data != NULL) |
---|
156 | free (post_data); |
---|
157 | } |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | char *post_data = new char[cgi->cgiContentLength + 1]; |
---|
162 | int r = FCGX_GetStr(post_data,cgi->cgiContentLength,request->in); |
---|
163 | if ( r > 0) |
---|
164 | { |
---|
165 | post_data[r] = '\0'; |
---|
166 | cgi->cgiContentLength = r; |
---|
167 | tmpMap = createMap ("request", post_data); |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | post_data[0] = '\0'; |
---|
172 | char **array, **arrayStep; |
---|
173 | if (cgiFormEntries (&array,&cgi) != cgiFormSuccess) |
---|
174 | { |
---|
175 | return 1; |
---|
176 | } |
---|
177 | arrayStep = array; |
---|
178 | while (*arrayStep) |
---|
179 | { |
---|
180 | char *ivalue = new char[cgi->cgiContentLength]; |
---|
181 | cgiFormStringNoNewlines (*arrayStep, ivalue, |
---|
182 | cgi->cgiContentLength,&cgi); |
---|
183 | char *tmpValueFinal = |
---|
184 | (char *) |
---|
185 | malloc ((strlen (*arrayStep) + strlen (ivalue) + |
---|
186 | 1) * sizeof (char)); |
---|
187 | sprintf (tmpValueFinal, "%s=%s", *arrayStep, ivalue); |
---|
188 | if (strlen (post_data) == 0) |
---|
189 | { |
---|
190 | sprintf (post_data, "%s", tmpValueFinal); |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | char *tmp = zStrdup (post_data); |
---|
195 | sprintf (post_data, "%s&%s", tmp, tmpValueFinal); |
---|
196 | free (tmp); |
---|
197 | } |
---|
198 | free (tmpValueFinal); |
---|
199 | #ifdef DEBUG |
---|
200 | fprintf (stderr, "(( \n %s \n %s \n ))", *arrayStep, |
---|
201 | ivalue); |
---|
202 | #endif |
---|
203 | delete[]ivalue; |
---|
204 | arrayStep++; |
---|
205 | } |
---|
206 | if (tmpMap != NULL) |
---|
207 | addToMap (tmpMap, "request", post_data); |
---|
208 | else |
---|
209 | tmpMap = createMap ("request", post_data); |
---|
210 | } |
---|
211 | delete[]post_data; |
---|
212 | } |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | #ifdef DEBUG |
---|
217 | dumpMap (tmpMap); |
---|
218 | #endif |
---|
219 | |
---|
220 | char **array, **arrayStep; |
---|
221 | if (cgiFormEntries (&array,&cgi) != cgiFormSuccess) |
---|
222 | { |
---|
223 | return 1; |
---|
224 | } |
---|
225 | arrayStep = array; |
---|
226 | while (*arrayStep) |
---|
227 | { |
---|
228 | char *value = new char[cgi->cgiContentLength]; |
---|
229 | cgiFormStringNoNewlines (*arrayStep, value, cgi->cgiContentLength,&cgi); |
---|
230 | #ifdef DEBUG |
---|
231 | fprintf (stderr, "(( \n %s \n %s \n ))", *arrayStep, value); |
---|
232 | #endif |
---|
233 | if (tmpMap != NULL) |
---|
234 | addToMap (tmpMap, *arrayStep, value); |
---|
235 | else |
---|
236 | tmpMap = createMap (*arrayStep, value); |
---|
237 | arrayStep++; |
---|
238 | delete[]value; |
---|
239 | } |
---|
240 | cgiStringArrayFree (array); |
---|
241 | } |
---|
242 | |
---|
243 | #ifdef WIN32 |
---|
244 | map *tmpReq = getMap (tmpMap, "rfile"); |
---|
245 | if (tmpReq != NULL) |
---|
246 | { |
---|
247 | FILE *lf = fopen (tmpReq->value, "r"); |
---|
248 | fseek (lf, 0, SEEK_END); |
---|
249 | long flen = ftell (lf); |
---|
250 | fseek (lf, 0, SEEK_SET); |
---|
251 | char *buffer = (char *) malloc ((flen + 1) * sizeof (char)); |
---|
252 | fread (buffer, flen, 1, lf); |
---|
253 | fclose (lf); |
---|
254 | addToMap (tmpMap, "request", buffer); |
---|
255 | free (buffer); |
---|
256 | cgiContentLength = flen + 9; |
---|
257 | } |
---|
258 | #endif |
---|
259 | /** |
---|
260 | * In case that the POST method was used, then check if params came in XML |
---|
261 | * format else try to use the attribute "request" which should be the only |
---|
262 | * one. |
---|
263 | */ |
---|
264 | if (strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0 || |
---|
265 | (count (tmpMap) == 1 && strncmp (tmpMap->value, "<", 1) == 0) |
---|
266 | #ifdef WIN32 |
---|
267 | || tmpReq != NULL |
---|
268 | #endif |
---|
269 | ) |
---|
270 | { |
---|
271 | /** |
---|
272 | * Store the original XML request in xrequest map |
---|
273 | */ |
---|
274 | map *t1 = getMap (tmpMap, "request"); |
---|
275 | if (t1 != NULL && strncasecmp (t1->value, "<", 1) == 0) |
---|
276 | { |
---|
277 | addToMap (tmpMap, "xrequest", t1->value); |
---|
278 | xmlInitParser (); |
---|
279 | xmlDocPtr doc = xmlParseMemory (t1->value, cgi->cgiContentLength); |
---|
280 | { |
---|
281 | xmlXPathObjectPtr reqptr = extractFromDoc (doc, |
---|
282 | "/*[local-name()='Envelope']/*[local-name()='Body']/*"); |
---|
283 | if (reqptr != NULL) |
---|
284 | { |
---|
285 | xmlNodeSet *req = reqptr->nodesetval; |
---|
286 | if (req != NULL && req->nodeNr == 1) |
---|
287 | { |
---|
288 | addToMap (tmpMap, "soap", "true"); |
---|
289 | for (int k = 0; k < req->nodeNr; k++) |
---|
290 | { |
---|
291 | //xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi"); |
---|
292 | xmlDocSetRootElement (doc, req->nodeTab[k]); |
---|
293 | xmlChar *xmlbuff; |
---|
294 | int buffersize; |
---|
295 | xmlDocDumpFormatMemoryEnc (doc, &xmlbuff, |
---|
296 | &buffersize, "utf-8", |
---|
297 | 1); |
---|
298 | addToMap (tmpMap, "xrequest", (char *) xmlbuff); |
---|
299 | xmlFree (xmlbuff); |
---|
300 | } |
---|
301 | } |
---|
302 | xmlXPathFreeObject (reqptr); |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | xmlNodePtr cur = xmlDocGetRootElement (doc); |
---|
307 | char *tval; |
---|
308 | tval = NULL; |
---|
309 | tval = (char *) xmlGetProp (cur, BAD_CAST "service"); |
---|
310 | if (tval != NULL) |
---|
311 | { |
---|
312 | addToMap (tmpMap, "service", tval); |
---|
313 | xmlFree (tval); |
---|
314 | } |
---|
315 | tval = NULL; |
---|
316 | tval = (char *) xmlGetProp (cur, BAD_CAST "language"); |
---|
317 | if (tval != NULL) |
---|
318 | { |
---|
319 | addToMap (tmpMap, "language", tval); |
---|
320 | xmlFree (tval); |
---|
321 | } |
---|
322 | const char *requests[3] = |
---|
323 | { "GetCapabilities", "DescribeProcess", "Execute" }; |
---|
324 | for (int j = 0; j < 3; j++) |
---|
325 | { |
---|
326 | char tt[128]; |
---|
327 | sprintf (tt, "/*[local-name()='%s']", requests[j]); |
---|
328 | xmlXPathObjectPtr reqptr = extractFromDoc (doc, tt); |
---|
329 | if (reqptr != NULL) |
---|
330 | { |
---|
331 | xmlNodeSet *req = reqptr->nodesetval; |
---|
332 | #ifdef DEBUG |
---|
333 | fprintf (stderr, "%i", req->nodeNr); |
---|
334 | #endif |
---|
335 | if (req != NULL && req->nodeNr == 1) |
---|
336 | { |
---|
337 | if (t1->value != NULL) |
---|
338 | free (t1->value); |
---|
339 | t1->value = zStrdup (requests[j]); |
---|
340 | j = 2; |
---|
341 | } |
---|
342 | xmlXPathFreeObject (reqptr); |
---|
343 | } |
---|
344 | } |
---|
345 | if (strncasecmp (t1->value, "GetCapabilities", 15) == 0) |
---|
346 | { |
---|
347 | xmlXPathObjectPtr versptr = |
---|
348 | extractFromDoc (doc, "/*/*/*[local-name()='Version']"); |
---|
349 | xmlNodeSet *vers = versptr->nodesetval; |
---|
350 | xmlChar *content = xmlNodeListGetString (doc, |
---|
351 | vers-> |
---|
352 | nodeTab |
---|
353 | [0]->xmlChildrenNode, |
---|
354 | 1); |
---|
355 | addToMap (tmpMap, "version", (char *) content); |
---|
356 | xmlXPathFreeObject (versptr); |
---|
357 | xmlFree (content); |
---|
358 | } |
---|
359 | else |
---|
360 | { |
---|
361 | tval = NULL; |
---|
362 | tval = (char *) xmlGetProp (cur, BAD_CAST "version"); |
---|
363 | if (tval != NULL) |
---|
364 | { |
---|
365 | addToMap (tmpMap, "version", tval); |
---|
366 | xmlFree (tval); |
---|
367 | } |
---|
368 | tval = (char *) xmlGetProp (cur, BAD_CAST "language"); |
---|
369 | if (tval != NULL) |
---|
370 | { |
---|
371 | addToMap (tmpMap, "language", tval); |
---|
372 | xmlFree (tval); |
---|
373 | } |
---|
374 | xmlXPathObjectPtr idptr = |
---|
375 | extractFromDoc (doc, "/*/*[local-name()='Identifier']"); |
---|
376 | if (idptr != NULL) |
---|
377 | { |
---|
378 | xmlNodeSet *id = idptr->nodesetval; |
---|
379 | if (id != NULL) |
---|
380 | { |
---|
381 | char *identifiers = NULL; |
---|
382 | identifiers = |
---|
383 | (char *) calloc (cgi->cgiContentLength, sizeof (char)); |
---|
384 | identifiers[0] = 0; |
---|
385 | for (int k = 0; k < id->nodeNr; k++) |
---|
386 | { |
---|
387 | xmlChar *content = xmlNodeListGetString (doc, |
---|
388 | id->nodeTab |
---|
389 | [k]-> |
---|
390 | xmlChildrenNode, |
---|
391 | 1); |
---|
392 | if (strlen (identifiers) > 0) |
---|
393 | { |
---|
394 | char *tmp = zStrdup (identifiers); |
---|
395 | snprintf (identifiers, |
---|
396 | strlen (tmp) + |
---|
397 | xmlStrlen (content) + 2, "%s,%s", |
---|
398 | tmp, content); |
---|
399 | free (tmp); |
---|
400 | } |
---|
401 | else |
---|
402 | { |
---|
403 | snprintf (identifiers, |
---|
404 | xmlStrlen (content) + 1, "%s", |
---|
405 | content); |
---|
406 | } |
---|
407 | xmlFree (content); |
---|
408 | } |
---|
409 | xmlXPathFreeObject (idptr); |
---|
410 | addToMap (tmpMap, "Identifier", identifiers); |
---|
411 | free (identifiers); |
---|
412 | } |
---|
413 | } |
---|
414 | } |
---|
415 | xmlFreeDoc (doc); |
---|
416 | xmlCleanupParser (); |
---|
417 | } |
---|
418 | else |
---|
419 | { |
---|
420 | freeMap (&tmpMap); |
---|
421 | free (tmpMap); |
---|
422 | tmpMap = createMap ("not_valid", "true"); |
---|
423 | } |
---|
424 | |
---|
425 | char *token, *saveptr; |
---|
426 | token = strtok_r (cgi->cgiQueryString, "&", &saveptr); |
---|
427 | while (token != NULL) |
---|
428 | { |
---|
429 | char *token1, *saveptr1; |
---|
430 | char *name = NULL; |
---|
431 | char *value = NULL; |
---|
432 | token1 = strtok_r (token, "=", &saveptr1); |
---|
433 | while (token1 != NULL) |
---|
434 | { |
---|
435 | if (name == NULL) |
---|
436 | name = zStrdup (token1); |
---|
437 | else |
---|
438 | value = zStrdup (token1); |
---|
439 | token1 = strtok_r (NULL, "=", &saveptr1); |
---|
440 | } |
---|
441 | addToMap (tmpMap, name, value); |
---|
442 | free (name); |
---|
443 | free (value); |
---|
444 | name = NULL; |
---|
445 | value = NULL; |
---|
446 | token = strtok_r (NULL, "&", &saveptr); |
---|
447 | } |
---|
448 | |
---|
449 | } |
---|
450 | |
---|
451 | if (strncasecmp (cgi->cgiContentType, "multipart/form-data", 19) == 0) |
---|
452 | { |
---|
453 | map *tmp = getMap (tmpMap, "dataInputs"); |
---|
454 | if (tmp != NULL) |
---|
455 | { |
---|
456 | addToMap (tmpMap, "dataInputs", |
---|
457 | strstr (strQuery, "dataInputs=") + 11); |
---|
458 | } |
---|
459 | } |
---|
460 | |
---|
461 | if (strQuery != NULL) |
---|
462 | free (strQuery); |
---|
463 | /* |
---|
464 | json_object *obj; |
---|
465 | maptojson(&obj,tmpMap); |
---|
466 | fprintf(stderr,"%s\n",json_object_to_json_string(obj)); |
---|
467 | fflush(stderr); |
---|
468 | */ |
---|
469 | runRequest (&tmpMap,&cgi,request); |
---|
470 | |
---|
471 | /** |
---|
472 | * Required but can't be made after executing a process using POST requests. |
---|
473 | */ |
---|
474 | if ( /*strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && */ tmpMap != NULL) |
---|
475 | { |
---|
476 | freeMap (&tmpMap); |
---|
477 | free (tmpMap); |
---|
478 | } |
---|
479 | // a verifier fait planter |
---|
480 | cgiFreeResources (&cgi); |
---|
481 | |
---|
482 | FCGX_Finish_r(request); |
---|
483 | return 0; |
---|
484 | } |
---|
485 | |
---|
486 | |
---|
487 | |
---|
488 | int |
---|
489 | main (int argc, char *argv[]) |
---|
490 | { |
---|
491 | int debug_flag = 0; |
---|
492 | int background_flag = 0; |
---|
493 | char *file_value = NULL; |
---|
494 | int index; |
---|
495 | int c; |
---|
496 | opterr = 0; |
---|
497 | while ((c = getopt (argc, argv, "dbhf:")) != -1) |
---|
498 | switch (c) |
---|
499 | { |
---|
500 | case 'd': |
---|
501 | debug_flag = 1; |
---|
502 | break; |
---|
503 | case 'b': |
---|
504 | background_flag = 1; |
---|
505 | break; |
---|
506 | case 'h': |
---|
507 | fprintf(stderr,"TODO: need to print help\n"); |
---|
508 | fflush(stderr); |
---|
509 | return 0; |
---|
510 | case 'f': |
---|
511 | file_value = optarg; |
---|
512 | break; |
---|
513 | case '?': |
---|
514 | if (optopt == 'f') |
---|
515 | fprintf (stderr, "Option -%c requires an argument.\n", optopt); |
---|
516 | else if (isprint (optopt)) |
---|
517 | fprintf (stderr, "Unknown option `-%c'.\n", optopt); |
---|
518 | else |
---|
519 | fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt); |
---|
520 | return 1; |
---|
521 | default: |
---|
522 | abort (); |
---|
523 | } |
---|
524 | |
---|
525 | |
---|
526 | maps *conf; |
---|
527 | conf = (maps *) malloc (MAPS_SIZE); |
---|
528 | |
---|
529 | int ret = conf_read (file_value, conf); |
---|
530 | if ( ret == 2){ |
---|
531 | //a verifier mais conf_read ne renvoie jamais 0 |
---|
532 | fprintf(stderr,"Erreur lors de la lecture de %s\n",file_value); |
---|
533 | return 1; |
---|
534 | } |
---|
535 | /* |
---|
536 | json_object *jobj; |
---|
537 | mapstojson(&jobj,conf); |
---|
538 | fprintf (stderr,"The json object created: %s\n",json_object_to_json_string(jobj)); |
---|
539 | freeMaps(&conf); |
---|
540 | |
---|
541 | maps *conf_tmp; |
---|
542 | jsontomaps(jobj,&conf_tmp); |
---|
543 | dumpMaps(conf_tmp); |
---|
544 | return 1; |
---|
545 | */ |
---|
546 | char *rootDir; |
---|
547 | map *m_rootDir = getMapFromMaps (conf, "server", "rootDir"); |
---|
548 | if (m_rootDir == NULL){ |
---|
549 | fprintf(stderr,"Configuration error: rootDir"); |
---|
550 | return 2; |
---|
551 | } |
---|
552 | else { |
---|
553 | rootDir = (char*)malloc((strlen(m_rootDir->value) +1)*sizeof(char*)); |
---|
554 | strncpy(rootDir,m_rootDir->value,strlen(m_rootDir->value)); |
---|
555 | rootDir[strlen(m_rootDir->value)] = '\0'; |
---|
556 | //freeMap(&m_rootDir); |
---|
557 | } |
---|
558 | |
---|
559 | char *regDir; |
---|
560 | map *m_regDir = getMapFromMaps (conf, "main", "registry"); |
---|
561 | if (m_regDir == NULL){ |
---|
562 | fprintf(stderr,"Configuration error: regDir"); |
---|
563 | return 2; |
---|
564 | } |
---|
565 | else { |
---|
566 | regDir = (char*)malloc((strlen(m_regDir->value) +1)*sizeof(char*)); |
---|
567 | strncpy(regDir,m_regDir->value,strlen(m_regDir->value)); |
---|
568 | regDir[strlen(m_regDir->value)] = '\0'; |
---|
569 | } |
---|
570 | |
---|
571 | |
---|
572 | |
---|
573 | int req_worker; |
---|
574 | map *m_req_worker = getMapFromMaps (conf, "server", "req_worker"); |
---|
575 | if (m_req_worker == NULL){ |
---|
576 | fprintf(stderr,"Configuration error: req.worker not found\n"); |
---|
577 | return 2; |
---|
578 | } |
---|
579 | else { |
---|
580 | req_worker=atoi(m_req_worker->value); |
---|
581 | //freeMap(&m_req_worker); |
---|
582 | if (req_worker == 0){ |
---|
583 | fprintf(stderr,"Configuration error: req.worker\n"); |
---|
584 | return 2; |
---|
585 | } |
---|
586 | } |
---|
587 | |
---|
588 | int async_worker; |
---|
589 | map *m_async_worker = getMapFromMaps (conf, "server", "async_worker"); |
---|
590 | if (m_async_worker == NULL){ |
---|
591 | fprintf(stderr,"Configuration error: async_worker not found"); |
---|
592 | return 2; |
---|
593 | } |
---|
594 | else { |
---|
595 | async_worker = atoi(m_async_worker->value); |
---|
596 | //freeMap(&m_async_worker); |
---|
597 | if (async_worker == 0){ |
---|
598 | fprintf(stderr,"Configuration error: req_worker"); |
---|
599 | return 2; |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | int max_requests; |
---|
604 | map *m_max_requests = getMapFromMaps (conf, "server", "max_requests"); |
---|
605 | if (m_max_requests == NULL){ |
---|
606 | fprintf(stderr,"Configuration error: max_requests"); |
---|
607 | return 2; |
---|
608 | } |
---|
609 | else { |
---|
610 | max_requests = atoi(m_max_requests->value); |
---|
611 | //freeMap(&m_max_requests); |
---|
612 | if (max_requests == 0){ |
---|
613 | fprintf(stderr,"Configuration error: max_requests"); |
---|
614 | return 2; |
---|
615 | } |
---|
616 | } |
---|
617 | |
---|
618 | map *m_listen = getMapFromMaps (conf, "server", "listen"); |
---|
619 | char *listen; |
---|
620 | if (m_listen == NULL){ |
---|
621 | fprintf(stderr,"Configuration error: listen not found"); |
---|
622 | return 2; |
---|
623 | } |
---|
624 | else { |
---|
625 | listen = (char *)malloc((strlen(m_listen->value) +1)*sizeof(char*)); |
---|
626 | strncpy(listen,m_listen->value,strlen(m_listen->value)); |
---|
627 | listen[strlen(m_listen->value)] = '\0'; |
---|
628 | //freeMap(&m_listen); |
---|
629 | } |
---|
630 | int listen_owner; |
---|
631 | map *m_listen_owner = getMapFromMaps (conf, "server", "listen_owner"); |
---|
632 | if (m_listen_owner == NULL){ |
---|
633 | fprintf(stderr,"Configuration error: listen_owner"); |
---|
634 | return 2; |
---|
635 | } |
---|
636 | else { |
---|
637 | listen_owner = atoi(m_listen_owner->value); |
---|
638 | //freeMap(&m_listen_owner); |
---|
639 | if (listen_owner == 0){ |
---|
640 | fprintf(stderr,"Configuration error: listen_owner"); |
---|
641 | return 2; |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | int listen_group; |
---|
646 | map *m_listen_group = getMapFromMaps (conf, "server", "listen_group"); |
---|
647 | if (m_listen_group == NULL){ |
---|
648 | fprintf(stderr,"Configuration error: listen_group"); |
---|
649 | return 2; |
---|
650 | } |
---|
651 | else { |
---|
652 | listen_group = atoi(m_listen_group->value); |
---|
653 | //freeMap(&m_listen_group); |
---|
654 | if (listen_group == 0){ |
---|
655 | fprintf(stderr,"Configuration error: listen_group"); |
---|
656 | return 2; |
---|
657 | } |
---|
658 | } |
---|
659 | |
---|
660 | char * listen_mode; |
---|
661 | map *m_listen_mode = getMapFromMaps (conf, "server", "listen_mode"); |
---|
662 | if (m_listen_mode == NULL){ |
---|
663 | fprintf(stderr,"Configuration error: listen_mode"); |
---|
664 | return 2; |
---|
665 | } |
---|
666 | else { |
---|
667 | listen_mode = (char *)malloc((strlen(m_listen_mode->value) +1)*sizeof(char*)); |
---|
668 | strncpy(listen_mode,m_listen_mode->value,strlen(m_listen_mode->value)); |
---|
669 | listen_mode[strlen(m_listen_mode->value)] = '\0'; |
---|
670 | //freeMap(&m_listen_mode); |
---|
671 | } |
---|
672 | |
---|
673 | int listen_queue; |
---|
674 | map *m_listen_queue = getMapFromMaps (conf, "server", "listen_queue"); |
---|
675 | if (m_listen_queue == NULL){ |
---|
676 | fprintf(stderr,"Configuration error: listen_queue"); |
---|
677 | return 2; |
---|
678 | } |
---|
679 | else { |
---|
680 | listen_queue = atoi(m_listen_queue->value); |
---|
681 | //freeMap(&m_listen_queue); |
---|
682 | if (listen_queue == 0){ |
---|
683 | fprintf(stderr,"Configuration error: listen_queue"); |
---|
684 | return 2; |
---|
685 | } |
---|
686 | } |
---|
687 | |
---|
688 | int id_user; |
---|
689 | map *m_user = getMapFromMaps (conf, "server", "uid"); |
---|
690 | if (m_user == NULL){ |
---|
691 | fprintf(stderr,"Configuration error: id_user"); |
---|
692 | return 2; |
---|
693 | } |
---|
694 | else { |
---|
695 | id_user = atoi(m_user->value); |
---|
696 | //freeMap(&m_user); |
---|
697 | if (id_user == 0){ |
---|
698 | fprintf(stderr,"Configuration error: id_user"); |
---|
699 | return 2; |
---|
700 | } |
---|
701 | } |
---|
702 | |
---|
703 | |
---|
704 | int id_group; |
---|
705 | map *m_group = getMapFromMaps (conf, "server", "gid"); |
---|
706 | if (m_group == NULL){ |
---|
707 | fprintf(stderr,"Configuration error: gid"); |
---|
708 | return 2; |
---|
709 | } |
---|
710 | else { |
---|
711 | id_group = atoi(m_group->value); |
---|
712 | //freeMap(&m_group); |
---|
713 | if (id_group == 0){ |
---|
714 | fprintf(stderr,"Configuration error: id_group"); |
---|
715 | return 2; |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | |
---|
720 | char * amqp_host; |
---|
721 | map * m_amqp_host = getMapFromMaps (conf, "rabbitmq", "host"); |
---|
722 | if (m_amqp_host == NULL){ |
---|
723 | fprintf(stderr,"Configuration error: [rabbitmq] host"); |
---|
724 | return 2; |
---|
725 | } |
---|
726 | else { |
---|
727 | amqp_host = (char *)malloc((strlen(m_amqp_host->value) +1)*sizeof(char*)); |
---|
728 | strncpy(amqp_host,m_amqp_host->value,strlen(m_amqp_host->value)); |
---|
729 | amqp_host[strlen(m_amqp_host->value)] = '\0'; |
---|
730 | } |
---|
731 | |
---|
732 | int amqp_port; |
---|
733 | map *m_amqp_port = getMapFromMaps (conf, "rabbitmq", "port"); |
---|
734 | if (m_amqp_port == NULL){ |
---|
735 | fprintf(stderr,"Configuration error: [rabbitmq] port"); |
---|
736 | return 2; |
---|
737 | } |
---|
738 | else { |
---|
739 | amqp_port = atoi(m_amqp_port->value); |
---|
740 | if (amqp_port == 0){ |
---|
741 | fprintf(stderr,"Configuration error: [rabbitmq] port"); |
---|
742 | return 2; |
---|
743 | } |
---|
744 | } |
---|
745 | |
---|
746 | char * amqp_user; |
---|
747 | map * m_amqp_user = getMapFromMaps (conf, "rabbitmq", "user"); |
---|
748 | if (m_amqp_user == NULL){ |
---|
749 | fprintf(stderr,"Configuration error: [rabbitmq] user"); |
---|
750 | return 2; |
---|
751 | } |
---|
752 | else { |
---|
753 | amqp_user = (char *)malloc((strlen(m_amqp_user->value) +1)*sizeof(char*)); |
---|
754 | strncpy(amqp_user,m_amqp_user->value,strlen(m_amqp_user->value)); |
---|
755 | amqp_user[strlen(m_amqp_user->value)] = '\0'; |
---|
756 | } |
---|
757 | |
---|
758 | char * amqp_passwd; |
---|
759 | map * m_amqp_passwd = getMapFromMaps (conf, "rabbitmq", "passwd"); |
---|
760 | if (m_amqp_passwd == NULL){ |
---|
761 | fprintf(stderr,"Configuration error: [rabbitmq] passwd"); |
---|
762 | return 2; |
---|
763 | } |
---|
764 | else { |
---|
765 | amqp_passwd = (char *)malloc((strlen(m_amqp_passwd->value) +1)*sizeof(char*)); |
---|
766 | strncpy(amqp_passwd,m_amqp_passwd->value,strlen(m_amqp_passwd->value)); |
---|
767 | amqp_passwd[strlen(m_amqp_passwd->value)] = '\0'; |
---|
768 | } |
---|
769 | |
---|
770 | char * amqp_exchange; |
---|
771 | map * m_amqp_exchange = getMapFromMaps (conf, "rabbitmq", "exchange"); |
---|
772 | if (m_amqp_exchange == NULL){ |
---|
773 | fprintf(stderr,"Configuration error: [rabbitmq] exchange"); |
---|
774 | return 2; |
---|
775 | } |
---|
776 | else { |
---|
777 | amqp_exchange = (char *)malloc((strlen(m_amqp_exchange->value) +1)*sizeof(char*)); |
---|
778 | strncpy(amqp_exchange,m_amqp_exchange->value,strlen(m_amqp_exchange->value)); |
---|
779 | amqp_exchange[strlen(m_amqp_exchange->value)] = '\0'; |
---|
780 | } |
---|
781 | |
---|
782 | char * amqp_routingkey; |
---|
783 | map * m_amqp_routingkey = getMapFromMaps (conf, "rabbitmq", "routingkey"); |
---|
784 | if (m_amqp_routingkey == NULL){ |
---|
785 | fprintf(stderr,"Configuration error: [amqp] routingkey"); |
---|
786 | return 2; |
---|
787 | } |
---|
788 | else { |
---|
789 | amqp_routingkey = (char *)malloc((strlen(m_amqp_routingkey->value) +1)*sizeof(char*)); |
---|
790 | strncpy(amqp_routingkey,m_amqp_routingkey->value,strlen(m_amqp_routingkey->value)); |
---|
791 | amqp_routingkey[strlen(m_amqp_routingkey->value)] = '\0'; |
---|
792 | } |
---|
793 | |
---|
794 | char * amqp_queue; |
---|
795 | map * m_amqp_queue = getMapFromMaps (conf, "rabbitmq", "queue"); |
---|
796 | if (m_amqp_queue == NULL){ |
---|
797 | fprintf(stderr,"Configuration error: [rabbitmq] queue"); |
---|
798 | return 2; |
---|
799 | } |
---|
800 | else { |
---|
801 | amqp_queue = (char *)malloc((strlen(m_amqp_queue->value) +1)*sizeof(char*)); |
---|
802 | strncpy(amqp_queue,m_amqp_queue->value,strlen(m_amqp_queue->value)); |
---|
803 | amqp_queue[strlen(m_amqp_queue->value)] = '\0'; |
---|
804 | } |
---|
805 | /* |
---|
806 | char * status_user; |
---|
807 | map * m_status_user = getMapFromMaps (conf, "status", "user"); |
---|
808 | if (m_status_user == NULL){ |
---|
809 | fprintf(stderr,"Configuration error: [status] user"); |
---|
810 | return 2; |
---|
811 | } |
---|
812 | else { |
---|
813 | status_user = (char *)malloc((strlen(m_status_user->value) +1)*sizeof(char*)); |
---|
814 | strncpy(status_user,m_status_user->value,strlen(m_status_user->value)); |
---|
815 | status_user[strlen(m_status_user->value)] = '\0'; |
---|
816 | } |
---|
817 | |
---|
818 | |
---|
819 | char * status_passwd; |
---|
820 | map * m_status_passwd = getMapFromMaps (conf, "status", "passwd"); |
---|
821 | if (m_status_passwd == NULL){ |
---|
822 | fprintf(stderr,"Configuration error: [status] passwd"); |
---|
823 | return 2; |
---|
824 | } |
---|
825 | else { |
---|
826 | status_passwd = (char *)malloc((strlen(m_status_passwd->value) +1)*sizeof(char*)); |
---|
827 | strncpy(status_passwd,m_status_passwd->value,strlen(m_status_passwd->value)); |
---|
828 | status_passwd[strlen(m_status_passwd->value)] = '\0'; |
---|
829 | } |
---|
830 | |
---|
831 | char * status_bdd; |
---|
832 | map * m_status_bdd = getMapFromMaps (conf, "status", "bdd"); |
---|
833 | if (m_status_bdd == NULL){ |
---|
834 | fprintf(stderr,"Configuration error: [status] bdd"); |
---|
835 | return 2; |
---|
836 | } |
---|
837 | else { |
---|
838 | status_bdd = (char *)malloc((strlen(m_status_bdd->value) +1)*sizeof(char*)); |
---|
839 | strncpy(status_bdd,m_status_bdd->value,strlen(m_status_bdd->value)); |
---|
840 | status_bdd[strlen(m_status_bdd->value)] = '\0'; |
---|
841 | } |
---|
842 | |
---|
843 | char * status_host; |
---|
844 | map * m_status_host = getMapFromMaps (conf, "status", "host"); |
---|
845 | if (m_status_host == NULL){ |
---|
846 | fprintf(stderr,"Configuration error: [status] host"); |
---|
847 | return 2; |
---|
848 | } |
---|
849 | else { |
---|
850 | status_host = (char *)malloc((strlen(m_status_host->value) +1)*sizeof(char*)); |
---|
851 | strncpy(status_host,m_status_host->value,strlen(m_status_host->value)); |
---|
852 | status_host[strlen(m_status_host->value)] = '\0'; |
---|
853 | } |
---|
854 | |
---|
855 | int status_port; |
---|
856 | map *m_status_port = getMapFromMaps (conf, "status", "port"); |
---|
857 | if (m_status_port == NULL){ |
---|
858 | fprintf(stderr,"Configuration error: [status] port"); |
---|
859 | return 2; |
---|
860 | } |
---|
861 | else { |
---|
862 | status_port = atoi(m_status_port->value); |
---|
863 | if (status_port == 0){ |
---|
864 | fprintf(stderr,"Configuration error: [status] port"); |
---|
865 | return 2; |
---|
866 | } |
---|
867 | } |
---|
868 | */ |
---|
869 | int sock = FCGX_OpenSocket(listen, listen_queue); |
---|
870 | init_services_conf (rootDir,regDir); |
---|
871 | |
---|
872 | ret = chown(listen, listen_owner, listen_group); |
---|
873 | if (ret != 0){ |
---|
874 | fprintf(stderr,"Change owner error on : %s\n",listen); |
---|
875 | return 3; |
---|
876 | } |
---|
877 | /* |
---|
878 | ret = setgid(id_group); |
---|
879 | if (ret != 0){ |
---|
880 | fprintf(stderr,"Change gid error\n"); |
---|
881 | return 3; |
---|
882 | } |
---|
883 | |
---|
884 | ret = setuid(id_user); |
---|
885 | if (ret != 0){ |
---|
886 | fprintf(stderr,"Change uid error\n"); |
---|
887 | return 3; |
---|
888 | } |
---|
889 | */ |
---|
890 | |
---|
891 | #ifdef AMQP |
---|
892 | init_amqp(amqp_host,amqp_port,amqp_user, amqp_passwd, amqp_exchange, amqp_routingkey,amqp_queue); |
---|
893 | #endif |
---|
894 | |
---|
895 | int fork_status = fork(); |
---|
896 | if (fork_status == 0){ |
---|
897 | //child |
---|
898 | int master_sync= getpid(); |
---|
899 | fprintf(stderr,"Master sync%d\n",getpid()); |
---|
900 | FCGX_Init(); |
---|
901 | FCGX_Request request; |
---|
902 | FCGX_InitRequest(&request, sock, 0); |
---|
903 | int i; |
---|
904 | int count_request = 0; |
---|
905 | for (i = 0; i< req_worker; i++){ |
---|
906 | fork_status = fork(); |
---|
907 | if (fork_status == 0){ |
---|
908 | fprintf(stderr,"child sync %d \n",getpid()); |
---|
909 | fflush(stderr); |
---|
910 | break; |
---|
911 | } |
---|
912 | } |
---|
913 | while(1){ |
---|
914 | /* mode synchrone */ |
---|
915 | if (master_sync != getpid()){ |
---|
916 | while(FCGX_Accept_r(&request) == 0){ |
---|
917 | process(&request); |
---|
918 | count_request ++; |
---|
919 | if (count_request >= max_requests){ |
---|
920 | fprintf(stderr,"Max request stop process\n"); |
---|
921 | fflush(stderr); |
---|
922 | exit(0); |
---|
923 | } |
---|
924 | } |
---|
925 | } |
---|
926 | else { |
---|
927 | wait(0); |
---|
928 | fprintf(stderr,"Master sync %d\n",getpid()); |
---|
929 | fprintf(stderr,"New sync Child\n"); |
---|
930 | fflush(stderr); |
---|
931 | fork(); |
---|
932 | } |
---|
933 | } |
---|
934 | } |
---|
935 | while(1); |
---|
936 | |
---|
937 | return 0; |
---|
938 | } |
---|
939 | |
---|