summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/file_fetcher.rs146
-rw-r--r--cli/http_cache.rs25
-rw-r--r--cli/http_util.rs84
3 files changed, 48 insertions, 207 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs
index 7dc39be71..2101866dc 100644
--- a/cli/file_fetcher.rs
+++ b/cli/file_fetcher.rs
@@ -404,7 +404,7 @@ impl SourceFileFetcher {
};
let (mut source_file, headers) = result;
- if let Some(redirect_to) = headers.get("location").and_then(|e| e.first()) {
+ if let Some(redirect_to) = headers.get("location") {
let redirect_url = match Url::parse(redirect_to) {
Ok(redirect_url) => redirect_url,
Err(url::ParseError::RelativeUrlWithoutBase) => {
@@ -427,15 +427,9 @@ impl SourceFileFetcher {
let fake_filepath = PathBuf::from(module_url.path());
let (media_type, charset) = map_content_type(
&fake_filepath,
- headers
- .get("content-type")
- .and_then(|e| e.first())
- .map(|e| e.as_str()),
+ headers.get("content-type").map(|e| e.as_str()),
);
- let types_header = headers
- .get("x-typescript-types")
- .and_then(|e| e.first())
- .map(|e| e.to_string());
+ let types_header = headers.get("x-typescript-types").map(|e| e.to_string());
Ok(Some(SourceFile {
url: module_url.clone(),
filename: cache_filename,
@@ -499,10 +493,7 @@ impl SourceFileFetcher {
let dir = self.clone();
let module_url = module_url.clone();
let module_etag = match self.http_cache.get(&module_url) {
- Ok((_, headers)) => headers
- .get("etag")
- .and_then(|e| e.first())
- .map(|e| e.to_string()),
+ Ok((_, headers)) => headers.get("etag").map(String::from),
Err(_) => None,
};
let permissions = permissions.clone();
@@ -541,16 +532,11 @@ impl SourceFileFetcher {
let fake_filepath = PathBuf::from(module_url.path());
let (media_type, charset) = map_content_type(
&fake_filepath,
- headers
- .get("content-type")
- .and_then(|e| e.first())
- .map(|e| e.as_str()),
+ headers.get("content-type").map(String::as_str),
);
- let types_header = headers
- .get("x-typescript-types")
- .and_then(|e| e.first())
- .map(|e| e.to_string());
+ let types_header =
+ headers.get("x-typescript-types").map(String::to_string);
let source_file = SourceFile {
url: module_url.clone(),
@@ -822,9 +808,7 @@ mod tests {
metadata.headers = HashMap::new();
metadata
.headers
- .entry("content-type".to_string())
- .or_insert_with(Vec::new)
- .push("text/javascript".to_string());
+ .insert("content-type".to_string(), "text/javascript".to_string());
metadata.write(&cache_filename).unwrap();
let result2 = fetcher_1
@@ -847,23 +831,13 @@ mod tests {
assert_eq!(&(r2.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher_2.http_cache.get(&module_url_1).unwrap();
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "text/javascript"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
// Modify .headers.json again, but the other way around
metadata.headers = HashMap::new();
metadata
.headers
- .entry("content-type".to_string())
- .or_insert_with(Vec::new)
- .push("application/json".to_string());
+ .insert("content-type".to_string(), "application/json".to_string());
metadata.write(&cache_filename).unwrap();
let result3 = fetcher_2
@@ -886,13 +860,7 @@ mod tests {
assert_eq!(&(r3.media_type), &MediaType::Json);
let metadata = crate::http_cache::Metadata::read(&cache_filename).unwrap();
assert_eq!(
- metadata
- .headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ metadata.headers.get("content-type").unwrap(),
"application/json"
);
@@ -942,15 +910,7 @@ mod tests {
assert_eq!(r.source_code.bytes, expected);
assert_eq!(&(r.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "text/javascript"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
// Modify .headers.json
let mut metadata =
@@ -958,9 +918,7 @@ mod tests {
metadata.headers = HashMap::new();
metadata
.headers
- .entry("content-type".to_string())
- .or_insert_with(Vec::new)
- .push("text/typescript".to_string());
+ .insert("content-type".to_string(), "text/typescript".to_string());
metadata.write(&cache_filename).unwrap();
let result2 = fetcher
@@ -982,13 +940,7 @@ mod tests {
assert_eq!(&(r2.media_type), &MediaType::TypeScript);
let metadata = crate::http_cache::Metadata::read(&cache_filename).unwrap();
assert_eq!(
- metadata
- .headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ metadata.headers.get("content-type").unwrap(),
"text/typescript"
);
@@ -1012,15 +964,7 @@ mod tests {
// (due to http fetch)
assert_eq!(&(r3.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "text/javascript"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
}
#[tokio::test]
@@ -1106,7 +1050,7 @@ mod tests {
assert_eq!(fs::read_to_string(&redirect_source_filename).unwrap(), "");
let (_, headers) = fetcher.http_cache.get(&redirect_module_url).unwrap();
assert_eq!(
- headers.get("location").unwrap().first().unwrap().as_str(),
+ headers.get("location").unwrap(),
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js"
);
// The target of redirection is downloaded instead.
@@ -1159,16 +1103,10 @@ mod tests {
assert_eq!(fs::read_to_string(&redirect_path).unwrap(), "");
let (_, headers) = fetcher.http_cache.get(&double_redirect_url).unwrap();
- assert_eq!(
- headers.get("location").unwrap().first().unwrap(),
- &redirect_url.to_string()
- );
+ assert_eq!(headers.get("location").unwrap(), &redirect_url.to_string());
let (_, headers) = fetcher.http_cache.get(&redirect_url).unwrap();
- assert_eq!(
- headers.get("location").unwrap().first().unwrap(),
- &target_url.to_string()
- );
+ assert_eq!(headers.get("location").unwrap(), &target_url.to_string());
// The target of redirection is downloaded instead.
assert_eq!(
@@ -1321,7 +1259,7 @@ mod tests {
assert_eq!(fs::read_to_string(&redirect_source_filename).unwrap(), "");
let (_, headers) = fetcher.http_cache.get(&redirect_module_url).unwrap();
assert_eq!(
- headers.get("location").unwrap().first().unwrap().as_str(),
+ headers.get("location").unwrap(),
"/cli/tests/subdir/redirects/redirect1.js"
);
// The target of redirection is downloaded instead.
@@ -1435,9 +1373,7 @@ mod tests {
metadata.headers = HashMap::new();
metadata
.headers
- .entry("content-type".to_string())
- .or_insert_with(Vec::new)
- .push("text/javascript".to_string());
+ .insert("content-type".to_string(), "text/javascript".to_string());
metadata.write(&cache_filename).unwrap();
let result2 = fetcher.fetch_cached_remote_source(&module_url, 1);
@@ -1468,15 +1404,7 @@ mod tests {
assert_eq!(r.source_code.bytes, b"export const loaded = true;\n");
assert_eq!(&(r.media_type), &MediaType::TypeScript);
let (_, headers) = fetcher.http_cache.get(module_url).unwrap();
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "text/typescript"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "text/typescript");
}
#[tokio::test]
@@ -1500,15 +1428,7 @@ mod tests {
assert_eq!(r2.source_code.bytes, b"export const loaded = true;\n");
assert_eq!(&(r2.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher.http_cache.get(module_url).unwrap();
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "text/javascript"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
}
#[tokio::test]
@@ -1532,15 +1452,7 @@ mod tests {
assert_eq!(r3.source_code.bytes, b"export const loaded = true;\n");
assert_eq!(&(r3.media_type), &MediaType::TypeScript);
let (_, headers) = fetcher.http_cache.get(module_url).unwrap();
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "text/typescript"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "text/typescript");
}
#[tokio::test]
@@ -1896,10 +1808,7 @@ mod tests {
assert_eq!(&(source.media_type), &MediaType::TypeScript);
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
- assert_eq!(
- headers.get("etag").unwrap().first().unwrap().as_str(),
- "33a64df551425fcc55e"
- );
+ assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
let metadata_path = crate::http_cache::Metadata::filename(
&fetcher.http_cache.get_cache_filename(&module_url),
@@ -2024,12 +1933,7 @@ mod tests {
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
&format!("application/typescript;charset={}", charset)
);
}
diff --git a/cli/http_cache.rs b/cli/http_cache.rs
index d14f3d2ad..6b2465b38 100644
--- a/cli/http_cache.rs
+++ b/cli/http_cache.rs
@@ -215,14 +215,11 @@ mod tests {
let cache = HttpCache::new(dir.path());
let url = Url::parse("https://deno.land/x/welcome.ts").unwrap();
let mut headers = HashMap::new();
- headers
- .entry("content-type".to_string())
- .or_insert_with(Vec::new)
- .push("application/javascript".to_string());
- headers
- .entry("etag".to_string())
- .or_insert_with(Vec::new)
- .push("as5625rqdsfb".to_string());
+ headers.insert(
+ "content-type".to_string(),
+ "application/javascript".to_string(),
+ );
+ headers.insert("etag".to_string(), "as5625rqdsfb".to_string());
let content = b"Hello world";
let r = cache.set(&url, headers, content);
eprintln!("result {:?}", r);
@@ -234,18 +231,10 @@ mod tests {
file.read_to_string(&mut content).unwrap();
assert_eq!(content, "Hello world");
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
"application/javascript"
);
- assert_eq!(
- headers.get("etag").unwrap().first().unwrap().as_str(),
- "as5625rqdsfb"
- );
+ assert_eq!(headers.get("etag").unwrap(), "as5625rqdsfb");
assert_eq!(headers.get("foobar"), None);
}
diff --git a/cli/http_util.rs b/cli/http_util.rs
index ebd84972d..015bfaa46 100644
--- a/cli/http_util.rs
+++ b/cli/http_util.rs
@@ -76,7 +76,10 @@ fn resolve_url_from_location(base_url: &Url, location: &str) -> Url {
}
}
-pub type HeadersMap = HashMap<String, Vec<String>>;
+// TODO(ry) HTTP headers are not unique key, value pairs. There may be more than
+// one header line with the same key. This should be changed to something like
+// Vec<(String, String)>
+pub type HeadersMap = HashMap<String, String>;
#[derive(Debug, PartialEq)]
pub enum FetchOnceResult {
@@ -109,7 +112,7 @@ pub async fn fetch_once(
return Ok(FetchOnceResult::NotModified);
}
- let mut headers_: HashMap<String, Vec<String>> = HashMap::new();
+ let mut headers_: HashMap<String, String> = HashMap::new();
let headers = response.headers();
if let Some(warning) = headers.get("X-Deno-Warning") {
@@ -128,10 +131,7 @@ pub async fn fetch_once(
.map(|e| e.to_str().unwrap().to_string())
.collect::<Vec<String>>()
.join(",");
- headers_
- .entry(key_str)
- .or_insert_with(Vec::new)
- .push(values_str);
+ headers_.insert(key_str, values_str);
}
if response.status().is_redirection() {
@@ -248,15 +248,7 @@ mod tests {
let result = fetch_once(client, &url, None).await;
if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert!(!body.is_empty());
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "application/json"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "application/json");
assert_eq!(headers.get("etag"), None);
assert_eq!(headers.get("x-typescript-types"), None);
} else {
@@ -277,12 +269,7 @@ mod tests {
if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
"application/javascript"
);
assert_eq!(headers.get("etag"), None);
@@ -302,18 +289,10 @@ mod tests {
assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
"application/typescript"
);
- assert_eq!(
- headers.get("etag").unwrap().first().unwrap().as_str(),
- "33a64df551425fcc55e"
- );
+ assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
} else {
panic!();
}
@@ -337,12 +316,7 @@ mod tests {
assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
"application/javascript"
);
assert_eq!(headers.get("etag"), None);
@@ -425,15 +399,7 @@ mod tests {
let result = fetch_once(client, &url, None).await;
if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert!(!body.is_empty());
- assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
- "application/json"
- );
+ assert_eq!(headers.get("content-type").unwrap(), "application/json");
assert_eq!(headers.get("etag"), None);
assert_eq!(headers.get("x-typescript-types"), None);
} else {
@@ -460,12 +426,7 @@ mod tests {
if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
"application/javascript"
);
assert_eq!(headers.get("etag"), None);
@@ -491,18 +452,10 @@ mod tests {
assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
"application/typescript"
);
- assert_eq!(
- headers.get("etag").unwrap().first().unwrap().as_str(),
- "33a64df551425fcc55e"
- );
+ assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
assert_eq!(headers.get("x-typescript-types"), None);
} else {
panic!();
@@ -533,12 +486,7 @@ mod tests {
assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
assert_eq!(
- headers
- .get("content-type")
- .unwrap()
- .first()
- .unwrap()
- .as_str(),
+ headers.get("content-type").unwrap(),
"application/javascript"
);
assert_eq!(headers.get("etag"), None);