Storing GET/POST parameters in an associative list

This function is part of the file handyFcts.c

Example:
http://www.webkb.org/bin/categSearch.cgi?categ=dog&recursLink=%3C&hyperlinks=



              /* GET/POST parameters to CGI script -> paramList */
ErrMess initListParamWithCGIparam (initP HTAssocList *paramList)
{
  int c,c1,c2, i, allocSize;   char *s, *name, *value;
  if (!paramList) return "Internal error: no parameter list to initialize";


  if (strEqual(getenv("REQUEST_METHOD"),"GET")) /* no CONTENT_LENGTH */
  {
    char *query = getenv("QUERY_STRING"); /* *queryBeginning=query */
    if (!query || !*query) return "No query information to process.";

    allocSize=ALLOC_SIZE; s=(char*) my_calloc(1,sizeof(char)*(allocSize+1));
    if (!s) return "Server memory problem: cannot store the parameters";

    for (i=0, name=s, value=""; ((c = *query)); i++, query++) 
    { if (i==allocSize)
      { allocSize+=ALLOC_SIZE;
        s[i]='\0';  s=(char*)my_realloc(s,sizeof(char)*(allocSize+1));
      }
      if (c=='+')  s[i]=' ';
      else if (c=='%') { c1= *(++query); c2= *(++query);  s[i]=unescapeURLchar(c1,c2); }
      else if (c=='=') { s[i]='\0'; value= &s[i+1]; }
      else if (c=='&')
      { s[i]='\0';  /* pr("name:%s\n",name); pr("value:%s\n",value); */
        if (HTAssocList_addObject(paramList,name,value)==NO)
           return "Internal error in storing the parameters";
        i=-1;  name=s; value="";
      }
      else s[i]=c;
    }
    if (i>0) /* && s[i-1]!='&' */
    { s[i]='\0';  /* pr("name2:%s\n",name); pr("value:%s\n",value); */
      if (HTAssocList_addObject(paramList,name,value)==NO)
        return "Internal error in storing the parameters";
    }
  }

  else if (strEqual(getenv("REQUEST_METHOD"),"POST"))
  {
    int l=atoi(getenv("CONTENT_LENGTH"));
    if (l==0) return "Server internal error: CONTENT_LENGTH==0.";
    else if (l>ALLOC_SIZE) allocSize=ALLOC_SIZE; else allocSize=l;

    s= (char*) my_calloc(1,sizeof(char)*(allocSize+1));
    if (!s) return "Server memory problem: cannot store the parameters";

    for (i=0, name=s, value=""; (c=getc(stdin))!=EOF && (l>0); i++,l--) 
    { if (i==allocSize) 
      { allocSize+=ALLOC_SIZE;
        s[i]='\0';  s=(char*)my_realloc(s,sizeof(char)*(allocSize+1));
      }
      if (c=='+')  s[i]=' ';
      else if (c=='%') { c1=getc(stdin); s[i]=unescapeURLchar(c1,getc(stdin)); l-=2; }
      else if (c=='=') { s[i]='\0'; value= &s[i+1]; }
      else if (c=='&')
      { s[i]='\0';
        if (HTAssocList_addObject(paramList,name,value)==NO) 
        { MY_FREE(s);  return "Internal error in storing the parameters."; }
        i= -1;  name=s; value="";
      }
      else s[i]=c;
    }
    if (i>0) /* => s[i-1]!='&' */
    { s[i]='\0';
      if (HTAssocList_addObject(paramList,name,value)==NO)
      { MY_FREE(s);  return "Internal error in storing the parameters"; }
    }
  }


  else return "This script should be referenced with a METHOD of GET or POST.\n"
              "If you don't understand this, see this forms overview";


  MY_FREE(s);  return NULL;
}