Rust Web 全棧開發(二):構建 HTTP Server
- Rust Web 全棧開發(二):構建 HTTP Server
- 創建成員包/庫:httpserver、http
- 解析 HTTP 請求
- HTTP 請求的構成
- 構建 HttpRequest
- 構建 HTTP 響應
- HTTP 響應的構成
- 構建 HttpResponse
- lib
Rust Web 全棧開發(二):構建 HTTP Server
參考視頻:https://www.bilibili.com/video/BV1RP4y1G7KF
Web Server 的消息流動圖:
Server:監聽 TCP 字節流
Router:接收 HTTP 請求,并決定調用哪個 Handler
Handler:處理 HTTP 請求,構建 HTTP 響應
HTTP Library:
- 解釋字節流,把它轉換為 HTTP 請求
- 把 HTTP 響應轉換回字節流
構建步驟:
- 解析 HTTP 請求消息
- 構建 HTTP 響應消息
- 路由與 Handler
- 測試 Web Server
創建成員包/庫:httpserver、http
在原項目下新建成員包 httpserver、成員庫 http:
cargo new httpserver
cargo new --lib http
在工作區內運行 cargo new 會自動將新創建的包添加到工作區內 Cargo.toml 的 [workspace] 定義中的 members 鍵中,如下所示:
在 http 成員庫的 src 目錄下新建兩個文件:httprequest.rs、httpresponse.rs。
此時,我們可以通過運行 cargo build 來構建工作區。項目目錄下的文件應該是這樣的:
├── Cargo.lock
├── Cargo.toml
├── httpserver
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── http
│ ├── Cargo.toml
│ └── src
│ └── lib.rs
│ └── httprequest.rs
│ └── httpresponse.rs
├── tcpclient
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── tcpserver
│ ├── Cargo.toml
│ └── src
│ └── main.rs
└── target
解析 HTTP 請求
HTTP 請求的構成
HTTP 請求報文由 3 部分組成:請求行、請求頭、請求體。
構建 HttpRequest
3 個數據結構:
名稱 | 類型 | 描述 |
---|---|---|
HttpRequest | struct | 表示 HTTP 請求 |
Method | enum | 指定所允許的 HTTP 方法 |
Version | enum | 指定所允許的 HTTP 版本 |
以上 3 個數據結構都需要實現的 3 個 trait:
名稱 | 描述 |
---|---|
From<&str> | 用于把傳進來的字符串切片轉換為 HttpRequest |
Debug | 打印調試信息 |
PartialEq | 用于解析和自動化測試腳本里做比較 |
打開 http 成員庫中的 httprequest.rs,編寫代碼:
use std::collections::HashMap;#[derive(Debug, PartialEq)]
pub enum Method {Get,Post,Uninitialized,
}
impl From<&str> for Method {fn from(s: &str) -> Method {match s {"GET" => Method::Get,"POST" => Method::Post,_ => Method::Uninitialized,}}
}#[derive(Debug, PartialEq)]
pub enum Version {V1_1,V2_0,Uninitialized,
}impl From<&str> for Version {fn from(s: &str) -> Version {match s {"HTTP/1.1" => Version::V1_1,_ => Version::Uninitialized,}}
}#[derive(Debug, PartialEq)]
pub enum Resource {Path(String),
}#[derive(Debug)]
pub struct HttpRequest {pub method: Method,pub resource: Resource,pub version: Version,pub headers: HashMap<String, String>,pub body: String,
}impl From<String> for HttpRequest {fn from(request: String) -> HttpRequest {let mut parsed_method = Method::Uninitialized;let mut parsed_resource = Resource::Path("".to_string());let mut parsed_version = Version::V1_1;let mut parsed_headers = HashMap::new();let mut parsed_body = "";for line in request.lines() {if line.contains("HTTP") {let (method, resource, version) = process_request_line(line);parsed_method = method;parsed_resource = resource;parsed_version = version;} else if line.contains(":") {let (key, value) = process_header_line(line);parsed_headers.insert(key, value);} else if line.len() == 0 {} else {parsed_body = line;}}HttpRequest {method: parsed_method,resource: parsed_resource,version: parsed_version,headers: parsed_headers,body: parsed_body.to_string(),}}
}fn process_header_line(s: &str) -> (String, String) {let mut header_items = s.split(":");let mut key = String::from("");let mut value = String::from("");if let Some(k) = header_items.next() {key = k.to_string();}if let Some(v) = header_items.next() {value = v.to_string();}(key, value)
}fn process_request_line(s: &str) -> (Method, Resource, Version) {let mut words = s.split_whitespace();let method = words.next().unwrap();let resource = words.next().unwrap();let version = words.next().unwrap();(method.into(),Resource::Path(resource.to_string()),version.into())
}#[cfg(test)]
mod test {use super::*;#[test]fn test_method_into() {let method: Method = "GET".into();assert_eq!(method, Method::Get);}#[test]fn test_version_into() {let version: Version = "HTTP/1.1".into();assert_eq!(version, Version::V1_1);}#[test]fn test_read_http() {let s = String::from("GET /greeting HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.71.1\r\nAccept: */*\r\n\r\n");let mut headers_excepted = HashMap::new();headers_excepted.insert("Host".into(), " localhost".into());headers_excepted.insert("Accept".into(), " */*".into());headers_excepted.insert("User-Agent".into(), " curl/7.71.1".into());let request: HttpRequest = s.into();assert_eq!(request.method, Method::Get);assert_eq!(request.resource, Resource::Path("/greeting".to_string()));assert_eq!(request.version, Version::V1_1);assert_eq!(request.headers, headers_excepted);}
}
運行命令 cargo test -p http,測試 http 成員庫。
3 個測試都通過了:
構建 HTTP 響應
HTTP 響應的構成
HTTP 響應報文由 3 部分組成:響應行、響應頭、響應體。
構建 HttpResponse
HttpResponse 需要實現的方法或 trait:
名稱 | 描述 |
---|---|
Default trait | 指定成員的默認值 |
From trait | 將 HttpResponse 轉化為 String |
new() | 使用默認值創建一個新的 HttpResponse 結構體 |
getter 方法 | 獲取 HttpResponse 成員變量的值 |
send_response() | 構建響應,將原始字節通過 TCP 傳送 |
打開 http 成員庫中的 httpresponse.rs,編寫代碼:
use std::collections::HashMap;
use std::io::{Result, Write};#[derive(Debug, PartialEq, Clone)]
pub struct HttpResponse<'a> {version: &'a str,status_code: &'a str,status_text: &'a str,headers: Option<HashMap<&'a str, &'a str>>,body: Option<String>,
}impl<'a> Default for HttpResponse<'a> {fn default() -> Self {Self {version: "HTTP/1.1".into(),status_code: "200".into(),status_text: "OK".into(),headers: None,body: None,}}
}impl<'a> From<HttpResponse<'a>> for String {fn from(response: HttpResponse) -> String {let res = response.clone();format!("{} {} {}\r\n{}Content-Length: {}\r\n\r\n{}",&res.version(),&res.status_code(),&res.status_text(),&res.headers(),&response.body.unwrap().len(),&res.body(),)}
}impl<'a> HttpResponse<'a> {pub fn new(status_code: &'a str,headers: Option<HashMap<&'a str, &'a str>>,body: Option<String>,) -> HttpResponse<'a> {let mut response: HttpResponse<'a> = HttpResponse::default();if status_code != "200" {response.status_code = status_code.into();}response.status_text = match response.status_code {// 消息"100" => "Continue".into(),"101" => "Switching Protocols".into(),"102" => "Processing".into(),// 成功"200" => "OK".into(),"201" => "Created".into(),"202" => "Accepted".into(),"203" => "Non-Authoritative Information".into(),"204" => "No Content".into(),"205" => "Reset Content".into(),"206" => "Partial Content".into(),"207" => "Multi-Status".into(),// 重定向"300" => "Multiple Choices".into(),"301" => "Moved Permanently".into(),"302" => "Move Temporarily".into(),"303" => "See Other".into(),"304" => "Not Modified".into(),"305" => "Use Proxy".into(),"306" => "Switch Proxy".into(),"307" => "Temporary Redirect".into(),// 請求錯誤"400" => "Bad Request".into(),"401" => "Unauthorized".into(),"402" => "Payment Required".into(),"403" => "Forbidden".into(),"404" => "Not Found".into(),"405" => "Method Not Allowed".into(),"406" => "Not Acceptable".into(),"407" => "Proxy Authentication Required".into(),"408" => "Request Timeout".into(),"409" => "Conflict".into(),"410" => "Gone".into(),"411" => "Length Required".into(),"412" => "Precondition Failed".into(),"413" => "Request Entity Too Large".into(),"414" => "Request-URI Too Long".into(),"415" => "Unsupported Media Type".into(),"416" => "Requested Range Not Satisfiable".into(),"417" => "Expectation Failed".into(),"421" => "Misdirected Request".into(),"422" => "Unprocessable Entity".into(),"423" => "Locked".into(),"424" => "Failed Dependency".into(),"425" => "Too Early".into(),"426" => "Upgrade Required".into(),"449" => "Retry With".into(),"451" => "Unavailable For Legal Reasons".into(),// 服務器錯誤"500" => "Internal Server Error".into(),"501" => "Not Implemented".into(),"502" => "Bad Gateway".into(),"503" => "Service Unavailable".into(),"504" => "Gateway Timeout".into(),"505" => "HTTP Version Not Supported".into(),"506" => "Variant Also Negotiates".into(),"507" => "Insufficient Storage".into(),"509" => "Bandwidth Limit Exceeded".into(),"510" => "Not Extended".into(),"600" => "Unparseable Response Headers".into(),_ => "Not Found".into(),};response.headers = match &headers {Some(_h) => headers,None => {let mut header = HashMap::new();header.insert("Content-Type", "text/html");Some(header)}};response.body = body;response}fn version(&self) -> &str {self.version}fn status_code(&self) -> &str {self.status_code}fn status_text(&self) -> &str {self.status_text}fn headers(&self) -> String {let map = self.headers.clone().unwrap();let mut headers_string = "".into();for (key, value) in map.iter() {headers_string = format!("{headers_string}{}:{}\r\n", key, value);}headers_string}fn body(&self) -> &str {match &self.body {Some(b) => b.as_str(),None => "",}}pub fn send_response(&self, write_stream: &mut impl Write) -> Result<()> {let response = self.clone();let response_string: String = String::from(response);let _ = write!(write_stream, "{}", response_string);Ok(())}
}#[cfg(test)]
mod test {use super::*;#[test]fn test_response_struct_creation_200() {let response_actual = HttpResponse::new("200",None,Some("xxxx".into()),);let response_excepted = HttpResponse {version: "HTTP/1.1",status_code: "200",status_text: "OK",headers: {let mut h = HashMap::new();h.insert("Content-Type", "text/html");Some(h)},body: Some("xxxx".into()),};assert_eq!(response_actual, response_excepted);}#[test]fn test_response_struct_creation_404() {let response_actual = HttpResponse::new("404",None,Some("xxxx".into()),);let response_excepted = HttpResponse {version: "HTTP/1.1",status_code: "404",status_text: "Not Found",headers: {let mut h = HashMap::new();h.insert("Content-Type", "text/html");Some(h)},body: Some("xxxx".into()),};assert_eq!(response_actual, response_excepted);}#[test]fn test_http_response_creation() {let response_excepted = HttpResponse {version: "HTTP/1.1",status_code: "404",status_text: "Not Found",headers: {let mut h = HashMap::new();h.insert("Content-Type", "text/html");Some(h)},body: Some("xxxx".into()),};let http_string: String = response_excepted.into();let actual_string = "HTTP/1.1 404 Not Found\r\nContent-Type:text/html\r\nContent-Length: 4\r\n\r\nxxxx";assert_eq!(http_string, actual_string);}
}
運行命令 cargo test -p http,測試 http 成員庫。
現在一共有 6 個測試,都通過了:
lib
打開 http 成員庫中的 lib.rs,編寫代碼:
pub mod httprequest;
pub mod httpresponse;