2020-02-19 08:17:13 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
/// This module is meant to eventually implement HTTP cache
|
|
|
|
/// as defined in RFC 7234 (https://tools.ietf.org/html/rfc7234).
|
|
|
|
/// Currently it's a very simplified version to fulfill Deno needs
|
|
|
|
/// at hand.
|
2020-11-16 14:48:50 -05:00
|
|
|
use crate::fs_util;
|
2020-02-19 08:17:13 -05:00
|
|
|
use crate::http_util::HeadersMap;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-02-28 13:15:31 -05:00
|
|
|
use serde::Serialize;
|
2020-02-19 08:17:13 -05:00
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
2020-05-07 08:32:57 -04:00
|
|
|
use std::io;
|
2020-02-19 08:17:13 -05:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-10-26 12:58:58 -04:00
|
|
|
pub const CACHE_PERM: u32 = 0o644;
|
|
|
|
|
2020-02-19 08:17:13 -05:00
|
|
|
/// Turn base of url (scheme, hostname, port) into a valid filename.
|
|
|
|
/// This method replaces port part with a special string token (because
|
|
|
|
/// ":" cannot be used in filename on some platforms).
|
2020-02-28 13:15:31 -05:00
|
|
|
/// Ex: $DENO_DIR/deps/https/deno.land/
|
|
|
|
fn base_url_to_filename(url: &Url) -> PathBuf {
|
2020-02-19 08:17:13 -05:00
|
|
|
let mut out = PathBuf::new();
|
|
|
|
|
|
|
|
let scheme = url.scheme();
|
|
|
|
out.push(scheme);
|
|
|
|
|
|
|
|
match scheme {
|
|
|
|
"http" | "https" => {
|
|
|
|
let host = url.host_str().unwrap();
|
|
|
|
let host_port = match url.port() {
|
|
|
|
Some(port) => format!("{}_PORT{}", host, port),
|
|
|
|
None => host.to_string(),
|
|
|
|
};
|
|
|
|
out.push(host_port);
|
|
|
|
}
|
2021-01-05 21:22:38 -05:00
|
|
|
"data" => (),
|
2020-02-19 08:17:13 -05:00
|
|
|
scheme => {
|
|
|
|
unimplemented!(
|
|
|
|
"Don't know how to create cache name for scheme: {}",
|
|
|
|
scheme
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn provided `url` into a hashed filename.
|
|
|
|
/// URLs can contain a lot of characters that cannot be used
|
|
|
|
/// in filenames (like "?", "#", ":"), so in order to cache
|
|
|
|
/// them properly they are deterministically hashed into ASCII
|
|
|
|
/// strings.
|
|
|
|
///
|
|
|
|
/// NOTE: this method is `pub` because it's used in integration_tests
|
|
|
|
pub fn url_to_filename(url: &Url) -> PathBuf {
|
|
|
|
let mut cache_filename = base_url_to_filename(url);
|
|
|
|
|
|
|
|
let mut rest_str = url.path().to_string();
|
|
|
|
if let Some(query) = url.query() {
|
2020-09-14 18:05:17 -04:00
|
|
|
rest_str.push('?');
|
2020-02-19 08:17:13 -05:00
|
|
|
rest_str.push_str(query);
|
|
|
|
}
|
|
|
|
// NOTE: fragment is omitted on purpose - it's not taken into
|
|
|
|
// account when caching - it denotes parts of webpage, which
|
|
|
|
// in case of static resources doesn't make much sense
|
2020-06-29 08:17:37 -04:00
|
|
|
let hashed_filename = crate::checksum::gen(&[rest_str.as_bytes()]);
|
2020-02-19 08:17:13 -05:00
|
|
|
cache_filename.push(hashed_filename);
|
|
|
|
cache_filename
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-02-19 08:17:13 -05:00
|
|
|
pub struct HttpCache {
|
|
|
|
pub location: PathBuf,
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:15:31 -05:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Metadata {
|
|
|
|
pub headers: HeadersMap,
|
|
|
|
pub url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Metadata {
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn write(&self, cache_filename: &Path) -> Result<(), AnyError> {
|
2020-02-28 13:15:31 -05:00
|
|
|
let metadata_filename = Self::filename(cache_filename);
|
|
|
|
let json = serde_json::to_string_pretty(self)?;
|
2020-12-18 13:30:49 -05:00
|
|
|
fs_util::atomic_write_file(&metadata_filename, json, CACHE_PERM)?;
|
2020-02-28 13:15:31 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-12 07:25:25 -04:00
|
|
|
#[cfg(test)]
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn read(cache_filename: &Path) -> Result<Metadata, AnyError> {
|
2020-02-28 13:15:31 -05:00
|
|
|
let metadata_filename = Metadata::filename(&cache_filename);
|
|
|
|
let metadata = fs::read_to_string(metadata_filename)?;
|
|
|
|
let metadata: Metadata = serde_json::from_str(&metadata)?;
|
|
|
|
Ok(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ex: $DENO_DIR/deps/https/deno.land/c885b7dcf1d6936e33a9cc3a2d74ec79bab5d733d3701c85a029b7f7ec9fbed4.metadata.json
|
|
|
|
pub fn filename(cache_filename: &Path) -> PathBuf {
|
|
|
|
cache_filename.with_extension("metadata.json")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 08:17:13 -05:00
|
|
|
impl HttpCache {
|
2020-05-07 08:32:57 -04:00
|
|
|
/// Returns a new instance.
|
2020-05-15 10:32:52 -04:00
|
|
|
///
|
|
|
|
/// `location` must be an absolute path.
|
2020-05-07 08:32:57 -04:00
|
|
|
pub fn new(location: &Path) -> Self {
|
2020-05-15 10:32:52 -04:00
|
|
|
assert!(location.is_absolute());
|
2020-05-07 08:32:57 -04:00
|
|
|
Self {
|
2020-02-19 08:17:13 -05:00
|
|
|
location: location.to_owned(),
|
2020-05-07 08:32:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensures the location of the cache.
|
2020-05-24 13:20:40 -04:00
|
|
|
fn ensure_dir_exists(&self, path: &Path) -> io::Result<()> {
|
|
|
|
if path.is_dir() {
|
2020-05-07 08:32:57 -04:00
|
|
|
return Ok(());
|
|
|
|
}
|
2020-05-24 13:20:40 -04:00
|
|
|
fs::create_dir_all(&path).map_err(|e| {
|
2020-05-07 08:32:57 -04:00
|
|
|
io::Error::new(
|
|
|
|
e.kind(),
|
|
|
|
format!(
|
|
|
|
"Could not create remote modules cache location: {:?}\nCheck the permission of the directory.",
|
2020-05-24 13:20:40 -04:00
|
|
|
path
|
2020-05-07 08:32:57 -04:00
|
|
|
),
|
|
|
|
)
|
2020-02-19 08:17:13 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_cache_filename(&self, url: &Url) -> PathBuf {
|
|
|
|
self.location.join(url_to_filename(url))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): this method should check headers file
|
|
|
|
// and validate against ETAG/Last-modified-as headers.
|
|
|
|
// ETAG check is currently done in `cli/file_fetcher.rs`.
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn get(&self, url: &Url) -> Result<(File, HeadersMap), AnyError> {
|
2020-02-19 08:17:13 -05:00
|
|
|
let cache_filename = self.location.join(url_to_filename(url));
|
2020-02-28 13:15:31 -05:00
|
|
|
let metadata_filename = Metadata::filename(&cache_filename);
|
2020-02-19 08:17:13 -05:00
|
|
|
let file = File::open(cache_filename)?;
|
2020-02-28 13:15:31 -05:00
|
|
|
let metadata = fs::read_to_string(metadata_filename)?;
|
|
|
|
let metadata: Metadata = serde_json::from_str(&metadata)?;
|
|
|
|
Ok((file, metadata.headers))
|
|
|
|
}
|
|
|
|
|
2020-02-19 08:17:13 -05:00
|
|
|
pub fn set(
|
|
|
|
&self,
|
|
|
|
url: &Url,
|
|
|
|
headers_map: HeadersMap,
|
|
|
|
content: &[u8],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-02-19 08:17:13 -05:00
|
|
|
let cache_filename = self.location.join(url_to_filename(url));
|
|
|
|
// Create parent directory
|
|
|
|
let parent_filename = cache_filename
|
|
|
|
.parent()
|
|
|
|
.expect("Cache filename should have a parent dir");
|
2020-05-24 13:20:40 -04:00
|
|
|
self.ensure_dir_exists(parent_filename)?;
|
2020-02-19 08:17:13 -05:00
|
|
|
// Cache content
|
2020-12-18 13:30:49 -05:00
|
|
|
fs_util::atomic_write_file(&cache_filename, content, CACHE_PERM)?;
|
2020-02-28 13:15:31 -05:00
|
|
|
|
|
|
|
let metadata = Metadata {
|
|
|
|
url: url.to_string(),
|
|
|
|
headers: headers_map,
|
|
|
|
};
|
|
|
|
metadata.write(&cache_filename)
|
2020-02-19 08:17:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::Read;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_cache() {
|
|
|
|
let dir = TempDir::new().unwrap();
|
|
|
|
let mut cache_path = dir.path().to_owned();
|
|
|
|
cache_path.push("foobar");
|
2020-05-24 13:20:40 -04:00
|
|
|
// HttpCache should be created lazily on first use:
|
|
|
|
// when zipping up a local project with no external dependencies
|
|
|
|
// "$DENO_DIR/deps" is empty. When unzipping such project
|
|
|
|
// "$DENO_DIR/deps" might not get restored and in situation
|
|
|
|
// when directory is owned by root we might not be able
|
|
|
|
// to create that directory. However if it's not needed it
|
|
|
|
// doesn't make sense to return error in such specific scenarios.
|
|
|
|
// For more details check issue:
|
|
|
|
// https://github.com/denoland/deno/issues/5688
|
2020-05-07 08:32:57 -04:00
|
|
|
let cache = HttpCache::new(&cache_path);
|
2020-05-24 13:20:40 -04:00
|
|
|
assert!(!cache.location.exists());
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&Url::parse("http://example.com/foo/bar.js").unwrap(),
|
|
|
|
HeadersMap::new(),
|
|
|
|
b"hello world",
|
|
|
|
)
|
|
|
|
.expect("Failed to add to cache");
|
|
|
|
assert!(cache.ensure_dir_exists(&cache.location).is_ok());
|
2020-02-19 08:17:13 -05:00
|
|
|
assert!(cache_path.is_dir());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_set() {
|
|
|
|
let dir = TempDir::new().unwrap();
|
2020-05-07 08:32:57 -04:00
|
|
|
let cache = HttpCache::new(dir.path());
|
2020-02-19 08:17:13 -05:00
|
|
|
let url = Url::parse("https://deno.land/x/welcome.ts").unwrap();
|
|
|
|
let mut headers = HashMap::new();
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.insert(
|
|
|
|
"content-type".to_string(),
|
|
|
|
"application/javascript".to_string(),
|
|
|
|
);
|
|
|
|
headers.insert("etag".to_string(), "as5625rqdsfb".to_string());
|
2020-02-19 08:17:13 -05:00
|
|
|
let content = b"Hello world";
|
|
|
|
let r = cache.set(&url, headers, content);
|
|
|
|
eprintln!("result {:?}", r);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let r = cache.get(&url);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
let (mut file, headers) = r.unwrap();
|
|
|
|
let mut content = String::new();
|
|
|
|
file.read_to_string(&mut content).unwrap();
|
|
|
|
assert_eq!(content, "Hello world");
|
|
|
|
assert_eq!(
|
2020-09-16 07:34:16 -04:00
|
|
|
headers.get("content-type").unwrap(),
|
2020-02-19 08:17:13 -05:00
|
|
|
"application/javascript"
|
|
|
|
);
|
2020-09-16 07:34:16 -04:00
|
|
|
assert_eq!(headers.get("etag").unwrap(), "as5625rqdsfb");
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(headers.get("foobar"), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_url_to_filename() {
|
|
|
|
let test_cases = [
|
|
|
|
("https://deno.land/x/foo.ts", "https/deno.land/2c0a064891b9e3fbe386f5d4a833bce5076543f5404613656042107213a7bbc8"),
|
|
|
|
(
|
|
|
|
"https://deno.land:8080/x/foo.ts",
|
|
|
|
"https/deno.land_PORT8080/2c0a064891b9e3fbe386f5d4a833bce5076543f5404613656042107213a7bbc8",
|
|
|
|
),
|
|
|
|
("https://deno.land/", "https/deno.land/8a5edab282632443219e051e4ade2d1d5bbc671c781051bf1437897cbdfea0f1"),
|
|
|
|
(
|
|
|
|
"https://deno.land/?asdf=qwer",
|
|
|
|
"https/deno.land/e4edd1f433165141015db6a823094e6bd8f24dd16fe33f2abd99d34a0a21a3c0",
|
|
|
|
),
|
|
|
|
// should be the same as case above, fragment (#qwer) is ignored
|
|
|
|
// when hashing
|
|
|
|
(
|
|
|
|
"https://deno.land/?asdf=qwer#qwer",
|
|
|
|
"https/deno.land/e4edd1f433165141015db6a823094e6bd8f24dd16fe33f2abd99d34a0a21a3c0",
|
|
|
|
),
|
2021-01-05 21:22:38 -05:00
|
|
|
(
|
|
|
|
"data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=",
|
|
|
|
"data/c21c7fc382b2b0553dc0864aa81a3acacfb7b3d1285ab5ae76da6abec213fb37",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"data:text/plain,Hello%2C%20Deno!",
|
|
|
|
"data/967374e3561d6741234131e342bf5c6848b70b13758adfe23ee1a813a8131818",
|
|
|
|
)
|
2020-02-19 08:17:13 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
for (url, expected) in test_cases.iter() {
|
|
|
|
let u = Url::parse(url).unwrap();
|
|
|
|
let p = url_to_filename(&u);
|
|
|
|
assert_eq!(p, PathBuf::from(expected));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|