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>
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...