2020-06-20 07:05:39 +02:00
|
|
|
use chrono::prelude::*;
|
2020-01-07 05:22:28 +01:00
|
|
|
use reqwest::blocking::Client;
|
2019-12-10 03:13:25 +01:00
|
|
|
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
|
2020-01-10 20:18:15 +01:00
|
|
|
use reqwest::Url;
|
2019-10-23 00:33:22 +02:00
|
|
|
use std::collections::HashMap;
|
2020-03-08 20:31:42 +01:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2020-01-15 04:26:04 +01:00
|
|
|
use std::io::{self, Error, Write};
|
2020-03-08 20:31:42 +01:00
|
|
|
use std::path::Path;
|
2020-01-15 04:26:04 +01:00
|
|
|
use std::process;
|
2019-12-10 03:13:25 +01:00
|
|
|
use std::time::Duration;
|
2019-08-23 05:17:15 +02:00
|
|
|
|
2020-06-24 09:16:40 +02:00
|
|
|
use monolith::html::{html_to_dom, stringify_document, walk_and_embed_assets};
|
|
|
|
use monolith::url::{data_url_to_data, is_data_url, is_file_url, is_http_url};
|
|
|
|
use monolith::utils::retrieve_asset;
|
|
|
|
|
2020-03-29 09:54:20 +02:00
|
|
|
mod args;
|
|
|
|
mod macros;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
|
|
|
use crate::args::AppArgs;
|
|
|
|
|
2020-01-15 04:26:04 +01:00
|
|
|
enum Output {
|
|
|
|
Stdout(io::Stdout),
|
2020-03-08 20:31:42 +01:00
|
|
|
File(fs::File),
|
2020-01-15 04:26:04 +01:00
|
|
|
}
|
2019-12-26 06:41:03 +01:00
|
|
|
|
2020-01-15 04:26:04 +01:00
|
|
|
impl Output {
|
|
|
|
fn new(file_path: &str) -> Result<Output, Error> {
|
|
|
|
if file_path.is_empty() {
|
|
|
|
Ok(Output::Stdout(io::stdout()))
|
|
|
|
} else {
|
2020-03-08 20:31:42 +01:00
|
|
|
Ok(Output::File(fs::File::create(file_path)?))
|
2020-01-15 04:26:04 +01:00
|
|
|
}
|
2019-12-26 06:41:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 04:26:04 +01:00
|
|
|
fn writeln_str(&mut self, s: &str) -> Result<(), Error> {
|
|
|
|
match self {
|
|
|
|
Output::Stdout(stdout) => {
|
|
|
|
writeln!(stdout, "{}", s)?;
|
|
|
|
stdout.flush()
|
|
|
|
}
|
|
|
|
Output::File(f) => {
|
|
|
|
writeln!(f, "{}", s)?;
|
|
|
|
f.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-26 06:41:03 +01:00
|
|
|
}
|
|
|
|
|
2019-08-23 05:17:15 +02:00
|
|
|
fn main() {
|
2019-10-10 07:28:12 +02:00
|
|
|
let app_args = AppArgs::get();
|
2020-04-02 09:04:21 +02:00
|
|
|
let original_target: &str = &app_args.target;
|
2020-03-08 20:31:42 +01:00
|
|
|
let target_url: &str;
|
2020-02-13 06:56:30 +01:00
|
|
|
let base_url;
|
|
|
|
let dom;
|
2019-12-26 06:41:03 +01:00
|
|
|
|
2020-03-08 20:31:42 +01:00
|
|
|
// Pre-process the input
|
|
|
|
let cwd_normalized: String =
|
|
|
|
str!(env::current_dir().unwrap().to_str().unwrap()).replace("\\", "/");
|
2020-03-29 09:54:20 +02:00
|
|
|
let path = Path::new(original_target);
|
|
|
|
let mut target: String = str!(original_target.clone()).replace("\\", "/");
|
2020-03-08 20:31:42 +01:00
|
|
|
let path_is_relative: bool = path.is_relative();
|
2020-03-29 09:54:20 +02:00
|
|
|
|
|
|
|
if target.clone().len() == 0 {
|
2020-03-08 20:31:42 +01:00
|
|
|
eprintln!("No target specified");
|
2020-01-15 04:26:04 +01:00
|
|
|
process::exit(1);
|
2020-03-29 09:54:20 +02:00
|
|
|
} else if is_http_url(target.clone()) || is_data_url(target.clone()) {
|
|
|
|
target_url = target.as_str();
|
|
|
|
} else if is_file_url(target.clone()) {
|
|
|
|
target_url = target.as_str();
|
2020-03-08 20:31:42 +01:00
|
|
|
} else if path.exists() {
|
|
|
|
if !path.is_file() {
|
|
|
|
eprintln!("Local target is not a file: {}", original_target);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2020-03-29 09:54:20 +02:00
|
|
|
target.insert_str(0, if cfg!(windows) { "file:///" } else { "file://" });
|
2020-03-08 20:31:42 +01:00
|
|
|
if path_is_relative {
|
2020-03-29 09:54:20 +02:00
|
|
|
target.insert_str(if cfg!(windows) { 8 } else { 7 }, &cwd_normalized);
|
|
|
|
target.insert_str(
|
2020-03-08 20:31:42 +01:00
|
|
|
if cfg!(windows) { 8 } else { 7 } + &cwd_normalized.len(),
|
|
|
|
"/",
|
|
|
|
);
|
|
|
|
}
|
2020-03-29 09:54:20 +02:00
|
|
|
target_url = target.as_str();
|
2020-03-08 20:31:42 +01:00
|
|
|
} else {
|
2020-03-29 09:54:20 +02:00
|
|
|
target.insert_str(0, "http://");
|
|
|
|
target_url = target.as_str();
|
2019-12-26 06:41:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 04:26:04 +01:00
|
|
|
let mut output = Output::new(&app_args.output).expect("Could not prepare output");
|
2020-01-21 05:01:22 +01:00
|
|
|
|
|
|
|
// Initialize client
|
2020-01-15 04:26:04 +01:00
|
|
|
let mut cache = HashMap::new();
|
2020-01-21 05:01:22 +01:00
|
|
|
let mut header_map = HeaderMap::new();
|
|
|
|
header_map.insert(
|
|
|
|
USER_AGENT,
|
|
|
|
HeaderValue::from_str(&app_args.user_agent).expect("Invalid User-Agent header specified"),
|
|
|
|
);
|
|
|
|
|
2020-02-03 06:38:21 +01:00
|
|
|
let timeout: u64 = if app_args.timeout > 0 {
|
|
|
|
app_args.timeout
|
|
|
|
} else {
|
|
|
|
std::u64::MAX / 4
|
|
|
|
};
|
2020-01-21 05:01:22 +01:00
|
|
|
let client = Client::builder()
|
2020-02-03 06:38:21 +01:00
|
|
|
.timeout(Duration::from_secs(timeout))
|
2020-01-21 05:01:22 +01:00
|
|
|
.danger_accept_invalid_certs(app_args.insecure)
|
|
|
|
.default_headers(header_map)
|
|
|
|
.build()
|
|
|
|
.expect("Failed to initialize HTTP client");
|
2019-09-22 02:06:00 +02:00
|
|
|
|
2020-01-15 04:26:04 +01:00
|
|
|
// Retrieve root document
|
2020-03-08 20:31:42 +01:00
|
|
|
if is_file_url(target_url) || is_http_url(target_url) {
|
2020-05-02 12:13:28 +02:00
|
|
|
match retrieve_asset(&mut cache, &client, target_url, target_url, app_args.silent) {
|
|
|
|
Ok((data, final_url, _media_type)) => {
|
|
|
|
base_url = final_url;
|
|
|
|
dom = html_to_dom(&String::from_utf8_lossy(&data));
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!("Could not retrieve target document");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2020-02-13 06:56:30 +01:00
|
|
|
} else if is_data_url(target_url) {
|
2020-05-02 12:13:28 +02:00
|
|
|
let (media_type, data): (String, Vec<u8>) = data_url_to_data(target_url);
|
2020-04-10 11:06:07 +02:00
|
|
|
if !media_type.eq_ignore_ascii_case("text/html") {
|
|
|
|
eprintln!("Unsupported data URL media type");
|
2020-02-14 05:46:08 +01:00
|
|
|
process::exit(1);
|
|
|
|
}
|
2020-03-08 20:31:42 +01:00
|
|
|
base_url = str!(target_url);
|
2020-05-02 12:13:28 +02:00
|
|
|
dom = html_to_dom(&String::from_utf8_lossy(&data));
|
2020-02-13 06:56:30 +01:00
|
|
|
} else {
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2019-08-23 05:17:15 +02:00
|
|
|
|
2020-06-20 07:05:39 +02:00
|
|
|
let time_saved = Utc::now();
|
2020-01-09 00:51:18 +01:00
|
|
|
|
2020-01-15 04:26:04 +01:00
|
|
|
walk_and_embed_assets(
|
|
|
|
&mut cache,
|
|
|
|
&client,
|
2020-02-13 06:56:30 +01:00
|
|
|
&base_url,
|
2020-01-15 04:26:04 +01:00
|
|
|
&dom.document,
|
|
|
|
app_args.no_css,
|
2020-04-22 09:37:02 +02:00
|
|
|
app_args.no_fonts,
|
|
|
|
app_args.no_frames,
|
2020-01-15 04:26:04 +01:00
|
|
|
app_args.no_js,
|
|
|
|
app_args.no_images,
|
|
|
|
app_args.silent,
|
|
|
|
);
|
|
|
|
|
2020-04-26 02:59:34 +02:00
|
|
|
let mut html: String = stringify_document(
|
2020-01-15 04:26:04 +01:00
|
|
|
&dom.document,
|
|
|
|
app_args.no_css,
|
|
|
|
app_args.no_frames,
|
|
|
|
app_args.no_js,
|
|
|
|
app_args.no_images,
|
|
|
|
app_args.isolate,
|
|
|
|
);
|
|
|
|
|
2020-04-26 02:59:34 +02:00
|
|
|
if !app_args.no_metadata {
|
|
|
|
// Safe to unwrap (we just put this through an HTTP request)
|
|
|
|
let mut clean_url = Url::parse(&base_url).unwrap();
|
|
|
|
clean_url.set_fragment(None);
|
2020-06-01 11:28:02 +02:00
|
|
|
// Prevent credentials from getting into metadata
|
|
|
|
if is_http_url(&base_url) {
|
|
|
|
// Only HTTP(S) URLs may feature credentials
|
|
|
|
clean_url.set_username("").unwrap();
|
|
|
|
clean_url.set_password(None).unwrap();
|
|
|
|
}
|
2020-06-20 07:05:39 +02:00
|
|
|
let timestamp = time_saved.to_rfc3339_opts(SecondsFormat::Secs, true);
|
2020-04-26 02:59:34 +02:00
|
|
|
let metadata_comment = if is_http_url(&base_url) {
|
|
|
|
format!(
|
2020-05-01 02:23:09 +02:00
|
|
|
"<!-- Saved from {} at {} using {} v{} -->\n",
|
2020-04-26 02:59:34 +02:00
|
|
|
&clean_url,
|
2020-06-20 07:05:39 +02:00
|
|
|
timestamp,
|
2020-04-26 02:59:34 +02:00
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
)
|
2019-12-26 06:41:03 +01:00
|
|
|
} else {
|
2020-04-26 02:59:34 +02:00
|
|
|
format!(
|
2020-05-01 02:23:09 +02:00
|
|
|
"<!-- Saved from local source at {} using {} v{} -->\n",
|
2020-06-20 07:05:39 +02:00
|
|
|
timestamp,
|
2020-04-26 02:59:34 +02:00
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
html.insert_str(0, &metadata_comment);
|
2019-08-23 05:17:15 +02:00
|
|
|
}
|
2020-04-26 02:59:34 +02:00
|
|
|
|
2020-01-15 04:26:04 +01:00
|
|
|
output
|
|
|
|
.writeln_str(&html)
|
|
|
|
.expect("Could not write HTML output");
|
2019-08-23 05:17:15 +02:00
|
|
|
}
|