From 3f6b85c1b9ecc86a99cd5e35b9e5f0ccf0abff1a Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Mon, 19 Feb 2024 21:45:25 +1030 Subject: [PATCH 01/11] added serve in doc load conditional --- src/cli/rustup_mode.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index e7682d1be1..da22fbdc24 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1608,6 +1608,16 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let doc_path = toolchain.doc_path(doc_url)?; writeln!(process().stdout().lock(), "{}", doc_path.display())?; Ok(utils::ExitCode(0)) + } else if m.get_flag("serve") { + //TODO: implement serve + const PORT: u16 = 0; + writeln!( + process().stdout().lock(), + "Serving documentation at {} on {}", + doc_url, + PORT + )?; + Ok(utils::ExitCode(0)) } else { toolchain.open_docs(doc_url)?; Ok(utils::ExitCode(0)) From 046ee4754d234510dc74fea0e09b2b3fa57469a1 Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Mon, 19 Feb 2024 22:54:09 +1030 Subject: [PATCH 02/11] added tokio and axum for fn doc to serve on localhost --- Cargo.lock | 97 +++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 2 + src/cli/rustup_mode.rs | 29 ++++++++++--- 3 files changed, 116 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 26041a54b5..f676539a1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,7 +135,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", - "axum-core", + "axum-core 0.3.4", "bitflags 1.3.2", "bytes", "futures-util", @@ -156,6 +156,40 @@ dependencies = [ "tower-service", ] +[[package]] +name = "axum" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1236b4b292f6c4d6dc34604bb5120d85c3fe1d1aa596bd5cc52ca054d13e7b9e" +dependencies = [ + "async-trait", + "axum-core 0.4.3", + "bytes", + "futures-util", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.1.0", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "axum-core" version = "0.3.4" @@ -173,6 +207,27 @@ dependencies = [ "tower-service", ] +[[package]] +name = "axum-core" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "backtrace" version = "0.3.69" @@ -811,6 +866,25 @@ dependencies = [ "tracing", ] +[[package]] +name = "h2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31d030e59af851932b72ceebadf4a2b5986dba4c3b99dd2493f8273a0f151943" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 1.0.0", + "indexmap 2.2.3", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -932,7 +1006,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", + "h2 0.3.24", "http 0.2.11", "http-body 0.4.6", "httparse", @@ -955,6 +1029,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", + "h2 0.4.2", "http 1.0.0", "http-body 1.0.0", "httparse", @@ -1735,7 +1810,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2", + "h2 0.3.24", "http 0.2.11", "http-body 0.4.6", "hyper 0.14.28", @@ -1870,6 +1945,7 @@ name = "rustup" version = "1.27.0" dependencies = [ "anyhow", + "axum 0.7.4", "cc", "cfg-if 1.0.0", "chrono", @@ -2055,6 +2131,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebd154a240de39fdebcf5775d2675c204d7c13cf39a4c697be6493c8e734337c" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_spanned" version = "0.6.5" @@ -2504,12 +2590,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3082666a3a6433f7f511c7192923fa1fe07c69332d3c6a2e6bb040b569199d5a" dependencies = [ "async-trait", - "axum", + "axum 0.6.20", "base64", "bytes", "futures-core", "futures-util", - "h2", + "h2 0.3.24", "http 0.2.11", "http-body 0.4.6", "hyper 0.14.28", @@ -2563,6 +2649,7 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", diff --git a/Cargo.toml b/Cargo.toml index f5a6d1fe42..abcb9cc368 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,6 +95,8 @@ wait-timeout = "0.2" walkdir = { workspace = true, optional = true } xz2 = "0.1.3" zstd = "0.13" +tokio = { version = "0.2.22", features = ["full"] } +axum = "0.7.4" [target."cfg(windows)".dependencies] cc = "1" diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index da22fbdc24..d5030eed04 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use anyhow::{anyhow, Error, Result}; +use axum::http::Request; use clap::{ builder::{EnumValueParser, PossibleValue, PossibleValuesParser}, Arg, ArgAction, ArgGroup, ArgMatches, Command, ValueEnum, @@ -11,6 +12,10 @@ use clap::{ use clap_complete::Shell; use itertools::Itertools; +use axum::{routing::get, Router}; +use tokio::main; +use tokio::net::TcpListener; + use crate::{ cli::{ common::{self, PackageUpdate}, @@ -45,6 +50,7 @@ use crate::{ utils::utils, Cfg, Notification, }; +use axum::Router; const TOOLCHAIN_OVERRIDE_ERROR: &str = "To override the toolchain using the 'rustup +toolchain' syntax, \ @@ -717,6 +723,13 @@ pub(crate) fn cli() -> Command { .help("Only print the path to the documentation") .action(ArgAction::SetTrue), ) + .arg( + //TODO: Test this with `rustup doc --serve` + Arg::new("serve") + .help("serve doc over HTTP") + .long("serve") + .num_args(1) + ) .arg( Arg::new("toolchain") .help(OFFICIAL_TOOLCHAIN_ARG_HELP) @@ -1562,6 +1575,7 @@ const DOCS_DATA: &[(&str, &str, &str)] = &[ ("embedded-book", "The Embedded Rust Book", "embedded-book/index.html"), ]; +#[tokio::main] fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?; @@ -1610,13 +1624,14 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { Ok(utils::ExitCode(0)) } else if m.get_flag("serve") { //TODO: implement serve - const PORT: u16 = 0; - writeln!( - process().stdout().lock(), - "Serving documentation at {} on {}", - doc_url, - PORT - )?; + let app = Router::new().route( + "/", + get(|| async { + doc_url; + }), + ); + let listener = tokio::net::TcpListener::bind("127.0.0.1.0"); + axum::serve(listener, app).await.unwrap(); Ok(utils::ExitCode(0)) } else { toolchain.open_docs(doc_url)?; From 8bac84176f37c1c13fae10b35625cf6300662063 Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Tue, 20 Feb 2024 23:19:24 +1030 Subject: [PATCH 03/11] added tiny url, serve flag on doc command added --- Cargo.lock | 123 +++++++++++------------------------------ Cargo.toml | 5 +- src/cli/rustup_mode.rs | 33 +++++------ 3 files changed, 50 insertions(+), 111 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f676539a1e..8e5596401d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,12 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "async-compression" version = "0.4.6" @@ -135,7 +141,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", - "axum-core 0.3.4", + "axum-core", "bitflags 1.3.2", "bytes", "futures-util", @@ -156,40 +162,6 @@ dependencies = [ "tower-service", ] -[[package]] -name = "axum" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1236b4b292f6c4d6dc34604bb5120d85c3fe1d1aa596bd5cc52ca054d13e7b9e" -dependencies = [ - "async-trait", - "axum-core 0.4.3", - "bytes", - "futures-util", - "http 1.0.0", - "http-body 1.0.0", - "http-body-util", - "hyper 1.1.0", - "hyper-util", - "itoa", - "matchit", - "memchr", - "mime", - "percent-encoding", - "pin-project-lite", - "rustversion", - "serde", - "serde_json", - "serde_path_to_error", - "serde_urlencoded", - "sync_wrapper", - "tokio", - "tower", - "tower-layer", - "tower-service", - "tracing", -] - [[package]] name = "axum-core" version = "0.3.4" @@ -207,27 +179,6 @@ dependencies = [ "tower-service", ] -[[package]] -name = "axum-core" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" -dependencies = [ - "async-trait", - "bytes", - "futures-util", - "http 1.0.0", - "http-body 1.0.0", - "http-body-util", - "mime", - "pin-project-lite", - "rustversion", - "sync_wrapper", - "tower-layer", - "tower-service", - "tracing", -] - [[package]] name = "backtrace" version = "0.3.69" @@ -339,6 +290,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + [[package]] name = "clap" version = "4.5.0" @@ -866,25 +823,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "h2" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31d030e59af851932b72ceebadf4a2b5986dba4c3b99dd2493f8273a0f151943" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http 1.0.0", - "indexmap 2.2.3", - "slab", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -1006,7 +944,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.3.24", + "h2", "http 0.2.11", "http-body 0.4.6", "httparse", @@ -1029,7 +967,6 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.2", "http 1.0.0", "http-body 1.0.0", "httparse", @@ -1810,7 +1747,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.3.24", + "h2", "http 0.2.11", "http-body 0.4.6", "hyper 0.14.28", @@ -1945,7 +1882,6 @@ name = "rustup" version = "1.27.0" dependencies = [ "anyhow", - "axum 0.7.4", "cc", "cfg-if 1.0.0", "chrono", @@ -1988,6 +1924,7 @@ dependencies = [ "termcolor", "thiserror", "threadpool", + "tiny_http", "tokio", "toml", "tracing", @@ -2131,16 +2068,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_path_to_error" -version = "0.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebd154a240de39fdebcf5775d2675c204d7c13cf39a4c697be6493c8e734337c" -dependencies = [ - "itoa", - "serde", -] - [[package]] name = "serde_spanned" version = "0.6.5" @@ -2439,6 +2366,19 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny_http" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0d6ef4e10d23c1efb862eecad25c5054429a71958b4eeef85eb5e7170b477ca" +dependencies = [ + "ascii", + "chunked_transfer", + "log", + "time", + "url", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2590,12 +2530,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3082666a3a6433f7f511c7192923fa1fe07c69332d3c6a2e6bb040b569199d5a" dependencies = [ "async-trait", - "axum 0.6.20", + "axum", "base64", "bytes", "futures-core", "futures-util", - "h2 0.3.24", + "h2", "http 0.2.11", "http-body 0.4.6", "hyper 0.14.28", @@ -2649,7 +2589,6 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ - "log", "pin-project-lite", "tracing-attributes", "tracing-core", diff --git a/Cargo.toml b/Cargo.toml index abcb9cc368..7a6aa2694e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,13 +12,13 @@ build = "build.rs" [features] curl-backend = ["download/curl-backend"] + default = [ "curl-backend", "reqwest-backend", "reqwest-default-tls", "reqwest-rustls-tls", ] - reqwest-backend = ["download/reqwest-backend"] vendored-openssl = ['openssl/vendored'] @@ -86,6 +86,7 @@ termcolor.workspace = true thiserror.workspace = true threadpool = "1" tokio = { workspace = true, optional = true } +tiny_http = "0.11" toml = "0.8" tracing-opentelemetry = { workspace = true, optional = true } tracing-subscriber = { workspace = true, optional = true, features = ["env-filter"] } @@ -95,8 +96,6 @@ wait-timeout = "0.2" walkdir = { workspace = true, optional = true } xz2 = "0.1.3" zstd = "0.13" -tokio = { version = "0.2.22", features = ["full"] } -axum = "0.7.4" [target."cfg(windows)".dependencies] cc = "1" diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index d5030eed04..3b9e87d5da 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1,10 +1,10 @@ use std::fmt; +use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; use std::str::FromStr; use anyhow::{anyhow, Error, Result}; -use axum::http::Request; use clap::{ builder::{EnumValueParser, PossibleValue, PossibleValuesParser}, Arg, ArgAction, ArgGroup, ArgMatches, Command, ValueEnum, @@ -12,9 +12,7 @@ use clap::{ use clap_complete::Shell; use itertools::Itertools; -use axum::{routing::get, Router}; -use tokio::main; -use tokio::net::TcpListener; +use ::tiny_http::{Response, Server, StatusCode}; use crate::{ cli::{ @@ -50,7 +48,6 @@ use crate::{ utils::utils, Cfg, Notification, }; -use axum::Router; const TOOLCHAIN_OVERRIDE_ERROR: &str = "To override the toolchain using the 'rustup +toolchain' syntax, \ @@ -728,7 +725,6 @@ pub(crate) fn cli() -> Command { Arg::new("serve") .help("serve doc over HTTP") .long("serve") - .num_args(1) ) .arg( Arg::new("toolchain") @@ -1575,7 +1571,6 @@ const DOCS_DATA: &[(&str, &str, &str)] = &[ ("embedded-book", "The Embedded Rust Book", "embedded-book/index.html"), ]; -#[tokio::main] fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?; @@ -1624,15 +1619,21 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { Ok(utils::ExitCode(0)) } else if m.get_flag("serve") { //TODO: implement serve - let app = Router::new().route( - "/", - get(|| async { - doc_url; - }), - ); - let listener = tokio::net::TcpListener::bind("127.0.0.1.0"); - axum::serve(listener, app).await.unwrap(); - Ok(utils::ExitCode(0)) + let server = Server::http("0.0.0.0:8000").unwrap(); + loop { + for request in server.incoming_requests() { + println!( + "received request! method: {:?}, url: {:?}, headers: {:?}", + request.method(), + request.url(), + request.headers() + ); + let response = tiny_http::Response::from_string(doc_url); + let _ = request.respond(response); + } + } + + //Ok(utils::ExitCode(0)) } else { toolchain.open_docs(doc_url)?; Ok(utils::ExitCode(0)) From ec74a12351d85d2d7a554cd0f57fc6eade4cefaf Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Fri, 23 Feb 2024 22:03:18 +1030 Subject: [PATCH 04/11] got subcommand out of docs for serve --- src/cli/rustup_mode.rs | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 3b9e87d5da..32085391de 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1,5 +1,4 @@ use std::fmt; -use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; use std::str::FromStr; @@ -12,8 +11,6 @@ use clap::{ use clap_complete::Shell; use itertools::Itertools; -use ::tiny_http::{Response, Server, StatusCode}; - use crate::{ cli::{ common::{self, PackageUpdate}, @@ -720,12 +717,6 @@ pub(crate) fn cli() -> Command { .help("Only print the path to the documentation") .action(ArgAction::SetTrue), ) - .arg( - //TODO: Test this with `rustup doc --serve` - Arg::new("serve") - .help("serve doc over HTTP") - .long("serve") - ) .arg( Arg::new("toolchain") .help(OFFICIAL_TOOLCHAIN_ARG_HELP) @@ -734,6 +725,9 @@ pub(crate) fn cli() -> Command { .value_parser(partial_toolchain_desc_parser), ) .arg(Arg::new("topic").help(TOPIC_ARG_HELP)) + .subcommand( + Command::new("servedoc") + ) .group( ArgGroup::new("page").args( DOCS_DATA @@ -747,7 +741,7 @@ pub(crate) fn cli() -> Command { .iter() .map(|&(name, help_msg, _)| Arg::new(name).long(name).help(help_msg).action(ArgAction::SetTrue)) .collect::>(), - ), + ) ); if cfg!(not(target_os = "windows")) { @@ -1602,6 +1596,10 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { } }; + //get servedoc argument out of m + let servedoc = m.subcommand(); + println!("servedoc: {:?}", servedoc); + let topical_path: PathBuf; let doc_url = if let Some(topic) = m.get_one::("topic") { @@ -1617,23 +1615,6 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let doc_path = toolchain.doc_path(doc_url)?; writeln!(process().stdout().lock(), "{}", doc_path.display())?; Ok(utils::ExitCode(0)) - } else if m.get_flag("serve") { - //TODO: implement serve - let server = Server::http("0.0.0.0:8000").unwrap(); - loop { - for request in server.incoming_requests() { - println!( - "received request! method: {:?}, url: {:?}, headers: {:?}", - request.method(), - request.url(), - request.headers() - ); - let response = tiny_http::Response::from_string(doc_url); - let _ = request.respond(response); - } - } - - //Ok(utils::ExitCode(0)) } else { toolchain.open_docs(doc_url)?; Ok(utils::ExitCode(0)) From c18c1650b8f6a3b4c56772f7c6910a252616f57c Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Sun, 25 Feb 2024 20:15:36 +1030 Subject: [PATCH 05/11] server running, using tiny http --- src/cli/rustup_mode.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 32085391de..3cc2d81a5f 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -10,6 +10,7 @@ use clap::{ }; use clap_complete::Shell; use itertools::Itertools; +use tiny_http::{Response, Server}; use crate::{ cli::{ @@ -1597,9 +1598,25 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { }; //get servedoc argument out of m - let servedoc = m.subcommand(); - println!("servedoc: {:?}", servedoc); + if let Some(servedoc) = m.subcommand_matches("servedoc") { + println!("servedoc: {:?}", servedoc); + + println!("hehe"); + loop { + let server = Server::http("127.0.0.1:3000").unwrap(); + for request in server.incoming_requests() { + println!( + "received request! method: {:?}, url: {:?}, headers: {:?}", + request.method(), + request.url(), + request.headers() + ); + let response = Response::from_string("hello world"); + request.respond(response); + } + } + } let topical_path: PathBuf; let doc_url = if let Some(topic) = m.get_one::("topic") { From 1b2bd367139ba192b25b6ce38863ed8130ac30ac Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Sun, 25 Feb 2024 20:35:06 +1030 Subject: [PATCH 06/11] server interupt doc opening on servedoc subcommand --- src/cli/rustup_mode.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 3cc2d81a5f..358eb34df0 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1597,26 +1597,6 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { } }; - //get servedoc argument out of m - if let Some(servedoc) = m.subcommand_matches("servedoc") { - println!("servedoc: {:?}", servedoc); - - println!("hehe"); - loop { - let server = Server::http("127.0.0.1:3000").unwrap(); - for request in server.incoming_requests() { - println!( - "received request! method: {:?}, url: {:?}, headers: {:?}", - request.method(), - request.url(), - request.headers() - ); - - let response = Response::from_string("hello world"); - request.respond(response); - } - } - } let topical_path: PathBuf; let doc_url = if let Some(topic) = m.get_one::("topic") { @@ -1632,6 +1612,22 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let doc_path = toolchain.doc_path(doc_url)?; writeln!(process().stdout().lock(), "{}", doc_path.display())?; Ok(utils::ExitCode(0)) + } else if m.subcommand_matches("servedoc").is_some() { + loop { + let server = Server::http("127.0.0.1:3000").unwrap(); + for request in server.incoming_requests() { + println!( + "received request! method: {:?}, url: {:?}, headers: {:?}", + request.method(), + request.url(), + request.headers() + ); + + let response = Response::from_string(doc_url); + request.respond(response); + } + } + Ok(utils::ExitCode(0)) } else { toolchain.open_docs(doc_url)?; Ok(utils::ExitCode(0)) From 77b4769525afd81ac4b626072019981a16a68985 Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Sun, 25 Feb 2024 21:20:39 +1030 Subject: [PATCH 07/11] docs are served on server, navigation is broken --- src/cli/rustup_mode.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 358eb34df0..2f8bfad6d3 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1613,9 +1613,13 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { writeln!(process().stdout().lock(), "{}", doc_path.display())?; Ok(utils::ExitCode(0)) } else if m.subcommand_matches("servedoc").is_some() { + let doc_path = toolchain.doc_path(doc_url)?; + let doc_path_str = doc_path.to_string_lossy().into_owned(); + println!("serving doc from {}", doc_path.display()); loop { let server = Server::http("127.0.0.1:3000").unwrap(); for request in server.incoming_requests() { + let file = std::fs::File::open(&doc_path_str).unwrap(); println!( "received request! method: {:?}, url: {:?}, headers: {:?}", request.method(), @@ -1623,7 +1627,7 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { request.headers() ); - let response = Response::from_string(doc_url); + let response = Response::from_file(file); request.respond(response); } } From ff3e3e7c3d31d9c41b23c6b953bec087a805ed26 Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Mon, 26 Feb 2024 18:47:30 +1030 Subject: [PATCH 08/11] path manipulation --- src/cli/rustup_mode.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 2f8bfad6d3..139e6f9fa1 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1613,13 +1613,24 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { writeln!(process().stdout().lock(), "{}", doc_path.display())?; Ok(utils::ExitCode(0)) } else if m.subcommand_matches("servedoc").is_some() { - let doc_path = toolchain.doc_path(doc_url)?; - let doc_path_str = doc_path.to_string_lossy().into_owned(); - println!("serving doc from {}", doc_path.display()); + let doc_path_index = toolchain.doc_path(doc_url)?; + let doc_path_base = toolchain.doc_path("")?; + let doc_path_str = doc_path_base.to_string_lossy().into_owned(); + println!("serving doc from {}", doc_path_base.display()); loop { let server = Server::http("127.0.0.1:3000").unwrap(); for request in server.incoming_requests() { - let file = std::fs::File::open(&doc_path_str).unwrap(); + //TODO get request path and serve filebased on that + let request_path = request.url(); + println!("request path: {}", request_path); + let mut response_path = + doc_path_base.clone().to_string_lossy().into_owned() + request_path; + if request_path == "/" { + response_path = doc_path_index.clone().to_string_lossy().into_owned(); + } + let request_path_from_doc = doc_path_str.clone() + request_path; + println!("request path: {}", request_path_from_doc); + let file = std::fs::File::open(&response_path).unwrap(); println!( "received request! method: {:?}, url: {:?}, headers: {:?}", request.method(), From 1a4cfe65ecc5a8be5c8397528380889084e90735 Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Thu, 7 Mar 2024 06:48:01 +1030 Subject: [PATCH 09/11] http doc path WIP --- src/cli/rustup_mode.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 139e6f9fa1..5be6e60849 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1616,33 +1616,27 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let doc_path_index = toolchain.doc_path(doc_url)?; let doc_path_base = toolchain.doc_path("")?; let doc_path_str = doc_path_base.to_string_lossy().into_owned(); - println!("serving doc from {}", doc_path_base.display()); loop { let server = Server::http("127.0.0.1:3000").unwrap(); for request in server.incoming_requests() { //TODO get request path and serve filebased on that - let request_path = request.url(); - println!("request path: {}", request_path); + let request_path = request.url().strip_prefix('/'); + println!("request path: {:?}", request_path); let mut response_path = doc_path_base.clone().to_string_lossy().into_owned() + request_path; - if request_path == "/" { + println!("response path: {}", response_path); + if request_path == Option::from("/") { response_path = doc_path_index.clone().to_string_lossy().into_owned(); } - let request_path_from_doc = doc_path_str.clone() + request_path; + let request_path_from_doc = doc_path_str.clone() + request_path.strip_prefix('/'); println!("request path: {}", request_path_from_doc); let file = std::fs::File::open(&response_path).unwrap(); - println!( - "received request! method: {:?}, url: {:?}, headers: {:?}", - request.method(), - request.url(), - request.headers() - ); + println!("received request! url: {:?}", request.url(),); let response = Response::from_file(file); - request.respond(response); + request.respond(response).unwrap(); } } - Ok(utils::ExitCode(0)) } else { toolchain.open_docs(doc_url)?; Ok(utils::ExitCode(0)) From 468bcafdb8915f29e0e33e3659a1f0c896beb801 Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Fri, 8 Mar 2024 22:29:10 +1030 Subject: [PATCH 10/11] tiny server running docs on servedoc --- src/cli/rustup_mode.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 5be6e60849..9377088715 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1621,20 +1621,28 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { for request in server.incoming_requests() { //TODO get request path and serve filebased on that let request_path = request.url().strip_prefix('/'); - println!("request path: {:?}", request_path); - let mut response_path = - doc_path_base.clone().to_string_lossy().into_owned() + request_path; - println!("response path: {}", response_path); - if request_path == Option::from("/") { - response_path = doc_path_index.clone().to_string_lossy().into_owned(); + match request_path { + Some(mut request_path) => { + println!("doc url: {:?}", &doc_url); + println!("Req file: {:?}", &request_path); + let base_string = doc_path_str.clone(); + if request_path == "" { + request_path = doc_url; + } + //strip search params + if let Some(index) = request_path.find('?') { + request_path = &request_path[..index]; + } + let path = String::from(base_string + &request_path); + println!("Serving file: {:?}", &path); + let file = std::fs::File::open(path).unwrap(); + request.respond(Response::from_file(file)).unwrap(); + } + None => { + let file = std::fs::File::open(&doc_path_index).unwrap(); + request.respond(Response::from_file(file)).unwrap(); + } } - let request_path_from_doc = doc_path_str.clone() + request_path.strip_prefix('/'); - println!("request path: {}", request_path_from_doc); - let file = std::fs::File::open(&response_path).unwrap(); - println!("received request! url: {:?}", request.url(),); - - let response = Response::from_file(file); - request.respond(response).unwrap(); } } } else { From aab283816eed77cbcb56e726f3cef004f92f2438 Mon Sep 17 00:00:00 2001 From: Brett Earle Date: Fri, 8 Mar 2024 22:49:13 +1030 Subject: [PATCH 11/11] working when ignoring favicon and search params for fontAwesome --- src/cli/rustup_mode.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 9377088715..d862ba715a 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1617,7 +1617,8 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let doc_path_base = toolchain.doc_path("")?; let doc_path_str = doc_path_base.to_string_lossy().into_owned(); loop { - let server = Server::http("127.0.0.1:3000").unwrap(); + let server = Server::http("127.0.0.1:0").unwrap(); + println!("Serving documentation at {}", server.server_addr()); for request in server.incoming_requests() { //TODO get request path and serve filebased on that let request_path = request.url().strip_prefix('/'); @@ -1633,6 +1634,10 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { if let Some(index) = request_path.find('?') { request_path = &request_path[..index]; } + //ignore favicon requests + if request_path == "favicon.ico" { + continue; + } let path = String::from(base_string + &request_path); println!("Serving file: {:?}", &path); let file = std::fs::File::open(path).unwrap();