A multithreaded HTTP Proxy Web Server built in C using socket programming, pthreads, DNS resolution, and an LRU (Least Recently Used) caching mechanism.
- Computer Networking
- Socket Programming
- HTTP Protocol
- Multithreading using pthread
- DNS Resolution
- Cache Management
- LRU Eviction Policy
- Linux System Programming
- Multithreaded proxy server using pthread
- HTTP request forwarding
- DNS hostname resolution
- LRU cache implementation
- Thread-safe cache handling using mutex
- Modular project architecture
- HTTP request parsing
- Client-server communication using TCP sockets
proxy_project/
│
├── main.c
├── server.c
├── server.h
│
├── thread_pool.c
├── thread_pool.h
│
├── proxy_handler.c
├── proxy_handler.h
│
├── parser.c
├── parser.h
│
├── dns_resolver.c
├── dns_resolver.h
│
├── cache.c
├── cache.h
│
├── lru.c
├── lru.h
│
├── utils.c
├── utils.h
│
└── Makefile
Browser
↓
Proxy Server
↓
Cache Lookup
↓
(Cache Hit) → Return Cached Response
↓
(Cache Miss)
↓
DNS Resolution
↓
Remote Web Server
↓
Fetch Response
↓
Store in LRU Cache
↓
Send Response to Browser
Entry point of the proxy server.
- Initialize cache
- Initialize thread pool
- Start proxy server
Handles all server-side socket operations.
- Create socket
- Bind socket to port
- Listen for incoming clients
- Accept client connections
- Send client requests to worker threads
Implements multithreading using pthreads.
- Create worker threads
- Manage task queue
- Synchronize threads using mutex and condition variables
- Handle concurrent client requests
Core proxy logic implementation.
- Receive browser requests
- Parse HTTP requests
- Check cache
- Connect to remote servers
- Fetch responses
- Return responses to clients
Parses incoming HTTP requests.
- Extract HTTP method
- Extract host name
- Extract requested path
- Process request headers
Handles DNS resolution.
- Convert domain names into IP addresses
- Use
getaddrinfo()for hostname lookup - Resolve remote server addresses
Manages proxy cache operations.
- Store server responses
- Retrieve cached responses
- Thread-safe cache access
- Cache insertion and lookup
Implements the LRU (Least Recently Used) cache policy.
- Maintain doubly linked list
- Move recently accessed items to front
- Remove least recently used items
- Cache eviction management
Contains helper utility functions.
- Logging
- Error handling
- Common reusable helper functions
Automates project compilation.
- Compile source files
- Generate object files
- Link executable
- Clean build files
$ make
$ make cleanBrowser
↓
HTTP Proxy Server
↓
Thread Pool
↓
Request Parser
↓
LRU Cache
↓
DNS Resolver
↓
Remote Web Server
- C Programming
- POSIX Socket API
- pthread Library
- TCP/IP Networking
- DNS Resolution using getaddrinfo()
- Makefile
$ makeor
$ gcc *.c -o proxy -pthread$ ./proxySet browser proxy configuration:
HTTP Proxy : 127.0.0.1
Port : 8080
Then open:
http://neverssl.com
The proxy server uses an LRU (Least Recently Used) cache to improve performance by storing previously fetched responses.
-
Cache Hit
- Return cached response immediately
- Move cache node to front
-
Cache Miss
- Fetch response from remote server
- Store response in cache
- Remove least recently used entry if cache is full
The server uses a thread pool architecture:
- Main thread accepts client connections
- Worker threads process requests concurrently
- Mutex and condition variables ensure synchronization
- HTTP Proxying
- Concurrent Client Handling
- DNS Lookup
- Response Caching
- TCP Socket Communication
- HTTPS CONNECT Support
- epoll-based scalable I/O
- Dynamic memory buffering
- Full HTTP header parsing
- HashMap-based O(1) cache lookup
- Logging system
- Cache expiration mechanism
- Chunked transfer encoding support
This project helps understand:
- Low-level networking
- Concurrent programming
- Proxy server architecture
- Cache systems
- Linux networking internals
- System design fundamentals
Feel free to fork and improve this project.
This project is for learning purposes.