Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Update actix web to v4.0.0-beta.15 #677

Merged
merged 2 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
590 changes: 238 additions & 352 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ codegen-units = 1
panic = 'abort'

[dependencies]
actix-web = "4.0.0-beta.8"
actix-files = "0.6.0-beta.6"
actix-multipart = "0.4.0-beta.5"
actix-web-httpauth = "0.6.0-beta.2"
actix-web = "4.0.0-beta.15"
actix-files = "0.6.0-beta.11"
actix-multipart = "0.4.0-beta.11"
actix-web-httpauth = "0.6.0-beta.6"
maud = "0.23"
yansi = "0.5"
simplelog = "0.11"
Expand Down Expand Up @@ -50,7 +50,8 @@ mime = "0.3"
httparse = "1"
http = "0.2"
atty = "0.2"
rustls = { version = "0.19", optional = true }
rustls = { version = "0.20", optional = true }
rustls-pemfile = { version = "0.2", optional = true }
socket2 = "0.4"
get_if_addrs = "0.5"

Expand All @@ -60,7 +61,7 @@ default = ["tls"]
# See also https://github.com/briansmith/ring/issues/1182
# and https://github.com/briansmith/ring/issues/562
# and https://github.com/briansmith/ring/issues/1367
tls = ["rustls", "actix-web/rustls"]
tls = ["rustls", "rustls-pemfile", "actix-web/rustls"]

[dev-dependencies]
assert_cmd = "2"
Expand Down
2 changes: 1 addition & 1 deletion src/archive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use actix_web::http::ContentEncoding;
use actix_web::http::header::ContentEncoding;
use libflate::gzip::Encoder;
use serde::Deserialize;
use std::fs::File;
Expand Down
29 changes: 25 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use anyhow::{Context, Result};
use http::HeaderMap;

#[cfg(feature = "tls")]
use rustls::internal::pemfile::{certs, pkcs8_private_keys};
use rustls_pemfile::{certs, pkcs8_private_keys};

use crate::{args::CliArgs, auth::RequiredAuth};

