A project for understanding socket programming in C and implementing a fully functional multi-threaded HTTP proxy server with request forwarding capabilities.
This project demonstrates advanced concepts of network programming in C, including multi-threaded socket handling, HTTP request parsing, proxy forwarding, and response handling. It's designed as an educational tool for learning TCP socket programming, multi-threading, HTTP protocol implementation, and proxy server architecture.
multi-threaded-proxy-server/
├── main.c # Main program entry point
├── socket/
│ ├── socket.c # Socket creation and multi-threading
│ ├── socket.h # Socket function declarations
│ └── conn.c # Client connection handling
├── proxy/
│ ├── proxy.c # Proxy forwarding logic
│ └── proxy.h # Proxy function declarations
├── utils/
│ ├── logger.c # Logging functionality
│ ├── logger.h # Logger declarations
│ ├── parser.c # HTTP request parser
│ ├── parser.h # Parser declarations
│ ├── helper.c # Utility functions
│ └── helper.h # Helper declarations
├── Makefile # Build configuration
├── main # Compiled executable
└── README.md # This file
- Multi-threaded Server: Handles multiple client connections simultaneously
- Socket Creation & Binding: TCP socket creation with proper error handling
- Connection Acceptance: Accepts client connections with thread creation
- Dynamic Buffer Management: Handles requests larger than buffer size
- HTTP Request Parsing: Manual character-by-character parsing
- Header Extraction: Parses all HTTP headers into linked list structure
- Body Handling: Extracts request body when present
- Completion Detection: Detects complete HTTP requests with proper boundary checking
- URL Path Extraction: Converts full URLs to proper HTTP request paths
- Request Reconstruction: Rebuilds HTTP requests for target servers
- Target Server Connection: Establishes connections to target servers
- Response Forwarding: Forwards complete responses back to clients
- Chunked Transfer Encoding: Properly handles chunked responses
- Content-Length Support: Handles responses with Content-Length headers
- Complete Response Detection: Ensures full responses are received before forwarding
- Dynamic Memory Allocation: Proper buffer expansion for large requests
- Linked List Headers: Efficient storage of HTTP headers
- Memory Cleanup: Proper resource management
# Clean and build
make clean && make
# Run the proxy server
./main <port># Start the proxy server on port 8888
./main 8888
# Test with HTTP client (Postman, curl, etc.)
# Use localhost:8888 as the proxy serverCreates, binds, and starts listening for connections with multi-threading support.
Handles individual client connections with dynamic buffer management and HTTP parsing.
Manually parses HTTP requests, extracting method, path, headers, and body.
forwardRequest(struct parsed_request *req, char *req_buff, int req_buff_len, char *resp_buff, size_t *resp_len)
Forwards HTTP requests to target servers and receives responses.
Receives complete HTTP responses from target servers, handling both chunked and Content-Length encoding.
- Multi-threaded server implementation
- Dynamic buffer expansion
- HTTP request parsing (method, path, headers, body)
- Signal handling for graceful shutdown
- Memory management and cleanup
- Structured logging system
- Proxy forwarding logic
- Target server connection
- Response forwarding
- Chunked transfer encoding support
- URL path extraction and request reconstruction
- Complete response handling
- Methods: GET, POST, PUT, DELETE, etc.
- Headers: All standard HTTP headers
- Body: Request and response body handling
- Encoding: Chunked transfer encoding and Content-Length
- URL Parsing: Extracts paths from full URLs
- Request Forwarding: Reconstructs requests for target servers
- Response Handling: Forwards complete responses to clients
- Multi-threading: Handles multiple concurrent requests
- Connection Errors: Proper handling of connection failures
- Memory Errors: Graceful handling of allocation failures
- Protocol Errors: Robust HTTP parsing with error recovery
This project helps understand:
- Socket programming fundamentals
- Multi-threading with pthread
- TCP connection lifecycle
- Signal handling
- Proxy server architecture
- HTTP request structure
- Header parsing techniques
- Body handling
- Protocol completion detection
- Chunked transfer encoding
- URL parsing and reconstruction
- Dynamic memory allocation
- Buffer management
- Linked list implementation
- Resource cleanup
- File descriptor management
- Thread synchronization
- Error handling
- Process signals
The proxy server can be tested with:
- Postman: Send HTTP requests to any target server
- curl: Command-line HTTP requests
- Web browsers: Configure proxy settings
- Custom HTTP clients
# Test with local server
curl -x localhost:8888 http://localhost:3000/
# Test with external server
curl -x localhost:8888 http://httpbin.org/get
# Test with Postman
# Set proxy to localhost:8888 and send requests to any URL- Request/response caching
- Load balancing
- Request filtering
- Performance monitoring
- Logging
- Error handling
- Memory management
- Thread synchronization
- File descriptor management
- Process signals