This project implements a simple, lightweight, multi-process HTTP server in C. The server is designed to handle HTTP GET requests from multiple client connections concurrently using process-based concurrency. The server fetches static files and serves them with appropriate HTTP responses utilizing concepts in socket programming, TCP/IP networking communication, process management, and server architecture.
The server can be accessed locally using a web browser or using the client code provided.
All information and errors are logged in a web-server.log file.
- Concurrent Client Handling: Each client connection is handled in a separate process for concurrency.
- Request Handling: The server supports HTTP GET requests, serves static files, and logs connection details.
- Socket Programming: Uses low-level POSIX APIs for creating and managing TCP sockets.
- Signal Handling: Automatically reaps child processes with
SIGCHLDto prevent zombie processes. - File Serving: Supports multiple file types (HTML, HTM, TXT, GIF, JPG, JPEG, PNG) and returns appropriate HTTP headers and content to clients.
- Modular Design: Clearly separates server initialization, connection handling, and request processing for maintainability.
- Response Sending: Correctly formats HTTP server responses based off of status code from request (ie 404, 405, 200, 500).
- Initialization: The server creates a socket, binds it to a specified port, and listens for incoming connections.
- Connection Handling: The
accept()call blocks until a client connects. Upon connection, the server creates a child process withfork()to handle the client. - Request Processing: The child process handles the client’s request and logs the connection details. The parent process continues to listen for new connections.
- Response: The server then sends a response back to the client with the requested file, or an error message detailing the request/server error along with an HTTP status code.
- GCC or another C compiler.
- A Unix-based operating system (Linux or macOS).
-
Clone the Repository
Download the project files from your repository or source directory.git clone https://github.com/will-mcintyre04/http-web-server.git cd http-web-server -
Compile the Server
The server can be run locally using the Makefile provided. Themake runcommand creates the required directories, compiles the c code, assembles the object files and links them together to create and run an executable.make run ARGS="<port-number>"Note that you can use the
makecommand to just compile and link the files without running the server. -
Accessing the Server
Open a browser or use curl to test the server:curl --output http://localhost:<port-number>/index.html
This should return a 404 error indicating the requested resource is not found.
-
Populating the Server
To populate the server with static files that can be accessed, create a file in the root directory. For example:cd http-web-server touch example.txt nano example.txt <FILL IN FILE WITH CONTENT> make run ASRGS="<port-number>" curl --output http://localhost:<port-number>/example.txt
The response should contain the content of the example file created.