1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

added awc ssl example

This commit is contained in:
dowwie 2020-03-01 08:55:00 -05:00
parent 50332ca491
commit 54a2c3da92
5 changed files with 66 additions and 1 deletions

View File

@ -4,6 +4,7 @@ members = [
"async_ex1", "async_ex1",
"async_ex2", "async_ex2",
"async_pg", "async_pg",
"awc_https",
"basics", "basics",
"cookie-auth", "cookie-auth",
"cookie-session", "cookie-session",

12
awc_https/Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "awc_https"
version = "0.1.0"
authors = ["dowwie <dkcdkg@gmail.com>"]
edition = "2018"
workspace = ".."
[dependencies]
actix-rt = "1.0.0"
actix-web = { version = "2.0.0", features = ["openssl"] }
openssl = "0.10.28"

9
awc_https/README.md Normal file
View File

@ -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

43
awc_https/src/main.rs Normal file
View File

@ -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
}

View File

@ -1,7 +1,7 @@
[package] [package]
name = "redis_session" name = "redis_session"
version = "2.0.0" version = "2.0.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>", "dowwie <dkcdkg@gmail.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]