Remembering Pete
--#--
As you recall from Pete.java, all the request parameters will be
strung together into one long string. We also want to URL encode them.
The function I'll include here simply converts space characters, but
you may want to explore a more complete encoding.
char *formContent() {
int i, len = 0;
char s[128];
char *newStr;
numParams = sizeof(params) / sizeof(char *);
for (i = 0; i< numParams; i++)
len += strlen(urlEncode(s, params[i]));
len += numParams;
if ((newStr = (char *)malloc(len)) == NULL)
die("Out of memory");
strcpy(newStr, urlEncode(s, params[0]));
strcat(newStr, "&");
for (i=1; i< numParams; i++) {
strcat(newStr, urlEncode(s, params[i]));
strcat(newStr, "&");
}
newStr[len-1] = '\0';
return newStr;
}
We calculate the number of parameters by measuring the array that
holds them, divided by the size of each parameter (which is, after
all, a pointer to the first char in the parameter). Then we measure
the length of the request as we string all the parameters together.
The extra padding (len += numParams) accounts for the ampersand that
joins the elements together. At the end we lop off the last
ampersand.