summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2021-06-13 00:30:06 -0300
committerSoniEx2 <endermoneymod@gmail.com>2021-06-13 00:30:06 -0300
commit664f28009be26a02639325951445b84f523fc3a0 (patch)
treea47d93d2dcd37c9741b8263523d5969c1be92be8
parent7c1613d49b6f4e84266262f04c0dd012c3415368 (diff)
Fix base64 handling
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs19
2 files changed, 18 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 12c7351..996f8b2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "testserver"
-version = "0.1.0"
+version = "0.1.1"
description = "Static HTTP webserver that stores resources as strings"
readme = "README.md"
authors = ["SoniEx2 <endermoneymod@gmail.com>"]
diff --git a/src/lib.rs b/src/lib.rs
index a6c16d9..af2f994 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -53,6 +53,13 @@ impl Server {
}
}
+/// Decodes base64 as output by the `base64` utility. Properly handles
+/// 76-column base64.
+fn decode_base64(s: &'static str) -> Result<Vec<u8>, base64::DecodeError> {
+ // cba to avoid allocation
+ base64::decode(s.split('\n').collect::<String>())
+}
+
/// Spawns a webserver to serve the given archive.
///
/// The archive is an .a (ar) file, in a format that can be parsed by the ar
@@ -153,8 +160,16 @@ pub fn serve(archive: &'static str) -> Server {
current = Some(Archive::new(inner));
} else if kind == ".b" {
// base64
- let inner = base64::decode(inner.into_inner());
- let inner = Cursor::new(inner.unwrap());
+ let inner = decode_base64(inner.into_inner());
+ let inner = match inner {
+ Ok(inner) => inner,
+ Err(e) => {
+ eprintln!("{}", e);
+ break
+ }
+ };
+ let size = inner.len();
+ let inner = Cursor::new(inner);
response = Response::new(
200.into(),
vec![],