C Webvserver - STDIN,套接字和CGI管道

时间:2013-12-13 17:48:04

标签: c sockets webserver pipe dup2

我想为GNUNet创建一个遥控器,所以我开始为GNU OS编写一个自制的多线程通用网络服务器,能够对用户进行身份验证(从system user database阅读)并能够执行通用的CGI程序/脚本。我从头开始,现在只是一个草案。但是,一切似乎都运转良好。

我只是一个问题。

如您所知,CGI程序/脚本从STDIN读取POST字符串并将其内容发送到STDOUT。以下是我编写的代码(的一部分)。它似乎有效。

if (pipe(cgiPipe))
{
  perror("pipe");
}

cgiPid = fork();

if (cgiPid == 0)
{

  /* child */

  /* piping the POST content... */

  /* first, send the truncated part of the POST string contained within the request string... */
  if (nPOSTLength && (nSentChrs = write(cgiPipe[1], sPOSTSegment, 
      nReqLen + requestString - sPOSTSegment)) > 0)
  {
    nPOSTLength -= nSentChrs;

    /* after, read and send the rest of the POST string not received yet... */
    while (nPOSTLength > 0 && (nReadChrs = read(nRemote, reservedBuffer, 
        BUFFER_SIZE_PER_USER)) > 0 && (nSentChrs = write(cgiPipe[1], reservedBuffer, 
        nReadChrs)) > 0 && nReadChrs == nSentChrs)
    {
      nPOSTLength -= nReadChrs;
    }

    if (nReadChrs < 0)
    {
      printf("Error reading POST string.\n");
      goto closeThread;
    }
    if (nSentChrs < 0)
    {
      printf("Error sending POST string.\n");
      goto closeThread;
    }
  }
  else
  {
    write(cgiPipe[1], "(null)", 6);
  }

  close(cgiPipe[1]);

  /* redirecting the output of the pipe to the STDIN of the child process */
  dup2(cgiPipe[0], STDIN_FILENO);
  /* redirecting STDOUT of the child process to the remote client */
  dup2(nRemote, STDOUT_FILENO);
  setuid(nUserID);

  if (execve(sLocalPath, NULL, aCGIEnv))
  {
    /* unable to execute CGI... */
    perror("execve");
    sendString(nRemote,
        "HTTP/1.1 200 OK\r\n"
        "Content-length: 97\r\n"
        "Content-Type: text/html\r\n\r\n"
        "<!doctype html><html><head><title>CGI Error</title></head><body><h1>CGI Error.</h1></body></html>\r\n"
    );
  }

  goto closeThread;

}
else if (cgiPid > 0)
{

  /* parent */

  close(cgiPipe[0]);

  /* wait for child process. */
  if (waitpid(cgiPid, NULL, 0) == -1)
  {
    perror("wait");
  }

  goto closeThread;

}
else
{

  /* parent */

  perror("fork");

  /* let's try to send it as normal file, if the user has the right permissions... */

}

正如您所看到的,执行CGI程序之前,整个POST字符串是从客户端接收并通过管道传输的(首先是请求中包含的截断部分) string - 通常是几个字节 - 然后是其余的)。 然后,执行CGI程序。

现在我的问题......

如果我尝试上传几个MB的文件,则在调用CGI之前管道好几个MB 有没有办法将套接字直接重定向到新的STDIN进程,以便之前不读它?但是,当然,我必须先发送POST字符串的读取截断部分。所以,我可以用这种方式模拟我想做的事情:

  • 将一个字符串(几个字节)连接到STDIN,然后
  • 将套接字(客户端)重定向到STDIN,然后
  • 执行外部流程(CGI程序)

有可能吗?你能告诉我如何

2 个答案:

答案 0 :(得分:1)

解决!!

我只需将发送过程放在父级而不是子级中。通过这种方式,CGI立即执行:

if (pipe(cgiPipe))
{
  perror("pipe");
}

cgiPid = fork();

if (cgiPid == 0)
{

  /* child */

  /* piping the POST content... */

  close(cgiPipe[1]);

  /* redirecting the output of the pipe to the STDIN of the child process */
  dup2(cgiPipe[0], STDIN_FILENO);
  /* redirecting STDOUT of the child process to the remote client */
  dup2(nRemote, STDOUT_FILENO);
  setuid(nUserID);

  if (execve(sLocalPath, NULL, aCGIEnv))
  {
    /* unable to execute CGI... */
    perror("execve");
    sendString(nRemote,
        "HTTP/1.1 200 OK\r\n"
        "Content-length: 97\r\n"
        "Content-Type: text/html\r\n\r\n"
        "<!doctype html><html><head><title>CGI Error</title></head><body><h1>CGI Error.</h1></body></html>\r\n"
    );
  }

  goto closeThread;

}
else if (cgiPid > 0)
{

  /* parent */

  close(cgiPipe[0]);

  /* first, send the truncated part of the POST string contained within the request string... */
  if (nPOSTLength && (nSentChrs = write(cgiPipe[1], sPOSTSegment, 
      nReqLen + requestString - sPOSTSegment)) > 0)
  {
    nPOSTLength -= nSentChrs;

    /* after, read and send the rest of the POST string not received yet... */
    while (nPOSTLength > 0 && (nReadChrs = read(nRemote, reservedBuffer, 
        BUFFER_SIZE_PER_USER)) > 0 && (nSentChrs = write(cgiPipe[1], reservedBuffer, 
        nReadChrs)) > 0 && nReadChrs == nSentChrs)
    {
      nPOSTLength -= nReadChrs;
    }

    if (nReadChrs < 0)
    {
      printf("Error reading POST string.\n");
      goto closeThread;
    }
    if (nSentChrs < 0)
    {
      printf("Error sending POST string.\n");
      goto closeThread;
    }
  }
  else
  {
    write(cgiPipe[1], "(null)", 6);
  }

  /* wait for child process. */
  if (waitpid(cgiPid, NULL, 0) == -1)
  {
    perror("wait");
  }

  goto closeThread;

}
else
{

  /* parent */

  perror("fork");

  /* let's try to send it as normal file, if the user has the right permissions... */

}

感谢您的帮助!

而且......我们希望尽早看到GNUNet的遥控器! :)

答案 1 :(得分:0)

这可以通过使用dup2()将文件描述符STDIN_FILENO替换为打开的套接字来实现。您还应该在子进程中关闭原始套接字:

dup2(socket_fd, STDIN_FILENO);
close(socket_fd); ; 
execve("cgi_process", args, env);

execve执行另一个STDIN绑定到socket_fd的进程。