Fixing small files fetching (#2742)

* Fixing small files fetching

* Fixing a case where tranfer encoding is not chunked
This commit is contained in:
Tomasz Drwięga 2016-10-20 12:57:48 +02:00 committed by Gav Wood
parent 7db42df1db
commit cd4e21e1b4
3 changed files with 21 additions and 7 deletions

View File

@ -31,6 +31,7 @@ use url::Url;
pub enum FetchError { pub enum FetchError {
InvalidAddress, InvalidAddress,
ReadingCaCertificates, ReadingCaCertificates,
UnexpectedStatus(String),
CaCertificates(io::Error), CaCertificates(io::Error),
Io(io::Error), Io(io::Error),
Notify(mio::NotifyError<ClientMessage>), Notify(mio::NotifyError<ClientMessage>),
@ -162,10 +163,13 @@ impl mio::Handler for ClientLoop {
if let Ok(mut tlsclient) = TlsClient::new(mio::Token(token), &url, writer, abort, callback, self.size_limit.clone()) { if let Ok(mut tlsclient) = TlsClient::new(mio::Token(token), &url, writer, abort, callback, self.size_limit.clone()) {
let httpreq = format!( let httpreq = format!(
"GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\nAccept-Encoding: identity\r\n\r\n", "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\nUser-Agent: {}/{}\r\nAccept-Encoding: identity\r\n\r\n",
url.path(), url.path(),
url.hostname() url.hostname(),
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
); );
debug!("Requesting content: {}", httpreq);
let _ = tlsclient.write(httpreq.as_bytes()); let _ = tlsclient.write(httpreq.as_bytes());
tlsclient.register(event_loop); tlsclient.register(event_loop);

View File

@ -52,11 +52,14 @@ impl HttpProcessor {
} }
} }
#[cfg(test)]
pub fn status(&self) -> Option<&String> { pub fn status(&self) -> Option<&String> {
self.status.as_ref() self.status.as_ref()
} }
pub fn status_is_ok(&self) -> bool {
self.status == Some("HTTP/1.1 200 OK".into())
}
#[cfg(test)] #[cfg(test)]
pub fn headers(&self) -> &[String] { pub fn headers(&self) -> &[String] {
&self.headers &self.headers
@ -153,7 +156,7 @@ impl HttpProcessor {
} }
try!(self.body_writer.write_all(self.buffer.get_ref())); try!(self.body_writer.write_all(self.buffer.get_ref()));
self.buffer_consume(len); self.buffer_consume(len);
return Ok(()); return self.body_writer.flush();
}, },
State::WaitingForChunk => { State::WaitingForChunk => {
match self.find_break_index() { match self.find_break_index() {
@ -189,7 +192,11 @@ impl HttpProcessor {
continue; continue;
} }
} }
try!(self.body_writer.write_all(&self.buffer.get_ref()[0..left])); {
let chunk = &self.buffer.get_ref()[0..left];
trace!("Writing chunk: {:?}", String::from_utf8_lossy(chunk));
try!(self.body_writer.write_all(chunk));
}
self.buffer_consume(left + BREAK_LEN); self.buffer_consume(left + BREAK_LEN);
self.set_state(State::WaitingForChunk); self.set_state(State::WaitingForChunk);
@ -200,7 +207,7 @@ impl HttpProcessor {
State::Finished => { State::Finished => {
let len = self.buffer.get_ref().len(); let len = self.buffer.get_ref().len();
self.buffer_consume(len); self.buffer_consume(len);
return Ok(()); return self.body_writer.flush();
}, },
} }
} }

View File

@ -137,8 +137,11 @@ impl TlsClient {
trace!("Connection closed"); trace!("Connection closed");
let callback = &mut self.callback; let callback = &mut self.callback;
callback(match self.error.take() { callback(match self.error.take() {
None if self.writer.status_is_ok() => Ok(()),
Some(err) => Err(err.into()), Some(err) => Err(err.into()),
None => Ok(()), None => {
Err(FetchError::UnexpectedStatus(format!("{:?}", self.writer.status())))
},
}); });
return true; return true;