Ignore:
Timestamp:
Oct 7, 2015, 8:41:43 AM (9 years ago)
Author:
david
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PublicaMundi_David_integration_01-devel/zoo-project/zoo-kernel/zoo_loader.c

    r677 r741  
    1 /*
     1/**
    22 * Author : Gérald FENOY
    33 *
     
    3030 */
    3131#ifndef WIN32
     32/*
    3233#include "fcgio.h"
    33 #include "fcgi_config.h"
    34 #include "fcgi_stdio.h"
    35 #endif
     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>
    3642#include <sys/types.h>
    3743#include <unistd.h>
    3844#include "service_internal.h"
    39 #include "response_print.h"
    40 #include "zoo_zcfg.h"
    41 
    4245#ifdef WIN32
    4346#include "windows.h"
     
    4548#endif
    4649
    47 extern "C" {
     50extern "C"
     51{
    4852#include "cgic.h"
    4953#include <libxml/tree.h>
     
    5256#include <libxml/xpath.h>
    5357#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
    5466}
    55 
    56 #include "service_internal.h"
     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"
    5773#include "request_parser.h"
    58 
    59 int runRequest(map**);
     74void
     75loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
     76                   maps ** inputs, maps ** ioutputs, int *eres,FCGX_Stream *out, FCGX_Stream *err);
     77
     78xmlXPathObjectPtr extractFromDoc (xmlDocPtr, const char *);
     79int runRequest (map **,struct cgi_env **,FCGX_Request *);
    6080
    6181using namespace std;
     
    6888#endif
    6989
    70 /**
    71  * Main entry point for cgic.
    72  * @return 0 on sucess.
    73  */
    74 int cgiMain(){
    75   /**
    76    * We'll use cgiOut as the default output (stdout) to produce plain text
    77    * response.
    78    */
    79   dup2(fileno(cgiOut),fileno(stdout));
     90
     91static 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
     102static int counts[THREAD_COUNT];
     103
     104
     105int 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);
    80199#ifdef DEBUG
    81   fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
    82   fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
    83   fflush(cgiOut);
    84   fprintf (stderr, "Addr:%s\n", cgiRemoteAddr);
    85   fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0);
    86   fprintf (stderr, "Request: %s\n", cgiQueryString);
    87   fprintf (stderr, "ContentType: %s\n", cgiContentType);
    88   fprintf (stderr, "ContentLength: %d\n", cgiContentLength);
    89   fflush(stderr);
    90 #endif
    91 
    92 
    93   init_services_conf ("/var/www/zoo-wps/cgi-bin","/var/www/zoo-wps/registry/");
    94 
    95 
    96   char *strQuery=NULL;
    97   if(cgiQueryString!=NULL)
    98     strQuery=zStrdup(cgiQueryString);
    99   map* tmpMap=NULL;
    100 
    101   if(strncmp(cgiContentType,"text/xml",8)==0 ||
    102      strncasecmp(cgiRequestMethod,"post",4)==0){
    103     if(cgiContentLength==0){
    104        char *buffer=new char[2];
    105        char *res=NULL;
    106        int r=0;
    107        while((r=fread(buffer,sizeof(char),1,cgiIn))){
    108          buffer[1]=0;
    109          if(res==NULL){
    110            res=(char*)malloc(2*sizeof(char));
    111            sprintf(res,"%s",buffer);
    112          }
    113          else{
    114            res=(char*)realloc(res,(cgiContentLength+2)*sizeof(char));
    115            memcpy(res + cgiContentLength, buffer, sizeof(char));
    116            res[cgiContentLength+1]=0;
    117          }
    118          cgiContentLength+=r;
    119        }
    120        delete[] buffer;
    121        if(res!=NULL && (strQuery==NULL || strlen(strQuery)==0))
    122          tmpMap=createMap("request",res);
    123        if(res!=NULL)
    124          free(res);
    125     }else{
    126       char *buffer=new char[cgiContentLength+1];
    127       if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)>0){
    128         buffer[cgiContentLength]=0;
    129         tmpMap=createMap("request",buffer);
    130       }else{
    131         buffer[0]=0;
    132         char **array, **arrayStep;
    133         if (cgiFormEntries(&array) != cgiFormSuccess) {
    134           return 1;
    135         }
    136         arrayStep = array;
    137         while (*arrayStep) {
    138           char *ivalue=new char[cgiContentLength];
    139           cgiFormStringNoNewlines(*arrayStep, ivalue, cgiContentLength);
    140           char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+2)*sizeof(char));       
    141           sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
    142 
    143           if(strlen(buffer)==0){               
    144             sprintf(buffer,"%s",tmpValueFinal);
    145           }else{               
    146             char *tmp=zStrdup(buffer);
    147             sprintf(buffer,"%s&%s",tmp,tmpValueFinal);
    148             free(tmp);
    149           }       
    150           free(tmpValueFinal);
     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        {
    151216#ifdef DEBUG
    152           fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue);
    153 #endif
    154           delete[]ivalue;
    155           arrayStep++;
    156         }       
    157         if(tmpMap!=NULL)
    158           addToMap(tmpMap,"request",buffer);
    159         else
    160           tmpMap=createMap("request",buffer);
    161       }
    162       delete[]buffer;
    163     }   
    164   }
    165   else{
     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);
    166230#ifdef DEBUG
    167     dumpMap(tmpMap);
    168 #endif
    169     char **array, **arrayStep;
    170     if (cgiFormEntries(&array) != cgiFormSuccess) {
    171       return 1;
    172     }
    173     arrayStep = array;
    174     while (*arrayStep) {
    175       char *value=new char[cgiContentLength];
    176       cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
    177 #ifdef DEBUG
    178       fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
    179 #endif
     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
    180243#ifdef WIN32
    181       char* tmp = url_decode(value);
    182       if(tmpMap!=NULL)         
    183         addToMap(tmpMap,*arrayStep,tmp);
    184       else             
    185         tmpMap=createMap(*arrayStep,tmp);
    186       free(tmp);
    187 #else
    188       if(tmpMap!=NULL)
    189         addToMap(tmpMap,*arrayStep,value);
    190       else
    191         tmpMap=createMap(*arrayStep,value);
    192 #endif 
    193       arrayStep++;
    194       delete[]value;
    195     }
    196     cgiStringArrayFree(array);
    197   }
    198 
    199 #ifdef WIN32
    200   map *tmpReq=getMap(tmpMap,"rfile");
    201   if(tmpReq!=NULL){
    202     FILE *lf=fopen(tmpReq->value,"r");
    203     fseek(lf,0,SEEK_END);
    204     long flen=ftell(lf);
    205     fseek(lf,0,SEEK_SET);
    206     char *buffer=(char*)malloc((flen+1)*sizeof(char));
    207     fread(buffer,flen,1,lf);
    208     fclose(lf);
    209     addToMap(tmpMap,"request",buffer);
    210     free(buffer);
    211     cgiContentLength=flen+9;
    212   }
     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        }
    213258#endif
    214259  /**
     
    217262   * one.
    218263   */
    219   if(strncasecmp(cgiRequestMethod,"post",4)==0 ||
    220      (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)
     264      if (strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0 ||
     265          (count (tmpMap) == 1 && strncmp (tmpMap->value, "<", 1) == 0)
    221266#ifdef WIN32
    222      ||tmpReq!=NULL
    223 #endif
    224      ){
     267          || tmpReq != NULL
     268#endif
     269        )
     270        {
    225271    /**
    226272     * Store the original XML request in xrequest map
    227273     */
    228     map* t1=getMap(tmpMap,"request");
    229     if(t1!=NULL && strncasecmp(t1->value,"<",1)==0) {
    230       addToMap(tmpMap,"xrequest",t1->value);
    231       xmlInitParser();
    232       xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
     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
     488int
     489main (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)
    233499      {
    234         xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*");
    235         if(reqptr!=NULL){
    236           xmlNodeSet* req=reqptr->nodesetval;
    237           if(req!=NULL && req->nodeNr==1){
    238             addToMap(tmpMap,"soap","true");
    239             for(int k=0;k < req->nodeNr;k++){
    240               xmlDocSetRootElement(doc, req->nodeTab[k]);
    241               xmlChar *xmlbuff;
    242               int buffersize;
    243               xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1);
    244               addToMap(tmpMap,"xrequest",(char*)xmlbuff);
    245               xmlFree(xmlbuff);
    246             }
    247           }
    248           xmlXPathFreeObject(reqptr);
    249         }
     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 ();
    250523      }
    251524
    252       xmlNodePtr cur = xmlDocGetRootElement(doc);
    253       char *tval;
    254       tval=NULL;
    255       tval = (char*) xmlGetProp(cur,BAD_CAST "service");
    256       if(tval!=NULL){
    257         addToMap(tmpMap,"service",tval);
    258         xmlFree(tval);
    259       }
    260       tval=NULL;
    261       tval = (char*) xmlGetProp(cur,BAD_CAST "language");
    262       if(tval!=NULL){
    263         addToMap(tmpMap,"language",tval);
    264         xmlFree(tval);
    265       }
    266       const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"};
    267       for(int j=0;j<3;j++){
    268         char tt[128];
    269         sprintf(tt,"/*[local-name()='%s']",requests[j]);
    270         xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
    271         if(reqptr!=NULL){
    272           xmlNodeSet* req=reqptr->nodesetval;
    273 #ifdef DEBUG
    274           fprintf(stderr,"%i",req->nodeNr);
    275 #endif
    276           if(req!=NULL && req->nodeNr==1){
    277             if(t1->value!=NULL)
    278               free(t1->value);
    279             t1->value=zStrdup(requests[j]);
    280             j=2;
    281           }
    282           xmlXPathFreeObject(reqptr);
    283         }
    284       }
    285       if(strncasecmp(t1->value,"GetCapabilities",15)==0){
    286         xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
    287         xmlNodeSet* vers=versptr->nodesetval;
    288         if(vers!=NULL && vers->nodeTab!=NULL && vers->nodeTab[0]!=NULL){
    289           xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
    290           addToMap(tmpMap,"version",(char*)content);
    291           xmlFree(content);
    292         }
    293         if(cur->ns){
    294           addToMap(tmpMap,"wps_schemas",(char*)cur->ns->href);
    295           int j=0;
    296           for(j=0;j<2;j++)
    297             if(strncasecmp(schemas[j][2],(char*)cur->ns->href,strlen(schemas[j][2]))==0){
    298               char vers[6];
    299               sprintf(vers,"%d.0.0",j+1);
    300               addToMap(tmpMap,"version",(char*)vers);
    301             }
    302         }
    303         xmlXPathFreeObject(versptr);
    304       }else{
    305         tval=NULL;
    306         tval = (char*) xmlGetProp(cur,BAD_CAST "version");
    307         if(tval!=NULL){
    308           addToMap(tmpMap,"version",tval);
    309           xmlFree(tval);
    310         }
    311         tval = (char*) xmlGetProp(cur,BAD_CAST "language");
    312         if(tval!=NULL){
    313           addToMap(tmpMap,"language",tval);
    314           xmlFree(tval);
    315         }
    316         xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
    317         if(idptr!=NULL){
    318           xmlNodeSet* id=idptr->nodesetval;
    319           if(id!=NULL){
    320             char* identifiers=NULL;
    321             identifiers=(char*)calloc(cgiContentLength,sizeof(char));
    322             identifiers[0]=0;
    323             for(int k=0;k<id->nodeNr;k++){
    324               xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
    325               if(strlen(identifiers)>0){
    326                 char *tmp=zStrdup(identifiers);
    327                 snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
    328                 free(tmp);
    329               }
    330               else{
    331                 snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
    332               }
    333               xmlFree(content);
    334             }
    335             xmlXPathFreeObject(idptr);
    336             addToMap(tmpMap,"Identifier",identifiers);
    337             free(identifiers);
    338           }
    339         }else{
    340           idptr=extractFromDoc(doc,"/*/*[local-name()='JobID']");
    341           if(idptr!=NULL){
    342             xmlNodeSet* id=idptr->nodesetval;
    343             if(id!=NULL){
    344               char* identifiers=NULL;
    345               identifiers=(char*)calloc(cgiContentLength,sizeof(char));
    346               identifiers[0]=0;
    347               for(int k=0;k<id->nodeNr;k++){
    348                 xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
    349                 if(strlen(identifiers)>0){
    350                   char *tmp=zStrdup(identifiers);
    351                   snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
    352                   free(tmp);
    353                 }
    354                 else{
    355                   snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
    356                 }
    357                 xmlFree(content);
    358               }
    359               xmlXPathFreeObject(idptr);
    360               addToMap(tmpMap,"JobID",identifiers);
    361               free(identifiers);
    362             }
    363         }
    364         }
    365       }
    366       xmlFreeDoc(doc);
    367       xmlCleanupParser();
    368     }else{
    369       freeMap(&tmpMap);
    370       free(tmpMap);
    371       tmpMap=createMap("not_valid","true");
    372     }
    373 
    374     char *token,*saveptr;
    375     token=strtok_r(cgiQueryString,"&",&saveptr);
    376     while(token!=NULL){
    377       char *token1,*saveptr1;
    378       char *name=NULL;
    379       char *value=NULL;
    380       token1=strtok_r(token,"=",&saveptr1);
    381       while(token1!=NULL){
    382         if(name==NULL)
    383           name=zStrdup(token1);
    384         else
    385           value=zStrdup(token1);
    386         token1=strtok_r(NULL,"=",&saveptr1);
    387       }   
    388       //addToMap(tmpMap,name,value);
    389           /* knut: strtok(_r) ignores delimiter bytes at start and end of string;
    390            * it will return non-empty string or NULL, e.g. "metapath=" yields value=NULL.
    391            * This modification sets value="" instead of NULL.
    392            */
    393           addToMap(tmpMap,name, value != NULL ? value : "");
    394 
    395       free(name);
    396       free(value);
    397       name=NULL;
    398       value=NULL;
    399       token=strtok_r(NULL,"&",&saveptr);
    400     }
    401    
    402   }
    403 
    404   if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){
    405     map* tmp=getMap(tmpMap,"dataInputs");
    406     if(tmp!=NULL){
    407       addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11);
    408     }
    409   }
    410 
    411   if(strQuery!=NULL)
    412     free(strQuery);
    413 
     525
     526  maps *conf;
     527  conf = (maps *) malloc (MAPS_SIZE);
    414528 
    415   runRequest(&tmpMap);
    416 
    417   if(tmpMap!=NULL){
    418     freeMap(&tmpMap);
    419     free(tmpMap);
    420   }
     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
     559char *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 
    421937  return 0;
    422 
    423938}
     939 
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png