Leanpub Header

Skip to main content

Rust from Sockets to Servers

Building a High-Performance Web Server in Rust from First Principles

This book is 100% completeLast updated on 2026-07-03

Most developers can build a web server with a framework. Far fewer understand what happens underneath. Rust from Sockets to Servers takes you from raw TCP sockets to a production-ready web server built in Rust, giving you a deep understanding of HTTP, async programming, TLS, routing, and the foundations behind every modern web application.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

You have used web frameworks. You know how to spin up an Axum or Actix server in minutes. But do you understand what happens when a browser sends a request and your server sends back a response? This book takes you on a hands-on journey from raw TCP sockets through the full stack of HTTP/1.1, HTTP/2, TLS, routing, middleware, templating, REST APIs, testing, benchmarking, and deployment. Every chapter adds a layer to a production-quality web server built entirely in Rust using Tokio and carefully chosen crates. By the end, you will not only have a working server but also the deep intuition that makes you a better developer no matter what tools you eventually use.

Share this book

Bundle

Bundles that include this book

Author

About the Author

Steve T. Publications

Steve T. is a cybersecurity leader, researcher, and engineer with more than 20 years of experience across application security, infrastructure security, vulnerability management, software development, and secure engineering practices. Having built his career alongside the growth of the modern internet, he has worked through multiple generations of technology, evolving security threats, and changing development methodologies.

He is currently part of the advanced research organization at a leading cybersecurity company, where he focuses on emerging threats, security innovation, and the practical application of research. His work involves investigating new attack techniques, evaluating emerging technologies, conducting deep technical analysis, and helping organizations better understand and manage complex security risks.

In addition to his research responsibilities, Steve leads a team of senior engineers and subject matter experts who create technical books, training programs, and educational resources for security professionals. Through this work, he helps engineers, developers, architects, and security practitioners strengthen their skills and build more secure systems.

Steve's technical expertise spans software development, reverse engineering, web application security, penetration testing, security architecture, incident response, vulnerability research, operating system internals, and secure software development. His ability to analyze systems at both the source code and binary levels enables him to bridge the worlds of software engineering, security research, and practical defense.

Over the course of his career, Steve has worked with organizations across a wide range of industries, helping them identify, assess, and remediate security weaknesses in critical applications and infrastructure. He is recognized for combining deep technical expertise with a pragmatic approach to security, focusing on solutions that are effective, sustainable, and aligned with business goals.

Through his work in research, engineering, leadership, and education, Steve continues to contribute to the advancement of cybersecurity and the development of secure, resilient technology systems.

Contents

Table of Contents

Building a High-Performance Web Server in Rust from First Principles

Introduction: Why Build Your Own Server?

  1. What You Will Build
  2. Prerequisites and Setup
  3. The Architecture at a Glance
  4. How to Read This Book

Chapter 1: Networking Fundamentals

  1. The Internet Protocol Stack
  2. IP Addresses and Ports
  3. TCP Connections and the Three-Way Handshake
  4. Raw Sockets in Rust
  5. Your First Listening Socket
  6. Exercise: Echo Server

Chapter 2: Understanding HTTP/1.1

  1. Anatomy of an HTTP Request
  2. Anatomy of an HTTP Response
  3. HTTP Methods and Status Codes
  4. Parsing the First Request
  5. Sending Your First Response
  6. Exercise: Minimal HTTP Server

Chapter 3: Asynchronous Programming with Tokio

  1. The Problem with Blocking I/O
  2. Futures and the Event Loop
  3. Tokio Runtime Architecture
  4. Async TCP Listeners
  5. Spawning Concurrent Tasks
  6. Exercise: Async Echo Server

Chapter 4: Building the Request and Response Types

  1. The HttpRequest Struct
  2. Parsing Headers into a HashMap
  3. The HttpResponse Struct
  4. Status Code Enum
  5. Converting to Wire Format
  6. Exercise: Round-Trip Request/Response

Chapter 5: Connection Handling and Keep-Alive

  1. The Connection Lifecycle
  2. HTTP Keep-Alive Semantics
  3. Reading Multiple Requests Per Connection
  4. Timeout and Idle Connection Management
  5. Backpressure and Flow Control
  6. Exercise: Persistent Connection Server

Chapter 6: Routing and Endpoint Registration

  1. The Router Data Structure
  2. Exact Path Matching
  3. Parameterized Routes
  4. Method Dispatching
  5. Handler Traits and Closures
  6. Exercise: Multi-Route Server

Chapter 7: Middleware and the Request Pipeline

  1. The Middleware Pattern
  2. Building a Middleware Chain
  3. Common Middleware: Logging and Timing
  4. CORS Middleware
  5. Error Handling Middleware
  6. Exercise: Custom Rate Limiter

Chapter 8: Static File Serving

  1. Detecting Static Routes
  2. Reading Files Asynchronously
  3. MIME Type Detection
  4. Content-Encoding and Compression
  5. Cache-Control and ETag Headers
  6. Exercise: Production Static Server

Chapter 9: Templating and Dynamic HTML

  1. Why Server-Side Rendering Still Matters
  2. The Tera Template Engine
  3. Passing Context Data
  4. Layouts and Partials
  5. Security: Escaping and XSS Prevention
  6. Exercise: Blog Frontend

Chapter 10: Building REST APIs

  1. REST Design Principles
  2. JSON Serialization with serde
  3. The In-Memory Store Pattern
  4. CRUD Endpoint Implementation
  5. Error Responses and Validation
  6. Exercise: Task Manager API

Chapter 11: Concurrency Patterns and Performance

  1. Tokio’s Work-Stealing Scheduler
  2. Channels for Inter-Task Communication
  3. Shared State with Arc and Mutex
  4. Connection Limits and Graceful Shutdown
  5. Load Testing with wrk
  6. Exercise: Stress-Tested Server

Chapter 12: HTTP/2 Fundamentals

  1. Why HTTP/2
  2. Binary Framing and Multiplexing
  3. HPACK Header Compression
  4. The h2 Crate
  5. Upgrading from HTTP/1.1 to HTTP/2
  6. Exercise: Dual-Protocol Server

Chapter 13: TLS and HTTPS

  1. Why Encryption Matters
  2. The TLS Handshake
  3. Generating Self-Signed Certificates
  4. Rustls vs native-tls
  5. Integrating TLS with Tokio
  6. Exercise: HTTPS Server

Chapter 14: Security Hardening

  1. Common HTTP Attacks
  2. Security Headers
  3. Input Validation and Request Size Limits
  4. Rate Limiting Implementation
  5. Content Security Policy
  6. Exercise: Security Audit Checklist

Chapter 15: Testing, Benchmarking, and Logging

  1. Unit Testing Handlers and Routes
  2. Integration Tests with Test Clients
  3. Property-Based Testing for Parsers
  4. Structured Logging with tracing
  5. Benchmarking with criterion
  6. Exercise: Test Suite

Chapter 16: Performance Optimization and Deployment

  1. Profiling with flamegraphs
  2. Memory Allocation Strategies
  3. Connection Pooling
  4. Docker Containerization
  5. Reverse Proxy with Caddy or Nginx
  6. Exercise: Production Deployment

Conclusion: What Comes Next

  1. Recap of the Architecture
  2. Extending Your Server
  3. When Frameworks Make Sense
  4. Continuing Your Rust Journey

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub