Skip to content

Commit

Permalink
Revert "fix(cli/http_utils): accept a single key-multiple values head…
Browse files Browse the repository at this point in the history
…ers (denoland#7375)" (denoland#7515)

This reverts commit f5c8492.
  • Loading branch information
bartlomieju authored Sep 16, 2020
1 parent aa657d6 commit d4a24c8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 207 deletions.
146 changes: 25 additions & 121 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
);

Expand Down Expand Up @@ -942,25 +910,15 @@ 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 =
crate::http_cache::Metadata::read(&cache_filename).unwrap();
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
Expand All @@ -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"
);

Expand All @@ -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]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
);
}
Expand Down
25 changes: 7 additions & 18 deletions cli/http_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit d4a24c8

Please sign in to comment.