Expand Down Expand Up @@ -156,18 +156,39 @@ impl MiniserveConfig {
let tls_rustls_server_config = if let (Some(tls_cert), Some(tls_key)) =
(args.tls_cert, args.tls_key)
{
let mut server_config = rustls::ServerConfig::new(rustls::NoClientAuth::new());
let cert_file = &mut BufReader::new(
File::open(&tls_cert)
.context(format!("Couldn't access TLS certificate {:?}", tls_cert))?,
);
let key_file = &mut BufReader::new(
File::open(&tls_key).context(format!("Couldn't access TLS key {:?}", tls_key))?,
);
let cert_chain = certs(cert_file).map_err(|_| anyhow!("Couldn't load certificates"))?;
let cert_chain = match rustls_pemfile::read_one(cert_file) {
Ok(item) => match item {
Some(item) => match item {
rustls_pemfile::Item::X509Certificate(item) => item,
_ => return Err(anyhow!("Certfile is not a X509Certificate")),
},
None => {
return Err(anyhow!(
"Certfile does not contain any recognized certificates"
))
}
},
_ => return Err(anyhow!("Could not read certfile")),
};
let mut keys =
pkcs8_private_keys(key_file).map_err(|_| anyhow!("Couldn't load private key"))?;
server_config.set_single_cert(cert_chain, keys.remove(0))?;
let server_config = rustls::ServerConfig::builder()
.with_safe_default_cipher_suites()
.with_safe_default_kx_groups()
.with_safe_default_protocol_versions()
.unwrap()
.with_no_client_auth()
.with_single_cert(
vec![rustls::Certificate(cert_chain)],
rustls::PrivateKey(keys.remove(0)),
)?;
Some(server_config)
} else {
None
Expand Down
22 changes: 11 additions & 11 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{renderer::render_error, MiniserveConfig};
use actix_web::{
body::AnyBody,
body::{BoxBody, MessageBody},
dev::{ResponseHead, Service, ServiceRequest, ServiceResponse},
http::{header, StatusCode},
HttpRequest, HttpResponse, ResponseError,
Expand Down Expand Up @@ -134,13 +134,15 @@ where
}
}

fn map_error_page(req: &HttpRequest, head: &mut ResponseHead, body: AnyBody) -> AnyBody {
let error_msg = match &body {
AnyBody::Bytes(bytes) => match std::str::from_utf8(bytes) {
Ok(msg) => msg,
_ => return body,
},
_ => return body,
fn map_error_page<'a>(req: &HttpRequest, head: &mut ResponseHead, body: BoxBody) -> BoxBody {
let error_msg = match body.try_into_bytes() {
Ok(bytes) => bytes,
Err(body) => return body,
};

let error_msg = match std::str::from_utf8(&error_msg) {
Ok(msg) => msg,
_ => return BoxBody::new(error_msg),
};

let conf = req.app_data::<MiniserveConfig>().unwrap();
Expand All @@ -155,9 +157,7 @@ fn map_error_page(req: &HttpRequest, head: &mut ResponseHead, body: AnyBody) ->
header::HeaderValue::from_static("text/html; charset=utf-8"),
);

render_error(error_msg, head.status, conf, return_address)
.into_string()
.into()
BoxBody::new(render_error(error_msg, head.status, conf, return_address).into_string())
}

pub fn log_error_chain(description: String) {
Expand Down
15 changes: 6 additions & 9 deletions src/file_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ async fn handle_multipart(
path: PathBuf,
overwrite_files: bool,
) -> Result<u64, ContextualError> {
let filename = field
.content_disposition()
.and_then(|cd| cd.get_filename().map(String::from))
.ok_or_else(|| {
ContextualError::ParseError(
"HTTP header".to_string(),
"Failed to retrieve the name of the file to upload".to_string(),
)
})?;
let filename = field.content_disposition().get_filename().ok_or_else(|| {
ContextualError::ParseError(
"HTTP header".to_string(),
"Failed to retrieve the name of the file to upload".to_string(),
)
})?;

let filename = sanitize_path(Path::new(&filename), false).ok_or_else(|| {
ContextualError::InvalidPathError("Invalid file name to upload".to_string())
Expand Down
6 changes: 3 additions & 3 deletions src/listing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::body::Body;
use actix_web::body::BoxBody;
use actix_web::dev::ServiceResponse;
use actix_web::web::Query;
use actix_web::{HttpRequest, HttpResponse};
use actix_web::{HttpMessage, HttpRequest, HttpResponse};
use bytesize::ByteSize;
use percent_encoding::{percent_decode_str, utf8_percent_encode};
use qrcodegen::{QrCode, QrCodeEcc};
Expand Down Expand Up @@ -225,7 +225,7 @@ pub fn directory_listing(
.body(qr_to_svg_string(&qr, 2)),
Err(err) => {
log::error!("URL is invalid (too long?): {:?}", err);
HttpResponse::UriTooLong().body(Body::Empty)
HttpResponse::UriTooLong().finish()
}
};
return Ok(ServiceResponse::new(req.clone(), res));
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::thread;
use std::time::Duration;

use actix_files::NamedFile;
use actix_web::body::BoxBody;
use actix_web::middleware::Compat;
use actix_web::web;
use actix_web::{http::header::ContentType, Responder};
use actix_web::{middleware, App, HttpRequest, HttpResponse};
Expand Down Expand Up @@ -198,7 +200,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
web::scope(inside_config.random_route.as_deref().unwrap_or(""))
.wrap(middleware::Condition::new(
!inside_config.auth.is_empty(),
HttpAuthentication::basic(auth::handle_auth),
Compat::new(HttpAuthentication::basic(auth::handle_auth)),
))
.configure(|c| configure_app(c, &inside_config)),
)
Expand Down Expand Up @@ -296,7 +298,7 @@ fn create_tcp_listener(addr: SocketAddr) -> io::Result<TcpListener> {
fn configure_header(conf: &MiniserveConfig) -> middleware::DefaultHeaders {
conf.header.iter().flatten().fold(
middleware::DefaultHeaders::new(),
|headers, (header_name, header_value)| headers.header(header_name, header_value),
|headers, (header_name, header_value)| headers.add((header_name, header_value)),
)
}

Expand Down Expand Up @@ -357,14 +359,14 @@ async fn favicon() -> impl Responder {
let logo = include_str!("../data/logo.svg");
HttpResponse::Ok()
.insert_header(ContentType(mime::IMAGE_SVG))
.message_body(logo.into())
.body(logo)
}

async fn css() -> impl Responder {
let css = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
HttpResponse::Ok()
.insert_header(ContentType(mime::TEXT_CSS))
.message_body(css.into())
.message_body(BoxBody::new(css))
}

// Prints to the console two inverted QrCodes side by side.
Expand Down