support async gzip decoding

This commit is contained in:
Ashley Mannix
2017-08-18 19:43:06 +10:00
parent aee559bdea
commit e2fa97254e
10 changed files with 605 additions and 203 deletions

77
tests/async.rs Normal file
View File

@@ -0,0 +1,77 @@
#![cfg(feature="unstable")]
extern crate futures;
extern crate tokio_core;
extern crate tokio_io;
extern crate reqwest;
extern crate libflate;
#[macro_use]
mod support;
use futures::{Future, Stream};
use tokio_core::reactor::Core;
use std::io::Write;
use std::time::Duration;
#[test]
fn async_test_gzip_response() {
test_gzip(10_000, 4096);
}
#[test]
fn async_test_gzip_single_byte_chunks() {
test_gzip(10, 1);
}
fn test_gzip(response_size: usize, chunk_size: usize) {
let content: String = (0..response_size).into_iter().map(|i| format!("test {}", i)).collect();
let mut encoder = ::libflate::gzip::Encoder::new(Vec::new()).unwrap();
match encoder.write(content.as_bytes()) {
Ok(n) => assert!(n > 0, "Failed to write to encoder."),
_ => panic!("Failed to gzip encode string."),
};
let gzipped_content = encoder.finish().into_result().unwrap();
let mut response = format!("\
HTTP/1.1 200 OK\r\n\
Server: test-accept\r\n\
Content-Encoding: gzip\r\n\
Content-Length: {}\r\n\
\r\n", &gzipped_content.len())
.into_bytes();
response.extend(&gzipped_content);
let server = server! {
request: b"\
GET /gzip HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
Accept-Encoding: gzip\r\n\
\r\n\
",
chunk_size: chunk_size,
write_timeout: Duration::from_millis(10),
response: response
};
let mut core = Core::new().unwrap();
let client = reqwest::unstable::async::Client::new(&core.handle()).unwrap();
let res_future = client.get(&format!("http://{}/gzip", server.addr()))
.unwrap()
.send()
.and_then(|mut res| res.body().concat2())
.and_then(|buf| {
let body = ::std::str::from_utf8(&buf).unwrap();
assert_eq!(body, &content);
Ok(())
});
core.run(res_future).unwrap();
}

View File

@@ -4,12 +4,15 @@ extern crate libflate;
#[macro_use]
mod support;
use std::time::Duration;
use std::io::{Read, Write};
#[test]
fn test_gzip_response() {
let content: String = (0..50).into_iter().map(|i| format!("test {}", i)).collect();
let chunk_size = content.len() / 3;
let mut encoder = ::libflate::gzip::Encoder::new(Vec::new()).unwrap();
match encoder.write(b"test request") {
match encoder.write(content.as_bytes()) {
Ok(n) => assert!(n > 0, "Failed to write to encoder."),
_ => panic!("Failed to gzip encode string."),
};
@@ -34,6 +37,8 @@ fn test_gzip_response() {
Accept-Encoding: gzip\r\n\
\r\n\
",
chunk_size: chunk_size,
write_timeout: Duration::from_millis(10),
response: response
};
let mut res = reqwest::get(&format!("http://{}/gzip", server.addr())).unwrap();
@@ -41,7 +46,7 @@ fn test_gzip_response() {
let mut body = String::new();
res.read_to_string(&mut body).unwrap();
assert_eq!(body, "test request");
assert_eq!(body, content);
}
#[test]

View File

@@ -23,6 +23,7 @@ pub struct Txn {
pub read_timeout: Option<Duration>,
pub response_timeout: Option<Duration>,
pub write_timeout: Option<Duration>,
pub chunk_size: Option<usize>,
}
static DEFAULT_USER_AGENT: &'static str =
@@ -55,10 +56,21 @@ pub fn spawn(txns: Vec<Txn>) -> Server {
}
if let Some(dur) = txn.write_timeout {
let headers_end = ::std::str::from_utf8(&reply).unwrap().find("\r\n\r\n").unwrap() + 4;
let headers_end = b"\r\n\r\n";
let headers_end = reply.windows(headers_end.len()).position(|w| w == headers_end).unwrap() + 4;
socket.write_all(&reply[..headers_end]).unwrap();
thread::park_timeout(dur);
socket.write_all(&reply[headers_end..]).unwrap();
let body = &reply[headers_end..];
if let Some(chunk_size) = txn.chunk_size {
for content in body.chunks(chunk_size) {
thread::park_timeout(dur);
socket.write_all(&content).unwrap();
}
} else {
thread::park_timeout(dur);
socket.write_all(&body).unwrap();
}
} else {
socket.write_all(&reply).unwrap();
}