2 Writing a basic web server from scratch

 

This chapter covers

  • Writing a TCP server in Rust
  • Writing an HTTP server in Rust

In this chapter, you will delve deep into TCP and HTTP communications using Rust. These protocols are generally abstracted away for developers through the higher-level libraries and frameworks that are used to build web applications. So why is it important to discuss low-level protocols? This is a fair question.

Learning to work with TCP and HTTP is important because they form the foundation for most communications on the internet. Popular application communication protocols and techniques such as REST, gRPC, and WebSockets use HTTP and TCP for transport. Designing and building basic TCP and HTTP servers in Rust will give you the confidence to design, develop, and troubleshoot higher-level application backend services.

However, if you are eager to get started with the example application, you can move ahead to chapter 3 and come back to this chapter when you want to understand more.

In this chapter, you will learn the following:

  • How to write a TCP client and server.
  • How to build a library to convert between TCP raw byte streams and HTTP messages.
  • How to build an HTTP server that can serve static web pages (a web server) as well as JSON data (a web service). You’ll test the server with standard HTTP clients such as the cURL (command line) tool and a web browser.

2.1 The networking model

2.2 Writing a TCP server in Rust

2.2.1 Designing the TCP/IP communication flow

2.2.2 Writing the TCP server and client

2.3 Writing an HTTP server in Rust

2.3.1 Parsing HTTP request messages

2.3.2 Constructing HTTP response messages

2.3.3 Writing the main() function and server module

2.3.4 Writing the router and handler modules

2.3.5 Testing the web server

Summary