Skip to content

Commit 46ea48e

Browse files
committed
fix body parser
1 parent 1dae096 commit 46ea48e

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/hteapot/brew.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl HttpRequest {
123123
println!("Read timeout");
124124
break;
125125
}
126-
Err(e) => return Err("Error reading"),
126+
Err(_) => return Err("Error reading"),
127127
}
128128
}
129129

src/hteapot/request.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ impl HttpRequestBuilder {
127127
fn read_body_len(&mut self) -> Option<()> {
128128
let body_left = self.body_size.saturating_sub(self.request.body.len());
129129
let to_take = min(body_left, self.buffer.len());
130-
let to_append = &self.buffer[..to_take];
130+
let to_append = self.buffer.drain(..to_take);
131+
let to_append = to_append.as_slice();
131132
self.request.body.extend_from_slice(to_append);
132-
self.buffer.drain(..to_take);
133+
let body_left = self.body_size.saturating_sub(self.request.body.len());
133134

134135
if body_left > 0 {
135136
return None;

src/hteapot/response.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use super::HttpStatus;
1111
use super::{BUFFER_SIZE, VERSION};
1212
use std::collections::{HashMap, VecDeque};
13+
use std::sync::Arc;
1314
use std::sync::atomic::{AtomicBool, Ordering};
1415
use std::sync::mpsc::{self, Receiver, SendError, Sender, TryRecvError};
15-
use std::sync::Arc;
1616
use std::thread;
1717
use std::thread::JoinHandle;
1818

@@ -36,14 +36,13 @@ impl BaseResponse {
3636
self.status.to_string(),
3737
headers_text
3838
);
39-
39+
4040
let mut response = Vec::new();
4141
response.extend_from_slice(response_header.as_bytes());
4242
response
4343
}
4444
}
4545

46-
4746
/// Represents a full HTTP response (headers + body).
4847
pub struct HttpResponse {
4948
base: BaseResponse,
@@ -83,13 +82,13 @@ impl HttpResponse {
8382
) -> Box<Self> {
8483
let mut headers = headers.unwrap_or(HashMap::new());
8584
let content = content.as_ref();
86-
85+
8786
headers.insert("Content-Length".to_string(), content.len().to_string());
8887
headers.insert(
8988
"Server".to_string(),
9089
format!("HTeaPot/{}", VERSION).to_string(),
9190
);
92-
91+
9392
Box::new(HttpResponse {
9493
base: BaseResponse { status, headers },
9594
content: content.to_owned(),
@@ -160,7 +159,7 @@ impl HttpResponseCommon for HttpResponse {
160159
if self.raw.is_none() {
161160
self.raw = Some(self.to_bytes());
162161
}
163-
162+
164163
let raw = self.raw.as_ref().unwrap();
165164
let mut raw = raw.chunks(BUFFER_SIZE).skip(self.index);
166165
let byte_chunk = raw.next().ok_or(IterError::Finished)?.to_vec();
@@ -193,7 +192,7 @@ impl ChunkSender {
193192
/// Sends a new chunk to the output stream.
194193
///
195194
/// Prepends the size in hex followed by CRLF, then the chunk, then another CRLF.
196-
195+
197196
// fn new(sender: Sender<Vec<u8>>) -> Self {
198197
// Self(sender)
199198
// }
@@ -232,14 +231,14 @@ impl StreamedResponse {
232231
status: HttpStatus::OK,
233232
headers: HashMap::new(),
234233
};
235-
234+
236235
base.headers
237-
.insert("Transfer-Encoding".to_string(), "chunked".to_string());
236+
.insert("Transfer-Encoding".to_string(), "chunked".to_string());
238237
base.headers.insert(
239238
"Server".to_string(),
240239
format!("HTeaPot/{}", VERSION).to_string(),
241240
);
242-
241+
243242
let _ = tx.send(base.to_bytes());
244243
let has_end = Arc::new(AtomicBool::new(false));
245244
let action_clon = action.clone();
@@ -270,7 +269,7 @@ impl HttpResponseCommon for StreamedResponse {
270269
fn base(&mut self) -> &mut BaseResponse {
271270
&mut self.base
272271
}
273-
272+
274273
fn next(&mut self) -> Result<Vec<u8>, IterError> {
275274
self.peek()
276275
}

0 commit comments

Comments
 (0)