luckycc27 发表于 2012-04-02 23:03

有关用户留言簿CGI程序设计 求解决 叩谢

有一个用户留言簿CGI程序困扰了我两个礼拜了 前置html网页POST进去但是linux下总是跑不通,大致程序如下:其中我有一大部分都注释掉了没必要看,主要替我看下主程序和主要的取数据程序,放到乌帮图虚拟机下程序虽然能GCC编译成功,但有个warning:passing argument 1 of 'fopen' from incompatible pointer type /usr/include/stdio.h:249:note:expected'const char *_restrict_'but argument is of type'struct FILE',可能对解决问题有用也可能无所谓,但我主要的问题就是我前置网页点击按键调用这个cgi程序时出现502BAD GATEWAY,这个问题很常见,一般来说就是程序的问题,我用gdb单步调试这个程序,gdb上显示Program received signal SIGSEGV,Segmentation fault.0x001a0de0 in strcpy() from /lib/tls/i686/cmov/libc.s0.6,这个问题不管我如何修改程序都有,除非我把get_inputs和voidprint_file都注释掉,请问这个问题该如何解决?这个错误究竟什么意思?这问题真的让我很头疼,我一度还怀疑环境变量不存在所以才读不进去,我env了下linux没发现REQUEST_STRING那些环境变量但是环境变量在这里应该是自动生成的呀,每天都在想这问题真的很头痛,求哪位大哥能帮我解决下,程序比较长对不住了。是不是我程序里的strcpy函数用的不对?导致读不进去,或者谁有交互式表单的CGI程序解我燃眉之急,主要功能就是html页面上输入信息传给服务器服务器能读取输入的数据并将数据显示在web浏览器上,叩谢了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//void translate(char *sourcestr);
int get_inputs();
void html_header(char *headtitle, char *bodytitle);
void html_foot();
void print_file(char *name , char *email , char *comments);
//void display_counter();
void print_form();

/*预定义的一些常量*/

#define advicefile "http://192.168.12.134/cgi-bin/UserAdvice.txt"
#define counter "http://192.168.12.134/cgi-bin/counter.dat"
#define keyfile1 "http://192.168.12.134/cgi-bin/keyfile1.dat"
#define keyfile2 "http://192.168.12.134/cgi-bin/keyfile2.dat"

/*变量说明*/

typedef struct {
char *name;
char *value;
} htmlinput_struct;
htmlinput_struct htmlinputs;
int htmlinputcount = 0;

char *yourname;
char *youremail;
char *yourcomments;

/*主程序*/

