无法读取post 请求body, 我应该怎么做呢?
我试着读取post请求内容,我使用TcpStream.read_exact()方法,但是总是读不到任何信息。为什么呢?
#[derive(Debug)]
pub struct Server<> {
host : String,
port : String,
}
impl Server {
pub fn new(host:String, port: String) -> Server{
let mut server = Server{host, port};
if server.host.is_empty(){
server.host = "127.0.0.1".to_string();
}
if server.port.is_empty() {
server.port = "8080".to_string();
}
server
}
pub fn run(&self){
let listener = TcpListener::bind(self.host.clone()+":"+self.port.clone().as_str()).unwrap();
for stream in listener.incoming() {
match stream {
Ok(mut _stream) => {
println!("Start...");
self.handle_connection(_stream);
},
Err(e) => {
eprintln!("Can't start the Tractors, because of the error {}", e);
}
}
}
}
fn handle_connection(&self, mut stream: TcpStream) {
thread::spawn(move || {
println!("handle connection");
let mut buf_reader = BufReader::new(&stream);
let mut is_header_body_split = false;
let req_lines:Vec<_> = buf_reader.by_ref().lines()
.map(|result| result.unwrap_or_else(|_| return "".to_string()))
.take_while(|line| !line.is_empty() )
.collect();
/*let req_lines:Vec<_> = buf_reader.lines()
.map(|result| result.unwrap_or_else(|_| return "".to_string()))
.take_while(|line| if line.is_empty() && !is_header_body_split {
is_header_body_split = true;
true
}else if line.is_empty() && is_header_body_split {
false
}else {
true
})
.collect();*/
let mut http_request = HttpRequest::parse_request(req_lines);
match http_request.method {
Method::GET => {}
Method::POST => {
if http_request.header.contains_key("Content-Length") {
let content_length_str= http_request.header.get("Content-Length").unwrap();
if content_length_str.is_empty() {
http_request.body = "".to_string();
} else {
let content_length = content_length_str.as_str().parse::<usize>().unwrap();
/*let buf_reader1 = BufReader::new(&mut stream);
let body_lines:Vec<String> = buf_reader1.lines()
.map(|result| result.unwrap_or_else(|_| return "".to_string()))
.take_while(|line| !line.is_empty() )
.collect();
let mut body = "".to_string();
for line in body_lines {
body = body +line.as_str();
}*/
let mut buf ;
if content_length <=1024 {
buf = Vec::with_capacity(content_length);
let current_size= buf_reader.by_ref().read_exact(&mut buf).unwrap();
http_request.body = String::from_utf8(buf).unwrap();
}else {
buf = Vec::with_capacity(1024);
let mut body_buf = vec![0u8,0];
let mut size =0;
loop {
if content_length - size >1024{
stream.read_exact(&mut buf).expect("TODO: panic message");
size +=1024;
body_buf.append(&mut buf[..1024].to_vec());
} else if content_length - size <1024 && content_length -size>0 {
buf = Vec::with_capacity(content_length - size);
stream.read_exact(&mut buf).expect("TODO: panic message");
size += content_length - size;
body_buf.append(&mut buf[..(content_length - size)].to_vec());
break;
} else {
break;
}
}
http_request.body = String::from_utf8(body_buf).unwrap();
}
}
}
...
}
}
println!("{:?}", http_request);
});
}
}
stream.read_exact(&mut buf).expect(“TODO: panic message”); 这里执行万,buf 没有任何内容。