Skip to content

Commit

Permalink
Prevent cloning paste on show where not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Feb 21, 2019
1 parent c2219e1 commit 5b78680
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 60 deletions.
83 changes: 48 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ authors = ["Jordan Doyle <[email protected]>"]
edition = "2018"

[dependencies]
owning_ref = "0.4"
linked-hash-map = "0.5"
rocket = { version = "0.4", default-features = false }
askama = "0.7"
askama = "0.8"
lazy_static = "1.2"
rand = { version = "0.6", features = ["nightly"] }
gpw = "0.1"
Expand Down
25 changes: 15 additions & 10 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
extern crate gpw;
extern crate linked_hash_map;
extern crate owning_ref;
extern crate rand;

use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

use linked_hash_map::LinkedHashMap;

use owning_ref::RwLockReadGuardRef;

use std::cell::RefCell;
use std::env;
use std::sync::RwLock;
use std::sync::{PoisonError, RwLock};

lazy_static! {
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
Expand All @@ -25,12 +28,12 @@ lazy_static! {
///
/// During the purge, `ENTRIES` is locked and the current thread will block.
fn purge_old() {
let entries_len = ENTRIES.read().unwrap_or_else(|p| p.into_inner()).len();
let entries_len = ENTRIES.read().unwrap_or_else(PoisonError::into_inner).len();

if entries_len > *BUFFER_SIZE {
let to_remove = entries_len - *BUFFER_SIZE;

let mut entries = ENTRIES.write().unwrap_or_else(|p| p.into_inner());
let mut entries = ENTRIES.write().unwrap_or_else(PoisonError::into_inner);

for _ in 0..to_remove {
entries.pop_front();
Expand All @@ -54,17 +57,19 @@ pub fn store_paste(id: String, content: String) {
purge_old();
ENTRIES
.write()
.unwrap_or_else(|p| p.into_inner())
.unwrap_or_else(PoisonError::into_inner)
.insert(id, content);
}

/// Get a paste by id.
///
/// Returns `None` if the paste doesn't exist.
pub fn get_paste(id: &str) -> Option<String> {
ENTRIES
.read()
.unwrap_or_else(|p| p.into_inner())
.get(id)
.cloned()
pub fn get_paste(id: &str) -> Option<RwLockReadGuardRef<LinkedHashMap<String, String>, String>> {
let or = RwLockReadGuardRef::new(ENTRIES.read().unwrap_or_else(PoisonError::into_inner));

if or.contains_key(id) {
Some(or.map(|x| x.get(id).unwrap()))
} else {
None
}
}
31 changes: 17 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ use highlight::highlight;
use io::{generate_id, get_paste, store_paste};
use params::{HostHeader, IsPlaintextRequest};

use askama::{MarkupDisplay, Template};
use askama::{Html as AskamaHtml, MarkupDisplay, Template};

use rocket::http::{ContentType, Status};
use rocket::request::Form;
use rocket::response::content::{Content, Html};
use rocket::response::Redirect;
use rocket::Data;

use std::borrow::Cow;
use std::io::Read;

///
Expand All @@ -39,7 +40,7 @@ struct Index;
fn index() -> Result<Html<String>, Status> {
Index
.render()
.map(|h| Html(h))
.map(Html)
.map_err(|_| Status::InternalServerError)
}

Expand Down Expand Up @@ -82,8 +83,8 @@ fn submit_raw(input: Data, host: HostHeader) -> std::io::Result<String> {
#[derive(Template)]
#[template(path = "paste.html")]
struct ShowPaste {
content: MarkupDisplay<String>,
struct ShowPaste<'a> {
content: MarkupDisplay<AskamaHtml, Cow<'a, String>>,
}

#[get("/<key>")]
Expand All @@ -94,22 +95,24 @@ fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Content<Stri

// get() returns a read-only lock, we're not going to be writing to this key
// again so we can hold this for as long as we want
let entry = get_paste(key).ok_or_else(|| Status::NotFound)?;
let entry = &*get_paste(key).ok_or_else(|| Status::NotFound)?;

if *plaintext {
Ok(Content(ContentType::Plain, entry))
Ok(Content(ContentType::Plain, entry.to_string()))
} else {
let content = match ext {
None => MarkupDisplay::Unsafe(entry),
Some(extension) => highlight(&entry, extension)
.map(|h| MarkupDisplay::Safe(h))
.ok_or_else(|| Status::NotFound)?,
Some(extension) => match highlight(&entry, extension) {
Some(html) => MarkupDisplay::new_safe(Cow::Owned(html), AskamaHtml),
None => return Err(Status::NotFound),
},
None => MarkupDisplay::new_unsafe(Cow::Borrowed(entry), AskamaHtml),
};

ShowPaste { content }
.render()
.map(|html| Content(ContentType::HTML, html))
.map_err(|_| Status::InternalServerError)
let template = ShowPaste { content };
match template.render() {
Ok(html) => Ok(Content(ContentType::HTML, html)),
Err(_) => Err(Status::InternalServerError),
}
}
}

Expand Down

0 comments on commit 5b78680

Please sign in to comment.