mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_http: use flexible-sized buffer for request reading
This commit is contained in:
parent
3a8e5a3abf
commit
136a79ce8d
2 changed files with 18 additions and 6 deletions
1
TODO.md
1
TODO.md
|
|
@ -7,7 +7,6 @@
|
||||||
|
|
||||||
## aquatic_http
|
## aquatic_http
|
||||||
* handshake stuff
|
* handshake stuff
|
||||||
* fixed size buffer is probably bad
|
|
||||||
* test tls
|
* test tls
|
||||||
* request parsing in protocol module instead of in network? Not obvious
|
* request parsing in protocol module instead of in network? Not obvious
|
||||||
what error return type to use then
|
what error return type to use then
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ pub enum RequestReadError {
|
||||||
pub struct EstablishedConnection {
|
pub struct EstablishedConnection {
|
||||||
stream: Stream,
|
stream: Stream,
|
||||||
pub peer_addr: SocketAddr,
|
pub peer_addr: SocketAddr,
|
||||||
buf: [u8; 1024],
|
buf: Vec<u8>,
|
||||||
bytes_read: usize,
|
bytes_read: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,25 +41,33 @@ impl EstablishedConnection {
|
||||||
Self {
|
Self {
|
||||||
stream,
|
stream,
|
||||||
peer_addr,
|
peer_addr,
|
||||||
buf: [0; 1024], // FIXME: fixed size is stupid
|
buf: Vec::new(),
|
||||||
bytes_read: 0,
|
bytes_read: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_request(&mut self) -> Result<Request, RequestReadError> {
|
pub fn read_request(&mut self) -> Result<Request, RequestReadError> {
|
||||||
|
if self.buf.len() - self.bytes_read < 512 {
|
||||||
|
self.buf.extend_from_slice(&[0; 1024]);
|
||||||
|
}
|
||||||
|
|
||||||
match self.stream.read(&mut self.buf[self.bytes_read..]){
|
match self.stream.read(&mut self.buf[self.bytes_read..]){
|
||||||
Ok(0) => {
|
Ok(0) => {
|
||||||
|
self.clear_buffer();
|
||||||
|
|
||||||
return Err(RequestReadError::StreamEnded);
|
return Err(RequestReadError::StreamEnded);
|
||||||
}
|
}
|
||||||
Ok(bytes_read) => {
|
Ok(bytes_read) => {
|
||||||
self.bytes_read += bytes_read;
|
self.bytes_read += bytes_read;
|
||||||
|
|
||||||
info!("parse request read {} bytes", bytes_read);
|
info!("read_request read {} bytes", bytes_read);
|
||||||
},
|
},
|
||||||
Err(err) if err.kind() == ErrorKind::WouldBlock => {
|
Err(err) if err.kind() == ErrorKind::WouldBlock => {
|
||||||
return Err(RequestReadError::NeedMoreData);
|
return Err(RequestReadError::NeedMoreData);
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
self.clear_buffer();
|
||||||
|
|
||||||
return Err(RequestReadError::Io(err));
|
return Err(RequestReadError::Io(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +81,7 @@ impl EstablishedConnection {
|
||||||
Request::from_http_get_path
|
Request::from_http_get_path
|
||||||
);
|
);
|
||||||
|
|
||||||
self.bytes_read = 0;
|
self.clear_buffer();
|
||||||
|
|
||||||
if let Some(request) = opt_request {
|
if let Some(request) = opt_request {
|
||||||
Ok(request)
|
Ok(request)
|
||||||
|
|
@ -85,7 +93,7 @@ impl EstablishedConnection {
|
||||||
Err(RequestReadError::NeedMoreData)
|
Err(RequestReadError::NeedMoreData)
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
self.bytes_read = 0;
|
self.clear_buffer();
|
||||||
|
|
||||||
Err(RequestReadError::Parse(err))
|
Err(RequestReadError::Parse(err))
|
||||||
}
|
}
|
||||||
|
|
@ -106,6 +114,11 @@ impl EstablishedConnection {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clear_buffer(&mut self){
|
||||||
|
self.bytes_read = 0;
|
||||||
|
self.buf = Vec::new();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue