- 论坛徽章:
- 0
|
我在网页里面有一个表单,然后上面有一个按钮,点击按钮后执行相应的表单提取程序,但是有问题。但是我在boa上面运行就没有问题,我怀疑是我的apache设置有问题,但是又不知道怎么回事,毕竟CGI程序都运行了
我的表单提取程序如下:
#include<stdio.h>;
#include<stdlib.h>;
#include<string.h>;
#ifndef _CGIVARS_H
#define _CGIVARS_H
#include "sqlite.h" /*sqlite db*/
/* method */
#define GET 0
#define POST 1
#endif /* !_CGIVARS_H */
#define MAXQRY 200 /* sqlite db*/
#define MAXLEN 32
/* function prototypes */
int getRequestMethod();
char **getGETvars();
char **getPOSTvars();
int cleanUp(int form_method, char **getvars, char **postvars);
char hex2char(char *hex);
void unescape_url(char *url);
char x2c(char *what);
//void createdb(char ** postvars);
int n;
//char **postvars = NULL; /* POST request data repository */
//char **getvars = NULL; /* GET request data repository */
main()
{
//char **postvars = NULL; /* POST request data repository */
//char **getvars = NULL; /* GET request data repository */
char **postvars,str[20];
char **getvars,str2[20];
int form_method; /* POST = 1, GET = 0 */
int i;
int m;
postvars=&
getvars=&str2;
form_method = getRequestMethod();
if(form_method == POST) {
//getvars = getGETvars();
postvars = getPOSTvars();
} else if(form_method == GET) {
getvars = getGETvars();
}
//createdb(postvars);
printf("Content-type:text/html\n\n" ;
printf("<HTML>;\n" ;
printf("<HEAD>;\n" ;
printf("<TITLE>;HTML Form & CGI Script Demo Result</TITLE>;\n" ;
printf("</HEAD>;\n" ;
printf("<BODY>;\n" ;
printf("<H1>;HTML Form & CGI Script Demo Result</H1>;\n" ;
printf("here is a summary for your record of the information" ;
printf("as we receivved it.\n" ;
printf("<HR>;\n" ;
printf("< RE>;\n");
for (m=0;m<n;m++)
{
printf("postvars[m]: %s\n",postvars[m]);
}
printf("\n");
printf("</PRE>;\n");
printf("</BODY>;\n");
printf("</HTML>;\n");
//createdb(postvars);
//cleanUp(form_method, getvars, postvars);
fflush(stdout);
exit(0);
}
/* hex2char */
/* RFC */
char hex2char(char *hex) {
char char_value;
char_value = (hex[0] >;= 'A' ? ((hex[0] & 0xdf) - 'A') + 10 : (hex[0] - '0'));
char_value *= 16;
char_value += (hex[1] >;= 'A' ? ((hex[1] & 0xdf) - 'A') + 10 : (hex[1] - '0'));
return char_value;
}
/* unescape_url */
/* RFC */
void unescape_url(char *url) {
int n, k;
for(n=0, k=0;url[k];++n, ++k) {
if((url[n] = url[k]) == '%') {
url[n] = hex2char(&url[k+1]);
k += 2;
}
}
url[n] = '\0';
}
/* getRequestMethod
* retn: from_method (GET or POST) on success,
* -1 on failure. */
int getRequestMethod() {
char *request_method;
int form_method;
request_method = getenv("REQUEST_METHOD");
if(request_method == NULL)
return -1;
if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD") ) {
form_method = GET;
} else if (!strcmp(request_method, " OST")) {
form_method = POST;
} else {
/* wtf was it then?!! */
return -1;
}
return form_method;
}
/* getGETvars
* retn: getvars */
char **getGETvars() {
int i;
char **getvars;
char *getinput;
char **pairlist;
int paircount = 0;
char *nvpair;
char *eqpos;
getinput = getenv("QUERY_STRING");
if (getinput)
getinput = strdup(getinput);
/* Change all plusses back to spaces */
for(i=0; getinput && getinput; i++)
if(getinput == '+')
getinput = ' ';
pairlist = (char **) malloc(256*sizeof(char **));
paircount = 0;
nvpair = getinput ? strtok(getinput, "&") : NULL;
while (nvpair) {
pairlist[paircount++]= strdup(nvpair);
if(!(paircount%256))
pairlist = (char **) realloc(pairlist,(paircount+256)*sizeof(char **));
nvpair = strtok(NULL, "&");
}
pairlist[paircount] = 0;
getvars = (char **) malloc((paircount*2+1)*sizeof(char **));
for (i= 0; i<paircount; i++) {
if(eqpos=strchr(pairlist, '=')) {
*eqpos = '\0';
unescape_url(getvars[i*2+1] = strdup(eqpos+1));
} else {
unescape_url(getvars[i*2+1] = strdup(""));
}
unescape_url(getvars[i*2] = strdup(pairlist));
}
getvars[paircount*2] = 0;
for(i=0;pairlist;i++)
free(pairlist);
free(pairlist);
if (getinput)
free(getinput);
return getvars;
}
/* getPOSTvars
* retn: postvars */
char **getPOSTvars() {
int i;
int content_length;
char **postvars;
char *postinput;
char **pairlist;
int paircount = 0;
char *nvpair;
char *eqpos;
postinput = getenv("CONTENT_LENGTH");
if (!postinput)
exit(1);
if(!(content_length = atoi(postinput)))
exit(1);
if(!(postinput = (char *) malloc(content_length+1)))
exit(1);
if (!fread(postinput, content_length, 1, stdin))
exit(1);
postinput[content_length] = '\0';
for(i=0;postinput;i++)
if(postinput == '+')
postinput = ' ';
pairlist = (char **) malloc(256*sizeof(char **));
paircount = 0;
nvpair = strtok(postinput, "&");
while (nvpair) {
pairlist[paircount++] = strdup(nvpair);
if(!(paircount%256))
pairlist = (char **) realloc(pairlist, (paircount+256)*sizeof(char **));
nvpair = strtok(NULL, "&");
}
n=paircount*2+1;
pairlist[paircount] = 0;
postvars = (char **) malloc((paircount*2+1)*sizeof(char **));
for(i = 0;i<paircount;i++) {
if(eqpos = strchr(pairlist, '=')) {
*eqpos= '\0';
unescape_url(postvars[i*2+1] = strdup(eqpos+1));
} else {
unescape_url(postvars[i*2+1] = strdup(""));
}
unescape_url(postvars[i*2]= strdup(pairlist));
}
postvars[paircount*2] = 0;
for(i=0;pairlist;i++)
free(pairlist);
free(pairlist);
free(postinput);
return postvars;
}
/* cleanUp
* free the mallocs */
int cleanUp(int form_method, char **getvars, char **postvars) {
int i;
if (postvars) {
for(i=0;postvars;i++)
free(postvars);
free(postvars);
}
if (getvars) {
for(i=0;getvars;i++)
free(getvars);
free(getvars);
}
return 0;
}
出现在访问页面上的是printf出的语句,还有一些乱七八糟的类似调试的乱码信息:
ELFp?44 (44 |
|