Ignore:
Timestamp:
Apr 9, 2015, 5:23:06 AM (9 years ago)
Author:
djay
Message:

Major update. Creation of a basic parsing api. Call validateRequest after fork if any.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-kernel/zoo_service_loader.c

    r620 r621  
    5454
    5555#include "service_internal.h"
     56#include "request_parser.h"
    5657
    5758#ifdef USE_PYTHON
     
    179180}
    180181
    181 /**
    182  * Create (or append to) an array valued maps value = "["",""]"
    183  *
    184  * @param m the conf maps containing the main.cfg settings
    185  * @param mo the map to update
    186  * @param mi the map to append
    187  * @param elem the elements containing default definitions
    188  * @return 0 on success, -1 on failure
    189  */
    190 int
    191 appendMapsToMaps (maps * m, maps * mo, maps * mi, elements * elem)
    192 {
    193   maps *tmpMaps = getMaps (mo, mi->name);
    194   map *tmap = getMapType (tmpMaps->content);
    195   elements *el = getElements (elem, mi->name);
    196   int hasEl = 1;
    197   if (el == NULL)
    198     hasEl = -1;
    199   if (tmap == NULL)
    200     {
    201       if (hasEl > 0)
    202         tmap = getMapType (el->defaults->content);
    203     }
    204 
    205   map *testMap = NULL;
    206   if (hasEl > 0)
    207     {
    208       testMap = getMap (el->content, "maxOccurs");
    209     }
    210   else
    211     {
    212       testMap = createMap ("maxOccurs", "unbounded");
    213     }
    214 
    215   if (testMap != NULL)
    216     {
    217       if (strncasecmp (testMap->value, "unbounded", 9) != 0
    218           && atoi (testMap->value) > 1)
    219         {
    220           addMapsArrayToMaps (&mo, mi, tmap->name);
    221           map* nb=getMapFromMaps(mo,mi->name,"length");
    222           if (nb!=NULL && atoi(nb->value)>atoi(testMap->value))
    223             {
    224               char emsg[1024];
    225               sprintf (emsg,
    226                        _("The maximum allowed occurrences for <%s> (%i) was exceeded."),
    227                        mi->name, atoi (testMap->value));
    228               errorException (m, emsg, "InternalError", NULL);
    229               return -1;
    230             }
    231         }
    232       else
    233         {
    234           if (strncasecmp (testMap->value, "unbounded", 9) == 0)
    235             {
    236               if (hasEl < 0)
    237                 {
    238                   freeMap (&testMap);
    239                   free (testMap);
    240                 }
    241               if (addMapsArrayToMaps (&mo, mi, tmap->name) < 0)
    242                 {
    243                   char emsg[1024];
    244                   map *tmpMap = getMap (mi->content, "length");
    245                   sprintf (emsg,
    246                            _
    247                            ("ZOO-Kernel was unable to load your data for %s position %s."),
    248                            mi->name, tmpMap->value);
    249                   errorException (m, emsg, "InternalError", NULL);
    250                   return -1;
    251                 }
    252             }
    253           else
    254             {
    255               char emsg[1024];
    256               sprintf (emsg,
    257                        _
    258                        ("The maximum allowed occurrences for <%s> is one."),
    259                        mi->name);
    260               errorException (m, emsg, "InternalError", NULL);
    261               return -1;
    262             }
    263         }
    264     }
    265   return 0;
    266 }
    267182
    268183/**
     
    495410  (void) closedir (dirp);
    496411  return 1;
    497 }
    498 
    499 /**
    500  * Apply XPath Expression on XML document.
    501  *
    502  * @param doc the XML Document
    503  * @param search the XPath expression
    504  * @return xmlXPathObjectPtr containing the resulting nodes set
    505  */
    506 xmlXPathObjectPtr
    507 extractFromDoc (xmlDocPtr doc, const char *search)
    508 {
    509   xmlXPathContextPtr xpathCtx;
    510   xmlXPathObjectPtr xpathObj;
    511   xpathCtx = xmlXPathNewContext (doc);
    512   xpathObj = xmlXPathEvalExpression (BAD_CAST search, xpathCtx);
    513   xmlXPathFreeContext (xpathCtx);
    514   return xpathObj;
    515412}
    516413
     
    17221619  maps *request_input_real_format = NULL;
    17231620  maps *tmpmaps = request_input_real_format;
    1724   map *postRequest = NULL;
    1725   postRequest = getMap (request_inputs, "xrequest");
    1726   if (postRequest == NULLMAP)
    1727     {
    1728     /**
    1729      * Parsing outputs provided as KVP
    1730      */
    1731       r_inputs = NULL;
    1732 #ifdef DEBUG
    1733       fprintf (stderr, "OUTPUT Parsing ... \n");
    1734 #endif
    1735       r_inputs = getMap (request_inputs, "ResponseDocument");
    1736       if (r_inputs == NULL)
    1737         r_inputs = getMap (request_inputs, "RawDataOutput");
    1738 
    1739 #ifdef DEBUG
    1740       fprintf (stderr, "OUTPUT Parsing ... \n");
    1741 #endif
    1742       if (r_inputs != NULL)
    1743         {
    1744 #ifdef DEBUG
    1745           fprintf (stderr, "OUTPUT Parsing start now ... \n");
    1746 #endif
    1747           char cursor_output[10240];
    1748           char *cotmp = zStrdup (r_inputs->value);
    1749           snprintf (cursor_output, 10240, "%s", cotmp);
    1750           free (cotmp);
    1751           j = 0;
    1752 
    1753       /**
    1754        * Put each Output into the outputs_as_text array
    1755        */
    1756           char *pToken;
    1757           maps *tmp_output = NULL;
    1758 #ifdef DEBUG
    1759           fprintf (stderr, "OUTPUT [%s]\n", cursor_output);
    1760 #endif
    1761           pToken = strtok (cursor_output, ";");
    1762           char **outputs_as_text = (char **) malloc (128 * sizeof (char *));
    1763           if (outputs_as_text == NULL)
    1764             {
    1765               return errorException (m, _("Unable to allocate memory"),
    1766                                      "InternalError", NULL);
    1767             }
    1768           i = 0;
    1769           while (pToken != NULL)
    1770             {
    1771 #ifdef DEBUG
    1772               fprintf (stderr, "***%s***\n", pToken);
    1773               fflush (stderr);
    1774               fprintf (stderr, "***%s***\n", pToken);
    1775 #endif
    1776               outputs_as_text[i] =
    1777                 (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
    1778               if (outputs_as_text[i] == NULL)
    1779                 {
    1780                   return errorException (m, _("Unable to allocate memory"),
    1781                                          "InternalError", NULL);
    1782                 }
    1783               snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s",
    1784                         pToken);
    1785               pToken = strtok (NULL, ";");
    1786               i++;
    1787             }
    1788           for (j = 0; j < i; j++)
    1789             {
    1790               char *tmp = zStrdup (outputs_as_text[j]);
    1791               free (outputs_as_text[j]);
    1792               char *tmpc;
    1793               tmpc = strtok (tmp, "@");
    1794               int k = 0;
    1795               while (tmpc != NULL)
    1796                 {
    1797                   if (k == 0)
    1798                     {
    1799                       if (tmp_output == NULL)
    1800                         {
    1801                           tmp_output = (maps *) malloc (MAPS_SIZE);
    1802                           if (tmp_output == NULL)
    1803                             {
    1804                               return errorException (m,
    1805                                                      _
    1806                                                      ("Unable to allocate memory."),
    1807                                                      "InternalError", NULL);
    1808                             }
    1809                           tmp_output->name = zStrdup (tmpc);
    1810                           tmp_output->content = NULL;
    1811                           tmp_output->next = NULL;
    1812                         }
    1813                     }
    1814                   else
    1815                     {
    1816                       char *tmpv = strstr (tmpc, "=");
    1817                       char tmpn[256];
    1818                       memset (tmpn, 0, 256);
    1819                       strncpy (tmpn, tmpc,
    1820                                (strlen (tmpc) -
    1821                                 strlen (tmpv)) * sizeof (char));
    1822                       tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
    1823 #ifdef DEBUG
    1824                       fprintf (stderr, "OUTPUT DEF [%s]=[%s]\n", tmpn,
    1825                                tmpv + 1);
    1826 #endif
    1827                       if (tmp_output->content == NULL)
    1828                         {
    1829                           tmp_output->content = createMap (tmpn, tmpv + 1);
    1830                           tmp_output->content->next = NULL;
    1831                         }
    1832                       else
    1833                         addToMap (tmp_output->content, tmpn, tmpv + 1);
    1834                     }
    1835                   k++;
    1836 #ifdef DEBUG
    1837                   fprintf (stderr, "***%s***\n", tmpc);
    1838 #endif
    1839                   tmpc = strtok (NULL, "@");
    1840                 }
    1841               if (request_output_real_format == NULL)
    1842                 request_output_real_format = dupMaps (&tmp_output);
    1843               else
    1844                 addMapsToMaps (&request_output_real_format, tmp_output);
    1845               freeMaps (&tmp_output);
    1846               free (tmp_output);
    1847               tmp_output = NULL;
    1848 #ifdef DEBUG
    1849               dumpMaps (tmp_output);
    1850               fflush (stderr);
    1851 #endif
    1852               free (tmp);
    1853             }
    1854           free (outputs_as_text);
    1855         }
    1856 
    1857 
    1858     /**
    1859      * Parsing inputs provided as KVP
    1860      */
    1861       r_inputs = getMap (request_inputs, "DataInputs");
    1862 #ifdef DEBUG
    1863       fprintf (stderr, "DATA INPUTS [%s]\n", r_inputs->value);
    1864 #endif
    1865       char cursor_input[40960];
    1866       if (r_inputs != NULL){
    1867         snprintf (cursor_input, 40960, "%s", r_inputs->value);
    1868         j = 0;
    1869 
    1870         /**
    1871          * Put each DataInputs into the inputs_as_text array
    1872          */
    1873         char *tmp1 = zStrdup (cursor_input);
    1874         char *pToken;
    1875         pToken = strtok (cursor_input, ";");
    1876         if (pToken != NULL && strncasecmp (pToken, tmp1, strlen (tmp1)) == 0)
    1877           {
    1878             char *tmp2 = url_decode (tmp1);
    1879             snprintf (cursor_input, (strlen (tmp2) + 1) * sizeof (char), "%s",
    1880                       tmp2);
    1881             free (tmp2);
    1882             pToken = strtok (cursor_input, ";");
    1883           }
    1884         free (tmp1);
    1885        
    1886         char **inputs_as_text = (char **) malloc (100 * sizeof (char *));
    1887         if (inputs_as_text == NULL)
    1888           {
    1889             return errorException (m, _("Unable to allocate memory."),
    1890                                    "InternalError", NULL);
    1891           }
    1892         i = 0;
    1893         while (pToken != NULL)
    1894           {
    1895 #ifdef DEBUG
    1896             fprintf (stderr, "***%s***\n", pToken);
    1897             fflush (stderr);
    1898             fprintf (stderr, "***%s***\n", pToken);
    1899 #endif
    1900             inputs_as_text[i] =
    1901               (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
    1902             if (inputs_as_text[i] == NULL)
    1903               {
    1904                 return errorException (m, _("Unable to allocate memory."),
    1905                                        "InternalError", NULL);
    1906               }
    1907             snprintf (inputs_as_text[i], strlen (pToken) + 1, "%s", pToken);
    1908             pToken = strtok (NULL, ";");
    1909             i++;
    1910           }
    1911        
    1912         for (j = 0; j < i; j++)
    1913           {
    1914             char *tmp = zStrdup (inputs_as_text[j]);
    1915             free (inputs_as_text[j]);
    1916             char *tmpc;
    1917             tmpc = strtok (tmp, "@");
    1918             while (tmpc != NULL)
    1919               {
    1920 #ifdef DEBUG
    1921                 fprintf (stderr, "***\n***%s***\n", tmpc);
    1922 #endif
    1923                 char *tmpv = strstr (tmpc, "=");
    1924                 char tmpn[256];
    1925                 memset (tmpn, 0, 256);
    1926                 if (tmpv != NULL)
    1927                   {
    1928                     strncpy (tmpn, tmpc,
    1929                              (strlen (tmpc) - strlen (tmpv)) * sizeof (char));
    1930                     tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
    1931                   }
    1932                 else
    1933                   {
    1934                     strncpy (tmpn, tmpc, strlen (tmpc) * sizeof (char));
    1935                     tmpn[strlen (tmpc)] = 0;
    1936                   }
    1937 #ifdef DEBUG
    1938                 fprintf (stderr, "***\n*** %s = %s ***\n", tmpn, tmpv + 1);
    1939 #endif
    1940                 if (tmpmaps == NULL)
    1941                   {
    1942                     tmpmaps = (maps *) malloc (MAPS_SIZE);
    1943                     if (tmpmaps == NULL)
    1944                       {
    1945                         return errorException (m,
    1946                                                _("Unable to allocate memory."),
    1947                                                "InternalError", NULL);
    1948                       }
    1949                     tmpmaps->name = zStrdup (tmpn);
    1950                     if (tmpv != NULL)
    1951                       {
    1952                         char *tmpvf = url_decode (tmpv + 1);
    1953                         tmpmaps->content = createMap ("value", tmpvf);
    1954                         free (tmpvf);
    1955                       }
    1956                     else
    1957                       tmpmaps->content = createMap ("value", "Reference");
    1958                     tmpmaps->next = NULL;
    1959                   }
    1960                 tmpc = strtok (NULL, "@");
    1961                 while (tmpc != NULL)
    1962                   {
    1963 #ifdef DEBUG
    1964                     fprintf (stderr, "*** KVP NON URL-ENCODED \n***%s***\n",
    1965                              tmpc);
    1966 #endif
    1967                     char *tmpv1 = strstr (tmpc, "=");
    1968 #ifdef DEBUG
    1969                     fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n",
    1970                              tmpv1 + 1);
    1971 #endif
    1972                     char tmpn1[1024];
    1973                     memset (tmpn1, 0, 1024);
    1974                     if (tmpv1 != NULL)
    1975                       {
    1976                         strncpy (tmpn1, tmpc, strlen (tmpc) - strlen (tmpv1));
    1977                         tmpn1[strlen (tmpc) - strlen (tmpv1)] = 0;
    1978                         addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
    1979                       }
    1980                     else
    1981                       {
    1982                         strncpy (tmpn1, tmpc, strlen (tmpc));
    1983                         tmpn1[strlen (tmpc)] = 0;
    1984                         map *lmap = getLastMap (tmpmaps->content);
    1985                         char *tmpValue =
    1986                           (char *) malloc ((strlen (tmpv) + strlen (tmpc) + 1) *
    1987                                            sizeof (char));
    1988                         sprintf (tmpValue, "%s@%s", tmpv + 1, tmpc);
    1989                         free (lmap->value);
    1990                         lmap->value = zStrdup (tmpValue);
    1991                         free (tmpValue);
    1992                         tmpc = strtok (NULL, "@");
    1993                         continue;
    1994                       }
    1995 #ifdef DEBUG
    1996                     fprintf (stderr, "*** NAME NON URL-ENCODED \n***%s***\n",
    1997                              tmpn1);
    1998                     fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n",
    1999                              tmpv1 + 1);
    2000 #endif
    2001                     if (strcmp (tmpn1, "xlink:href") != 0)
    2002                       addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
    2003                     else if (tmpv1 != NULL)
    2004                       {
    2005                         char *tmpx2 = url_decode (tmpv1 + 1);
    2006                         if (strncasecmp (tmpx2, "http://", 7) != 0 &&
    2007                             strncasecmp (tmpx2, "ftp://", 6) != 0 &&
    2008                             strncasecmp (tmpx2, "https://", 8) != 0)
    2009                           {
    2010                             char emsg[1024];
    2011                             sprintf (emsg,
    2012                                      _
    2013                                      ("Unable to find a valid protocol to download the remote file %s"),
    2014                                      tmpv1 + 1);
    2015                             errorException (m, emsg, "InternalError", NULL);
    2016                             freeMaps (&m);
    2017                             free (m);
    2018                             free (REQUEST);
    2019                             free (SERVICE_URL);
    2020                             InternetCloseHandle (&hInternet);
    2021                             freeService (&s1);
    2022                             free (s1);
    2023                             return 0;
    2024                           }
    2025 #ifdef DEBUG
    2026                         fprintf (stderr,
    2027                                  "REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",
    2028                                  tmpv1 + 1);
    2029 #endif
    2030                         addToMap (tmpmaps->content, tmpn1, tmpx2);
    2031 #ifndef WIN32
    2032                         if (CHECK_INET_HANDLE (hInternet))
    2033 #endif
    2034                           {
    2035                             if (loadRemoteFile
    2036                                 (&m, &tmpmaps->content, &hInternet, tmpx2) < 0)
    2037                               {
    2038                                 freeMaps (&m);
    2039                                 free (m);
    2040                                 free (REQUEST);
    2041                                 free (SERVICE_URL);
    2042                                 InternetCloseHandle (&hInternet);
    2043                                 freeService (&s1);
    2044                                 free (s1);
    2045                                 return 0;
    2046                               }
    2047                           }
    2048                         free (tmpx2);
    2049                         addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
    2050                       }
    2051                     tmpc = strtok (NULL, "@");
    2052                   }
    2053 #ifdef DEBUG
    2054                 dumpMaps (tmpmaps);
    2055                 fflush (stderr);
    2056 #endif
    2057                 if (request_input_real_format == NULL)
    2058                   request_input_real_format = dupMaps (&tmpmaps);
    2059                 else
    2060                   {
    2061                     maps *testPresence =
    2062                       getMaps (request_input_real_format, tmpmaps->name);
    2063                     if (testPresence != NULL)
    2064                       {
    2065                         elements *elem =
    2066                           getElements (s1->inputs, tmpmaps->name);
    2067                         if (elem != NULL)
    2068                           {
    2069                             if (appendMapsToMaps
    2070                                 (m, request_input_real_format, tmpmaps,
    2071                                  elem) < 0)
    2072                               {
    2073                                 freeMaps (&m);
    2074                                 free (m);
    2075                                 free (REQUEST);
    2076                                 free (SERVICE_URL);
    2077                                 InternetCloseHandle (&hInternet);
    2078                                 freeService (&s1);
    2079                                 free (s1);
    2080                                 return 0;
    2081                               }
    2082                           }
    2083                       }
    2084                     else
    2085                       addMapsToMaps (&request_input_real_format, tmpmaps);
    2086                   }
    2087                 freeMaps (&tmpmaps);
    2088                 free (tmpmaps);
    2089                 tmpmaps = NULL;
    2090                 free (tmp);
    2091               }
    2092           }
    2093         free (inputs_as_text);
    2094       }
    2095     }
    2096   else
    2097     {
    2098     /**
    2099      * Parse XML request
    2100      */
    2101       xmlInitParser ();
    2102 #ifdef DEBUG
    2103       fflush (stderr);
    2104       fprintf (stderr, "BEFORE %s\n", postRequest->value);
    2105       fflush (stderr);
    2106 #endif
    2107       xmlDocPtr doc = xmlParseMemory (postRequest->value, cgiContentLength);
    2108 #ifdef DEBUG
    2109       fprintf (stderr, "AFTER\n");
    2110       fflush (stderr);
    2111 #endif
    2112 
    2113     /**
    2114      * Parse every Input in DataInputs node.
    2115      */
    2116       xmlXPathObjectPtr tmpsptr =
    2117         extractFromDoc (doc, "/*/*/*[local-name()='Input']");
    2118       xmlNodeSet *tmps = tmpsptr->nodesetval;
    2119 #ifdef DEBUG
    2120       fprintf (stderr, "*****%d*****\n", tmps->nodeNr);
    2121 #endif
    2122       for (int k = 0; k < tmps->nodeNr; k++)
    2123         {
    2124           maps *tmpmaps = NULL;
    2125           xmlNodePtr cur = tmps->nodeTab[k];
    2126           if (tmps->nodeTab[k]->type == XML_ELEMENT_NODE)
    2127             {
    2128         /**
    2129          * A specific Input node.
    2130          */
    2131 #ifdef DEBUG
    2132               fprintf (stderr, "= element 0 node \"%s\"\n", cur->name);
    2133 #endif
    2134               xmlNodePtr cur2 = cur->children;
    2135               while (cur2 != NULL)
    2136                 {
    2137                   while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
    2138                     cur2 = cur2->next;
    2139                   if (cur2 == NULL)
    2140                     break;
    2141           /**
    2142            * Indentifier
    2143            */
    2144                   if (xmlStrncasecmp
    2145                       (cur2->name, BAD_CAST "Identifier",
    2146                        xmlStrlen (cur2->name)) == 0)
    2147                     {
    2148                       xmlChar *val =
    2149                         xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
    2150                       if (tmpmaps == NULL)
    2151                         {
    2152                           tmpmaps = (maps *) malloc (MAPS_SIZE);
    2153                           if (tmpmaps == NULL)
    2154                             {
    2155                               return errorException (m,
    2156                                                      _
    2157                                                      ("Unable to allocate memory."),
    2158                                                      "InternalError", NULL);
    2159                             }
    2160                           tmpmaps->name = zStrdup ((char *) val);
    2161                           tmpmaps->content = NULL;
    2162                           tmpmaps->next = NULL;
    2163                         }
    2164                       xmlFree (val);
    2165                     }
    2166           /**
    2167            * Title, Asbtract
    2168            */
    2169                   if (xmlStrncasecmp
    2170                       (cur2->name, BAD_CAST "Title",
    2171                        xmlStrlen (cur2->name)) == 0
    2172                       || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract",
    2173                                          xmlStrlen (cur2->name)) == 0)
    2174                     {
    2175                       xmlChar *val =
    2176                         xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
    2177                       if (tmpmaps == NULL)
    2178                         {
    2179                           tmpmaps = (maps *) malloc (MAPS_SIZE);
    2180                           if (tmpmaps == NULL)
    2181                             {
    2182                               return errorException (m,
    2183                                                      _
    2184                                                      ("Unable to allocate memory."),
    2185                                                      "InternalError", NULL);
    2186                             }
    2187                           tmpmaps->name = zStrdup ("missingIndetifier");
    2188                           tmpmaps->content =
    2189                             createMap ((char *) cur2->name, (char *) val);
    2190                           tmpmaps->next = NULL;
    2191                         }
    2192                       else
    2193                         {
    2194                           if (tmpmaps->content != NULL)
    2195                             addToMap (tmpmaps->content,
    2196                                       (char *) cur2->name, (char *) val);
    2197                           else
    2198                             tmpmaps->content =
    2199                               createMap ((char *) cur2->name, (char *) val);
    2200                         }
    2201 #ifdef DEBUG
    2202                       dumpMaps (tmpmaps);
    2203 #endif
    2204                       xmlFree (val);
    2205                     }
    2206           /**
    2207            * InputDataFormChoice (Reference or Data ?)
    2208            */
    2209                   if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0)
    2210                     {
    2211             /**
    2212              * Get every attribute from a Reference node
    2213              * mimeType, encoding, schema, href, method
    2214              * Header and Body gesture should be added here
    2215              */
    2216 #ifdef DEBUG
    2217                       fprintf (stderr, "REFERENCE\n");
    2218 #endif
    2219                       const char *refs[5] =
    2220                         { "mimeType", "encoding", "schema", "method",
    2221                         "href"
    2222                       };
    2223                       for (int l = 0; l < 5; l++)
    2224                         {
    2225 #ifdef DEBUG
    2226                           fprintf (stderr, "*** %s ***", refs[l]);
    2227 #endif
    2228                           xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]);
    2229                           if (val != NULL && xmlStrlen (val) > 0)
    2230                             {
    2231                               if (tmpmaps->content != NULL)
    2232                                 addToMap (tmpmaps->content, refs[l],
    2233                                           (char *) val);
    2234                               else
    2235                                 tmpmaps->content =
    2236                                   createMap (refs[l], (char *) val);
    2237                               map *ltmp = getMap (tmpmaps->content, "method");
    2238                               if (l == 4)
    2239                                 {
    2240                                   if (!
    2241                                       (ltmp != NULL
    2242                                        && strncmp (ltmp->value, "POST",
    2243                                                    4) == 0)
    2244                                       && CHECK_INET_HANDLE (hInternet))
    2245                                     {
    2246                                       if (loadRemoteFile
    2247                                           (&m, &tmpmaps->content, &hInternet,
    2248                                            (char *) val) != 0)
    2249                                         {
    2250                                           freeMaps (&m);
    2251                                           free (m);
    2252                                           free (REQUEST);
    2253                                           free (SERVICE_URL);
    2254                                           InternetCloseHandle (&hInternet);
    2255                                           freeService (&s1);
    2256                                           free (s1);
    2257                                           return 0;
    2258                                         }
    2259                                     }
    2260                                 }
    2261                             }
    2262 #ifdef DEBUG
    2263                           fprintf (stderr, "%s\n", val);
    2264 #endif
    2265                           xmlFree (val);
    2266                         }
    2267 #ifdef POST_DEBUG
    2268                       fprintf (stderr,
    2269                                "Parse Header and Body from Reference \n");
    2270 #endif
    2271                       xmlNodePtr cur3 = cur2->children;
    2272                       /*      HINTERNET hInternetP;
    2273                          hInternetP=InternetOpen(
    2274                          #ifndef WIN32
    2275                          (LPCTSTR)
    2276                          #endif
    2277                          "ZooWPSClient\0",
    2278                          INTERNET_OPEN_TYPE_PRECONFIG,
    2279                          NULL,NULL, 0); */
    2280                       //hInternet.ihandle[hInternet.nb].header=NULL;
    2281                       while (cur3 != NULL)
    2282                         {
    2283                           while (cur3 != NULL
    2284                                  && cur3->type != XML_ELEMENT_NODE)
    2285                             cur3 = cur3->next;
    2286                           if (cur3 == NULL)
    2287                             break;
    2288                           if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
    2289                               0)
    2290                             {
    2291                               const char *ha[2];
    2292                               ha[0] = "key";
    2293                               ha[1] = "value";
    2294                               int hai;
    2295                               char *has;
    2296                               char *key;
    2297                               for (hai = 0; hai < 2; hai++)
    2298                                 {
    2299                                   xmlChar *val =
    2300                                     xmlGetProp (cur3, BAD_CAST ha[hai]);
    2301 #ifdef POST_DEBUG
    2302                                   fprintf (stderr, "%s = %s\n", ha[hai],
    2303                                            (char *) val);
    2304 #endif
    2305                                   if (hai == 0)
    2306                                     {
    2307                                       key = zStrdup ((char *) val);
    2308                                     }
    2309                                   else
    2310                                     {
    2311                                       has =
    2312                                         (char *)
    2313                                         malloc ((4 + xmlStrlen (val) +
    2314                                                  strlen (key)) *
    2315                                                 sizeof (char));
    2316                                       if (has == NULL)
    2317                                         {
    2318                                           return errorException (m,
    2319                                                                  _
    2320                                                                  ("Unable to allocate memory."),
    2321                                                                  "InternalError",
    2322                                                                  NULL);
    2323                                         }
    2324                                       snprintf (has,
    2325                                                 (3 + xmlStrlen (val) +
    2326                                                  strlen (key)), "%s: %s", key,
    2327                                                 (char *) val);
    2328                                       free (key);
    2329 #ifdef POST_DEBUG
    2330                                       fprintf (stderr, "%s\n", has);
    2331 #endif
    2332                                     }
    2333                                   xmlFree (val);
    2334                                 }
    2335                               hInternet.ihandle[hInternet.nb].header =
    2336                                 curl_slist_append (hInternet.ihandle
    2337                                                    [hInternet.nb].header,
    2338                                                    has);
    2339                               if (has != NULL)
    2340                                 free (has);
    2341                             }
    2342                           else
    2343                             {
    2344 #ifdef POST_DEBUG
    2345                               fprintf (stderr,
    2346                                        "Try to fetch the body part of the request ...\n");
    2347 #endif
    2348                               if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
    2349                                   == 0)
    2350                                 {
    2351 #ifdef POST_DEBUG
    2352                                   fprintf (stderr, "Body part found !!!\n",
    2353                                            (char *) cur3->content);
    2354 #endif
    2355                                   char *tmp =
    2356                                     (char *) malloc (cgiContentLength +
    2357                                                      1 * sizeof (char));
    2358                                   memset (tmp, 0, cgiContentLength);
    2359                                   xmlNodePtr cur4 = cur3->children;
    2360                                   while (cur4 != NULL)
    2361                                     {
    2362                                       while (cur4->type != XML_ELEMENT_NODE)
    2363                                         cur4 = cur4->next;
    2364                                       xmlDocPtr bdoc =
    2365                                         xmlNewDoc (BAD_CAST "1.0");
    2366                                       bdoc->encoding =
    2367                                         xmlCharStrdup ("UTF-8");
    2368                                       xmlDocSetRootElement (bdoc, cur4);
    2369                                       xmlChar *btmps;
    2370                                       int bsize;
    2371                                       xmlDocDumpMemory (bdoc, &btmps, &bsize);
    2372 #ifdef POST_DEBUG
    2373                                       fprintf (stderr,
    2374                                                "Body part found !!! %s %s\n",
    2375                                                tmp, (char *) btmps);
    2376 #endif
    2377                                       if (btmps != NULL)
    2378                                         sprintf (tmp, "%s", (char *) btmps);
    2379                                       xmlFree (btmps);
    2380                                       cur4 = cur4->next;
    2381                                       xmlFreeDoc (bdoc);
    2382                                     }
    2383                                   map *btmp =
    2384                                     getMap (tmpmaps->content, "href");
    2385                                   if (btmp != NULL)
    2386                                     {
    2387 #ifdef POST_DEBUG
    2388                                       fprintf (stderr, "%s %s\n", btmp->value,
    2389                                                tmp);
    2390                                       curl_easy_setopt (hInternet.handle,
    2391                                                         CURLOPT_VERBOSE, 1);
    2392 #endif
    2393                                       hInternet.
    2394                                         waitingRequests[hInternet.nb] =
    2395                                         strdup (tmp);
    2396                                       InternetOpenUrl (&hInternet,
    2397                                                        btmp->value,
    2398                                                        hInternet.waitingRequests
    2399                                                        [hInternet.nb],
    2400                                                        strlen
    2401                                                        (hInternet.waitingRequests
    2402                                                         [hInternet.nb]),
    2403                                                        INTERNET_FLAG_NO_CACHE_WRITE,
    2404                                                        0);
    2405                                     }
    2406                                   free (tmp);
    2407                                 }
    2408                               else
    2409                                 if (xmlStrcasecmp
    2410                                     (cur3->name,
    2411                                      BAD_CAST "BodyReference") == 0)
    2412                                 {
    2413                                   xmlChar *val =
    2414                                     xmlGetProp (cur3, BAD_CAST "href");
    2415                                   HINTERNET bInternet, res1;
    2416                                   bInternet = InternetOpen (
    2417 #ifndef WIN32
    2418                                                              (LPCTSTR)
    2419 #endif
    2420                                                              "ZooWPSClient\0",
    2421                                                              INTERNET_OPEN_TYPE_PRECONFIG,
    2422                                                              NULL, NULL, 0);
    2423                                   if (!CHECK_INET_HANDLE (hInternet))
    2424                                     fprintf (stderr,
    2425                                              "WARNING : hInternet handle failed to initialize");
    2426 #ifdef POST_DEBUG
    2427                                   curl_easy_setopt (bInternet.handle,
    2428                                                     CURLOPT_VERBOSE, 1);
    2429 #endif
    2430                                   bInternet.waitingRequests[0] =
    2431                                     strdup ((char *) val);
    2432                                   res1 =
    2433                                     InternetOpenUrl (&bInternet,
    2434                                                      bInternet.waitingRequests
    2435                                                      [0], NULL, 0,
    2436                                                      INTERNET_FLAG_NO_CACHE_WRITE,
    2437                                                      0);
    2438                                   processDownloads (&bInternet);
    2439                                   char *tmp =
    2440                                     (char *)
    2441                                     malloc ((bInternet.ihandle[0].nDataLen +
    2442                                              1) * sizeof (char));
    2443                                   if (tmp == NULL)
    2444                                     {
    2445                                       return errorException (m,
    2446                                                              _
    2447                                                              ("Unable to allocate memory."),
    2448                                                              "InternalError",
    2449                                                              NULL);
    2450                                     }
    2451                                   size_t bRead;
    2452                                   InternetReadFile (bInternet.ihandle[0],
    2453                                                     (LPVOID) tmp,
    2454                                                     bInternet.
    2455                                                     ihandle[0].nDataLen,
    2456                                                     &bRead);
    2457                                   tmp[bInternet.ihandle[0].nDataLen] = 0;
    2458                                   InternetCloseHandle (&bInternet);
    2459                                   map *btmp =
    2460                                     getMap (tmpmaps->content, "href");
    2461                                   if (btmp != NULL)
    2462                                     {
    2463 #ifdef POST_DEBUG
    2464                                       fprintf (stderr, "%s %s\n", btmp->value,
    2465                                                tmp);
    2466 #endif
    2467                                       hInternet.
    2468                                         waitingRequests[hInternet.nb] =
    2469                                         strdup (tmp);
    2470                                       res =
    2471                                         InternetOpenUrl (&hInternet,
    2472                                                          btmp->value,
    2473                                                          hInternet.waitingRequests
    2474                                                          [hInternet.nb],
    2475                                                          strlen
    2476                                                          (hInternet.waitingRequests
    2477                                                           [hInternet.nb]),
    2478                                                          INTERNET_FLAG_NO_CACHE_WRITE,
    2479                                                          0);
    2480                                     }
    2481                                   free (tmp);
    2482                                 }
    2483                             }
    2484                           cur3 = cur3->next;
    2485                         }
    2486 #ifdef POST_DEBUG
    2487                       fprintf (stderr,
    2488                                "Header and Body was parsed from Reference \n");
    2489 #endif
    2490 #ifdef DEBUG
    2491                       dumpMap (tmpmaps->content);
    2492                       fprintf (stderr, "= element 2 node \"%s\" = (%s)\n",
    2493                                cur2->name, cur2->content);
    2494 #endif
    2495                     }
    2496                   else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
    2497                     {
    2498 #ifdef DEBUG
    2499                       fprintf (stderr, "DATA\n");
    2500 #endif
    2501                       xmlNodePtr cur4 = cur2->children;
    2502                       while (cur4 != NULL)
    2503                         {
    2504                           while (cur4 != NULL
    2505                                  && cur4->type != XML_ELEMENT_NODE)
    2506                             cur4 = cur4->next;
    2507                           if (cur4 == NULL)
    2508                             break;
    2509                           if (xmlStrcasecmp
    2510                               (cur4->name, BAD_CAST "LiteralData") == 0)
    2511                             {
    2512                 /**
    2513                  * Get every attribute from a LiteralData node
    2514                  * dataType , uom
    2515                  */
    2516                               char *list[2];
    2517                               list[0] = zStrdup ("dataType");
    2518                               list[1] = zStrdup ("uom");
    2519                               for (int l = 0; l < 2; l++)
    2520                                 {
    2521 #ifdef DEBUG
    2522                                   fprintf (stderr, "*** LiteralData %s ***",
    2523                                            list[l]);
    2524 #endif
    2525                                   xmlChar *val =
    2526                                     xmlGetProp (cur4, BAD_CAST list[l]);
    2527                                   if (val != NULL
    2528                                       && strlen ((char *) val) > 0)
    2529                                     {
    2530                                       if (tmpmaps->content != NULL)
    2531                                         addToMap (tmpmaps->content, list[l],
    2532                                                   (char *) val);
    2533                                       else
    2534                                         tmpmaps->content =
    2535                                           createMap (list[l], (char *) val);
    2536 #ifdef DEBUG
    2537                                       fprintf (stderr, "%s\n", val);
    2538 #endif
    2539                                     }
    2540                                   xmlFree (val);
    2541                                   free (list[l]);
    2542                                 }
    2543                             }
    2544                           else
    2545                             if (xmlStrcasecmp
    2546                                 (cur4->name, BAD_CAST "ComplexData") == 0)
    2547                             {
    2548                 /**
    2549                  * Get every attribute from a Reference node
    2550                  * mimeType, encoding, schema
    2551                  */
    2552                               const char *coms[3] =
    2553                                 { "mimeType", "encoding", "schema" };
    2554                               for (int l = 0; l < 3; l++)
    2555                                 {
    2556 #ifdef DEBUG
    2557                                   fprintf (stderr, "*** ComplexData %s ***\n",
    2558                                            coms[l]);
    2559 #endif
    2560                                   xmlChar *val =
    2561                                     xmlGetProp (cur4, BAD_CAST coms[l]);
    2562                                   if (val != NULL
    2563                                       && strlen ((char *) val) > 0)
    2564                                     {
    2565                                       if (tmpmaps->content != NULL)
    2566                                         addToMap (tmpmaps->content, coms[l],
    2567                                                   (char *) val);
    2568                                       else
    2569                                         tmpmaps->content =
    2570                                           createMap (coms[l], (char *) val);
    2571 #ifdef DEBUG
    2572                                       fprintf (stderr, "%s\n", val);
    2573 #endif
    2574                                     }
    2575                                   xmlFree (val);
    2576                                 }
    2577                             }
    2578 
    2579                           map *test = getMap (tmpmaps->content, "encoding");
    2580 
    2581                           if (test == NULL)
    2582                             {
    2583                               if (tmpmaps->content != NULL)
    2584                                 addToMap (tmpmaps->content, "encoding",
    2585                                           "utf-8");
    2586                               else
    2587                                 tmpmaps->content =
    2588                                   createMap ("encoding", "utf-8");
    2589                               test = getMap (tmpmaps->content, "encoding");
    2590                             }
    2591 
    2592                           if (strcasecmp (test->value, "base64") != 0)
    2593                             {
    2594                               xmlChar *mv = xmlNodeListGetString (doc,
    2595                                                                   cur4->
    2596                                                                   xmlChildrenNode,
    2597                                                                   1);
    2598                               map *ltmp =
    2599                                 getMap (tmpmaps->content, "mimeType");
    2600                               if (mv == NULL
    2601                                   ||
    2602                                   (xmlStrcasecmp
    2603                                    (cur4->name, BAD_CAST "ComplexData") == 0
    2604                                    && (ltmp == NULL
    2605                                        || strncasecmp (ltmp->value,
    2606                                                        "text/xml", 8) == 0)))
    2607                                 {
    2608                                   xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
    2609                                   int buffersize;
    2610                                   xmlNodePtr cur5 = cur4->children;
    2611                                   while (cur5 != NULL
    2612                                          && cur5->type != XML_ELEMENT_NODE
    2613                                          && cur5->type !=
    2614                                          XML_CDATA_SECTION_NODE)
    2615                                     cur5 = cur5->next;
    2616                                   if (cur5 != NULL
    2617                                       && cur5->type != XML_CDATA_SECTION_NODE)
    2618                                     {
    2619                                       xmlDocSetRootElement (doc1, cur5);
    2620                                       xmlDocDumpFormatMemoryEnc (doc1, &mv,
    2621                                                                  &buffersize,
    2622                                                                  "utf-8", 1);
    2623                                       char size[1024];
    2624                                       sprintf (size, "%d", buffersize);
    2625                                       addToMap (tmpmaps->content, "size",
    2626                                                 size);
    2627                                       xmlFreeDoc (doc1);
    2628                                     }
    2629                                   else
    2630                                     {
    2631                                       if (cur5 != NULL
    2632                                           && cur5->type == XML_CDATA_SECTION_NODE){
    2633                                         xmlDocPtr doc2 = xmlParseMemory((const char*)cur5->content,xmlStrlen(cur5->content));
    2634                                         xmlDocSetRootElement (doc1,xmlDocGetRootElement(doc2));
    2635                                         xmlDocDumpFormatMemoryEnc (doc1, &mv,
    2636                                                                    &buffersize,
    2637                                                                    "utf-8", 1);
    2638                                         char size[1024];
    2639                                         sprintf (size, "%d", buffersize);
    2640                                         addToMap (tmpmaps->content, "size",
    2641                                                   size);
    2642                                         xmlFreeDoc (doc2);
    2643                                         xmlFreeDoc (doc1);
    2644                                       }
    2645                                     }
    2646                                 }else{
    2647                                 xmlNodePtr cur5 = cur4->children;
    2648                                 while (cur5 != NULL
    2649                                        && cur5->type != XML_CDATA_SECTION_NODE)
    2650                                   cur5 = cur5->next;
    2651                                 if (cur5 != NULL
    2652                                     && cur5->type == XML_CDATA_SECTION_NODE){
    2653                                   xmlFree(mv);
    2654                                   mv=xmlStrdup(cur5->content);
    2655                                 }
    2656                               }
    2657                               if (mv != NULL)
    2658                                 {
    2659                                   addToMap (tmpmaps->content, "value",
    2660                                             (char *) mv);
    2661                                   xmlFree (mv);
    2662                                 }
    2663                             }
    2664                           else
    2665                             {
    2666                               xmlChar *tmp = xmlNodeListGetRawString (doc,
    2667                                                                       cur4->xmlChildrenNode,
    2668                                                                       0);
    2669                               addToMap (tmpmaps->content, "value",
    2670                                         (char *) tmp);
    2671                               map *tmpv = getMap (tmpmaps->content, "value");
    2672                               char *res = NULL;
    2673                               char *curs = tmpv->value;
    2674                               for (int i = 0; i <= strlen (tmpv->value) / 64;
    2675                                    i++)
    2676                                 {
    2677                                   if (res == NULL)
    2678                                     res =
    2679                                       (char *) malloc (67 * sizeof (char));
    2680                                   else
    2681                                     res =
    2682                                       (char *) realloc (res,
    2683                                                         (((i + 1) * 65) +
    2684                                                          i) * sizeof (char));
    2685                                   int csize = i * 65;
    2686                                   strncpy (res + csize, curs, 64);
    2687                                   if (i == xmlStrlen (tmp) / 64)
    2688                                     strcat (res, "\n\0");
    2689                                   else
    2690                                     {
    2691                                       strncpy (res + (((i + 1) * 64) + i),
    2692                                                "\n\0", 2);
    2693                                       curs += 64;
    2694                                     }
    2695                                 }
    2696                               free (tmpv->value);
    2697                               tmpv->value = zStrdup (res);
    2698                               free (res);
    2699                               xmlFree (tmp);
    2700                             }
    2701                           cur4 = cur4->next;
    2702                         }
    2703                     }
    2704 #ifdef DEBUG
    2705                   fprintf (stderr, "cur2 next \n");
    2706                   fflush (stderr);
    2707 #endif
    2708                   cur2 = cur2->next;
    2709                 }
    2710 #ifdef DEBUG
    2711               fprintf (stderr, "ADD MAPS TO REQUEST MAPS !\n");
    2712               fflush (stderr);
    2713 #endif
    2714 
    2715               {
    2716                 maps *testPresence =
    2717                   getMaps (request_input_real_format, tmpmaps->name);
    2718                 if (testPresence != NULL)
    2719                   {
    2720                     elements *elem = getElements (s1->inputs, tmpmaps->name);
    2721                     if (elem != NULL)
    2722                       {
    2723                         if (appendMapsToMaps
    2724                             (m, request_input_real_format, tmpmaps, elem) < 0)
    2725                           {
    2726                             freeMaps (&m);
    2727                             free (m);
    2728                             free (REQUEST);
    2729                             free (SERVICE_URL);
    2730                             InternetCloseHandle (&hInternet);
    2731                             freeService (&s1);
    2732                             free (s1);
    2733                             return 0;
    2734                           }
    2735                       }
    2736                   }
    2737                 else
    2738                   addMapsToMaps (&request_input_real_format, tmpmaps);
    2739               }
    2740 
    2741 #ifdef DEBUG
    2742               fprintf (stderr, "******TMPMAPS*****\n");
    2743               dumpMaps (tmpmaps);
    2744               fprintf (stderr, "******REQUESTMAPS*****\n");
    2745               dumpMaps (request_input_real_format);
    2746 #endif
    2747               freeMaps (&tmpmaps);
    2748               free (tmpmaps);
    2749               tmpmaps = NULL;
    2750             }
    2751 #ifdef DEBUG
    2752           dumpMaps (tmpmaps);
    2753 #endif
    2754         }
    2755 #ifdef DEBUG
    2756       fprintf (stderr, "Search for response document node\n");
    2757 #endif
    2758       xmlXPathFreeObject (tmpsptr);
    2759 
    2760       tmpsptr =
    2761         extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
    2762       bool asRaw = false;
    2763       tmps = tmpsptr->nodesetval;
    2764       if (tmps->nodeNr == 0)
    2765         {
    2766           xmlXPathFreeObject (tmpsptr);
    2767           tmpsptr =
    2768             extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
    2769           tmps = tmpsptr->nodesetval;
    2770           asRaw = true;
    2771         }
    2772 #ifdef DEBUG
    2773       fprintf (stderr, "*****%d*****\n", tmps->nodeNr);
    2774 #endif
    2775       if (asRaw == true)
    2776         {
    2777           addToMap (request_inputs, "RawDataOutput", "");
    2778           xmlNodePtr cur0 = tmps->nodeTab[0];
    2779           if (cur0->type == XML_ELEMENT_NODE)
    2780             {
    2781 
    2782               maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
    2783               if (tmpmaps == NULL)
    2784                 {
    2785                   return errorException (m, _("Unable to allocate memory."),
    2786                                          "InternalError", NULL);
    2787                 }
    2788               tmpmaps->name = zStrdup ("unknownIdentifier");
    2789               tmpmaps->content = NULL;
    2790               tmpmaps->next = NULL;
    2791 
    2792         /**
    2793          * Get every attribute from a RawDataOutput node
    2794          * mimeType, encoding, schema, uom
    2795          */
    2796               const char *outs[4] =
    2797                 { "mimeType", "encoding", "schema", "uom" };
    2798               for (int l = 0; l < 4; l++)
    2799                 {
    2800 #ifdef DEBUG
    2801                   fprintf (stderr, "*** %s ***\t", outs[l]);
    2802 #endif
    2803                   xmlChar *val = xmlGetProp (cur0, BAD_CAST outs[l]);
    2804                   if (val != NULL)
    2805                     {
    2806                       if (strlen ((char *) val) > 0)
    2807                         {
    2808                           if (tmpmaps->content != NULL)
    2809                             addToMap (tmpmaps->content, outs[l],
    2810                                       (char *) val);
    2811                           else
    2812                             tmpmaps->content =
    2813                               createMap (outs[l], (char *) val);
    2814                         }
    2815                       xmlFree (val);
    2816                     }
    2817                 }
    2818               xmlNodePtr cur2 = cur0->children;
    2819               while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
    2820                 cur2 = cur2->next;
    2821               while (cur2 != NULL)
    2822                 {
    2823                   if (xmlStrncasecmp
    2824                       (cur2->name, BAD_CAST "Identifier",
    2825                        xmlStrlen (cur2->name)) == 0)
    2826                     {
    2827                       xmlChar *val =
    2828                         xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
    2829                       free (tmpmaps->name);
    2830                       tmpmaps->name = zStrdup ((char *) val);
    2831                       xmlFree (val);
    2832                     }
    2833                   cur2 = cur2->next;
    2834                   while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
    2835                     cur2 = cur2->next;
    2836                 }
    2837               if (request_output_real_format == NULL)
    2838                 request_output_real_format = dupMaps (&tmpmaps);
    2839               else
    2840                 addMapsToMaps (&request_output_real_format, tmpmaps);
    2841               if (tmpmaps != NULL)
    2842                 {
    2843                   freeMaps (&tmpmaps);
    2844                   free (tmpmaps);
    2845                   tmpmaps = NULL;
    2846                 }
    2847             }
    2848         }
    2849       else
    2850           {
    2851                 addToMap (request_inputs, "ResponseDocument", "");
    2852                        
    2853                 xmlNodePtr cur = tmps->nodeTab[0]; // only one ResponseDocument node
    2854                 if (cur->type == XML_ELEMENT_NODE) {
    2855                 /**
    2856                  * Get every attribute: storeExecuteResponse, lineage, status
    2857                  */
    2858                         //map* attributes = NULL;
    2859                         const char *ress[3] =
    2860                           { "storeExecuteResponse", "lineage", "status" };
    2861                         xmlChar *val;
    2862                         for (int l = 0; l < 3; l++)
    2863                         {
    2864         #ifdef DEBUG
    2865                                 fprintf (stderr, "*** %s ***\t", ress[l]);
    2866         #endif
    2867                                 val = xmlGetProp (cur, BAD_CAST ress[l]);
    2868                                 if (val != NULL && strlen ((char *) val) > 0)
    2869                                 {
    2870                                 /*
    2871                                         if (attributes == NULL) {
    2872                                                 attributes = createMap (ress[l], (char *) val);
    2873                                         }
    2874                                         else {
    2875                                                 addToMap (attributes, ress[l], (char *) val);
    2876                                         }
    2877                                 */             
    2878                                         addToMap (request_inputs, ress[l], (char *) val);
    2879                                   }
    2880         #ifdef DEBUG
    2881                                 fprintf (stderr, "%s\n", val);
    2882         #endif
    2883                                 xmlFree (val);
    2884                         }
    2885                        
    2886                         xmlNodePtr cur1 = cur->children;               
    2887                         while (cur1 != NULL) // iterate over Output nodes
    2888                         {
    2889                                 if (cur1->type != XML_ELEMENT_NODE ||
    2890                                         xmlStrncasecmp(cur1->name, BAD_CAST "Output",
    2891                                                                    xmlStrlen (cur1->name)) != 0) {
    2892                                         cur1 = cur1->next;
    2893                                         continue;
    2894                                 }
    2895                                
    2896                                 maps *tmpmaps = (maps *) malloc (MAPS_SIZE); // one per Output node
    2897                                 if (tmpmaps == NULL) {
    2898                                         return errorException (m,
    2899                                                                                    _
    2900                                                                                    ("Unable to allocate memory."),
    2901                                                                                    "InternalError", NULL);
    2902                                 }
    2903                                 tmpmaps->name = zStrdup ("unknownIdentifier");
    2904                                 tmpmaps->content = NULL;
    2905                                 tmpmaps->next = NULL;
    2906                                
    2907                                 xmlNodePtr elems = cur1->children;
    2908                                
    2909                                 while (elems != NULL) {
    2910                                
    2911                                         /**
    2912                                          * Identifier
    2913                                          */             
    2914                                         if (xmlStrncasecmp
    2915                                                 (elems->name, BAD_CAST "Identifier",
    2916                                                  xmlStrlen (elems->name)) == 0)
    2917                                         {                       
    2918                                                 xmlChar *val =
    2919                                                   xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
    2920                                                
    2921                                                 free(tmpmaps->name);
    2922                                                 tmpmaps->name = zStrdup ((char *) val); 
    2923                                                 if (tmpmaps->content == NULL) {
    2924                                                         tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
    2925                                                 }
    2926                                                 else {
    2927                                                         addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
    2928                                                 }
    2929                                          
    2930                                                 map* tt = getMap (request_inputs, "ResponseDocument");                           
    2931                                                 if (strlen(tt->value) == 0) {
    2932                                                   addToMap (request_inputs, "ResponseDocument",
    2933                                                                         (char *) val);                                                 
    2934                                                 }       
    2935                                                 else {
    2936                                                         char* tmp = (char*) malloc((strlen(tt->value) + 1
    2937                                                                                                 + strlen((char*) val) + 1) * sizeof(char));
    2938                                                         sprintf (tmp, "%s;%s", tt->value, (char *) val);
    2939                                                         free(tt->value);
    2940                                                         tt->value = tmp;       
    2941                                                 }       
    2942                                                 xmlFree (val);
    2943                                         }                               
    2944                                         /**
    2945                                          * Title, Abstract
    2946                                          */
    2947                                         else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
    2948                                                                                         xmlStrlen (elems->name)) == 0
    2949                                                   || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
    2950                                                                                  xmlStrlen (elems->name)) == 0)
    2951                                         {
    2952                                                 xmlChar *val =
    2953                                                         xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
    2954                                                        
    2955                                                 if (tmpmaps->content == NULL) {
    2956                                                         tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
    2957                                                 }
    2958                                                 else {
    2959                                                         addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
    2960                                                 }
    2961                                                 xmlFree (val);
    2962                                         }
    2963                                         elems = elems->next;
    2964                                 }
    2965                                
    2966                                 /**
    2967                                  * Get every attribute from an Output node:
    2968                                  * mimeType, encoding, schema, uom, asReference
    2969                                  */
    2970                                 const char *outs[5] =
    2971                                           { "mimeType", "encoding", "schema", "uom", "asReference" };
    2972                                          
    2973                                 for (int l = 0; l < 5; l++) {
    2974         #ifdef DEBUG
    2975                                         fprintf (stderr, "*** %s ***\t", outs[l]);
    2976         #endif
    2977                                         xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                             
    2978                                         if (val != NULL && xmlStrlen(val) > 0) {
    2979                                                 if (tmpmaps->content != NULL) {
    2980                                                         addToMap (tmpmaps->content, outs[l], (char *) val);
    2981                                                 }                         
    2982                                                 else {
    2983                                                         tmpmaps->content = createMap (outs[l], (char *) val);
    2984                                                 }       
    2985                                         }
    2986         #ifdef DEBUG
    2987                                         fprintf (stderr, "%s\n", val);
    2988         #endif
    2989                                         xmlFree (val);
    2990                                 }
    2991                                
    2992                                 if (request_output_real_format == NULL) {
    2993                                         request_output_real_format = tmpmaps;
    2994                                 }       
    2995                                 else if (getMaps(request_output_real_format, tmpmaps->name) != NULL) {
    2996                                         return errorException (m,
    2997                                                                                    _
    2998                                                                                    ("Duplicate <Output> elements in WPS Execute request"),
    2999                                                                                    "InternalError", NULL);
    3000                                 }
    3001                                 else {
    3002                                         maps* mptr = request_output_real_format;
    3003                                         while (mptr->next != NULL) {
    3004                                                 mptr = mptr->next;
    3005                                         }
    3006                                         mptr->next = tmpmaps;   
    3007                                 }                                       
    3008                                 cur1 = cur1->next;
    3009                         }                       
    3010                 }
    3011           }
    3012       xmlXPathFreeObject (tmpsptr);
    3013       xmlFreeDoc (doc);
    3014       xmlCleanupParser ();
    3015     }
    3016 
    3017   runHttpRequests (&m, &request_input_real_format, &hInternet);
    3018 
    3019   //  if(CHECK_INET_HANDLE(hInternet))
    3020   InternetCloseHandle (&hInternet);
    3021 
    3022 #ifdef DEBUG
    3023   fprintf (stderr, "\n%d\n", __LINE__);
    3024   fflush (stderr);
    3025   dumpMaps (request_input_real_format);
    3026   dumpMaps (request_output_real_format);
    3027   dumpMap (request_inputs);
    3028   fprintf (stderr, "\n%d\n", __LINE__);
    3029   fflush (stderr);
    3030 #endif
    3031 
    3032   /**
    3033    * Ensure that each requested arguments are present in the request
    3034    * DataInputs and ResponseDocument / RawDataOutput
    3035    */
    3036   map* errI=NULL;
    3037 #ifdef DEBUG 
    3038   dumpMaps(request_input_real_format);
    3039 #endif 
    3040   char *dfv = addDefaultValues (&request_input_real_format, s1->inputs, m, 0,&errI);
    3041 #ifdef DEBUG 
    3042   dumpMaps(request_input_real_format);
    3043 #endif 
    3044   maps *ptr = request_input_real_format;
    3045   while (ptr != NULL)
    3046     {
    3047       map *tmp0 = getMap (ptr->content, "size");
    3048       map *tmp1 = getMap (ptr->content, "maximumMegabytes");
    3049       if (tmp1 != NULL && tmp0 != NULL)
    3050         {
    3051           float i = atof (tmp0->value) / 1048576.0;
    3052           if (i >= atoi (tmp1->value))
    3053             {
    3054               char tmps[1024];
    3055               map *tmpe = createMap ("code", "FileSizeExceeded");
    3056               snprintf (tmps, 1024,
    3057                         _
    3058                         ("The <%s> parameter has a size limit (%s MB) defined in the ZOO ServicesProvider configuration file, but the reference you provided exceeds this limit (%f MB)."),
    3059                         ptr->name, tmp1->value, i);
    3060               addToMap (tmpe, "locator", ptr->name);
    3061               addToMap (tmpe, "text", tmps);
    3062               printExceptionReportResponse (m, tmpe);
    3063               freeService (&s1);
    3064               free (s1);
    3065               freeMap (&tmpe);
    3066               free (tmpe);
    3067               freeMaps (&m);
    3068               free (m);
    3069               free (REQUEST);
    3070               free (SERVICE_URL);
    3071               freeMaps (&request_input_real_format);
    3072               free (request_input_real_format);
    3073               freeMaps (&request_output_real_format);
    3074               free (request_output_real_format);
    3075               freeMaps (&tmpmaps);
    3076               free (tmpmaps);
    3077               return 1;
    3078             }
    3079         }
    3080       ptr = ptr->next;
    3081     }
    3082 
    3083   map* errO=NULL;
    3084   char *dfv1 =
    3085     addDefaultValues (&request_output_real_format, s1->outputs, m, 1,&errO);
    3086   if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
    3087     {
    3088       char tmps[1024];
    3089       map *tmpe = NULL;
    3090       if (strcmp (dfv, "") != 0)
    3091         {
    3092           tmpe = createMap ("code", "MissingParameterValue");
    3093           int nb=0;
    3094           int length=1;
    3095           map* len=getMap(errI,"length");
    3096           if(len!=NULL)
    3097             length=atoi(len->value);
    3098           for(nb=0;nb<length;nb++){
    3099             map* errp=getMapArray(errI,"value",nb);
    3100             snprintf (tmps, 1024,
    3101                       _
    3102                       ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
    3103                       errp->value);
    3104             setMapArray (tmpe, "locator", nb , errp->value);
    3105             setMapArray (tmpe, "text", nb , tmps);
    3106             setMapArray (tmpe, "code", nb , "MissingParameterValue");
    3107           }
    3108         }
    3109       if (strcmp (dfv1, "") != 0)
    3110         {
    3111           int ilength=0;
    3112           if(tmpe==NULL)
    3113             tmpe = createMap ("code", "InvalidParameterValue");
    3114           else{
    3115             map* len=getMap(tmpe,"length");
    3116             if(len!=NULL)
    3117               ilength=atoi(len->value);
    3118           }
    3119           int nb=0;
    3120           int length=1;
    3121           map* len=getMap(errO,"length");
    3122           if(len!=NULL)
    3123             length=atoi(len->value);
    3124           for(nb=0;nb<length;nb++){
    3125             map* errp=getMapArray(errO,"value",nb);
    3126             snprintf (tmps, 1024,
    3127                       _
    3128                       ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
    3129                       errp->value,
    3130                       ((getMap(request_inputs,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
    3131             setMapArray (tmpe, "locator", nb+ilength , errp->value);
    3132             setMapArray (tmpe, "text", nb+ilength , tmps);
    3133             setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
    3134           }
    3135         }
    3136       printExceptionReportResponse (m, tmpe);
    3137       freeService (&s1);
    3138       free (s1);
    3139       freeMap (&tmpe);
    3140       free (tmpe);
    3141       freeMaps (&m);
    3142       free (m);
    3143       free (REQUEST);
    3144       free (SERVICE_URL);
    3145       freeMaps (&request_input_real_format);
    3146       free (request_input_real_format);
    3147       freeMaps (&request_output_real_format);
    3148       free (request_output_real_format);
    3149       freeMaps (&tmpmaps);
    3150       free (tmpmaps);
    3151       if(errI!=NULL){
    3152         freeMap(&errI);
    3153         free(errI);
    3154       }
    3155       if(errO!=NULL){
    3156         freeMap(&errO);
    3157         free(errO);
    3158       }
    3159       return 1;
    3160     }
    3161   maps *tmpReqI = request_input_real_format;
    3162   while (tmpReqI != NULL)
    3163     {
    3164       char name[1024];
    3165       if (getMap (tmpReqI->content, "isFile") != NULL)
    3166         {
    3167           if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
    3168               cgiFormSuccess)
    3169             {
    3170               int BufferLen = 1024;
    3171               cgiFilePtr file;
    3172               int targetFile;
    3173               char storageNameOnServer[2048];
    3174               char fileNameOnServer[64];
    3175               char contentType[1024];
    3176               char buffer[1024];
    3177               char *tmpStr = NULL;
    3178               int size;
    3179               int got, t;
    3180               map *path = getMapFromMaps (m, "main", "tmpPath");
    3181               cgiFormFileSize (tmpReqI->name, &size);
    3182               cgiFormFileContentType (tmpReqI->name, contentType,
    3183                                       sizeof (contentType));
    3184               if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
    3185                 {
    3186                   t = -1;
    3187                   while (1)
    3188                     {
    3189                       tmpStr = strstr (name + t + 1, "\\");
    3190                       if (NULL == tmpStr)
    3191                         tmpStr = strstr (name + t + 1, "/");
    3192                       if (NULL != tmpStr)
    3193                         t = (int) (tmpStr - name);
    3194                       else
    3195                         break;
    3196                     }
    3197                   strcpy (fileNameOnServer, name + t + 1);
    3198 
    3199                   sprintf (storageNameOnServer, "%s/%s", path->value,
    3200                            fileNameOnServer);
    3201 #ifdef DEBUG
    3202                   fprintf (stderr, "Name on server %s\n",
    3203                            storageNameOnServer);
    3204                   fprintf (stderr, "fileNameOnServer: %s\n",
    3205                            fileNameOnServer);
    3206 #endif
    3207                   targetFile =
    3208                     open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
    3209                           S_IRWXU | S_IRGRP | S_IROTH);
    3210                   if (targetFile < 0)
    3211                     {
    3212 #ifdef DEBUG
    3213                       fprintf (stderr, "could not create the new file,%s\n",
    3214                                fileNameOnServer);
    3215 #endif
    3216                     }
    3217                   else
    3218                     {
    3219                       while (cgiFormFileRead (file, buffer, BufferLen, &got)
    3220                              == cgiFormSuccess)
    3221                         {
    3222                           if (got > 0)
    3223                             write (targetFile, buffer, got);
    3224                         }
    3225                     }
    3226                   addToMap (tmpReqI->content, "lref", storageNameOnServer);
    3227                   cgiFormFileClose (file);
    3228                   close (targetFile);
    3229 #ifdef DEBUG
    3230                   fprintf (stderr, "File \"%s\" has been uploaded",
    3231                            fileNameOnServer);
    3232 #endif
    3233                 }
    3234             }
    3235         }
    3236       tmpReqI = tmpReqI->next;
    3237     }
    3238 
    3239   ensureDecodedBase64 (&request_input_real_format);
    3240 
    3241 #ifdef DEBUG
    3242   fprintf (stderr, "REQUEST_INPUTS\n");
    3243   dumpMaps (request_input_real_format);
    3244   fprintf (stderr, "REQUEST_OUTPUTS\n");
    3245   dumpMaps (request_output_real_format);
    3246 #endif
     1621
     1622
     1623  if(parseRequest(&m,&request_inputs,s1,&request_input_real_format,&request_output_real_format,&hInternet)<0){
     1624    freeMaps (&m);
     1625    free (m);
     1626    free (REQUEST);
     1627    free (SERVICE_URL);
     1628    InternetCloseHandle (&hInternet);
     1629    freeService (&s1);
     1630    free (s1);
     1631    return 0;
     1632  }
    32471633
    32481634  maps *curs = getMaps (m, "env");
     
    34901876  if (status == NULLMAP)
    34911877    {
     1878      if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
     1879        freeService (&s1);
     1880        free (s1);
     1881        freeMaps (&m);
     1882        free (m);
     1883        free (REQUEST);
     1884        free (SERVICE_URL);
     1885        freeMaps (&request_input_real_format);
     1886        free (request_input_real_format);
     1887        freeMaps (&request_output_real_format);
     1888        free (request_output_real_format);
     1889        freeMaps (&tmpmaps);
     1890        free (tmpmaps);
     1891        return -1;
     1892      }
     1893
    34921894      loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format,
    34931895                         &request_output_real_format, &eres);
     
    35301932      else if (pid == 0)
    35311933        {
    3532       /**
    3533        * son : have to close the stdout, stdin and stderr to let the parent
    3534        * process answer to http client.
    3535        */
     1934          /**
     1935           * son : have to close the stdout, stdin and stderr to let the parent
     1936           * process answer to http client.
     1937           */
    35361938#ifndef WIN32
    35371939          zSleep (1);
     
    35861988#endif
    35871989          free (flog);
     1990          if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
     1991            freeService (&s1);
     1992            free (s1);
     1993            freeMaps (&m);
     1994            free (m);
     1995            free (REQUEST);
     1996            free (SERVICE_URL);
     1997            freeMaps (&request_input_real_format);
     1998            free (request_input_real_format);
     1999            freeMaps (&request_output_real_format);
     2000            free (request_output_real_format);
     2001            freeMaps (&tmpmaps);
     2002            free (tmpmaps);
     2003            fflush (stdout);
     2004            unlockShm (lid);
     2005            fflush (stderr);
     2006            return -1;
     2007          }
    35882008      /**
    35892009       * set status to SERVICE_STARTED and flush stdout to ensure full
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