f1437cec3b3c4441c9da6fa80bee99fe8e328d87 kent Wed Apr 3 00:33:14 2013 -0700 Adding cgiEncodeIntoDy into library. Exposing cgiParseInputAbort to public. Improving a comment. diff --git src/lib/cheapcgi.c src/lib/cheapcgi.c index 763812f..cdf4635 100644 --- src/lib/cheapcgi.c +++ src/lib/cheapcgi.c @@ -561,36 +561,37 @@ /* Get raw CGI input into inputString. Method can be "POST", "QUERY", "GET" or NULL * for unknown. */ { if (inputString == NULL) { method = cgiInputSource(method); if (sameWord(method, "POST")) getPostInput(); else if (sameWord(method, "QUERY") || sameWord(method, "GET")) getQueryInput(); else errAbort("Unknown form method"); } } -static void cgiParseInputAbort(char *input, struct hash **retHash, +void cgiParseInputAbort(char *input, struct hash **retHash, struct cgiVar **retList) /* Parse cgi-style input into a hash table and list. This will alter * the input data. The hash table will contain references back * into input, so please don't free input until you're done with - * the hash. Prints message aborts if there's an error.*/ + * the hash. Prints message aborts if there's an error. + * To clean up - slFreeList, hashFree, and only then free input. */ { char *namePt, *dataPt, *nextNamePt; struct hash *hash = *retHash; struct cgiVar *list = *retList, *el; if (!hash) hash = newHash(6); slReverse(&list); namePt = input; while (namePt != NULL && namePt[0] != 0) { dataPt = strchr(namePt, '='); if (dataPt == NULL) { @@ -616,31 +617,32 @@ } static jmp_buf cgiParseRecover; static void cgiParseAbort() /* Abort cgi parsing. */ { longjmp(cgiParseRecover, -1); } boolean cgiParseInput(char *input, struct hash **retHash, struct cgiVar **retList) /* Parse cgi-style input into a hash table and list. This will alter * the input data. The hash table will contain references back * into input, so please don't free input until you're done with - * the hash. Prints message and returns FALSE if there's an error.*/ + * the hash. Prints message and returns FALSE if there's an error. + * To clean up - slFreeList, hashFree, and only then free input. */ { boolean ok = TRUE; int status = setjmp(cgiParseRecover); if (status == 0) /* Always true except after long jump. */ { pushAbortHandler(cgiParseAbort); cgiParseInputAbort(input, retHash, retList); } else /* They long jumped here because of an error. */ { ok = FALSE; } popAbortHandler(); return ok; } @@ -1920,30 +1922,42 @@ char *e; for (cv = inputList; cv != NULL; cv = cv->next) { if (cv != inputList) dyStringAppend(dy, "&"); e = cgiEncode(cv->val); dyStringPrintf(dy, "%s=", cv->name); dyStringAppend(dy, e); freez(&e); } return dy; } +void cgiEncodeIntoDy(char *var, char *val, struct dyString *dy) +/* Add a CGI-encoded &var=val string to dy. */ +{ +if (dy->stringSize != 0) + dyStringAppendC(dy, '&'); +dyStringAppend(dy, var); +dyStringAppendC(dy, '='); +char *s = cgiEncode(val); +dyStringAppend(dy, s); +freez(&s); +} + void cgiContinueAllVars() /* Write back all CGI vars as hidden input for next time around. */ { struct cgiVar *cv; for (cv = inputList; cv != NULL; cv = cv->next) cgiMakeHiddenVar(cv->name, cv->val); } boolean cgiFromCommandLine(int *pArgc, char *argv[], boolean preferWeb) /* Use the command line to set up things as if we were a CGI program. * User types in command line (assuming your program called cgiScript) * like: * cgiScript nonCgiArg1 var1=value1 var2=value2 var3=value3 nonCgiArg2 * or like