1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 12:58:54 -05:00

Revert "fix(cli/http_utils): accept a single key-multiple values headers (#7375)" (#7515)

This reverts commit f5c84920c2.
This commit is contained in:
Bartek Iwańczuk 2020-09-16 13:34:16 +02:00 committed by GitHub
parent aa657d6493
commit d4a24c870e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 207 deletions

View file

@ -404,7 +404,7 @@ impl SourceFileFetcher {
}; };
let (mut source_file, headers) = result; 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) { let redirect_url = match Url::parse(redirect_to) {
Ok(redirect_url) => redirect_url, Ok(redirect_url) => redirect_url,
Err(url::ParseError::RelativeUrlWithoutBase) => { Err(url::ParseError::RelativeUrlWithoutBase) => {
@ -427,15 +427,9 @@ impl SourceFileFetcher {
let fake_filepath = PathBuf::from(module_url.path()); let fake_filepath = PathBuf::from(module_url.path());
let (media_type, charset) = map_content_type( let (media_type, charset) = map_content_type(
&fake_filepath, &fake_filepath,
headers headers.get("content-type").map(|e| e.as_str()),
.get("content-type")
.and_then(|e| e.first())
.map(|e| e.as_str()),
); );
let types_header = headers let types_header = headers.get("x-typescript-types").map(|e| e.to_string());
.get("x-typescript-types")
.and_then(|e| e.first())
.map(|e| e.to_string());
Ok(Some(SourceFile { Ok(Some(SourceFile {
url: module_url.clone(), url: module_url.clone(),
filename: cache_filename, filename: cache_filename,
@ -499,10 +493,7 @@ impl SourceFileFetcher {
let dir = self.clone(); let dir = self.clone();
let module_url = module_url.clone(); let module_url = module_url.clone();
let module_etag = match self.http_cache.get(&module_url) { let module_etag = match self.http_cache.get(&module_url) {
Ok((_, headers)) => headers Ok((_, headers)) => headers.get("etag").map(String::from),
.get("etag")
.and_then(|e| e.first())
.map(|e| e.to_string()),
Err(_) => None, Err(_) => None,
}; };
let permissions = permissions.clone(); let permissions = permissions.clone();
@ -541,16 +532,11 @@ impl SourceFileFetcher {
let fake_filepath = PathBuf::from(module_url.path()); let fake_filepath = PathBuf::from(module_url.path());
let (media_type, charset) = map_content_type( let (media_type, charset) = map_content_type(
&fake_filepath, &fake_filepath,
headers headers.get("content-type").map(String::as_str),
.get("content-type")
.and_then(|e| e.first())
.map(|e| e.as_str()),
); );
let types_header = headers let types_header =
.get("x-typescript-types") headers.get("x-typescript-types").map(String::to_string);
.and_then(|e| e.first())
.map(|e| e.to_string());
let source_file = SourceFile { let source_file = SourceFile {
url: module_url.clone(), url: module_url.clone(),
@ -822,9 +808,7 @@ mod tests {
metadata.headers = HashMap::new(); metadata.headers = HashMap::new();
metadata metadata
.headers .headers
.entry("content-type".to_string()) .insert("content-type".to_string(), "text/javascript".to_string());
.or_insert_with(Vec::new)
.push("text/javascript".to_string());
metadata.write(&cache_filename).unwrap(); metadata.write(&cache_filename).unwrap();
let result2 = fetcher_1 let result2 = fetcher_1
@ -847,23 +831,13 @@ mod tests {
assert_eq!(&(r2.media_type), &MediaType::JavaScript); assert_eq!(&(r2.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher_2.http_cache.get(&module_url_1).unwrap(); let (_, headers) = fetcher_2.http_cache.get(&module_url_1).unwrap();
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"text/javascript"
);
// Modify .headers.json again, but the other way around // Modify .headers.json again, but the other way around
metadata.headers = HashMap::new(); metadata.headers = HashMap::new();
metadata metadata
.headers .headers
.entry("content-type".to_string()) .insert("content-type".to_string(), "application/json".to_string());
.or_insert_with(Vec::new)
.push("application/json".to_string());
metadata.write(&cache_filename).unwrap(); metadata.write(&cache_filename).unwrap();
let result3 = fetcher_2 let result3 = fetcher_2
@ -886,13 +860,7 @@ mod tests {
assert_eq!(&(r3.media_type), &MediaType::Json); assert_eq!(&(r3.media_type), &MediaType::Json);
let metadata = crate::http_cache::Metadata::read(&cache_filename).unwrap(); let metadata = crate::http_cache::Metadata::read(&cache_filename).unwrap();
assert_eq!( assert_eq!(
metadata metadata.headers.get("content-type").unwrap(),
.headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/json" "application/json"
); );
@ -942,15 +910,7 @@ mod tests {
assert_eq!(r.source_code.bytes, expected); assert_eq!(r.source_code.bytes, expected);
assert_eq!(&(r.media_type), &MediaType::JavaScript); assert_eq!(&(r.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"text/javascript"
);
// Modify .headers.json // Modify .headers.json
let mut metadata = let mut metadata =
@ -958,9 +918,7 @@ mod tests {
metadata.headers = HashMap::new(); metadata.headers = HashMap::new();
metadata metadata
.headers .headers
.entry("content-type".to_string()) .insert("content-type".to_string(), "text/typescript".to_string());
.or_insert_with(Vec::new)
.push("text/typescript".to_string());
metadata.write(&cache_filename).unwrap(); metadata.write(&cache_filename).unwrap();
let result2 = fetcher let result2 = fetcher
@ -982,13 +940,7 @@ mod tests {
assert_eq!(&(r2.media_type), &MediaType::TypeScript); assert_eq!(&(r2.media_type), &MediaType::TypeScript);
let metadata = crate::http_cache::Metadata::read(&cache_filename).unwrap(); let metadata = crate::http_cache::Metadata::read(&cache_filename).unwrap();
assert_eq!( assert_eq!(
metadata metadata.headers.get("content-type").unwrap(),
.headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"text/typescript" "text/typescript"
); );
@ -1012,15 +964,7 @@ mod tests {
// (due to http fetch) // (due to http fetch)
assert_eq!(&(r3.media_type), &MediaType::JavaScript); assert_eq!(&(r3.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"text/javascript"
);
} }
#[tokio::test] #[tokio::test]
@ -1106,7 +1050,7 @@ mod tests {
assert_eq!(fs::read_to_string(&redirect_source_filename).unwrap(), ""); assert_eq!(fs::read_to_string(&redirect_source_filename).unwrap(), "");
let (_, headers) = fetcher.http_cache.get(&redirect_module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&redirect_module_url).unwrap();
assert_eq!( assert_eq!(
headers.get("location").unwrap().first().unwrap().as_str(), headers.get("location").unwrap(),
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js" "http://localhost:4545/cli/tests/subdir/redirects/redirect1.js"
); );
// The target of redirection is downloaded instead. // The target of redirection is downloaded instead.
@ -1159,16 +1103,10 @@ mod tests {
assert_eq!(fs::read_to_string(&redirect_path).unwrap(), ""); assert_eq!(fs::read_to_string(&redirect_path).unwrap(), "");
let (_, headers) = fetcher.http_cache.get(&double_redirect_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&double_redirect_url).unwrap();
assert_eq!( assert_eq!(headers.get("location").unwrap(), &redirect_url.to_string());
headers.get("location").unwrap().first().unwrap(),
&redirect_url.to_string()
);
let (_, headers) = fetcher.http_cache.get(&redirect_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&redirect_url).unwrap();
assert_eq!( assert_eq!(headers.get("location").unwrap(), &target_url.to_string());
headers.get("location").unwrap().first().unwrap(),
&target_url.to_string()
);
// The target of redirection is downloaded instead. // The target of redirection is downloaded instead.
assert_eq!( assert_eq!(
@ -1321,7 +1259,7 @@ mod tests {
assert_eq!(fs::read_to_string(&redirect_source_filename).unwrap(), ""); assert_eq!(fs::read_to_string(&redirect_source_filename).unwrap(), "");
let (_, headers) = fetcher.http_cache.get(&redirect_module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&redirect_module_url).unwrap();
assert_eq!( assert_eq!(
headers.get("location").unwrap().first().unwrap().as_str(), headers.get("location").unwrap(),
"/cli/tests/subdir/redirects/redirect1.js" "/cli/tests/subdir/redirects/redirect1.js"
); );
// The target of redirection is downloaded instead. // The target of redirection is downloaded instead.
@ -1435,9 +1373,7 @@ mod tests {
metadata.headers = HashMap::new(); metadata.headers = HashMap::new();
metadata metadata
.headers .headers
.entry("content-type".to_string()) .insert("content-type".to_string(), "text/javascript".to_string());
.or_insert_with(Vec::new)
.push("text/javascript".to_string());
metadata.write(&cache_filename).unwrap(); metadata.write(&cache_filename).unwrap();
let result2 = fetcher.fetch_cached_remote_source(&module_url, 1); 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.source_code.bytes, b"export const loaded = true;\n");
assert_eq!(&(r.media_type), &MediaType::TypeScript); assert_eq!(&(r.media_type), &MediaType::TypeScript);
let (_, headers) = fetcher.http_cache.get(module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(module_url).unwrap();
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "text/typescript");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"text/typescript"
);
} }
#[tokio::test] #[tokio::test]
@ -1500,15 +1428,7 @@ mod tests {
assert_eq!(r2.source_code.bytes, b"export const loaded = true;\n"); assert_eq!(r2.source_code.bytes, b"export const loaded = true;\n");
assert_eq!(&(r2.media_type), &MediaType::JavaScript); assert_eq!(&(r2.media_type), &MediaType::JavaScript);
let (_, headers) = fetcher.http_cache.get(module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(module_url).unwrap();
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"text/javascript"
);
} }
#[tokio::test] #[tokio::test]
@ -1532,15 +1452,7 @@ mod tests {
assert_eq!(r3.source_code.bytes, b"export const loaded = true;\n"); assert_eq!(r3.source_code.bytes, b"export const loaded = true;\n");
assert_eq!(&(r3.media_type), &MediaType::TypeScript); assert_eq!(&(r3.media_type), &MediaType::TypeScript);
let (_, headers) = fetcher.http_cache.get(module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(module_url).unwrap();
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "text/typescript");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"text/typescript"
);
} }
#[tokio::test] #[tokio::test]
@ -1896,10 +1808,7 @@ mod tests {
assert_eq!(&(source.media_type), &MediaType::TypeScript); assert_eq!(&(source.media_type), &MediaType::TypeScript);
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
assert_eq!( assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
headers.get("etag").unwrap().first().unwrap().as_str(),
"33a64df551425fcc55e"
);
let metadata_path = crate::http_cache::Metadata::filename( let metadata_path = crate::http_cache::Metadata::filename(
&fetcher.http_cache.get_cache_filename(&module_url), &fetcher.http_cache.get_cache_filename(&module_url),
@ -2024,12 +1933,7 @@ mod tests {
let (_, headers) = fetcher.http_cache.get(&module_url).unwrap(); let (_, headers) = fetcher.http_cache.get(&module_url).unwrap();
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
&format!("application/typescript;charset={}", charset) &format!("application/typescript;charset={}", charset)
); );
} }

View file

@ -215,14 +215,11 @@ mod tests {
let cache = HttpCache::new(dir.path()); let cache = HttpCache::new(dir.path());
let url = Url::parse("https://deno.land/x/welcome.ts").unwrap(); let url = Url::parse("https://deno.land/x/welcome.ts").unwrap();
let mut headers = HashMap::new(); let mut headers = HashMap::new();
headers headers.insert(
.entry("content-type".to_string()) "content-type".to_string(),
.or_insert_with(Vec::new) "application/javascript".to_string(),
.push("application/javascript".to_string()); );
headers headers.insert("etag".to_string(), "as5625rqdsfb".to_string());
.entry("etag".to_string())
.or_insert_with(Vec::new)
.push("as5625rqdsfb".to_string());
let content = b"Hello world"; let content = b"Hello world";
let r = cache.set(&url, headers, content); let r = cache.set(&url, headers, content);
eprintln!("result {:?}", r); eprintln!("result {:?}", r);
@ -234,18 +231,10 @@ mod tests {
file.read_to_string(&mut content).unwrap(); file.read_to_string(&mut content).unwrap();
assert_eq!(content, "Hello world"); assert_eq!(content, "Hello world");
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/javascript" "application/javascript"
); );
assert_eq!( assert_eq!(headers.get("etag").unwrap(), "as5625rqdsfb");
headers.get("etag").unwrap().first().unwrap().as_str(),
"as5625rqdsfb"
);
assert_eq!(headers.get("foobar"), None); assert_eq!(headers.get("foobar"), None);
} }

View file

@ -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)] #[derive(Debug, PartialEq)]
pub enum FetchOnceResult { pub enum FetchOnceResult {
@ -109,7 +112,7 @@ pub async fn fetch_once(
return Ok(FetchOnceResult::NotModified); 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(); let headers = response.headers();
if let Some(warning) = headers.get("X-Deno-Warning") { 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()) .map(|e| e.to_str().unwrap().to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(","); .join(",");
headers_ headers_.insert(key_str, values_str);
.entry(key_str)
.or_insert_with(Vec::new)
.push(values_str);
} }
if response.status().is_redirection() { if response.status().is_redirection() {
@ -248,15 +248,7 @@ mod tests {
let result = fetch_once(client, &url, None).await; let result = fetch_once(client, &url, None).await;
if let Ok(FetchOnceResult::Code(body, headers)) = result { if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert!(!body.is_empty()); assert!(!body.is_empty());
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "application/json");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/json"
);
assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("etag"), None);
assert_eq!(headers.get("x-typescript-types"), None); assert_eq!(headers.get("x-typescript-types"), None);
} else { } else {
@ -277,12 +269,7 @@ mod tests {
if let Ok(FetchOnceResult::Code(body, headers)) = result { if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')"); assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/javascript" "application/javascript"
); );
assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("etag"), None);
@ -302,18 +289,10 @@ mod tests {
assert!(!body.is_empty()); assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')"); assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/typescript" "application/typescript"
); );
assert_eq!( assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
headers.get("etag").unwrap().first().unwrap().as_str(),
"33a64df551425fcc55e"
);
} else { } else {
panic!(); panic!();
} }
@ -337,12 +316,7 @@ mod tests {
assert!(!body.is_empty()); assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');"); assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/javascript" "application/javascript"
); );
assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("etag"), None);
@ -425,15 +399,7 @@ mod tests {
let result = fetch_once(client, &url, None).await; let result = fetch_once(client, &url, None).await;
if let Ok(FetchOnceResult::Code(body, headers)) = result { if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert!(!body.is_empty()); assert!(!body.is_empty());
assert_eq!( assert_eq!(headers.get("content-type").unwrap(), "application/json");
headers
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/json"
);
assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("etag"), None);
assert_eq!(headers.get("x-typescript-types"), None); assert_eq!(headers.get("x-typescript-types"), None);
} else { } else {
@ -460,12 +426,7 @@ mod tests {
if let Ok(FetchOnceResult::Code(body, headers)) = result { if let Ok(FetchOnceResult::Code(body, headers)) = result {
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')"); assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/javascript" "application/javascript"
); );
assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("etag"), None);
@ -491,18 +452,10 @@ mod tests {
assert!(!body.is_empty()); assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')"); assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/typescript" "application/typescript"
); );
assert_eq!( assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
headers.get("etag").unwrap().first().unwrap().as_str(),
"33a64df551425fcc55e"
);
assert_eq!(headers.get("x-typescript-types"), None); assert_eq!(headers.get("x-typescript-types"), None);
} else { } else {
panic!(); panic!();
@ -533,12 +486,7 @@ mod tests {
assert!(!body.is_empty()); assert!(!body.is_empty());
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');"); assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
assert_eq!( assert_eq!(
headers headers.get("content-type").unwrap(),
.get("content-type")
.unwrap()
.first()
.unwrap()
.as_str(),
"application/javascript" "application/javascript"
); );
assert_eq!(headers.get("etag"), None); assert_eq!(headers.get("etag"), None);