From b810a5abbab2ca37e1269df58be99b10a5da4650 Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 11 May 2021 13:52:07 -0700 Subject: [PATCH] * Fixing message body unwrap to safer unwrap_or unwrap had a chance to panic where as the unwrap_or now defaults to an empty Bytes object which lets the code after it fail with an HTTP 400. This is preferrable to a 500 error as there really is no error, just bad/unpollable input from the end user. --- json-api/src/messages.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/json-api/src/messages.rs b/json-api/src/messages.rs index 2eef93c..2fd53cd 100644 --- a/json-api/src/messages.rs +++ b/json-api/src/messages.rs @@ -1,6 +1,7 @@ use mysql_async::Pool; use hyper::{Response, Body, HeaderMap, StatusCode}; use hyper::body::to_bytes; +use hyper::body::Bytes; use serde_json::json; use std::collections::HashMap; @@ -94,7 +95,7 @@ pub async fn send_message(pool: &Pool, response: &mut Response, body: Body let channel_id = qs_param!(params, "channel_id", u64); // Black magic - let body_bytes: &[u8] = &to_bytes(body).await.unwrap(); // yolo + let body_bytes: &[u8] = &to_bytes(body).await.unwrap_or(Bytes::new()); let content = String::from_utf8_lossy(body_bytes); // 400 on empty bodies or missing channel id's @@ -164,3 +165,4 @@ pub async fn recent_messages(pool: &Pool, response: &mut Response, params: } else { } } +