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

Some refactoring #846

Closed
wants to merge 4 commits into from
Closed
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
68 changes: 63 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 40 additions & 40 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
[package]
name = "miniserve"
version = "0.20.0"
description = "For when you really just want to serve some files over HTTP right now!"
authors = ["Sven-Hendrik Haase <svenstaro@gmail.com>", "Boastful Squirrel <boastful.squirrel@gmail.com>"]
repository = "https://github.com/svenstaro/miniserve"
license = "MIT"
readme = "README.md"
keywords = ["serve", "http-server", "static-files", "http", "server"]
categories = ["command-line-utilities", "network-programming", "web-programming::http-server"]
description = "For when you really just want to serve some files over HTTP right now!"
edition = "2021"
keywords = ["serve", "http-server", "static-files", "http", "server"]
license = "MIT"
name = "miniserve"
readme = "README.md"
repository = "https://github.com/svenstaro/miniserve"
version = "0.20.0"

[profile.release]
codegen-units = 1
lto = true
opt-level = 'z'
codegen-units = 1
panic = 'abort'

[dependencies]
actix-web = "4"
actix-files = "0.6"
actix-multipart = "0.4"
actix-web = "4"
actix-web-httpauth = "0.7"
maud = "0.23"
yansi = "0.5"
simplelog = "0.12"
percent-encoding = "2"
port_check = "0.1"
bytesize = "1"
nanoid = "0.4"
alphanumeric-sort = "1"
clap = { version = "3.2", features = ["derive", "cargo", "wrap_help"] }
clap_complete = "3.2.3"
clap_mangen = "0.1"
anyhow = "1"
atty = "0.2"
bytesize = "1"
chrono = "0.4"
chrono-humanize = "0.2"
serde = { version = "1", features = ["derive"] }
tar = "0.4"
clap = {version = "3.2", features = ["derive", "cargo", "wrap_help"]}
clap_complete = "3.2.3"
clap_mangen = "0.1"
futures = "0.3"
get_if_addrs = "0.5"
hex = "0.4"
http = "0.2"
httparse = "1"
libflate = "1"
thiserror = "1"
anyhow = "1"
log = "0.4"
strum = "0.24"
strum_macros = "0.24"
sha2 = "0.10"
hex = "0.4"
zip = { version = "0.6.2", default-features = false }
qrcodegen = "1"
maud = "0.23"
mime = "0.3"
httparse = "1"
http = "0.2"
atty = "0.2"
rustls = { version = "0.20", optional = true }
rustls-pemfile = { version = "1.0", optional = true }
nanoid = "0.4"
percent-encoding = "2"
port_check = "0.1"
qrcode = "0.12.0"
rustls = {version = "0.20", optional = true}
rustls-pemfile = {version = "1.0", optional = true}
serde = {version = "1", features = ["derive"]}
sha2 = "0.10"
simplelog = "0.12"
socket2 = "0.4"
get_if_addrs = "0.5"
strum = "0.24"
strum_macros = "0.24"
tar = "0.4"
thiserror = "1"
yansi = "0.5"
zip = {version = "0.6.2", default-features = false}

[features]
default = ["tls"]
Expand All @@ -66,14 +66,14 @@ tls = ["rustls", "rustls-pemfile", "actix-web/rustls"]

[dev-dependencies]
assert_cmd = "2"
reqwest = { version = "0.11", features = ["blocking", "multipart", "rustls-tls"], default-features = false }
assert_fs = "1"
select = "0.5"
rstest = "0.15"
regex = "1"
predicates = "2"
pretty_assertions = "1.2"
regex = "1"
reqwest = {version = "0.11", features = ["blocking", "multipart", "rustls-tls"], default-features = false}
rstest = "0.15"
select = "0.5"
url = "2"
predicates = "2"

[build-dependencies]
grass = "0.11"
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This empty config file ensures the default formatter settings are enforced for
# all contributors, regardless of their custom global settings.
4 changes: 4 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use qrcode::EcLevel;

/// The error correction level to use for all QR code generation.
pub const QR_EC_LEVEL: EcLevel = EcLevel::M;
41 changes: 5 additions & 36 deletions src/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use actix_web::web::Query;
use actix_web::{HttpMessage, HttpRequest, HttpResponse};
use bytesize::ByteSize;
use percent_encoding::{percent_decode_str, utf8_percent_encode};
use qrcodegen::{QrCode, QrCodeEcc};
use qrcode::QrCode;
use serde::Deserialize;
use strum_macros::{Display, EnumString};

use crate::archive::ArchiveMethod;
use crate::auth::CurrentUser;
use crate::errors::{self, ContextualError};
use crate::renderer;
use crate::{consts, renderer};

use self::percent_encode_sets::PATH_SEGMENT;

Expand Down Expand Up @@ -219,10 +219,10 @@ pub fn directory_listing(

// If the `qrcode` parameter is included in the url, then should respond to the QR code
if let Some(url) = query_params.qrcode {
let res = match QrCode::encode_text(&url, QrCodeEcc::Medium) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like someone was in a hurry and overlooked this. url is not actually the URL, so we're basically encoding an arbitrary query value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm not sure how to fix this. What should this QR code point to, exactly? (Or more broadly, what's the purpose of this QR code?) And if it points to the current URL, should the qrcode query be removed?

let res = match QrCode::with_error_correction_level(url, consts::QR_EC_LEVEL) {
Ok(qr) => HttpResponse::Ok()
.append_header(("Content-Type", "image/svg+xml"))
.body(qr_to_svg_string(&qr, 2)),
.content_type("text/html; charset=utf-8")
.body(renderer::qr_code_page(&qr).into_string()),
Err(err) => {
log::error!("URL is invalid (too long?): {:?}", err);
HttpResponse::UriTooLong().finish()
Expand Down Expand Up @@ -397,34 +397,3 @@ pub fn extract_query_parameters(req: &HttpRequest) -> QueryParameters {
}
}
}

// Returns a string of SVG code for an image depicting
// the given QR Code, with the given number of border modules.
// The string always uses Unix newlines (\n), regardless of the platform.
fn qr_to_svg_string(qr: &QrCode, border: i32) -> String {
assert!(border >= 0, "Border must be non-negative");
let mut result = String::new();
result += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
result += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
let dimension = qr
.size()
.checked_add(border.checked_mul(2).unwrap())
.unwrap();
result += &format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 {0} {0}\" stroke=\"none\">\n", dimension);
result += "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
result += "\t<path d=\"";
for y in 0..qr.size() {
for x in 0..qr.size() {
if qr.get_module(x, y) {
if x != 0 || y != 0 {
result += " ";
}
result += &format!("M{},{}h1v1h-1z", x + border, y + border);
}
}
}
result += "\" fill=\"#000000\"/>\n";
result += "</svg>\n";
result
}
Loading