X-Git-Url: http://the.earth.li/gitweb/?p=onak.git;a=blobdiff_plain;f=getcgi.c;h=c6f60c89afd5d6ce977733d252c384fa84039586;hp=26c3c50f5f52cf2b6bce08a0b962458640aa2f81;hb=de18b56efecadc4b5d2473904828db9c08cd2162;hpb=200b89a2c66c58c41a27883f021850445698b337 diff --git a/getcgi.c b/getcgi.c index 26c3c50..c6f60c8 100644 --- a/getcgi.c +++ b/getcgi.c @@ -1,14 +1,27 @@ /* * getcgivars.c - routine to read CGI input variables into an array. * - * Jonathan McDowell + * Copyright 2002 Jonathan McDowell * * The x2c() and unescape_url() routines were lifted directly * from NCSA's sample program util.c, packaged with their HTTPD. + * + * 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 . */ #include #include +#include #include #include "getcgi.h" @@ -66,7 +79,7 @@ char *txt2html(const char *string) */ void start_html(const char *title) { - puts("Content-Type: text/html; charset=utf8\n"); + puts("Content-Type: text/html; charset=UTF-8\n"); puts(""); puts(""); puts(""); @@ -128,25 +141,26 @@ char **getcgivars(int argc, char *argv[]) int i; char *request_method; int content_length, paircount; - char *cgiinput; - char **cgivars; - char **pairlist; + char *cgiinput = NULL; + char **cgivars = NULL; + char **pairlist = NULL; char *nvpair,*eqpos; /* Depending on the request method, read all CGI input into cgiinput */ /* (really should produce HTML error messages, instead of exit()ing) */ - request_method=getenv("REQUEST_METHOD"); + request_method = getenv("REQUEST_METHOD"); if (request_method == NULL) { if (argc > 1) { - cgiinput = argv[1]; + cgiinput = strdup(argv[1]); } else { return NULL; } } else if (strlen(request_method)==0) { return NULL; - } else if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD")) { + } else if (!strcmp(request_method, "GET") || + !strcmp(request_method, "HEAD")) { cgiinput=strdup(getenv("QUERY_STRING")); } else if (!strcmp(request_method, "POST")) { if (getenv("CONTENT_TYPE") != NULL && @@ -157,12 +171,14 @@ char **getcgivars(int argc, char *argv[]) } if (!(content_length = atoi(getenv("CONTENT_LENGTH")))) { - printf("getcgivars(): No Content-Length was sent with the POST request.\n"); + printf("getcgivars(): No Content-Length was sent with" + " the POST request.\n"); exit(1); } if (!(cgiinput= (char *) malloc(content_length+1))) { - printf("getcgivars(): Could not malloc for cgiinput.\n"); + printf("getcgivars(): Could not malloc for " + "cgiinput.\n"); exit(1); } @@ -183,12 +199,15 @@ char **getcgivars(int argc, char *argv[]) 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 **)); + if (!(paircount%256)) { + pairlist= realloc(pairlist, + (paircount+256)*sizeof(char *)); + } nvpair=strtok(NULL, "&") ; } @@ -196,7 +215,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