如何解决调试服务器的cgi程序
| 我正在尝试用C制作一个可以处理对动态内容的请求的Web服务器。 网络服务器部分已经完成。我正在尝试执行以下命令: http:// localhost:1601 / cgi-bin / test?3&7 带有程序测试的代码如下:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <wordexp.h>
#define MAXLINE 300
int main(int narg,char * arg[]) {
char *buf,*p;
char arg1[MAXLINE],arg2[MAXLINE],content[MAXLINE];
int n1=0,n2=0;
/* Extract the two arguments */
if ((buf = getenv(\"QUERY_STRING\")) != NULL) {
p = strchr(buf,\'&\');
*p = \'\\0\';
strcpy(arg1,buf);
strcpy(arg2,p+1);
n1 = atoi(arg1);
n2 = atoi(arg2);
}
/* Make the response body */
sprintf(content,\"Welcome to add.com: \");
sprintf(content,\"%sTHE Internet addition portal.\\r\\n<p>\",content);
sprintf(content,\"%sThe answer is: %d + %d = %d\\r\\n<p>\",content,n1,n2,n1 + n2);
sprintf(content,\"%sThanks for visiting!\\r\\n\",content);
/* Generate the HTTP response */
printf(\"Content-length: %d\\r\\n\",(int)strlen(content));
printf(\"Content-type: text/html\\r\\n\\r\\n\");
printf(\"%s\",content);
if (fork()==0) {
printf(\"asdfagloiauergauhfgaiudfhg\");
execvp(\"ls\",arg);
printf(\"child of adder error\");
}
printf(\"%s\",content);
fflush(stdout);
exit(0);
}
/* $end adder */
运行良好。但是,我想知道为什么子代码(行printf(\“ asdfagloiauergauhfgaiudfhg \”;和execvp)没有打印到Web服务器的输出中。尽管测试中的其他所有内容均正确输出。
解决方法
对于初学者,您可以将“ 1”标头设置为内容的长度,然后发送内容,然后在两个线程中发送更多数据。浏览器有权忽略输出流中内容长度字节之后的所有内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。