vecread -> cursor
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use qstring::QString;
|
||||
use serde_json;
|
||||
use std::sync::Arc;
|
||||
use std::io::Cursor;
|
||||
use std::io::empty;
|
||||
|
||||
lazy_static! {
|
||||
static ref URL_BASE: Url = { Url::parse("http://localhost/").expect("Failed to parse URL_BASE") };
|
||||
@@ -39,15 +41,18 @@ impl Default for Payload {
|
||||
impl Payload {
|
||||
fn into_read(self) -> (Option<usize>, Box<Read + 'static>) {
|
||||
match self {
|
||||
Payload::Empty => (Some(0), Box::new(VecRead::from_str(""))),
|
||||
Payload::Empty => (Some(0), Box::new(empty())),
|
||||
Payload::Text(s) => {
|
||||
let read = VecRead::from_str(&s);
|
||||
(Some(read.len()), Box::new(read))
|
||||
let bytes = s.into_bytes();
|
||||
let len = bytes.len();
|
||||
let cursor = Cursor::new(bytes);
|
||||
(Some(len), Box::new(cursor))
|
||||
}
|
||||
Payload::JSON(v) => {
|
||||
let vec = serde_json::to_vec(&v).expect("Bad JSON in payload");
|
||||
let read = VecRead::from_vec(vec);
|
||||
(Some(read.len()), Box::new(read))
|
||||
let bytes = serde_json::to_vec(&v).expect("Bad JSON in payload");
|
||||
let len = bytes.len();
|
||||
let cursor = Cursor::new(bytes);
|
||||
(Some(len), Box::new(cursor))
|
||||
}
|
||||
Payload::Reader(read) => (None, read),
|
||||
}
|
||||
|
||||
@@ -246,9 +246,10 @@ fn parse_status_line(line: &str) -> Result<((usize, usize), u16), Error> {
|
||||
impl FromStr for Response {
|
||||
type Err = Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut read = VecRead::from_str(s);
|
||||
let mut resp = Self::do_from_read(&mut read)?;
|
||||
resp.set_stream(Stream::Read(Box::new(read)));
|
||||
let bytes = s.as_bytes().to_owned();
|
||||
let mut cursor = Cursor::new(bytes);
|
||||
let mut resp = Self::do_from_read(&mut cursor)?;
|
||||
resp.set_stream(Stream::Read(Box::new(cursor)));
|
||||
Ok(resp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use url::Url;
|
||||
use util::vecread::VecRead;
|
||||
use std::io::Cursor;
|
||||
|
||||
mod agent_test;
|
||||
mod auth;
|
||||
@@ -42,9 +42,9 @@ pub fn make_response(
|
||||
}
|
||||
write!(&mut buf, "\r\n").ok();
|
||||
buf.append(&mut body);
|
||||
let read = VecRead::from_vec(buf);
|
||||
let cursor = Cursor::new(buf);
|
||||
let write: Vec<u8> = vec![];
|
||||
Ok(Stream::Test(Box::new(read), write))
|
||||
Ok(Stream::Test(Box::new(cursor), write))
|
||||
}
|
||||
|
||||
pub fn resolve_handler(req: &Request, url: &Url) -> Result<Stream, Error> {
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
use std::io::Read;
|
||||
use std::io::Result;
|
||||
|
||||
pub struct VecRead {
|
||||
bytes: Vec<u8>,
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl VecRead {
|
||||
pub fn new(bytes: &[u8]) -> Self {
|
||||
Self::from_vec(bytes.to_owned())
|
||||
}
|
||||
pub fn from_vec(bytes: Vec<u8>) -> Self {
|
||||
VecRead {
|
||||
bytes,
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
pub fn from_str(s: &str) -> Self {
|
||||
Self::new(s.as_bytes())
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.bytes.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for VecRead {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let len = buf.len().min(self.bytes.len() - self.index);
|
||||
(&mut buf[0..len]).copy_from_slice(&self.bytes[self.index..self.index + len]);
|
||||
self.index += len;
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user