Skip to content

Do not panic when the response is not utf-8 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,26 @@ impl<T: Read+Write> Client<T> {
let mut found_tag_line = false;
let start_str = format!("{}{} ", TAG_PREFIX, self.tag);
let mut lines: Vec<String> = Vec::new();
let mut error = None;

while !found_tag_line {
let raw_data = try!(self.readline());
let line = String::from_utf8(raw_data).unwrap();
lines.push(line.clone());
if (&*line).starts_with(&*start_str) {
found_tag_line = true;
// if there is an encoding error, still continue until the tag
match String::from_utf8(raw_data) {
Err(e) => error = Some(e),
Ok(line) => {
lines.push(line.clone());
if (&*line).starts_with(&*start_str) {
found_tag_line = true;
}
}
}
}

Ok(lines)
match error {
Some(e) => Err(e.into()),
None => Ok(lines)
}
}

fn read_greeting(&mut self) -> Result<()> {
Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::result;
use std::fmt;
use std::error::Error as StdError;
use std::net::TcpStream;
use std::string::FromUtf8Error;

use openssl::ssl::HandshakeError as SslError;

Expand Down Expand Up @@ -35,6 +36,12 @@ impl From<SslError<TcpStream>> for Error {
}
}

impl From<FromUtf8Error> for Error {
fn from(err: FromUtf8Error) -> Error {
Error::Parse(ParseError::FromUtf8(err))
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down Expand Up @@ -67,6 +74,8 @@ impl StdError for Error {

#[derive(Debug)]
pub enum ParseError {
// Error in the decoding of data.
FromUtf8(FromUtf8Error),
// Indicates an error parsing the status response. Such as OK, NO, and BAD.
StatusResponse(Vec<String>),
// Error parsing the cabability response.
Expand All @@ -86,6 +95,7 @@ impl fmt::Display for ParseError {
impl StdError for ParseError {
fn description(&self) -> &str {
match *self {
ParseError::FromUtf8(_) => "Unable to decode the response as UTF-8.",
ParseError::StatusResponse(_) => "Unable to parse status response",
ParseError::Capability(_) => "Unable to parse capability response",
ParseError::Authentication(_) => "Unable to parse authentication response"
Expand Down