]> the.earth.li Git - onak.git/blobdiff - getcgi.c
Switch from Travis CI to GitHub Actions
[onak.git] / getcgi.c
index ac37cff339d64e523c3b5a5f2db1bcd432aea051..7c0656020690be5e14dddb57fcf5fb8a98f59f52 100644 (file)
--- a/getcgi.c
+++ b/getcgi.c
@@ -1,16 +1,27 @@
 /*
  * getcgivars.c - routine to read CGI input variables into an array.
  *
- * Jonathan McDowell <noodles@earth.li>
+ * Copyright 2002 Jonathan McDowell <noodles@earth.li>
  *
  * The x2c() and unescape_url() routines were lifted directly
  * from NCSA's sample program util.c, packaged with their HTTPD.
  *
- * $Id: getcgi.c,v 1.5 2003/06/04 20:57:07 noodles Exp $
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 #include <stdio.h>
 #include <string.h>
+#include <strings.h>
 #include <stdlib.h>
 
 #include "getcgi.h"
@@ -128,7 +139,7 @@ void unescape_url(char *url)
 char **getcgivars(int argc, char *argv[]) 
 {
        int i;
-       char *request_method;
+       char *request_method, *env;
        int content_length, paircount;
        char *cgiinput = NULL;
        char **cgivars = NULL;
@@ -150,27 +161,31 @@ char **getcgivars(int argc, char *argv[])
                return NULL;
        } else if (!strcmp(request_method, "GET") ||
                        !strcmp(request_method, "HEAD")) {
-               cgiinput=strdup(getenv("QUERY_STRING"));
+               env = getenv("QUERY_STRING");
+               if (env != NULL) {
+                       cgiinput = strdup(env);
+               }
        } else if (!strcmp(request_method, "POST")) {
-               if (getenv("CONTENT_TYPE") != NULL &&
-                               strcasecmp(getenv("CONTENT_TYPE"),
-                                       "application/x-www-form-urlencoded")) {
+               env = getenv("CONTENT_TYPE");
+               if ((env != NULL) && strcasecmp(env,
+                               "application/x-www-form-urlencoded")) {
                        printf("getcgivars(): Unsupported Content-Type.\n");
                        exit(1);
                }
-               
-               if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) {
+
+               env = getenv("CONTENT_LENGTH");
+               if ((env == NULL) || !(content_length = atoi(env))) {
                        printf("getcgivars(): No Content-Length was sent with"
                                        " the POST request.\n");
                        exit(1);
                }
-               
-               if (!(cgiinput= (char *) malloc(content_length+1))) {
+
+               if (!(cgiinput = (char *) malloc(content_length+1))) {
                        printf("getcgivars(): Could not malloc for "
                                        "cgiinput.\n");
                        exit(1);
                }
-               
+
                if (!fread(cgiinput, content_length, 1, stdin)) {
                        printf("Couldn't read CGI input from STDIN.\n");
                        exit(1);
@@ -183,19 +198,24 @@ char **getcgivars(int argc, char *argv[])
                exit(1);
        }
 
+       /* If we didn't get any cgiinput info, nothing to return */
+       if (cgiinput == NULL) {
+               return NULL;
+       }
+
        /* Change all plusses back to spaces */
 
        for(i=0; cgiinput[i]; i++) if (cgiinput[i]=='+') cgiinput[i] = ' ';
 
        /* First, split on "&" to extract the name-value pairs into pairlist */
-       pairlist=(char **) malloc(256*sizeof(char **));
+       pairlist= malloc(256*sizeof(char *));
        paircount=0;
        nvpair=strtok(cgiinput, "&");
        while (nvpair) {
                pairlist[paircount++]= strdup(nvpair) ;
                if (!(paircount%256)) {
-                       pairlist=(char **) realloc(pairlist,
-                                       (paircount+256)*sizeof(char **));
+                       pairlist= realloc(pairlist,
+                                       (paircount+256)*sizeof(char *));
                }
                nvpair=strtok(NULL, "&") ;
        }
@@ -204,7 +224,7 @@ char **getcgivars(int argc, char *argv[])
 
        /* Then, from the list of pairs, extract the names and values */
        
-       cgivars=(char **) malloc((paircount*2+1)*sizeof(char **));
+       cgivars= malloc((paircount*2+1)*sizeof(char *));
        
        for (i=0; i<paircount; i++) {
                if ((eqpos=strchr(pairlist[i], '='))!=NULL) {