summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorWiktor Kwapisiewicz <wiktor@metacode.biz>2020-07-30 09:46:00 +0200
committerWiktor Kwapisiewicz <wiktor@metacode.biz>2020-08-18 10:13:20 +0200
commite80c467375501ed1f3e465c2e1e4860122dad7b4 (patch)
tree8be9509efaf79a5bf35727099ed0fe33ec606d47 /openpgp
parentf76086687a3d16a97f15171fd3d966ddffb1ac95 (diff)
openpgp: Fix Reader::headers() for partially indented lines.
When reading armored data that is indented in all lines except the first one `Reader::headers()` returned header names that included leading whitespace. This change adjusts the code to strip any leading whitespace from header keys.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/src/armor.rs32
1 files changed, 27 insertions, 5 deletions
diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs
index aa9b039e..cb356745 100644
--- a/openpgp/src/armor.rs
+++ b/openpgp/src/armor.rs
@@ -608,8 +608,7 @@ impl<'a> Reader<'a> {
/// # extern crate sequoia_openpgp as openpgp;
/// # use openpgp::armor::{Reader, ReaderMode, Kind};
/// # use std::io::{self, Result};
- /// # fn main() { f().unwrap(); }
- /// # fn f() -> Result<()> {
+ /// # fn main() -> Result<()> {
/// let data =
/// "-----BEGIN PGP ARMORED FILE-----
/// First: value
@@ -625,8 +624,8 @@ impl<'a> Reader<'a> {
/// let mut content = String::new();
/// reader.read_to_string(&mut content)?;
/// assert_eq!(reader.headers().unwrap(),
- /// &[(" First".into(), "value".into()),
- /// (" Header".into(), "value".into())]);
+ /// &[("First".into(), "value".into()),
+ /// ("Header".into(), "value".into())]);
/// # Ok(())
/// # }
/// ```
@@ -869,7 +868,7 @@ impl<'a> Reader<'a> {
break;
}
} else {
- let key = key_value[0];
+ let key = key_value[0].trim_start();
let value = key_value[1];
self.headers.push((key.into(), value.into()));
@@ -1752,4 +1751,27 @@ mod test {
// `data` is malformed, expect an error.
reader.read_to_end(&mut buf).unwrap_err();
}
+
+ #[test]
+ fn test_headers() -> std::io::Result<()> {
+ use crate::armor::{Reader, ReaderMode, Kind};
+ let data =
+ "-----BEGIN PGP ARMORED FILE-----
+ First: value
+ Header: value
+
+ SGVsbG8gd29ybGQh
+ =s4Gu
+ -----END PGP ARMORED FILE-----";
+
+ let mut cursor = std::io::Cursor::new(&data);
+ let mut reader = Reader::new(&mut cursor, ReaderMode::Tolerant(Some(Kind::File)));
+
+ let mut content = String::new();
+ reader.read_to_string(&mut content)?;
+ assert_eq!(reader.headers().unwrap(),
+ &[("First".into(), "value".into()),
+ ("Header".into(), "value".into())]);
+ Ok(())
+ }
}