/* pieter_cgi.c See pieter_cgi.h for the API specification. PoZeTools v 0.46, may 8, 2004. Copyright (c) 2002-2004 Pieter Suurmond. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include "pieter_cgi.h" /* 4 constants, 1 typedef, 2 function prototypes. */ /*-----------------------------------------------------------------------------------------------*/ int CGI_receive(CGI_INPUT** handle, long max_bytes, int max_pairs) { char *rd, *wr, *Name, *Value, *endOfData, *empty=""; CGI_INPUT* ptr; int hex, c, succes, dellen, e = -99; /* -99 = unknown error. */ long togo; if (!handle) /* No handle supplied! */ return -1; ptr = *handle = malloc(sizeof(CGI_INPUT)); /* Possibly resets *handle. */ if (!ptr) return -2; ptr->data = (char*)NULL; /* We'll allocate later, now for safe mem-release. */ togo = (long)max_pairs * (long)sizeof(char*); ptr->names = malloc(togo); ptr->values = malloc(togo); ptr->filenames = malloc(togo); /* Safe to call (release_CGI_INPUT) from now on. */ ptr->conttypes = malloc(togo); ptr->datasizes = malloc(max_pairs * sizeof(long)); if (!(ptr->names && ptr->values && ptr->filenames && ptr->conttypes && ptr->datasizes)) { e = -3; goto receiveDone; } for (togo = 0; togo < max_pairs; togo++) { ptr->names[togo] = ptr->values[togo] = ptr->filenames[togo] = ptr->conttypes[togo] = (char*)NULL; ptr->datasizes[togo] = 0L; } ptr->content_type = kCGI_content_type_NORMAL; /* Default for both GET and POST. */ ptr->pairs = 0; /*--------------------------------------------------------------------------------*/ rd = getenv("REQUEST_METHOD"); if (!rd) { e = -4; goto receiveDone; } /* Frees data and resets *handle. */ if (!strcmp(rd, "GET")) { ptr->request_method = kCGI_request_method_GET; rd = getenv("QUERY_STRING"); /* String rd is read below! */ if (!rd) { e = -5; goto receiveDone; } /* Frees data and resets *handle. */ ptr->length = (long)strlen(rd); /* Strlen() is inefficient here? */ } else if (!strcmp(rd, "POST")) { ptr->request_method = kCGI_request_method_POST; rd = getenv("CONTENT_LENGTH"); if (!rd) { e = -6; goto receiveDone; } /* Frees data and resets *handle. */ ptr->length = atol(rd); /* How many bytes from common gateway? */ rd = getenv("CONTENT_TYPE"); /* Only POST can be multipart! */ if (!rd) { e = -7; goto receiveDone; } if (!strncmp(rd, "multipart/form-data;", 20)) /* Check 20 first charachters. */ ptr->content_type = kCGI_content_type_MULTI; } else { e = -8; goto receiveDone; } /* Neither "GET" nor "POST". */ /*-----------------------------------------------------------------------------------*/ if (ptr->length < 1) /* No input data or negative!!! length. */ { e = 0; goto receiveDone; } /* (0 is not regarded an error.) */ if (ptr->length > max_bytes) { e = -9; goto receiveDone; } /* Too much input. */ ptr->data = malloc(1 + ptr->length); /* One extra for string termination. */ if (!ptr->data) /* During decoding, input SHORTENS. */ { e = -10; goto receiveDone; } /* Not enough memory. */ wr = ptr->data; /*-----------------------------------------------------------------------------------*/ if (ptr->content_type == kCGI_content_type_MULTI) { if (ptr->request_method != kCGI_request_method_POST) { e = -11; goto receiveDone; } /* Combi GET & MULTI not supported yet! */ /*----------------- Read all at once (binary): ----------------------------------*/ if (ptr->length != fread (ptr->data, sizeof(char), ptr->length, stdin)) { e = -12; goto receiveDone; } /* No decoding necessary. */ endOfData = &(ptr->data[ptr->length]); /* Remember end for perhaps binary read. */ *endOfData = (char)0; /* Terminate whole. */ /*-------------------------- first line contains multipart-delimiter: -----------*/ while ((c = *wr)) { if ((c==10)||(c==13)) break; else wr++; } if (!c) { e = -13; goto receiveDone; } /* Delimiter stored at ptr->data, */ dellen = wr - ptr->data; /* stripped all trailing newlines */ *wr++ = (char)0; /* and carriage returns, terminated. */ while (*wr) { if ((*wr==10)||(*wr==13)) wr++; /*-------------------------- One or more lines containing type name(s): -----*/ rd = wr; wr = strstr (rd, "\n\r\n"); /* Look for single empty line. */ if (!wr) { e = -14; goto receiveDone; } *wr = (char)0; wr += 3; rd = strstr(rd, "name=\""); /* Look for name attribute. */ if (!rd) { e = -15; goto receiveDone; } rd += 6; ptr->names[ptr->pairs] = rd; /* Store name. */ rd = strchr (rd, '\"'); if (!rd) { e = -16; goto receiveDone; } *rd++ = (char)0; /* Terminate name. */ if (!*ptr->names[ptr->pairs]) ptr->names[ptr->pairs] = (char*)NULL; /* Name may be missing, i.e. NULL ?! */ rd = strstr(rd, "filename=\""); /* Look for filename attribute. */ if (rd) { rd += 10; ptr->filenames[ptr->pairs] = rd; /* Store filename. */ rd = strchr (rd, '\"'); if (!rd) { e = -17; goto receiveDone; } *rd++ = (char)0; /* Terminate filename. */ if (!*ptr->filenames[ptr->pairs]) ptr->filenames[ptr->pairs] = (char*)NULL; /* NULL instead of "". */ /*-------- "Content-Type: (text/html or image/gif, etc): --------------*/ rd = strstr(rd, "Content-Type: "); if (rd) { rd += 14; /* Could be already terminated when followed by empty line. */ ptr->conttypes[ptr->pairs] = rd; while ((c = *rd)) { if ((c == 10) || (c == 13)) { *rd = (char)0; break; } rd++; } if (!*ptr->conttypes[ptr->pairs]) ptr->conttypes[ptr->pairs] = (char*)NULL; /* NULL instead of "". */ } } /*-------------------------- One or more lines containing value: ------*/ rd = wr; /* We're just after an empty line now. */ while (wr < endOfData) /* Look for delimiter preceded by "\r\n", */ { /* strstr() cannot be used in binary data. */ c = *wr++; if ((c == 10) || (c == 13)) { if (!strncmp(ptr->data, wr, dellen)) goto eofValFnd; /* Bit ugly, all these jumps, but trying */ } /* to keep this loop as small as possible. */ } e = -19; goto receiveDone; /* -18 means delimiter/terminator not found! */ /*-----------------------------------------------------------------------*/ eofValFnd: wr -= 2; c = *wr; if ((c != 10) && (c != 13)) /* Normally both, but just 1 is also ok. */ wr++; *wr = (char)0; /* Terminate value. */ ptr->datasizes[ptr->pairs] = wr - rd; if (ptr->datasizes[ptr->pairs]) /* Store value, EVEN WHEN IT STARTS WITH \0! */ ptr->values[ptr->pairs] = rd; /* Only NULL when really no data there. */ if ((c == 10) || (c == 13)) wr++; wr += 1 + dellen; ptr->pairs++; if (!strcmp(wr, "--\r\n")) { e = 1; goto receiveDone; } /* OK. 1 means some data is there. */ } e = -20; goto receiveDone; /* Premature end or parsing error. */ } /* End of multipart data reception. */ /*-------------------------------------------------------- Decoding for both GET and POST: --*/ Name = wr; /* Remember start of type-string. */ Value = empty; togo = ptr->length; /* Already tested for <0 above... Hmmm.... */ while (togo-- > 0) { if (ptr->request_method == kCGI_request_method_GET) /* Read from string or stdin. */ c = *rd++; else c = getchar(); switch (c) { case '+': /* Convert space-character. */ *wr++ = ' '; break; case '%': togo -= 2; /* Read from STDIN or from string */ if (ptr->request_method == kCGI_request_method_GET) { succes = sscanf(rd, "%2x", &hex); rd += 2; } else succes = scanf("%2x", &hex); /* Read next 2 chars and interpret as hex.*/ if (succes != 1) /* (succes == EOF) Unexpected HEX end! */ { e = -21; goto receiveDone; } *wr++ = (char)hex; break; case '=': *wr++ = (char)0; /* Mark end of type-string. */ if (*Name == 0) /* Empty type string: NULL-pointer! */ { e = -22; goto receiveDone; } /* Missing type! */ ptr->names[ptr->pairs] = Name; /* Name-string appeared to be valid. */ Name = empty; Value = wr; /* Remember start of expected value. */ break; /* Multiple = chars not allowed. */ case '&': *wr++ = (char)0; /* Terminate value-string. */ if (*Value) /* Assign pointer if str not empty. */ { ptr->values[ptr->pairs] = Value; Value = empty; } if (!ptr->names[ptr->pairs]) /* Don't accept multiple &-chars. */ { e = -23; goto receiveDone; } /* Like &&&& */ ptr->pairs++; if (ptr->pairs >= max_pairs) /* Haven't got that many pointers? */ { e = -24; goto receiveDone; } /* Too many pairs! */ Name = wr; /* Remember beginning of new type. */ break; case EOF: { e = -25; goto receiveDone; } /* Unexpected end of input */ /* Not needed: break; */ default: *wr++ = c; break; } } /* Trailing & doesn't matter. */ *wr = (char)0; /* Terminate last value-string. */ if (*Value) /* Assign pointer if string not empty. */ ptr->values[ptr->pairs] = Value; if (ptr->names[0]) { ptr->pairs++; /* The number of names (some values may be NULL). */ e = 1; /* Ok, data there. */ } else e = -26; /* Error (not even one '=' to terminate first type). */ receiveDone: if (e < 0) /* Less than zero means error, no object returned. */ CGI_release(handle); /* Zero means no inputdata, but object will be there. */ return e; /* Above zero means some inputdata will be in object. */ } /*-------------------------------------------------------------------------------*/ void CGI_release(CGI_INPUT** handle) { if (handle) { if (*handle) { if ((*handle)->names) free((*handle)->names); if ((*handle)->filenames) /* Only used in case of file-upload. */ free((*handle)->filenames); if ((*handle)->values) free((*handle)->values); if ((*handle)->conttypes) free((*handle)->conttypes); if ((*handle)->datasizes) free((*handle)->datasizes); if ((*handle)->data) /* Assume NULL when not allocated! */ free((*handle)->data); free(*handle); /* Free object itself. */ *handle = (CGI_INPUT*)NULL; /* Also clear its' reference. */ } } }