This project implements a basic multi-threaded HTTP proxy server in C, capable of handling HTTP GET requests and caching responses to improve performance for frequently accessed resources. The proxy acts as an intermediary between clients and web servers, forwarding requests and serving cached content when available.
- Multi-threaded Architecture: Handles multiple client connections concurrently using POSIX threads (
pthread). - Request Parsing: Utilizes a custom HTTP request parsing library (
parse_server.h/.c) to break down incoming requests into structured components (method, host, path, headers, etc.). - HTTP GET Support: Primarily designed to process HTTP GET requests.
- Least Recently Used (LRU) Cache: Implements an in-memory cache to store HTTP responses. When the cache reaches its maximum size, the least recently used elements are evicted to make space for new ones.
- Thread Safety for Cache: Uses a mutex lock (
pthread_mutex_t) to ensure thread-safe access and modification of the shared cache. - Concurrency Control: Employs a semaphore (
sem_t) to limit the number of concurrent client connections, preventing resource exhaustion. - Error Handling: Sends appropriate HTTP error responses (e.g., 400 Bad Request, 404 Not Found, 500 Internal Server Error) to clients for various issues.
- Dynamic Port Selection: The proxy can be configured to listen on a specified port number via command-line arguments.
- Threading
- Locks
- Semaphore
- Cache (LRU algorithm)
-
Clone the repository
git clone https://github.com/Harshvardhan2164/Multithreaded-HTTP-Server.git cd Multithreaded-HTTP-Server/ -
Execute the
Makefileusingcmakemake all
-
Run the generated
proxyfile./proxy <port>
The server will start listening on port 8080. You can then configure your web browser or applications to use
localhost:8080as a proxy.
-
Start the proxy server:
./proxy_server 8080
-
Configure your browser/application to use the proxy:
- For Browsers (e.g., Chrome, Firefox): Go to your browser's network settings or proxy settings. Set the HTTP proxy to 127.0.0.1 (or localhost) and the port to 8080.
- For curl:
curl -x http://localhost:8080 [http://example.com](http://example.com)
This will send a request to example.com through your proxy.
-
HTTP Version Support: The checkHTTPVersion function currently treats both HTTP/1.0 and HTTP/1.1 as version 1. While it validates the prefix "HTTP/", it doesn't differentiate between the minor versions for specific protocol behaviors.
-
GET Method Only: The proxy is designed primarily for GET requests. Other HTTP methods will not be handled and will result in an error.
-
Cache Eviction Logic: The LRU cache implementation finds the least recently used element by iterating through the entire linked list. For very large caches, this could become inefficient. A more optimized LRU could involve moving accessed elements to the head of the list.
-
Fixed-Size Buffer in
thread_func: When serving cached content, the response buffer in thread_func is a fixed MAX_BYTES size. If temp->data (the cached response) is larger, it might lead to issues in the last send call, potentially sending uninitialized data or incorrect lengths.
- Implement support for other HTTP methods (POST, PUT, DELETE, HEAD, etc.).
- Improve the LRU cache by moving accessed elements to the head of the linked list for more efficient lookup and eviction.
- Enhance URL parsing robustness to handle more complex or edge-case URLs.
- Add more comprehensive logging for server activity and errors.
- Consider a thread pool implementation for more efficient thread management and resource reuse.
- Implement connection keep-alive for both client-proxy and proxy-server connections.
Feel free to fork the repository, open issues, or submit pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.

