Skip to content

Commit

Permalink
Use 'reqwest' to implement HTTP client (#2822)
Browse files Browse the repository at this point in the history
Closes #2720
  • Loading branch information
bartlomieju authored and piscisaureus committed Aug 29, 2019
1 parent 50106bc commit 19839f6
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 283 deletions.
25 changes: 6 additions & 19 deletions cli/file_fetcher.rs
Expand Up @@ -5,14 +5,14 @@ use crate::deno_error::ErrorKind;
use crate::deno_error::GetErrorKind;
use crate::disk_cache::DiskCache;
use crate::http_util;
use crate::http_util::FetchOnceResult;
use crate::msg;
use crate::progress::Progress;
use crate::tokio_util;
use deno::ErrBox;
use deno::ModuleSpecifier;
use futures::future::Either;
use futures::Future;
use http;
use serde_json;
use std;
use std::collections::HashMap;
Expand All @@ -21,7 +21,6 @@ use std::path::Path;
use std::path::PathBuf;
use std::result::Result;
use std::str;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Mutex;
use url;
Expand Down Expand Up @@ -305,8 +304,6 @@ impl SourceFileFetcher {
no_remote_fetch: bool,
redirect_limit: i64,
) -> Box<SourceFileFuture> {
use crate::http_util::FetchOnceResult;

if redirect_limit < 0 {
return Box::new(futures::future::err(too_many_redirects()));
}
Expand Down Expand Up @@ -342,20 +339,14 @@ impl SourceFileFetcher {
}

let download_job = self.progress.add("Download", &module_url.to_string());

let module_uri = url_into_uri(&module_url);

let dir = self.clone();
let module_url = module_url.clone();

// Single pass fetch, either yields code or yields redirect.
let f =
http_util::fetch_string_once(module_uri).and_then(move |r| match r {
FetchOnceResult::Redirect(uri) => {
let f = http_util::fetch_string_once(&module_url).and_then(move |r| {
match r {
FetchOnceResult::Redirect(new_module_url) => {
// If redirects, update module_name and filename for next looped call.
let new_module_url = Url::parse(&uri.to_string())
.expect("http::uri::Uri should be parseable as Url");

dir
.save_source_code_headers(
&module_url,
Expand Down Expand Up @@ -409,7 +400,8 @@ impl SourceFileFetcher {

Either::B(futures::future::ok(source_file))
}
});
}
});

Box::new(f)
}
Expand Down Expand Up @@ -539,11 +531,6 @@ fn filter_shebang(bytes: Vec<u8>) -> Vec<u8> {
}
}

fn url_into_uri(url: &url::Url) -> http::uri::Uri {
http::uri::Uri::from_str(&url.to_string())
.expect("url::Url should be parseable as http::uri::Uri")
}

#[derive(Debug, Default)]
/// Header metadata associated with a particular "symbolic" source code file.
/// (the associated source code file might not be cached, while remaining
Expand Down
39 changes: 8 additions & 31 deletions cli/http_body.rs
@@ -1,27 +1,27 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

use futures::stream::Stream;
use futures::Async;
use futures::Poll;
use hyper::body::Payload;
use hyper::Body;
use hyper::Chunk;
use reqwest::r#async::Chunk;
use reqwest::r#async::Decoder;
use std::cmp::min;
use std::io;
use std::io::Read;
use tokio::io::AsyncRead;

/// Wraps `hyper::Body` so that it can be exposed as an `AsyncRead` and integrated
/// Wraps `reqwest::Decoder` so that it can be exposed as an `AsyncRead` and integrated
/// into resources more easily.
pub struct HttpBody {
body: Body,
decoder: Decoder,
chunk: Option<Chunk>,
pos: usize,
}

impl HttpBody {
pub fn from(body: Body) -> Self {
pub fn from(body: Decoder) -> Self {
Self {
body,
decoder: body,
chunk: None,
pos: 0,
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl AsyncRead for HttpBody {
assert_eq!(self.pos, 0);
}

let p = self.body.poll_data();
let p = self.decoder.poll();
match p {
Err(e) => Err(
// TODO Need to map hyper::Error into std::io::Error.
Expand Down Expand Up @@ -87,26 +87,3 @@ impl AsyncRead for HttpBody {
}
}
}

#[test]
fn test_body_async_read() {
use std::str::from_utf8;
let body = Body::from("hello world");
let mut body = HttpBody::from(body);

let buf = &mut [0, 0, 0, 0, 0];
let r = body.poll_read(buf);
assert!(r.is_ok());
assert_eq!(r.unwrap(), Async::Ready(5));
assert_eq!(from_utf8(buf).unwrap(), "hello");

let r = body.poll_read(buf);
assert!(r.is_ok());
assert_eq!(r.unwrap(), Async::Ready(5));
assert_eq!(from_utf8(buf).unwrap(), " worl");

let r = body.poll_read(buf);
assert!(r.is_ok());
assert_eq!(r.unwrap(), Async::Ready(1));
assert_eq!(from_utf8(&buf[0..1]).unwrap(), "d");
}

0 comments on commit 19839f6

Please sign in to comment.