3. HTTP响应
在前面我们编写了HTTP请求相关的的代码
现在我们在如下文件中继续编写与HTTP响应相关的代码
首先我们需要定义HttpResponse结构体
然后再为其实现一些trait和方法,最后我们会针对这写方法和trait进行测试
具体代码如下
use std::collections::HashMap;
use std::io::{Result,Write};
//import three traits
#[derive(Debug, PartialEq, Clone)]
//define a struct that includes some properties
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>,
}
//implement trait for struct above
//it can specify the default value to HttpResponse(because the trait has a method related to struct(HttpResponse))
impl <'a> Default for HttpResponse<'a> {
fn default()->Self {
//some default values
Self {
version: "HTTP/1.1".into(),
s