diff options
| author | rtr <rtr@NetBSD.org> | 2007-11-04 15:20:11 +0000 |
|---|---|---|
| committer | rtr <rtr@NetBSD.org> | 2007-11-04 15:20:11 +0000 |
| commit | 3aa5278c3ae94de7eddcea481fa6e010d123dbe7 (patch) | |
| tree | 7258cd31a9d4619474ea4854db18f73c06ad0870 /libexec/httpd/cgi-bozo.c | |
| parent | 4ba09b41c3ea7b82e5314fc6ac04bcfb6e259d4f (diff) | |
- remove const from h_header, this pointer is occasionally free'd
- remove unused write_str var
+ move header parsing to new function separation of parsing vs processing
+ alter some variable names to avoid confusion between header value and
header name (caused breakage with previous rev)
Diffstat (limited to 'libexec/httpd/cgi-bozo.c')
| -rw-r--r-- | libexec/httpd/cgi-bozo.c | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/libexec/httpd/cgi-bozo.c b/libexec/httpd/cgi-bozo.c index c755043579c..0f71e59c684 100644 --- a/libexec/httpd/cgi-bozo.c +++ b/libexec/httpd/cgi-bozo.c @@ -1,4 +1,4 @@ -/* $NetBSD: cgi-bozo.c,v 1.5 2007/11/04 06:07:52 rtr Exp $ */ +/* $NetBSD: cgi-bozo.c,v 1.6 2007/11/04 15:20:12 rtr Exp $ */ /* $eterna: cgi-bozo.c,v 1.13 2006/05/17 08:19:10 mrg Exp $ */ @@ -57,8 +57,9 @@ static char *cgibin; /* cgi-bin directory */ static int Cflag; /* added a cgi handler, always process_cgi() */ -static const char *content_cgihandler(http_req *, const char *); -static void finish_cgi_output(http_req *request, int, int); +static const char * content_cgihandler(http_req *, const char *); +static void finish_cgi_output(http_req *request, int, int); +static int parse_header(const char *, ssize_t, char **, char **); void set_cgibin(char *path) @@ -306,40 +307,21 @@ static void finish_cgi_output(http_req *request, int in, int nph) { char buf[WRSZ]; - char *str, *val; + char *str; size_t len; ssize_t rbytes; SIMPLEQ_HEAD(, headers) headers; struct headers *hdr; - int write_header, nheaders = 0, write_str = 0; + int write_header, nheaders = 0; /* much of this code is like read_request()'s header loop. hmmm... */ SIMPLEQ_INIT(&headers); write_header = nph == 0; while (nph == 0 && (str = dgetln(in, (ssize_t *)&len, read)) != NULL) { - char * tstr; + char * hdr_name, * hdr_value; - /* zero-length nothing to parse */ - if (*str == '\0') { - write_str = 1; + if (parse_header(str, (ssize_t)len, &hdr_name, &hdr_value)) break; - } - - /* skip leading space/tab */ - while (*str == ' ' || *str == '\t') - len--, str++; - - tstr = str = bozostrdup(str); /* we use this copy */ - - val = strnsep(&tstr, ":", (ssize_t *)&len); - debug((DEBUG_EXPLODING, - "read_req2: after strnsep: tstr ``%s'' val ``%s''", - tstr, val)); - if (val == NULL || len == -1) { - write_str = 1; - free(str); - break; - } /* * The CGI 1.{1,2} spec both say that if the cgi program @@ -352,19 +334,19 @@ finish_cgi_output(http_req *request, int in, int nph) * `Status:' header if it is returning a `Location:' header. * For compatibility we are going with the CGI 1.1 behavior. */ - if (strcasecmp(val, "status") == 0) { + if (strcasecmp(hdr_name, "status") == 0) { debug((DEBUG_OBESE, "process_cgi: writing HTTP header " - "from status %s ..", tstr)); - bozoprintf("%s %s\r\n", request->hr_proto, tstr); + "from status %s ..", hdr_value)); + bozoprintf("%s %s\r\n", request->hr_proto, hdr_value); bozoflush(stdout); write_header = 0; - free(str); + free(hdr_name); break; } hdr = bozomalloc(sizeof *hdr); - hdr->h_header = val; - hdr->h_value = str; + hdr->h_header = hdr_name; + hdr->h_value = hdr_value; SIMPLEQ_INSERT_TAIL(&headers, hdr, h_next); nheaders++; } @@ -380,7 +362,7 @@ finish_cgi_output(http_req *request, int in, int nph) "headers ..")); SIMPLEQ_FOREACH(hdr, &headers, h_next) { bozoprintf("%s: %s\r\n", hdr->h_header, hdr->h_value); - free(hdr->h_value); + free(hdr->h_header); free(hdr); } bozoflush(stdout); @@ -404,6 +386,35 @@ finish_cgi_output(http_req *request, int in, int nph) } } +static int +parse_header(const char * str, ssize_t len, char ** hdr_str, char ** hdr_val) +{ + char * name, * value; + + /* if the string passed is zero-length bail out */ + if (*str == '\0') + return -1; + + name = value = bozostrdup(str); + + /* locate the ':' separator in the header/value */ + name = strnsep(&value, ":", &len); + + if (NULL == name || -1 == len) { + free(name); + return -1; + } + + /* skip leading space/tab */ + while (*value == ' ' || *value == '\t') + len--, value++; + + *hdr_str = name; + *hdr_val = value; + + return 0; +} + /* * given the file name, return a CGI interpreter */ |
