Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Distributed URL Shortener

A URL shortener built from the ground up in C# without using ASP.NET Core or existing web frameworks. The goal of this project is to understand how web servers and backend frameworks work internally by implementing each layer manually.

This project is evolving into a complete distributed backend system, including a custom web server, router, Redis-compatible cache, rate limiter, and load balancer.


Project Goals

  • Learn how HTTP works over raw TCP sockets.
  • Build a custom web server from scratch.
  • Implement a routing engine similar to ASP.NET Core.
  • Build backend infrastructure without relying on existing frameworks.
  • Explore distributed systems concepts by integrating custom infrastructure components.
  • Gain hands-on experience with networking, concurrency, caching, and scalability.

Current Features

Custom TCP Web Server

  • Listens for incoming TCP connections.
  • Parses raw HTTP requests.
  • Sends properly formatted HTTP responses.
  • Supports multiple concurrent clients using asynchronous programming.

HTTP Request Parsing

  • Parses the request line.
  • Parses HTTP headers.
  • Parses query parameters.
  • Reads request bodies using the Content-Length header.

Custom Router

Supports:

  • Static routes
  • Dynamic route parameters
  • GET requests
  • POST requests

Example routes:

GET /hello
GET /hello?name=Bryan
POST /shorten
GET /{shortCode}

URL Shortener

  • Accepts long URLs through a POST endpoint.
  • Generates unique short codes.
  • Stores URL mappings in memory.
  • Resolves short codes back to their original URLs.
  • Redirects clients using HTTP 302 responses.

Project Structure

BasicWebServer/
│
├── Models/
│   ├── HttpRequest.cs
│   ├── HttpResponse.cs
│   └── Route.cs
│
├── Routing/
│   └── Router.cs
│
├── Services/
│   ├── UrlStore.cs
│   └── ShortCodeGenerator.cs
│
├── Program.cs
│
└── BasicWebServer.csproj

API

Create Short URL

Request

POST /shorten
Content-Type: application/json

Body

{
  "url": "https://google.com"
}

Response

{
  "shortCode": "abc123"
}

Redirect

Request

GET /abc123

Response

HTTP/1.1 302 Found
Location: https://google.com

Hello Endpoint

Request

GET /hello?name=Bryan

Response

Hello, Bryan

Running the Project

Clone the repository:

git clone https://github.com/<your-username>/<your-repository>.git

Navigate to the project directory:

cd BasicWebServer

Run the application:

dotnet run

The server listens on:

http://localhost:8080

Testing

Test the Hello Endpoint

Open:

http://localhost:8080/hello

Expected response:

Hello, world

Open:

http://localhost:8080/hello?name=Bryan

Expected response:

Hello, Bryan

Create a Short URL

Using curl:

curl -X POST http://localhost:8080/shorten \
-H "Content-Type: application/json" \
-d "{\"url\":\"https://google.com\"}"

Expected response:

{
  "shortCode": "abc123"
}

Windows PowerShell: The curl command syntax differs from Bash. You may prefer using Invoke-RestMethod or adjust quoting accordingly.


Test Redirect

Open:

http://localhost:8080/abc123

The browser should automatically redirect to:

https://google.com

Current Architecture

Client
   │
   ▼
TCP Listener
   │
   ▼
HTTP Request Parser
   │
   ▼
Router
   │
   ▼
Route Handler
   │
   ▼
HttpResponse
   │
   ▼
Client

Planned Improvements

HTTP Server

  • Middleware pipeline
  • Request logging
  • Exception handling
  • Better HTTP header support
  • Persistent connections
  • Static file serving

URL Shortener

  • URL validation
  • Custom aliases
  • URL expiration
  • Click analytics
  • Duplicate URL detection

Distributed Systems

  • Replace in-memory storage with a custom Redis server.
  • Implement a distributed API rate limiter.
  • Build a custom application load balancer.
  • Support multiple backend server instances.
  • Health checks.
  • Fault tolerance.
  • Horizontal scaling.

Infrastructure

  • Docker support.
  • Automated integration tests.
  • GitHub Actions CI.
  • Performance benchmarks.
  • Metrics endpoint.

Technologies

  • C#
  • .NET
  • TCP Sockets
  • HTTP/1.1
  • JSON
  • Asynchronous Programming

Learning Objectives

This project focuses on understanding backend engineering fundamentals by implementing core infrastructure manually instead of relying on existing frameworks. The long-term objective is to evolve this project into a distributed system composed of independently developed infrastructure components such as a custom Redis server, API rate limiter, and load balancer.


Roadmap

  • ✅ Build a custom TCP web server
  • ✅ Parse HTTP requests manually
  • ✅ Implement a custom routing engine
  • ✅ Build an in-memory URL shortener
  • ⏳ Implement middleware pipeline
  • ⏳ Build a custom Redis server
  • ⏳ Integrate Redis into the URL shortener
  • ⏳ Implement distributed API rate limiting
  • ⏳ Build a custom application load balancer
  • ⏳ Deploy multiple backend instances behind the load balancer

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages