Skip to content

Instantly share code, notes, and snippets.

@scode
Created February 5, 2015 07:13
Show Gist options
  • Select an option

  • Save scode/586bf070d5141062be0a to your computer and use it in GitHub Desktop.

Select an option

Save scode/586bf070d5141062be0a to your computer and use it in GitHub Desktop.
/code fn write_with_body(mut res: HttpResponse<Fresh>, mut body: Box<Reader + Send>) -> IoResult<()> {
let content_type = res.headers().get::<headers::ContentType>()
.map(|cx| cx.clone())
.unwrap_or_else(|| headers::ContentType("text/plain".parse().unwrap()));
res.headers_mut().set(content_type);
let mut res = try!(res.start());
// FIXME: Manually inlined old_io::util::copy
// because Box<Reader + Send> does not impl Reader.
//
// Tracking issue: rust-lang/rust#18542
let mut buf = &mut [0; 1024 * 64];
loop {
let len = match body.read(buf) {
Ok(len) => len,
Err(ref e) if e.kind == old_io::EndOfFile => break,
Err(e) => { return Err(e) },
};
try!(res.write_all(&buf[..len]))
}
res.end()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment