doutui7955 2015-03-10 02:44
浏览 43
已采纳

如何在C中创建支持PHP页面的Web服务器?

First of all, I know to create web servers which deal with static html pages.

I want to create a web server which supports PHP pages.

I have coded a server like below, -> main() function just create a socket, accept a request and calls connection(fd) function.

int main(int argc, char *argv[]) {
     int sockfd, newsockfd, portno, pid;
     socklen_t clilen;
     struct sockaddr_in serv_addr, cli_addr;

     if (argc < 2) {
          fprintf(stderr, "ERROR, no port provided
");
          exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
         error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        error("ERROR on binding");
     listen(sockfd, 5);
     clilen = sizeof(cli_addr);
     /*
      Server runs forever, forking off a separate
      process for each connection.
      */
     while (1) {
          newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
          if (newsockfd < 0)
             error("ERROR on accept");
          pid = fork();

          if (pid < 0)
              error("ERROR on fork");
          if (pid == 0) {
               close(sockfd);
               connection(newsockfd);
               exit(0);
          } else
            close(newsockfd);
     } /* end of while */
     close(sockfd);
     return 0; /* we never get here */
}

-> connection() function sets document root, and explicitly sets a PHP page(echo.php) and calls php-cgi() function. php-cgi() does the further process.

Actually i dont know what php5-cgi does. When i run the server and request a page from web browser, it shows "Am a PHP file" for 1 second, and hides, displays the message,

The connection was reset

1) What is the proper way to handle php pages in web server

2) Can anyone suggest me some books or anything to develop a web server in C which handle PHP pages?

3) Do i wanna try any other language? which is the most preferable language to create web server and proxies?

4) how to deal with query strings and POST method requests?

when i did telnet to the server it works,

GET /echo.php HTTP/1.1
HTTP/1.1 200 OK
X-Powered-By: PHP/5.5.9-1ubuntu4.6
Content-type: text/html

<html>
<body>
Am a PHP file
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongma2388 2015-03-10 16:26
    关注

    You are missing some HTTP header fields. One of the problems is that the browser doesn't know how much data to expect; so while it receives and parses data the connection is closed because your PHP script ends and the browser goes "Huh?".

    So, you must send either a Content-length header (if you know the length of your body in advance) or a Transfer-encoding field (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2). The first solution adds another problem: you must determine the lenght before sending the headers. This would mean capturing all output in a buffer, counting the bytes, then send the header(s) followed by the content. But that would add another fork() to your code... I tried adding a header like Transfer-Encoding: identity but that didn't work well. You may want to add a Connection: close header as well.

    Your other questions are a bit broad; it doesn't matter which language you use, you will have to do all the hard work of parsing HTTP request headers, assembling headers, forking, parsing parameters, etc. Your approach with fork(), execl() is theoretically sound but requires a lot more work. So yeah, using Apache, nginx or any other webserver sounds tempting to use...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合