Sheng Ye's Project: Web Server
USC Viterbi School of Engineering
Objective:
The objective of this project is to build a powerful and robust web server using C++ language. project specification
Design:
The server is implemented using 19 classes, including 1 driver class and 18 objects. The driver class,
Server, contains the main procedures of the server running process. Each of the 18 objects represents a
module that accomplishes a specific task. Some of the key objects are Configuration, AccessLog, DebugLog,
ErrorLog, AutoPathExpansion, DirectoryContent, HTTPresponse, HTTPrequest, HTTPserver, Dispatcher, HeartBeat,
and socket.
Organization of the Key Objects
Functionality Overview:
1. Configuration file:
It enables the host to change the home directory, number of thread, log file path, main port, virtual host, clustering number, etc. without making changes in the actually code. All the parameters are stores in the configuration file. Object Configuration is implemented to read the file and stores all the parameters.
2. Virtual web hosting:
This allows different hosts with different home directories to share one server. The hosts and their document roots are stored in pairs in configuration file, such as host: ugsparc80.eecg.utoronto.ca:54019 | document root: /nfs/ugsparcs/w-z/w-z/ye/ECE299/htmlfiles. Object Configuration is used to store the parameters, and object HTTPresponse uses Configuration to return a document root corresponding to the host that is given by HTTPrequest.
3. Automatic pathname expansion:
It enables the user to type in the user name after the host URL to directly access the user's home directory. Object AutoPathExpansion performs this function specifically. When in HTTPresponse detects a user name embedded in relative URL, it calls and uses AutoPathExpansion to return the home directory of that user.
4. Log files:
It is stores information about the server during running process. Such information includes access history, error history, and debugging output. Since there are 3 kinds of information needed to be stored, 3 objects are implemented to store them in 3 files. They are AccessLog, ErrorLog, and DebugLog. Each of them gets the paths of the file it writes in from Configuration. While they all share some common tasks such as writing in file, a base class Log is created to store the shared functions. The 3 subclass can be used in any other classes.
5. Error page and default page:
Error page is returned if any error occurred in a request or during the process of serving it. The type of error is specified in the error page. Default page is returned if a directory is requested and a default page exists in it. Both functions are implemented in object HTTPresponse. When the condition is met to return one of the pages, HTTPresponse calls either loadErrorPage or loadDefaultPage function.
6. Handling other content types:
The server can return various content types if requested, including: image, video, audio, application, etc. This is achieved by implementing function validFormat in HTTPresponse. When a file is requested, the function relates the extension of the file to a content type. If the content type exists, the returned header includes the content type, otherwise an error page is returned.
7. Listing directory contents:
It returns the content of the requested directory to user when the default page does not exist. Other relative information is also shown for each file and subdirectory in the directory. This task is completed in object DirectoryContent. It reads the content of a directory, and stores the names of the files and subdirectories as well as sizes and times last modified in a temporary html page. The page is then returned to user by HTTPresponse.
8. Handling dynamic content:
It uses external programs to generate custom web page based on client's input instead of returning a static page. The requests for dynamic content are in 2 types, dynamic GET and POST. Although they are
different in terms of the requesting method and parameters, they share some similarities in terms of the way they use dyn program to generate temporary html page. Function loadDynamicPage in HTTPresponse is used to serve both kinds of requests.
9. Multithreading:
It supports multiple users accessing the server simultaneously. The number of thread is static and is specified in the configuration file. The threads are of type HTTPserver and are created in the main class Server.
10. Clustering:
It enhances the processing ability by a multiple factor by allowing multiple machines to run and serve requests instead of one machine. There is one dispatcher server and some number of cluster servers that are specified in configuration file. When a request is made to the dispatcher server, it is redirected to one of the cluster servers. HTTPresponse includes function redirect to accomplish this for the dispatcher server. When a cluster server is down, the dispatcher server can detect and re-launch it. This communication mechanism between dispatcher server and cluster server is achieved by object Dispatcher and object HeartBeat.
click here to see load balancing strategy
11. Graceful server shut down:
When working in single-machine mode, shutting down server gracefully means no request can be made to the server, and existing users are still connected until they disconnect from the server themselves. With clustering, once the server is shutdown remotely from the dispatcher, the cluster servers also shutdown in the same way as one in single-machine mode. This can be achieved by setting a global flag serverOn in the driver class. All the objects
running in threads check if this flag has changed. Once the flag changed, those objects exit from threads. When all the threads exited, the server shuts down.
Classes:(.H files)
All rights reserved
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees