[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Fw: [cobalt-users] C libraries for CGI
Mr. WebMost,
If you want to try it in c, here's one that compiles okay on a Raq2 with
just the standard libraries; no flames- I'm just posting it here so
everyone can see how easy it is. Errors? I didn't see any- if you use
this you're taking your OWN chances. I REPEAT, if you kill your own
machine/ core dump/ whatever, don't blame me- I offer this as one
possible example, could be and probably is buggy, of how it could be
done. Furthermore, I probably stole this from someone many years ago.
Now with that said, just save the folowing text (minus my signature of
course) as a text file called "something.c", upload it to your machine,
telnet in to the same directory you uploaded it to, and type "cc
something.c -o something.cgi". Then do an "ls -o" to make sure the new
file called "something.cgi" is set to be executable. Next create any
form that posts data to this cgi's url and watch the fun; hey, it works
for me!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
/* C program example to parse data */
void closeup(int v)
{
exit(v);
}
void dewebify(char *str)
{
int x,y;
for (x=0,y=0; str[y] != '\0'; x++,y++)
switch (str[y]) {
case '+':
str[x] = ' ';
break;
case '%':
str[y+1] = toupper(str[y+1]);
str[y+2] = toupper(str[y+2]);
if (str[y+1] >= 'A') str[x] = (str[y+1] - 'A' + 10) * 16;
else str[x] = (str[y+1] - '0') * 16;
if (str[y+2] >= 'A') str[x] += (str[y+2] - 'A' + 10);
else str[x] += (str[y+2] - '0');
y+=2;
break;
default:
str[x] = str[y];
}
str[x]='\0';
}
int parse_input(char *name[], char *value[], int maxn)
{
int length,c,i;
char *line,*lstr;
/* Get the size of the input */
lstr = getenv("CONTENT_LENGTH");
if (lstr == NULL) {
printf("CONTENT_LENGTH environment variable is missing!\n");
closeup(-1);
}
length = atoi(lstr);
/* Allocate memory so we can read the data */
line = (char *)malloc(length+1);
if (line == NULL) {
printf("Out of memory!\n");
closeup(-1);
}
/* Read the data */
if (read(0,line,length) < length) {
printf("Error: Couldn't read input!\n");
closeup(-1);
}
/* Break the data up into name=value strings. */
for (c=0,name[c] = strtok(line,"&");
name[c] != NULL && c < maxn;
name[++c] = strtok(NULL,"&"));
for (i=0; i< c; i++) {
value[i] = strchr(name[i],'=');
if (value[i] == NULL) {
printf("No '=' found in name/value pair\n");
closeup(-1);
}
*value[i] = '\0';
value[i] = value[i]+1;
}
/* Clean up the remaining text. */
for (i=0; i<c; i++) {
dewebify(name[i]);
dewebify(value[i]);
}
/* Return the number of pairs read in */
return c;
}
int main(int argc, char **argv, char **envp)
{
int vcount,i;
char *name[16],*value[16],*reqm;
/* Tell the server and browser the kind of data we are sending*/
printf("Content-type: text/html\n\n");
printf("<TITLE>Parse CGI from C Example</TITLE>\n");
printf("<center>\n<H1>Parse CGI from C Example<br>\n<hr></H1></center>\n");
/* Parse the query string. */
vcount = parse_input(name,value,16);
/* Get the REQUEST_METHOD. */
reqm = getenv("REQUEST_METHOD");
if (reqm == NULL) {
printf("Unkown REQUEST_METHOD\n");
closeup(-1);
}
printf("Posted with the <b>%s</b> method<p>\n",reqm);
printf("%d Name/Value Pairs:<p><pre>\n",vcount);
/* Now print out the values */
for (i=0;i<vcount;i++)
printf("%10s: %s\n",name[i],value[i]);
printf("</pre>\n");
closeup(0);
/* You shouldn't ever get here */
return 0;
}
--
Clayton McGow
support@xxxxxxxxxxx
http://www.atoznet.com