From 664f28009be26a02639325951445b84f523fc3a0 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sun, 13 Jun 2021 00:30:06 -0300 Subject: Fix base64 handling --- Cargo.toml | 2 +- src/lib.rs | 19 +++++++++++++++++-- 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 "] 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, base64::DecodeError> { + // cba to avoid allocation + base64::decode(s.split('\n').collect::()) +} + /// 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![], -- cgit v1.2.3