Skip to content

Commit

Permalink
feat(doit): initial part writing
Browse files Browse the repository at this point in the history
We are a state-machine, and handle parts of it correctly.
However, we don't yet write the boundary at all, and could improve
our use of match.
  • Loading branch information
Byron committed Mar 18, 2015
1 parent fc589cb commit 71c827b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 337 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/rust/lib.rs"

[dependencies]
hyper = "*"
mime = "*"
rustc-serialize = "*"
yup-oauth2 = "*"

Expand Down
3 changes: 0 additions & 3 deletions src/mako/Cargo.toml.mako
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ mime = "*"
url = "*"
rustc-serialize = "*"
yup-oauth2 = "*"
[dev-dependencies]
yup-hyper-mock = "*"
10 changes: 5 additions & 5 deletions src/mako/lib/mbuild.mako
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,9 @@ else {
let mut body_reader: &mut io::Read = match ${simple_media_param.type.arg_name}.as_mut() {
Some(&mut (ref mut reader, size, ref mime)) => {
let rsize = request_value_reader.seek(io::SeekFrom::End(0)).unwrap();
request_value_reader.seek(io::SeekFrom::Start(0)).ok();
mp_reader = mp_reader.add_part(&mut request_value_reader, rsize, &json_mime_type)
.add_part(reader, size, mime);
request_value_reader.seek(io::SeekFrom::Start(0)).unwrap();
mp_reader.add_part(&mut request_value_reader, rsize, &json_mime_type)
.add_part(reader, size, mime);
content_type = ContentType(mp_reader.mime_type());
&mut mp_reader
}
Expand Down Expand Up @@ -675,7 +675,7 @@ else {
if !res.status.is_success() {
if ${delegate}.is_some() {
let mut json_err = String::new();
res.read_to_string(&mut json_err).ok();
res.read_to_string(&mut json_err).unwrap();
let error_info: cmn::JsonServerError = json::decode(&json_err).unwrap();
if let oauth2::Retry::After(d) = ${delegate_call}.http_failure(&res, error_info) {
sleep(d);
Expand All @@ -686,7 +686,7 @@ else {
}
% if response_schema:
let mut json_response = String::new();
res.read_to_string(&mut json_response).ok();
res.read_to_string(&mut json_response).unwrap();
let result_value = (res, json::decode(&json_response).unwrap());
% else:
let result_value = res;
Expand Down
37 changes: 34 additions & 3 deletions src/rust/cmn.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::marker::MarkerTrait;
use std::io::{self, Read, Seek, Cursor};
use std::io::{self, Read, Seek, Cursor, Write, SeekFrom};

use mime::{Mime, TopLevel, SubLevel, Attr, Value};
use oauth2;
use hyper;
use hyper::header::{ContentType, ContentLength, Headers};
use hyper::http::LINE_ENDING;

/// Identifies the Hub. There is only one per library, this trait is supposed
/// to make intended use more explicit.
Expand Down Expand Up @@ -145,7 +146,7 @@ impl<'a> MultiPartReader<'a> {
/// # Panics
///
/// If this method is called after the first `read` call, it will panic
pub fn add_part(mut self, reader: &'a mut Read, size: u64, mime_type: &Mime) -> MultiPartReader<'a> {
pub fn add_part(&mut self, reader: &'a mut Read, size: u64, mime_type: &Mime) -> &mut MultiPartReader<'a> {
let mut headers = Headers::new();
headers.set(ContentType(mime_type.clone()));
headers.set(ContentLength(size));
Expand All @@ -166,6 +167,36 @@ impl<'a> MultiPartReader<'a> {

impl<'a> Read for MultiPartReader<'a> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::from_os_error(0))
if self.raw_parts.len() == 0 && self.current_part.is_none() {
return Ok(0)
} else if self.raw_parts.len() > 0 && self.current_part.is_none() {
let (headers, reader) = self.raw_parts.remove(0);
let mut c = Cursor::new(Vec::<u8>::new());
write!(&mut c, "{}{}", headers, LINE_ENDING).unwrap();
c.seek(SeekFrom::Start(0)).unwrap();
self.current_part = Some((c, reader));
}
// read headers as long as possible
let (hb, rr) = {
let &mut (ref mut c, ref mut reader) = self.current_part.as_mut().unwrap();
let b = c.read(buf).unwrap_or(0);
(b, reader.read(&mut buf[b..]))
};

match rr {
Ok(bytes_read) => {
if bytes_read == 0 {
// We are depleted - this can trigger the next part to come in
self.current_part = None;
}
Ok(hb + bytes_read)
}
Err(err) => {
// fail permanently
self.current_part = None;
self.raw_parts.clear();
Err(err)
}
}
}
}
28 changes: 0 additions & 28 deletions src/rust/dev/common.rs

This file was deleted.

168 changes: 0 additions & 168 deletions src/rust/dev/mod.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/rust/dev/videos/mod.rs

This file was deleted.

Loading

0 comments on commit 71c827b

Please sign in to comment.