From 96e9b31b2e3147b53dda80a0bc50eb2d9853bda9 Mon Sep 17 00:00:00 2001 From: Fabio Anderegg Date: Thu, 7 Jan 2021 14:25:03 +0000 Subject: [PATCH] use from_utf8_lossy to avoid server error on invalid text data --- src/main.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 78fc66d..ce2f970 100644 --- a/src/main.rs +++ b/src/main.rs @@ -180,6 +180,16 @@ speculate! { assert_eq!(response.status(), Status::Ok); assert_eq!(response.body_bytes(), Some(contents)); } + + it "can cope with invalid unicode data" { + let invalid_data = unsafe { + String::from_utf8_unchecked(b"Hello \xF0\x90\x80World".to_vec()) + }; + let id = insert_data(&client, &invalid_data, "/"); + + let response = get_data(&client, id); + assert_eq!(response.status(), Status::Ok); + } } const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -414,7 +424,7 @@ fn get<'r>( let mut map = json!({ "is_created": "true", - "pastebin_code": std::str::from_utf8(entry.data().unwrap()).unwrap(), + "pastebin_code": String::from_utf8_lossy(entry.data().unwrap()), "pastebin_id": id, "pastebin_language": selected_lang, "version": VERSION,