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 higher-level libraries and frameworks used to build web applications. So, why is it important to discuss low level protocols? This would be 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 such as REST, gRPC, websockets and TLS use HTTP and TCP for transport. Designing and building basic TCP and HTTP servers in Rust gives 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 to Chapter 3, and later come back to this chapter at a time appropriate for you.

In this chapter, you will learn the following:

  • Write a TCP client and server.
  • Build a library to convert between TCP raw byte streams and HTTP messages.
  • Build an HTTP server that can serve static web pages (aka web server) as well as json data (aka web service). Test the server with standard HTTP clients such as cURL (commandline) tool and web browser.

2.1 Writing a TCP server in Rust

 
 

2.1.1 Designing the TCP/IP communication flow

 
 

2.1.2 Writing the TCP server and client

 
 

2.2 Writing an HTTP server in Rust

 
 
 
 

2.2.1 Parsing HTTP request messages

 
 
 

2.2.2 Constructing HTTP response messages

 
 

2.2.3 Writing the main() function and server module

 
 
 

2.2.4 Writing the router and handler modules

 
 

2.2.5 Testing the web server

 
 
 
 

2.4 Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest