This is a multi-threaded HTTP proxy server implemented in C. It handles HTTP GET requests, forwards them to remote servers, and caches responses using a Least Recently Used (LRU) policy to optimize performance. The server supports up to 400 concurrent client connections using POSIX threads and ensures thread-safe cache operations with mutex locks.
- Multi-threaded: Handles up to 400 concurrent clients using POSIX threads and semaphores.
- Caching: LRU cache with 200MB total size and 10MB per element.
- HTTP Parsing: Uses a custom
proxy_parse.hlibrary for request parsing. - Error Handling: Supports HTTP status codes (400, 403, 404, 500, 501, 505).
- Connection Management: Forwards requests to remote servers and relays responses.
- Thread Safety: Uses mutex locks for cache synchronization.
- Compiler: GCC or any C compiler with POSIX thread support.
- Operating System: Linux/Unix (due to POSIX threads and socket APIs).
- Libraries: Standard C libraries and POSIX threads (
-lpthread).
- Ensure you have the following files:
- proxy_server_with_cache.c (main server logic)
- proxy_parse.c, proxy_parse.h (HTTP parsing library)
- Makefile (build configuration)
- Compile the project:
$ makeThis generates the proxy executable.
- Clean up object files and executable:
$ make clean- Create a tarball (ass1.tgz):
$ make tar- Run the proxy server (default port 8080 or specify a custom port): $ ./proxy [port_number] Example: $ ./proxy 8081
- Configure your browser/client to use the proxy at localhost:<port_number>.
- Send HTTP GET requests. The server will:
- Check cache for response.
- On cache miss, fetch from remote server, cache response (if within size limits), and relay to client.
- Errors are handled with appropriate HTTP status codes (e.g., 400 Bad Request).
Cache Management:
- Cache is a linked list of
cache_elementstructures (URL, response data, size, LRU timestamp). - Total cache size: 200MB; max element size: 10MB.
- LRU eviction removes least recently used element when cache exceeds limit.
Threading:
- Each client connection runs in a separate thread.
- Semaphore limits connections to 400.
- Mutex ensures thread-safe cache access.
Request Handling:
- Supports only HTTP GET requests (others return 501 Not Implemented).
- Parses requests using
ParsedRequestfromproxy_parse.h. - Sets
Connection: closeand ensuresHostheader is present.
Error Handling:
sendErrorMessagesends formatted HTTP error responses with timestamps.- Handles errors: 400 (Bad Request), 403 (Forbidden), 404 (Not Found), 500 (Internal Server Error), 501 (Not Implemented), 505 (HTTP Version Not Supported).
- Supports only HTTP GET requests.
- Cache limited to 200MB total, 10MB per element.
- No HTTPS or persistent connection support.
- Limited error recovery for malformed requests or server failures.
- Add HTTPS support with SSL/TLS.
- Implement persistent connections.
- Support additional HTTP methods (e.g., POST).
- Enhance cache eviction (e.g., based on response freshness).
- proxy_server_with_cache.c: Main proxy server logic.
- proxy_parse.c, proxy_parse.h: HTTP request parsing utilities.
- Makefile: Build configuration.
- README.so: This documentation file.
- Requires
proxy_parse.hwithParsedRequeststructure and functions. - Ensure system resources support 400 concurrent connections.
- Server runs indefinitely until stopped (e.g., Ctrl+C).