#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 80
#define EXTRA 5
/* 4 for field name "data", 1 for "=" */
#define MAXINPUT MAXLEN+EXTRA+2
/* 1 for added line break, 1 for trailing NUL */
#define DATAFILE "/home/penge/cgi-bin/data.txt"
void unencode(char *src, char *last, char *dest)
{
for(; src != last; src++, dest++)
{
if(*src == '+')
*dest = ' ';
else if(*src == '%')
{
int code;
if(sscanf(src+1, "%2x", &code) != 1) code = '?';
*dest = code;
src +=2;
}
else
*dest = *src;
}
*dest = '\n';
*++dest = '\0';
}
int main(void)
{
char *lenstr;
char input[MAXINPUT], data[MAXINPUT];
long len;
printf("%s\n\n",
"Content-Type:text/html;charse=utf-8");
printf("<TITLE>Response</TITLE>");
lenstr = getenv("CONTENT_LENGTH");
if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > MAXLEN)
printf("<P>Error in invocation - wrong FORM probably.");
else
{
FILE *f = NULL;
fgets(input, len+1, stdin);
unencode(input+EXTRA, input+len, data);
printf("%s", input);
f = fopen(DATAFILE, "a");
if(f == NULL)
printf("<P>Sorry, cannot store your data.");
else
//fputs(data, f);
fclose(f);
printf("<P>谢谢! Your contribution has been stored.");
}
return 0;
}
程序去掉注释这句执行没问题,加上的话会出来这样的错误Premature end of script
headers,
我觉得可能是文件权限的问题,我把文件定义到/home/penge/cgi-bin/data.txt还是有
问题,应该怎么解决呢?
------解决方案--------------------
正确的HTTP响应头应该是:
HTTP/1.0 200
Content-Type:text/html;charse=utf-8
后面两个空行。
------解决方案--------------------
写cgi还是用perl吧。