Skip to content

abhinav-1504/proxy-server-multithreaded-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multithreaded HTTP Proxy Server with Caching

Overview

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.

Features

  • 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.h library 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.

Prerequisites

  • 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).

Build Instructions

  1. 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)
  2. Compile the project:
$ make

This generates the proxy executable.

  1. Clean up object files and executable:
$ make clean
  1. Create a tarball (ass1.tgz):
$ make tar

Usage

  1. Run the proxy server (default port 8080 or specify a custom port): $ ./proxy [port_number] Example: $ ./proxy 8081
  2. Configure your browser/client to use the proxy at localhost:<port_number>.
  3. 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.
  4. Errors are handled with appropriate HTTP status codes (e.g., 400 Bad Request).

Implementation Details

Cache Management:

  • Cache is a linked list of cache_element structures (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 ParsedRequest from proxy_parse.h.
  • Sets Connection: close and ensures Host header is present.

Error Handling:

  • sendErrorMessage sends 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).

Limitations

  • 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.

Future Improvements

  • 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).

File Structure

  • 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.

Notes

  • Requires proxy_parse.h with ParsedRequest structure and functions.
  • Ensure system resources support 400 concurrent connections.
  • Server runs indefinitely until stopped (e.g., Ctrl+C).

About

C-based multithreaded HTTP proxy server featuring 400-client concurrency, HTTP request parsing, and 200MB LRU cache management.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors