A multi-threaded HTTP proxy server implementation in C++ with caching capabilities. This proxy server acts as an intermediary between clients and web servers, handling HTTP GET requests while providing caching functionality to improve response times.
- Multi-threaded request handling
- LRU (Least Recently Used) caching system
- Support for HTTP/1.0 and HTTP/1.1 GET requests
- Thread-safe cache implementation using mutex locks
- Configurable cache size and client limits
- Error handling and logging
- Implements the caching system
- Uses LRU (Least Recently Used) replacement policy
- Thread-safe operations using mutex locks
- Manages cache size limits
- Represents individual cache entries
- Stores URL, response data, and metadata
- Tracks access times for LRU implementation
The server uses a thread-per-connection model:
- Main thread accepts incoming connections
- Each client request is handled by a separate worker thread
- Semaphore controls the maximum number of concurrent connections
- Thread-safe cache access using mutex locks
Key configurable parameters (defined as macros):
#define MAX_BYTES 4096 // Maximum request/response size
#define MAX_CLIENTS 400 // Maximum concurrent connections
#define MAX_SIZE 20*(1<<20) // Cache size (20MB)
#define MAX_ELEMENT_SIZE 1*(1<<20) // Maximum cache entry size (1MB)-
Connection Acceptance
- Server listens on specified port
- Accepts incoming client connections
- Creates new thread for each connection
-
Request Parsing
- Reads HTTP request from client
- Parses request headers and URL
- Validates HTTP method (only GET supported)
-
Cache Checking
- Checks if requested URL exists in cache
- If found, returns cached response
- Updates LRU tracking information
-
Remote Server Communication
- If not in cache, connects to remote server
- Forwards client request
- Receives server response
-
Response Handling
- Caches new responses (if applicable)
- Sends response back to client
- Closes connection
The cache implements LRU (Least Recently Used) policy:
- New entries are added to the front of the cache
- When cache is full, least recently used entries are removed
- Each access updates the entry's timestamp
- Thread-safe operations ensure cache consistency
The server handles various error conditions:
- Invalid requests (400 Bad Request)
- Server errors (500 Internal Server Error)
- Connection failures
- Memory allocation failures
- Invalid HTTP versions (505 HTTP Version Not Supported)
-
Compilation
git clone https://github.com/yashagarwal0812/ProxyServer.git cd ProxyServer make -
Running the Server
./proxy <port>
Example -
./proxy 8080
-
Configure Browser (Open this URl)
http://localhost:<port>/<url>
Example -
http://localhost:8080/https:/www.jiit.ac.in/
- This will work on new incognito windows because of cache implementation in new browsers.
- Threading: Each connection spawns a new thread, suitable for moderate traffic
- Cache Size: Configurable based on available memory
- Connection Limits: Prevents resource exhaustion
- Mutex Locks: Minimizes lock contention while ensuring thread safety
- Only supports HTTP GET requests
- No HTTPS support
- Basic caching strategy (LRU only)
- No compression support
- No authentication mechanism
- Add HTTPS support
- Implement more HTTP methods (POST, PUT, etc.)
- Add support for compressed responses
- Implement more sophisticated caching strategies
- Add configuration file support
- Implement connection pooling
- Add support for proxy authentication
- Improve logging and monitoring capabilities
- POSIX Threads (pthread)
- Standard C++ Library
- POSIX networking APIs
- POSIX-compliant operating system (Linux, Unix, macOS)
- C++11 or higher compiler
- pthread library