vecread -> cursor

This commit is contained in:
Martin Algesten
2018-06-14 15:17:40 +02:00
parent 7177a99d1f
commit 75c8a9bbde
4 changed files with 18 additions and 46 deletions

View File

@@ -1,6 +1,8 @@
use qstring::QString; use qstring::QString;
use serde_json; use serde_json;
use std::sync::Arc; use std::sync::Arc;
use std::io::Cursor;
use std::io::empty;
lazy_static! { lazy_static! {
static ref URL_BASE: Url = { Url::parse("http://localhost/").expect("Failed to parse URL_BASE") }; 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 { impl Payload {
fn into_read(self) -> (Option<usize>, Box<Read + 'static>) { fn into_read(self) -> (Option<usize>, Box<Read + 'static>) {
match self { match self {
Payload::Empty => (Some(0), Box::new(VecRead::from_str(""))), Payload::Empty => (Some(0), Box::new(empty())),
Payload::Text(s) => { Payload::Text(s) => {
let read = VecRead::from_str(&s); let bytes = s.into_bytes();
(Some(read.len()), Box::new(read)) let len = bytes.len();
let cursor = Cursor::new(bytes);
(Some(len), Box::new(cursor))
} }
Payload::JSON(v) => { Payload::JSON(v) => {
let vec = serde_json::to_vec(&v).expect("Bad JSON in payload"); let bytes = serde_json::to_vec(&v).expect("Bad JSON in payload");
let read = VecRead::from_vec(vec); let len = bytes.len();
(Some(read.len()), Box::new(read)) let cursor = Cursor::new(bytes);
(Some(len), Box::new(cursor))
} }
Payload::Reader(read) => (None, read), Payload::Reader(read) => (None, read),
} }

View File

@@ -246,9 +246,10 @@ fn parse_status_line(line: &str) -> Result<((usize, usize), u16), Error> {
impl FromStr for Response { impl FromStr for Response {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut read = VecRead::from_str(s); let bytes = s.as_bytes().to_owned();
let mut resp = Self::do_from_read(&mut read)?; let mut cursor = Cursor::new(bytes);
resp.set_stream(Stream::Read(Box::new(read))); let mut resp = Self::do_from_read(&mut cursor)?;
resp.set_stream(Stream::Read(Box::new(cursor)));
Ok(resp) Ok(resp)
} }
} }

View File

@@ -6,7 +6,7 @@ use std::collections::HashMap;
use std::io::Write; use std::io::Write;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use url::Url; use url::Url;
use util::vecread::VecRead; use std::io::Cursor;
mod agent_test; mod agent_test;
mod auth; mod auth;
@@ -42,9 +42,9 @@ pub fn make_response(
} }
write!(&mut buf, "\r\n").ok(); write!(&mut buf, "\r\n").ok();
buf.append(&mut body); buf.append(&mut body);
let read = VecRead::from_vec(buf); let cursor = Cursor::new(buf);
let write: Vec<u8> = vec![]; 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> { pub fn resolve_handler(req: &Request, url: &Url) -> Result<Stream, Error> {

View File

@@ -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)
}
}