Page MenuHomeDevCentral

No OneTemporary

diff --git a/Cargo.toml b/Cargo.toml
index 03cf152..2c1578e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,37 +1,37 @@
[package]
name = "limiting-factor"
-version = "0.7.2"
+version = "0.8.0"
authors = [
"Sébastien Santoro <dereckson@espace-win.org>",
]
description = "Library to create a REST API with Diesel and Rocket"
readme = "README.md"
keywords = [
"Diesel",
"API",
"Rocket",
"REST",
]
categories = [
"web-programming",
]
license = "BSD-2-Clause"
repository = "https://devcentral.nasqueron.org/source/limiting-factor/"
[dependencies]
diesel = { version = "^1.4.8", features = ["postgres", "r2d2", "chrono"], optional = true }
dotenv = "^0.15.0"
log = "^0.4.14"
r2d2 = { version = "^0.8.10", optional = true }
rocket = "^0.4.11"
rocket_contrib = { version = "^0.4.11", features = [ "json" ] }
-serde = { version = "^1.0.159", optional = true }
+serde = { version = "^1.0.159", features = [ "derive" ], optional = true }
[features]
default = ["minimal"]
minimal = ["serialization"]
full = ["pgsql", "serialization"]
pgsql = ["diesel", "r2d2"]
serialization = ["serde"]
diff --git a/src/api/guards.rs b/src/api/guards.rs
new file mode 100644
index 0000000..83dff16
--- /dev/null
+++ b/src/api/guards.rs
@@ -0,0 +1,92 @@
+//! # API guards
+//!
+//! This module provides reusable guards to use with Rocket.
+
+use rocket::data::{FromDataSimple, Outcome};
+use rocket::{Data, Request};
+use rocket::http::Status;
+use rocket::Outcome::{Failure, Success};
+use serde::{Deserialize, Serialize};
+
+use std::io::Read;
+
+/// The maximum number of characters to read, to avoid DoS
+const REQUEST_BODY_LIMIT: u64 = 1_000_000;
+
+/// A String representation of the request body. Useful when you need to pass it through as is.
+#[derive(Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord)]
+pub struct RequestBody {
+ /// The UTF-8 content of the request body
+ pub content: String,
+}
+
+impl RequestBody {
+ pub fn new () -> Self {
+ Self {
+ content: String::new(),
+ }
+ }
+
+ /// Convert the request body into a string
+ pub fn into_string (self) -> String {
+ self.content
+ }
+
+ /// Convert the request body into a string, or None if it's empty
+ pub fn into_optional_string (self) -> Option<String> {
+ if self.content.is_empty() {
+ None
+ } else {
+ Some(self.content)
+ }
+ }
+}
+
+impl FromDataSimple for RequestBody {
+ type Error = String;
+
+ fn from_data(_request: &Request, data: Data) -> Outcome<Self, Self::Error> {
+ let mut content = String::new();
+
+ if let Err(e) = data.open().take(REQUEST_BODY_LIMIT).read_to_string(&mut content) {
+ return Failure((Status::InternalServerError, format!("{:?}", e)));
+ }
+
+ Success(Self { content })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_request_body_new () {
+ let body = RequestBody::new();
+ assert_eq!(0, body.content.len(), "Content should be empty");
+ }
+
+ #[test]
+ fn test_request_body_into_string () {
+ let body = RequestBody { content: "quux".to_string() };
+ assert_eq!(String::from("quux"), body.into_string());
+ }
+
+ #[test]
+ fn test_request_body_into_string_when_empty () {
+ let body = RequestBody::new();
+ assert_eq!(String::new(), body.into_string(), "Content should be empty");
+ }
+
+ #[test]
+ fn test_request_body_into_optional_string () {
+ let body = RequestBody { content: "quux".to_string() };
+ assert_eq!(Some(String::from("quux")), body.into_optional_string());
+ }
+
+ #[test]
+ fn test_request_body_into_optional_string_when_empty () {
+ let body = RequestBody::new();
+ assert_eq!(None, body.into_optional_string());
+ }
+}
diff --git a/src/api/mod.rs b/src/api/mod.rs
index b350bcf..28039c6 100644
--- a/src/api/mod.rs
+++ b/src/api/mod.rs
@@ -1,9 +1,10 @@
//! # Utilities for API.
//!
//! This module provides useful code to create easily APIs.
/* -------------------------------------------------------------
Public submodules offered by this module
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+pub mod guards;
pub mod replies;

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 12:09 (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260146
Default Alt Text
(4 KB)

Event Timeline