简体   繁体   English

具有C包装程序的Perl CGI

[英]Perl CGI with C Wrapper

Basis of the problem. 问题的基础。 I have a university assignment requiring me to write a Perl/CGI based website for a phonebook. 我有一个大学作业,要求我为电话簿编写一个基于Perl / CGI的网站。 This part is fine and I'm happy with it, however, I have issues with wrapping the cgi files. 这部分很好,我对此感到满意,但是,我在包装cgi文件时遇到了问题。 I've done it once before no issues but been unable to replicate that success this time doing the same thing. 我已经完成了一次,没有任何问题,但是这次却无法复制同样的成功。

Basic Perl file to show user ID's: 基本的Perl文件,显示用户ID:

#!/usr/bin/perl -w

use English;

print "Content-type: text/html";

print "\n";
print "\n";
print "\n";

print "<html>\n";
print "<head><title>IDS.CGI</title></head>\n";
print "<body>\n"; 
print "<p>\nMy User ID is $UID\n</p>";
print "<p>\nMy effective User ID is $EUID\n</p>";
print "<p>\nMy Group ID is $GID\n</p>";
print "<p>\nMy effective Group ID is $EGID\n</p>";
print "\n</body>\n";
print "</html>\n";

Wrapper.C: 包装器C:

#include <stdio.h>
#include <unistd.h>

#define REAL_PATH "ids.pl"

int
main()
{
    execl( REAL_PATH, REAL_PATH, 0 );
    printf( "You should never see this message!\n" );
}

This is throwing an internal server error 500. I've tried my best to debug it including spacing for the headers etc. It runs fine in terminal but not in the web browsers. 这将引发内部服务器错误500。我已尽力对其进行了调试,包括标头等的间距。它在终端中运行良好,但在Web浏览器中运行不正常。 The servers httpd error log shows that the error of "Premature end of headers". 服务器httpd错误日志显示错误“标题的结尾过早”。 I cannot see how there is a premature end however. 但是,我看不到过早的结局。

Any help anyone can offer would be greatly appreciated. 任何人都可以提供的任何帮助将不胜感激。

Like any system call, you should always be checking for an error from execl() . 像任何系统调用一样,您应该始终检查execl()的错误。 Normally you'd look at the return value, but it's not necessary because success will terminate the program. 通常,您会查看返回值,但这不是必需的,因为成功将终止程序。

execl( REAL_PATH, REAL_PATH, 0 );
perror("exec of '"REAL_PATH"' failed");

This uses perror to handle turning errno into a human readable error string and printing it to stderr. 这使用perror处理将errno转换为人类可读的错误字符串并将其打印到stderr。

I'd also avoid using #define for string constants, they're awkward to work with. 我还要避免对字符串常量使用#define ,因为它们使用起来很尴尬。 Instead use static const char REAL_PATH[] = "ids.pl" as suggested in this answer . 而是按照此答案中的建议使用static const char REAL_PATH[] = "ids.pl"

And I don't understand why you need a C wrapper. 而且我不明白为什么需要C包装器。 Some sort of weird restriction on your web server running interpreted code? 对您的Web服务器运行解释代码有某种奇怪的限制吗?

I wasn't compiling the C Wrapper on the actual server therefore getting different machine codes which weren't compatible. 我没有在实际服务器上编译C Wrapper,因此得到了不兼容的不同机器代码。 Unfortunately, the server denied to compile it originally and I forgot when it did that it had to be compiled on that machine. 不幸的是,服务器拒绝最初对其进行编译,而我忘记了必须在该计算机上对其进行编译。 Doh me! 哦,我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM