|
Next, we'll show the steps a web server takes in response to a client request. For this purpose, we provide the following pseudocode: s = socket(); /* allocate listen socket */ bind(s, 80); /* bind to TCP port 80 */ listen(s); /* indicate willingness to accept */ while (1) { newconn = accept(s); /* accept new connection */ remoteIP = getsockname(newconn); /* get remote IP addr */ remoteHost = gethostbyname(remoteIP); /* get remote IP DNS name */ gettimeofday(currentTime); /* determine time of day */ read(newconn, reqBuffer, sizeof(reqBuffer)); /* read client request */ reqInfo = serverParse(reqBuffer); /* parse client request */ fileName = parseOutFileName(requestBuffer); /* determine file name */ fileAttr = stat(fileName); /* get file attributes */ serverCheckFileStuff(fileName, fileAttr); /* check permissions */ open(fileName); /* open file */ read(fileName, fileBuffer); /* read file into buffer */ headerBuffer = serverFigureHeaders(fileName, /* determine headers */ reqInfo); write(newSock, headerBuffer); /* write headers to socket */ write(newSock, fileBuffer); /* write file to socket */ close(newSock); /* close socket */ close(fileName); /* close file */ write(logFile, requestInfo); /* write log info to disk */ } This pseudocode is a relatively simple implementation of a server that does not employ any possible optimizations and can handle only one request at a time. This example only hints at the more complex functionality that is required by the server, such as how to parse the HTTP request and determine whether the client has the appropriate permissions to view the file. In addition, it has no error handlingfor example, if the client requests a file that does not exist. However, this example gives a good idea of what steps are required by a server. |
|