From 54a2c3da925646117eda8012fe5b64f4a9f5fe84 Mon Sep 17 00:00:00 2001 From: dowwie Date: Sun, 1 Mar 2020 08:55:00 -0500 Subject: [PATCH] added awc ssl example --- Cargo.toml | 1 + awc_https/Cargo.toml | 12 +++++++++++ awc_https/README.md | 9 +++++++++ awc_https/src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++++ redis-session/Cargo.toml | 2 +- 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 awc_https/Cargo.toml create mode 100644 awc_https/README.md create mode 100644 awc_https/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index dddde2da..b428143e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "async_ex1", "async_ex2", "async_pg", + "awc_https", "basics", "cookie-auth", "cookie-session", diff --git a/awc_https/Cargo.toml b/awc_https/Cargo.toml new file mode 100644 index 00000000..892a03a1 --- /dev/null +++ b/awc_https/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "awc_https" +version = "0.1.0" +authors = ["dowwie "] +edition = "2018" +workspace = ".." + + +[dependencies] +actix-rt = "1.0.0" +actix-web = { version = "2.0.0", features = ["openssl"] } +openssl = "0.10.28" diff --git a/awc_https/README.md b/awc_https/README.md new file mode 100644 index 00000000..fd768e6c --- /dev/null +++ b/awc_https/README.md @@ -0,0 +1,9 @@ +The goal of this example is to show you how to use the actix-web client (awc) +for https related communication. As of actix-web 2.0.0, one must be very +careful about setting up https communication. **You could use the default +awc api without configuring ssl but performance will be severely diminished**. + +This example downloads a 10MB image from wikipedia. + +To run: +> curl http://localhost:3000 -o image.jpg diff --git a/awc_https/src/main.rs b/awc_https/src/main.rs new file mode 100644 index 00000000..bc61562a --- /dev/null +++ b/awc_https/src/main.rs @@ -0,0 +1,43 @@ +use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, client::{Client, Connector}}; +use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode}; + + +async fn index(req: HttpRequest) -> HttpResponse { + let mut builder = SslConnector::builder(SslMethod::tls()).unwrap(); + builder.set_verify(SslVerifyMode::NONE); + + let client = Client::build() + .connector(Connector::new().ssl(builder.build()).finish()) + .finish(); + + let now = std::time::Instant::now(); + let payload = + client + .get("https://upload.wikimedia.org/wikipedia/commons/f/ff/Pizigani_1367_Chart_10MB.jpg") + .send() + .await + .unwrap() + .body() + .limit(20_000_000) // sets max allowable payload size + .await + .unwrap(); + + println!("awc time elapsed while reading bytes into memory: {} ms", now.elapsed().as_millis()); + + HttpResponse::Ok() + .content_type("image/jpeg") + .body(payload) +} + +#[actix_rt::main] +async fn main() -> std::io::Result<()> { + let port = 3000; + + HttpServer::new(|| { + App::new() + .service(web::resource("/").to(index)) + }) + .bind(("0.0.0.0", port))? + .run() + .await +} diff --git a/redis-session/Cargo.toml b/redis-session/Cargo.toml index 709e1125..620c5fc2 100644 --- a/redis-session/Cargo.toml +++ b/redis-session/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "redis_session" version = "2.0.0" -authors = ["Nikolay Kim "] +authors = ["Nikolay Kim ", "dowwie "] edition = "2018" [dependencies]