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 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码