diff --git a/Cargo.lock b/Cargo.lock index dbcc19e..0205d3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,12 +23,24 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + [[package]] name = "bumpalo" version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +[[package]] +name = "bytes" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" + [[package]] name = "case_insensitive_hashmap" version = "0.9.1" @@ -53,6 +65,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "chrono" version = "0.4.31" @@ -64,7 +82,7 @@ dependencies = [ "js-sys", "num-traits", "wasm-bindgen", - "windows-targets", + "windows-targets 0.48.5", ] [[package]] @@ -73,6 +91,104 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "core_affinity" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622892f5635ce1fc38c8f16dfc938553ed64af482edb5e150bf4caedbfcb2304" +dependencies = [ + "libc", + "num_cpus", + "winapi", +] + +[[package]] +name = "crossbeam" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "generator" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb949699c3e4df3a183b1d2142cb24277057055ed23c68ed58894f76c517223" +dependencies = [ + "cfg-if", + "libc", + "log", + "rustversion", + "windows", +] + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "httparse" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "iana-time-zone" version = "0.1.58" @@ -84,7 +200,7 @@ dependencies = [ "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "windows-core", + "windows-core 0.51.1", ] [[package]] @@ -96,6 +212,12 @@ dependencies = [ "cc", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "js-sys" version = "0.3.66" @@ -105,11 +227,27 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" -version = "0.2.151" +version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] [[package]] name = "log" @@ -117,6 +255,75 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "may" +version = "0.3.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf0655dc540a093eca6ecd7566dbabe9bb476cbfff87733dd1a3f0ac2e10b50" +dependencies = [ + "core_affinity", + "crossbeam", + "generator", + "lazy_static", + "libc", + "log", + "may_queue", + "nix", + "num_cpus", + "parking_lot", + "rustversion", + "smallvec", + "socket2", + "windows-sys", +] + +[[package]] +name = "may_minihttp" +version = "0.1.9" +source = "git+https://github.com/Xudong-Huang/may_minihttp?rev=e35c93cce95fbbb143c5ec3cb26ca1e0cc9308a7#e35c93cce95fbbb143c5ec3cb26ca1e0cc9308a7" +dependencies = [ + "bytes", + "httparse", + "httpdate", + "itoa", + "log", + "may", + "once_cell", +] + +[[package]] +name = "may_queue" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7026ba39000f40c16ba8ea206967380471726dd26fc34f51491b47fba9d84a94" +dependencies = [ + "crossbeam-utils", + "rustversion", + "smallvec", +] + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", + "memoffset", +] + [[package]] name = "num-traits" version = "0.2.17" @@ -126,12 +333,45 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -160,11 +400,51 @@ dependencies = [ name = "realfs" version = "0.1.0" dependencies = [ + "bytes", "case_insensitive_hashmap", "chrono", + "may", + "may_minihttp", "percent-encoding", ] +[[package]] +name = "redox_syscall" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "syn" version = "2.0.43" @@ -251,13 +531,108 @@ version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core 0.58.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows-core" version = "0.51.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result", + "windows-strings", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -266,13 +641,29 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -281,38 +672,86 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 62717c3..eee0a8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,7 @@ edition = "2021" chrono = "0.4.31" case_insensitive_hashmap = "0.9.0" percent-encoding = "2.3.1" +# may_minihttp = "0.1.9" +may_minihttp = { git = "https://github.com/Xudong-Huang/may_minihttp", rev = "e35c93cce95fbbb143c5ec3cb26ca1e0cc9308a7"} +bytes = "1.7.1" +may = "0.3.46" diff --git a/src/handlers/errors.rs b/src/handlers/errors.rs index ab100e4..eb2ef1a 100644 --- a/src/handlers/errors.rs +++ b/src/handlers/errors.rs @@ -4,6 +4,7 @@ pub const IS_DIR: &str = "Is a dir"; pub const NO_INPUT: &str = "No input"; pub const OUT_OF_FS: &str = "Out of scope"; pub const WRITE_ERROR: &str = "Write error"; +pub const TRUNCATE_ERROR: &str = "File truncate error"; pub const OPEN_ERROR: &str = "Open error"; pub const CREATION_ERROR: &str = "File didn't exist, creation caused errors"; diff --git a/src/handlers/list.rs b/src/handlers/list.rs index 3785d75..38b3412 100644 --- a/src/handlers/list.rs +++ b/src/handlers/list.rs @@ -1,4 +1,8 @@ +use std::io::Write; + +use bytes::BufMut; use case_insensitive_hashmap::CaseInsensitiveHashMap; +use may_minihttp::{Request, Response}; use percent_encoding::percent_decode_str; use crate::utils_lib::index_of; @@ -6,26 +10,18 @@ use crate::utils_lib::index_of; pub static BASE: &str = "/api/directory"; pub static METHOD: &str = "GET"; pub fn handle_list( - incoming_req: crate::http_lib::RequestDataToSet, + incoming_req: Request, mount_point: &str, -) -> crate::http_lib::ResponseDataToSet { - let mut method = "GET".to_string(); - let mut path = "/".to_string(); - crate::http_lib::extract_path_and_method( - incoming_req.method_and_path.as_str(), - &mut path, - &mut method, - ); + response: &mut Response, +) -> std::io::Result<()> { + let mut method = incoming_req.method(); + let mut path = incoming_req.path(); + response.header("Access-Control-Allow-Origin: *"); + let search_pos_opt = index_of(&path, "?"); if search_pos_opt.is_none() { - return crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: CaseInsensitiveHashMap::new(), - data: "wrong".as_bytes().to_vec(), - }, - code: 500, - msg: "Server error".to_string(), - }; + response.status_code(500, "Server error"); + return Ok(()); } let search_pos = search_pos_opt.unwrap(); let search = &path[search_pos..]; @@ -43,7 +39,7 @@ pub fn handle_list( literal_path = percent_decode_str(value).decode_utf8().unwrap().to_string(); } let mut error_reason = crate::handlers::errors::OK; - let mut final_data = [].to_vec(); + // let mut final_data = [].to_vec(); if !literal_path.is_empty() { if literal_path.starts_with('.') { // idk if there is any side effect of this... @@ -58,7 +54,22 @@ pub fn handle_list( } else if !super::is_path_within_mount_point(joined_path_buf, mount_point) { error_reason = crate::handlers::errors::OUT_OF_FS; } else { - final_data.extend( + // final_data.extend( + // std::fs::read_dir(joined_path_buf) + // .unwrap() + // .filter_map(|entry| { + // entry.ok().and_then(|e| { + // let path = e.path(); + // path.file_name().and_then(|n| n.to_str().map(String::from)) + // }) + // }) + // .collect::>() + // .join("\n") + // .as_bytes() + // .to_vec(), + // ); + let mut w = response.body_mut().writer(); + w.write_all( std::fs::read_dir(joined_path_buf) .unwrap() .filter_map(|entry| { @@ -69,9 +80,9 @@ pub fn handle_list( }) .collect::>() .join("\n") - .as_bytes() - .to_vec(), + .as_bytes(), ); + return Ok(()); } } else { error_reason = crate::handlers::errors::GENERAL_FAILURE; @@ -79,16 +90,6 @@ pub fn handle_list( } else { error_reason = crate::handlers::errors::NO_INPUT; } - crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: super::all_origins(), // very unsafe, TODO - data: final_data, - }, - code: if error_reason == crate::handlers::errors::OK { - 200 - } else { - 400 - }, - msg: error_reason.to_string(), - } + response.status_code(400, error_reason); + Ok(()) } diff --git a/src/handlers/read.rs b/src/handlers/read.rs index 4b7e2c0..192bf58 100644 --- a/src/handlers/read.rs +++ b/src/handlers/read.rs @@ -1,31 +1,34 @@ +use bytes::BufMut; use case_insensitive_hashmap::CaseInsensitiveHashMap; +use may_minihttp::{Request, Response}; use percent_encoding::percent_decode_str; +use std::io::{Read, Write}; use crate::utils_lib::index_of; pub static BASE: &str = "/api/file"; pub static METHOD: &str = "GET"; pub fn handle_read( - incoming_req: crate::http_lib::RequestDataToSet, + incoming_req: Request, mount_point: &str, -) -> crate::http_lib::ResponseDataToSet { - let mut method = "GET".to_string(); - let mut path = "/".to_string(); - crate::http_lib::extract_path_and_method( - incoming_req.method_and_path.as_str(), - &mut path, - &mut method, - ); + response: &mut Response, +) -> std::io::Result<()> { + let mut method = incoming_req.method(); + let mut path = incoming_req.path(); + response.header("Access-Control-Allow-Origin: *"); + let search_pos_opt = index_of(&path, "?"); if search_pos_opt.is_none() { - return crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: CaseInsensitiveHashMap::new(), - data: "wrong".as_bytes().to_vec(), - }, - code: 500, - msg: "Server error".to_string(), - }; + response.status_code(500, "Server error"); + return Ok(()); + // return crate::http_lib::ResponseDataToSet { + // base: crate::http_lib::BasicHTTPDataToSet { + // headers: CaseInsensitiveHashMap::new(), + // data: "wrong".as_bytes().to_vec(), + // }, + // code: 500, + // msg: "Server error".to_string(), + // }; } let search_pos = search_pos_opt.unwrap(); let search = &path[search_pos..]; @@ -43,7 +46,7 @@ pub fn handle_read( literal_path = percent_decode_str(value).decode_utf8().unwrap().to_string(); } let mut error_reason = crate::handlers::errors::OK; - let mut final_data = [].to_vec(); + // let mut final_data = [].to_vec(); if !literal_path.is_empty() { if literal_path.starts_with('.') { // idk if there is any side effect of this... @@ -58,7 +61,21 @@ pub fn handle_read( } else if !super::is_path_within_mount_point(joined_path_buf, mount_point) { error_reason = crate::handlers::errors::OUT_OF_FS; } else { - final_data.extend(std::fs::read(joined_path_buf).unwrap().to_vec()); + // final_data.extend(std::fs::read(joined_path_buf).unwrap().to_vec()); + // let w = response.body_mut(); + let mut file = std::fs::File::open(joined_path_buf)?; + // let _n = file.read_to_end(w.to_vec().as_mut())?; + let mut w = response.body_mut().writer(); + let mut chunk = [0; 1024]; + loop { + match file.read(&mut chunk)? { + 0 => break, // End of file + n => { + w.write_all(&chunk[..n])?; + } + } + } + return Ok(()); } } else { error_reason = crate::handlers::errors::GENERAL_FAILURE; @@ -66,16 +83,18 @@ pub fn handle_read( } else { error_reason = crate::handlers::errors::NO_INPUT; } - crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: super::all_origins(), // very unsafe, TODO - data: final_data, - }, - code: if error_reason == crate::handlers::errors::OK { - 200 - } else { - 400 - }, - msg: error_reason.to_string(), - } + response.status_code(400, error_reason); + Ok(()) + // crate::http_lib::ResponseDataToSet { + // base: crate::http_lib::BasicHTTPDataToSet { + // headers: super::all_origins(), // very unsafe, TODO + // data: final_data, + // }, + // code: if error_reason == crate::handlers::errors::OK { + // 200 + // } else { + // 400 + // }, + // msg: error_reason.to_string(), + // } } diff --git a/src/handlers/stat.rs b/src/handlers/stat.rs index 9fb3eb6..aa44ac5 100644 --- a/src/handlers/stat.rs +++ b/src/handlers/stat.rs @@ -1,30 +1,27 @@ -use crate::utils_lib::{self, index_of}; +use std::io::Write; + +use bytes::BufMut; use case_insensitive_hashmap::CaseInsensitiveHashMap; +use may_minihttp::{Request, Response}; use percent_encoding::percent_decode_str; +use crate::utils_lib::{self, index_of}; + pub static BASE: &str = "/api/stat"; pub static METHOD: &str = "GET"; pub fn handle_stat( - incoming_req: crate::http_lib::RequestDataToSet, + incoming_req: Request, mount_point: &str, -) -> crate::http_lib::ResponseDataToSet { - let mut method = "GET".to_string(); - let mut path = "/".to_string(); - crate::http_lib::extract_path_and_method( - incoming_req.method_and_path.as_str(), - &mut path, - &mut method, - ); + response: &mut Response, +) -> std::io::Result<()> { + let mut method = incoming_req.method(); + let mut path = incoming_req.path(); + response.header("Access-Control-Allow-Origin: *"); + let search_pos_opt = index_of(&path, "?"); if search_pos_opt.is_none() { - return crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: CaseInsensitiveHashMap::new(), - data: "wrong".as_bytes().to_vec(), - }, - code: 500, - msg: "Server error".to_string(), - }; + response.status_code(500, "Server error"); + return Ok(()); } let search_pos = search_pos_opt.unwrap(); let search = &path[search_pos..]; @@ -42,7 +39,7 @@ pub fn handle_stat( literal_path = percent_decode_str(value).decode_utf8().unwrap().to_string(); } let mut error_reason = crate::handlers::errors::OK; - let mut final_data = Vec::new(); + // let mut final_data = Vec::new(); if !literal_path.is_empty() { if literal_path.starts_with('.') { // idk if there is any side effect of this... @@ -114,7 +111,10 @@ pub fn handle_stat( out_raw.insert("ctime", true_creation_time.to_string()); out_raw.insert("mtime", true_modification_time.to_string()); out_raw.insert("atime", true_access_time.to_string()); - final_data.extend(utils_lib::create_options(out_raw).as_bytes()); + let mut w = response.body_mut().writer(); + // final_data.extend(utils_lib::create_options(out_raw).as_bytes()); + w.write_all(utils_lib::create_options(out_raw).as_bytes())?; + return Ok(()); } } else { error_reason = crate::handlers::errors::GENERAL_FAILURE; @@ -122,16 +122,6 @@ pub fn handle_stat( } else { error_reason = crate::handlers::errors::NO_INPUT; } - crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: super::all_origins(), // very unsafe, TODO - data: final_data, - }, - code: if error_reason == crate::handlers::errors::OK { - 200 - } else { - 400 - }, - msg: error_reason.to_string(), - } + response.status_code(400, error_reason); + Ok(()) } diff --git a/src/handlers/write.rs b/src/handlers/write.rs index 7f3d2a9..84c4c64 100644 --- a/src/handlers/write.rs +++ b/src/handlers/write.rs @@ -1,6 +1,9 @@ -use std::io::Write; +// use core::slice::SlicePattern; +use std::io::{Read, Write}; +use bytes::BufMut; use case_insensitive_hashmap::CaseInsensitiveHashMap; +use may_minihttp::{Request, Response}; use percent_encoding::percent_decode_str; use crate::utils_lib::index_of; @@ -8,26 +11,18 @@ use crate::utils_lib::index_of; pub static BASE: &str = "/api/file"; pub static METHOD: &str = "POST"; pub fn handle_write( - incoming_req: crate::http_lib::RequestDataToSet, + incoming_req: Request, mount_point: &str, -) -> crate::http_lib::ResponseDataToSet { - let mut method = "GET".to_string(); - let mut path = "/".to_string(); - crate::http_lib::extract_path_and_method( - incoming_req.method_and_path.as_str(), - &mut path, - &mut method, - ); + response: &mut Response, +) -> std::io::Result<()> { + let mut method = incoming_req.method(); + let mut path = incoming_req.path(); + response.header("Access-Control-Allow-Origin: *"); + let search_pos_opt = index_of(&path, "?"); if search_pos_opt.is_none() { - return crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: CaseInsensitiveHashMap::new(), - data: "wrong".as_bytes().to_vec(), - }, - code: 500, - msg: "Server error".to_string(), - }; + response.status_code(500, "Server error"); + return Ok(()); } let search_pos = search_pos_opt.unwrap(); let search = &path[search_pos..]; @@ -45,14 +40,14 @@ pub fn handle_write( literal_path = percent_decode_str(value).decode_utf8().unwrap().to_string(); } let mut error_reason = crate::handlers::errors::OK; - let mut response = Vec::new(); + // let mut response_body = Vec::new(); if !literal_path.is_empty() { if literal_path.starts_with('.') { // idk if there is any side effect of this... literal_path = literal_path[1..].to_string(); } let joined_path_buf = - &std::path::PathBuf::from(mount_point).join(format!(".{}", literal_path)); + &std::path::PathBuf::from(mount_point).join(format!("./{}", literal_path)); dbg!(joined_path_buf); let meta = std::fs::metadata(joined_path_buf); // if meta.is_err() { @@ -103,38 +98,45 @@ pub fn handle_write( everything_fine = true; } if everything_fine { - let mut buffer_copy = incoming_req.base.data.clone(); - crate::utils_lib::truncate_buffer( - &mut buffer_copy, - incoming_req - .base - .headers - .get("Content-Length") - .unwrap() - .parse::() - .unwrap(), - ); - // dbg!(&buffer_copy); - let res2 = res.unwrap().write_all(buffer_copy.as_slice()); - if res2.is_err() { - error_reason = crate::handlers::errors::WRITE_ERROR; + let mut req_body = incoming_req.body(); + let mut file = res.unwrap(); + // let mut buffer = [0u8; 1024]; + let mut buffer = [0u8; 4096]; + loop { + let read_result = req_body.read(&mut buffer); + if read_result.is_err() { + error_reason = crate::handlers::errors::WRITE_ERROR; + break; + } + let bytes_read = read_result.ok().unwrap(); + if bytes_read == 0 { + break; + } + let res2 = file.write_all(&buffer[..bytes_read]); + if res2.is_err() { + error_reason = crate::handlers::errors::WRITE_ERROR; + } } + let truncate_result = file.set_len(req_body.body_limit() as u64); + if truncate_result.is_err() { + error_reason = crate::handlers::errors::TRUNCATE_ERROR; + } + // let mut buf = Vec::new(); + // let read_result = req_body.read_to_end(&mut buf); + // if read_result.is_err() { + // error_reason = crate::handlers::errors::WRITE_ERROR; + // } + // file.write_all(buf.as_slice()); + file.flush()?; + // let res2 = res.unwrap().write_all(); // response.extend(format!("{}", buffer_copy.len()).as_bytes()); + response.body(error_reason); + return Ok(()); } } } else { error_reason = crate::handlers::errors::NO_INPUT; } - crate::http_lib::ResponseDataToSet { - base: crate::http_lib::BasicHTTPDataToSet { - headers: super::all_origins(), // very unsafe, TODO - data: response, - }, - code: if error_reason == crate::handlers::errors::OK { - 200 - } else { - 400 - }, - msg: error_reason.to_string(), - } + response.status_code(400, error_reason); + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 0eeb3ce..319fde9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,65 +1,46 @@ -use case_insensitive_hashmap::CaseInsensitiveHashMap; -mod http_lib; - +use may_minihttp::{HttpServer, HttpService, Request, Response}; +use std::io; mod handlers; +mod http_lib; mod utils_lib; +#[derive(Clone)] +struct RealFS; -fn main() { - let handler = |incoming_req: http_lib::RequestDataToSet| -> http_lib::ResponseDataToSet { - println!("got request"); - let mut method = "GET".to_string(); - let mut path = "/".to_string(); - // for http_method in http_lib::COMMON_HTTP_METHODS { - // if incoming_req.method_and_path.starts_with(http_method) { - // method = &incoming_req.method_and_path[..http_method.chars().count()].trim_end(); - // path = &incoming_req.method_and_path[http_method.chars().count()..].trim_start(); - // break; - // } - // } - http_lib::extract_path_and_method( - incoming_req.method_and_path.as_str(), - &mut path, - &mut method, - ); - // println!("method: {}\npath: {}", method, path); - // for (key, value) in &incoming_req.base.headers { - // println!("header: {}: {}", key, value); - // } - let send_error = |code: i32, msg: &str| http_lib::ResponseDataToSet { - base: http_lib::BasicHTTPDataToSet { - headers: CaseInsensitiveHashMap::new(), - data: [].to_vec(), - }, - code, - msg: msg.to_string(), - }; - // let method_str = method.as_str(); +static mut MNT: &str = ""; + +impl HttpService for RealFS { + fn call(&mut self, req: Request, res: &mut Response) -> io::Result<()> { + // res.header("Access-Control-Allow-Origin: *"); + // dbg!(unsafe { MNT }); + let path = req.path(); + let method = req.method(); if path.starts_with(handlers::read::BASE) && method == handlers::read::METHOD { - return handlers::read::handle_read( - incoming_req, - (std::env::args().collect::>()[1]).as_str(), - ); + return handlers::read::handle_read(req, unsafe { MNT }, res); } if path.starts_with(handlers::list::BASE) && method == handlers::list::METHOD { - return handlers::list::handle_list( - incoming_req, - (std::env::args().collect::>()[1]).as_str(), - ); - } - if path.starts_with(handlers::write::BASE) && method == handlers::write::METHOD { - return handlers::write::handle_write( - incoming_req, - (std::env::args().collect::>()[1]).as_str(), - ); + return handlers::list::handle_list(req, unsafe { MNT }, res); } if path.starts_with(handlers::stat::BASE) && method == handlers::stat::METHOD { - return handlers::stat::handle_stat( - incoming_req, - (std::env::args().collect::>()[1]).as_str(), - ); + return handlers::stat::handle_stat(req, unsafe { MNT }, res); + } + if path.starts_with(handlers::write::BASE) && method == handlers::write::METHOD { + return handlers::write::handle_write(req, unsafe { MNT }, res); } - send_error(500, "Server error") + Ok(()) + } +} + +fn main() { + may::config().set_stack_size(4096 * 2); + unsafe { + let t = Box::into_raw(Box::new(std::env::args().collect::>())); + let target = t.as_ref().unwrap().get(1).unwrap(); + println!("{target}"); + let target_raw = String::from(target); + let target_raw2 = Box::into_raw(Box::new(target_raw)); + MNT = target_raw2.as_ref().unwrap().as_str(); + t.drop_in_place(); }; - // http_lib::start_server(8080, handler); - http_lib::start_server(2137, handler); + let server = HttpServer(RealFS).start("0.0.0.0:2137").unwrap(); + server.join().unwrap(); }