QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1512|回复: 1

web服务器支持CGI的一个问题

[复制链接]
发表于 2005-5-24 18:27:03 | 显示全部楼层 |阅读模式
我用c写的一个web服务器,支持CGI时候出错,具体情况如下:
可以正确解析出cgiPath,caiName
如:cgiPath = "./cgi-bin/vote.pl";
      cgiName = "vote.pl";
envp也正确赋值
如:
      envp[0] = "REQUEST_METHOD=POST";
      envp[1] = "CONTENT_LENGTH=6";
      envp[2] = "CONTENT_TYPE=text/html";
其中,要执行的文件为vote.pl的perl脚本(execle(cgiPath,cgiName,(char *)0,envp)),为什么执行会出错?
已经chmod a+rx vote.pl,vote.pl 了,错误提示;Bad Adress,而且我已经试过把cgiPath改为"/root/my/cgi-bin/vote.pl"
现在关键问题是:cgi不能正常运行,不能将执行结果输出到STDOUT,请高手指点。
cgi脚本是从网上下的:
#!/usr/bin/perl
print"Content-type:text/html\n\n";
print"<titel>投票系统</title>";

if($ENV{'REQUEST_METHOD'}eq"POST"){
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
}elsif($ENV{'REQUEST_METHOD'}eq"GET"){
$buffer=$ENV{'QUERY_STIRNG'};
}

@pairs=split(/&/,$buffer);
foreach $pair(@pairs){
($name,$value)=split(/=/,$pair);
$value=~tr/+//;
$value=~s/%([a-f A-F 0-9][a-f A-f 0-9])/pack("C",hex($1))/eg;
$FORM{$name}=$value;}

$filename="/vote.dat";
%NAME=("A","张德陪","B","阿加西","C","桑普拉斯","D","贝克","E","顾懿");

if($ENV{'REQUEST_METHOD'}eq"POST"){
print"Content-type:text/html\n\n";
print"<titel>投票系统</title>";
print"<h1>投票系统的例子</h1>";
open(FILE,"<$filename")||die"不能打开文件,请和管理员联系\n";

for($i=0;$i<2;$i++){
$file[$i]=<FILE>;
$file[$i]=~s/\n$//;
}
close(FILE);

@item=split(/:/,$file[0]);
@vote=split(/:/,$file[1]);

for($i=0;$i<@item;$i++){
if($FORM{'idol'}eq$item[$i]){
$vote[$i]++;
last;
}
}
open(FILE,">filename")||die"Can't Open the file";
$item=join(":",@item);
$vote=join(":",@vote);
pirnt FILE "$item\n";
print FILE "$vote\n";

close (FILE);

print"<h2>您是投票给$NAME{$FORM{'idol'}},谢谢您的选票!<h2>";
print"查询<a href=\"/cgi-bin/vote.pl?command=viem\">投票结果系统</a>";

}

if($FORM{'command'}eq"view"){
print "HTTP/1.0 200\n";
print "Content-type:text/html\n\n";
print"<title>投票结果</title>";
print"<h1>投票结果</h1>";
open (FILE,"$filename")||die"文件打开错误";

for($i=0;$i<2;$i++){
$file[$i]=<FILE>;
$file[$i]=~s/\n$//;
}
close(FILE);

@item=split(/:/,$file[0]);
@vote=split(/:/,$file[1]);

print"<table border=1>";

for($i=0;$i<@item;$i++){
print"<tr><td>姓名</td><td>$NAME{$item[$i]}</td><td>票数</td>,td>$vote[$i]</td><tr>";

}
print "</table>";
}
发表于 2005-7-19 13:21:21 | 显示全部楼层
用c写cgi强,支持一下!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-24 21:37 , Processed in 0.109489 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表