int main ()
{
int i;
/*下面根据环境变量REQUEST_METHOD的值从标准输入中或者从环境变量QUERY_STRING中
读取从客户机发送来的表单输入数据。如果无用户输入数据,则显示一个HTML表单并结束程序。*/
if(!get_inputs())
{
print_form();
return 1 ;
}

/*下面从htmlinputs中读取HTML表单变量:yourname,
youremail,yourcomments*/
for (i=0;i<htmlinputcount;i++)
{
if (! strcmp(htmlinputs.name,"yourname"))
strcpy(yourname,htmlinputs.value);
if (! strcmp(htmlinputs.name,"youremail"))
strcpy(youremail,htmlinputs.value);
if (! strcmp(htmlinputs.name,"yourcomments"))
strcpy(yourcomments, htmlinputs.value);
}
/*检查yourname的值和yourcomments的值是否为空,
如果是的话,则输出"输入错误"的响应结果以后结束程序*/
if (! strcmp(yourname,"") || ! strcmp(yourcomments,""))
{
html_header("用户意见簿---相应","输入错误");
printf("<H2>必须输入你的名字并发表意见!</H2> \n");
html_foot();
return(1);
}
/*下面将用户信息存入用户意见文件UserAdvice.txt,
并输出“输入正确”的响应结果*/
print_file(yourname,youremail,yourcomments);
html_header("用户意见簿---响应","输入正确");
printf("<H2>非常感谢你的宝贵意见! </H2> \n");
//display_counter();
html_foot();
return(0);
}
/*子程序translate,功能:将字符串sourcestr中用%后跟两
个十六进制数表示的字符转换成正常字符*/
/*void translate(char *sourcestr)
{
int i = 0;
int j = 0;
char *tempstr;
char tempchar1 , tempchar2;
tempstr = malloc(sizeof(char) * strlen(sourcestr)+1);
while (sourcestr)
{
if ((tempstr = sourcestr) == '%')
{
if (sourcestr >= 'A')
tempchar1 = ((sourcestr & 0xdf) - 'A')+10;
else
tempchar1 = (sourcestr - '0');
if (sourcestr>='A')
tempchar2 = ((sourcestr & 0xdf) - 'A')+10;
else
tempchar2 = (sourcestr - '0');
tempstr = tempchar1*16 + tempchar2;
j=j+2;
}
i++;
j++;
}
tempstr = '\0';
strcpy(sourcestr,tempstr);
}
/*子程序:get_inputs*,功能:读取从HTML表单输入的用户信息。
如果有表单输入,则将表单变量与值成对存放到结构htmlinputs
中,变量数保存在htmlinputcount中,最后返回1.如果无表单输
入,则返回0*/
int get_inputs()
{
int i,j;
int length;
char *method;
char *inputstring;
char *tempstr;

strcpy(method, getenv("REQUEST_METHOD"));
if (! strcmp(method,"POST"))
{
length = atoi(getenv("CONTENT_LENGTH"));
if (length != 0)
{
inputstring = malloc(sizeof(char) * length +1);
fread(inputstring,sizeof(char),length,stdin);
}
}
else if (! strcmp(method, "GET"))
{
strcpy(inputstring,getenv("QUERY_STRING"));
length = strlen(inputstring);
}
if (length == 0)
return 0;
j = 0;
tempstr = malloc(sizeof(char) * length +1);
for (i=0;i<length;i++)
{
if (inputstring == '=')
{
tempstr = '\0';
//       translate(tempstr);
strcpy(htmlinputs.name,tempstr);
if (i == length -1)
{
strcpy(htmlinputs.value,"");
htmlinputcount++;
}
j=0;
}
else if ((inputstring == '&') || (i == length -1))
{
if (i == length -1)
{
tempstr = inputstring;
j++;
}
tempstr = '\0';
//       translate(tempstr);
strcpy(htmlinputs.value,tempstr);
htmlinputcount ++;
j=0;
}
else if (inputstring == '+')
{
tempstr = ' ';
j++;
}
else
{
tempstr = inputstring;
j++;
}
}
return 1;
}
/*子程序:html_header,功能:输出UserAdvice对用户信息响应结果
的头部分。无论响应结果如何,其头部分都是基本相同的。*/
void html_header(char * headtitle,char * bodytitle)
{
printf("Content-type: text/html \n\n");
printf("<HTML> \n");
printf("<HEAD> \n");
printf("<TITLE>%s</TITLE> \n");
printf("<H1>%s<H1> \n",bodytitle);
printf("</CENTER><BR> \n");
}
/*子程序:html_foot,功能:输出UserAdvice对用户信息的响应结果
的结尾部分。无论响应结果如何,其结尾部分都是相同的。*/
void html_foot()
{
printf("</BODY> \n");
printf("</HTML> \n");
}
/*子程序:print_file 功能:将用户所输入的HTML表单变量:yourname
youremail,yourcomments依次存入存放用户意见的文本文件UserAdvice.txt*/
void print_file(char*name , char*email , char*comments)
{
FILE *keyfile1_fp;
FILE *advicefile_fp;

while((keyfile1_fp = fopen(keyfile1_fp , "r"))!= NULL)
{
fclose(keyfile1_fp);
}
keyfile1_fp = fopen(keyfile1,"w");
fclose(keyfile1_fp);

if ((advicefile_fp = fopen(advicefile,"a")) == NULL)
{
unlink(keyfile1);
html_header("用户意见簿---响应","文件错误");
printf("<H2>不能打开%s文件,请重试。<H2> \n",advicefile);
html_foot();
exit(1);
}
fprintf(advicefile_fp , "所输入的名字:%s \n" , name);
fprintf(advicefile_fp,"所输入的email:%s \n",email);
fprintf(advicefile_fp,"所输入的意见:\n");
fprintf(advicefile_fp,"%s \n",comments);
fprintf(advicefile_fp,"********************\n");
fclose(advicefile_fp);
unlink(keyfile1);
}

/*子程序display_counter,功能:显示该用户是第几位提出意见的用户*/
/*void display_counter()
{
FILE *keyfile2_fp;
FILE *counter_fp;
char count_str;
int count;

while((keyfile2_fp = fopen(keyfile2,"r")) != NULL)
{
fclose(keyfile2_fp);
}
keyfile2_fp = fopen(keyfile2,"w");
fclose(keyfile2_fp);

if ((counter_fp = fopen(counter,"r")) == NULL)
{
unlink(keyfile2);
html_header("用户意见簿---响应","文件错误");
printf("<H2>不能打开%s文件,请重试。<H2>\n",counter);
html_foot();
exit(1);
}
fgets(count_str,10,counter_fp);
fclose(counter_fp);

count = atoi(count_str);
count++;
printf("<BR><BR><BR><B><CENTER>\n");
printf("您是给我们提意见的第%d位用户。\n",count);
printf("</B></CENTER>\n");
counter_fp = fopen(counter,"w");
fprintf(counter_fp,"%d\n",count);
fclose(counter_fp);
unlink(keyfile2);
}*/
void print_form()
{
html_header("用户意见簿","CGI程序实例:用户意见簿");
printf("<FORM METHOD = POST ACTION = /cgi-bin/UserAdvicef> \n");
printf("<B>您的名字:</B><INPUT SIZE = 50 NAME = \"yourname\"><BR>\n");
printf("<B>您的E-mail:</B><INPUT SIZE = 50 NAME = \"youremail\"><BR>\n");
printf("<B>请发表意见:</B>\n");
printf("<TEXTAREA ROWS = 5 COLS = 50 NAME = \"yourcomments\">\n");
printf("</TEXTAREA>\n");
printf("<BR><BR><CENTER>\n");
printf("<INPUT TYPE = \"SUBMIT\"VALUE = \"发送\"NAME = \"Submit\">\n");
printf("</CENTER></FORM>\n");
html_foot();
}

luckycc27 发表于 2012-04-03 10:33

有没有哪位高人?是strcpy()还是指针还是环境变量的问题 而且每次退出gdb会显示a debugging session is active ,然后提示要kill掉一个进程 搞不懂
页: [1]
查看完整版本: 有关用户留言簿CGI程序设计 求解决 叩谢