J'essaye de faire un serveur Web dans C qui peut manipuler la demande au contenu dynamique. La partie serveur Web est déjà terminée. je suis en train d'exécuter la commande suivante:programme cgi du serveur de débogage
http://localhost:1601/cgi-bin/test?3&7
avec le code de test du programme est le suivant:
#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 */
Il fonctionne bien. Cependant, je me demande pourquoi le code enfant (la ligne printf ("asdfagloiauergauhfgaiudfhg") et execvp) n'a pas été imprimé sur la sortie du serveur web. Bien que tout le reste dans le test de sortie correctement.
Quel navigateur? Avez-vous vu les deux copies du contenu que vous avez envoyé, ou juste une? –