gpt4 book ai didi

linux - 调试服务器的cgi程序

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:01 31 4
gpt4 key购买 nike

我正在尝试用 C 语言制作一个网络服务器,它可以处理对动态内容的请求。网络服务器部分已经完成。我正在尝试执行以下命令:

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)没有打印到网络服务器的输出。尽管测试输出中的其他所有内容都正确。

最佳答案

对于初学者,您将 Content-length header 设置为内容的长度,然后发送内容,然后在两个线程中发送更多数据。浏览器有权忽略输出流中内容长度字节之后的所有内容。

关于linux - 调试服务器的cgi程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6195940/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com