Hone logo
Hone
Problems

Asynchronous Web Server with async-std

This challenge focuses on building a basic asynchronous web server in Rust using the async-std runtime. You will learn to handle concurrent requests efficiently, a fundamental skill for modern network applications.

Problem Description

Your task is to create a simple HTTP server that listens on a specified port and responds to incoming requests. The server should be able to handle multiple connections concurrently without blocking. Specifically, you need to:

  • Listen on a port: The server should bind to a given port number.
  • Accept incoming connections: Continuously listen for and accept new TCP connections.
  • Handle requests asynchronously: For each incoming connection, spawn a new asynchronous task to handle the request and send a response.
  • Respond with a simple message: For any incoming request, the server should respond with a plain text message, for example, "Hello from async-std!".
  • Graceful shutdown: Implement a mechanism to gracefully shut down the server, for instance, by responding to a specific signal (e.g., Ctrl+C).

Examples

Example 1: Basic Request

Input:
A client sends an HTTP GET request to http://localhost:<port>/

Output:
The server responds with:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 22
\r\n
Hello from async-std!

Explanation: When a client connects and sends a GET request, the asynchronous task handling that connection reads the request, prepares the simple text response with appropriate headers, and sends it back to the client.

Example 2: Concurrent Requests

Input:
Multiple clients simultaneously send HTTP GET requests to http://localhost:<port>/

Output:
All clients receive the same plain text response as in Example 1, and the server remains responsive to new connections.

Explanation: The async-std runtime allows the server to spawn a separate asynchronous task for each incoming connection. This means that even if one client is slow to process its request, other clients can still connect and receive responses without delay.

Constraints

  • The server must use the async-std runtime.
  • The port number will be provided as a command-line argument or a hardcoded value (e.g., 8080).
  • The server should handle basic HTTP requests (GET requests are sufficient for this challenge).
  • The response body should be exactly "Hello from async-std!".
  • The server should be able to handle at least 100 concurrent connections.

Notes

  • Familiarize yourself with async-std::net::TcpListener and async_std::task::spawn.
  • Consider how to parse incoming HTTP requests, even if it's just to identify that a request has been received. For this challenge, a simplified approach of assuming any incoming data is a request is acceptable.
  • Remember to include necessary HTTP headers in your response (e.g., Content-Type, Content-Length).
  • Handling signals for graceful shutdown is a good practice for robust server applications. Look into async_std::channel and signal handling mechanisms.
Loading editor...
rust