From bc5fcf142ded05e77f849860e6d620071b817852 Mon Sep 17 00:00:00 2001 From: Lorenzo Leonardo Date: Tue, 27 Feb 2024 23:23:59 +0800 Subject: [PATCH] feat: use http 1.0.0 for request and response interfaces --- Cargo.lock | 809 +++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- examples/asynchronous.rs | 16 +- examples/download.rs | 15 +- examples/get.rs | 16 +- examples/post.rs | 16 +- examples/resume_download.rs | 15 +- examples/upload.rs | 15 +- src/http_client.rs | 64 +-- src/lib.rs | 2 - src/request.rs | 17 - src/response.rs | 12 - src/test/asynchronous.rs | 22 +- src/test/download.rs | 92 ++-- src/test/get.rs | 66 +-- src/test/headers.rs | 63 ++- src/test/post.rs | 58 +-- src/test/test_setup.rs | 16 +- src/test/upload.rs | 91 ++-- 19 files changed, 1071 insertions(+), 337 deletions(-) delete mode 100644 src/request.rs delete mode 100644 src/response.rs diff --git a/Cargo.lock b/Cargo.lock index dfb954b..4a8ad00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,60 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aead" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fc95d1bdb8e6666b2b217308eeeb09f2d6728d104be3e31916cc74d15420331" +dependencies = [ + "generic-array", +] + +[[package]] +name = "aes" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "884391ef1066acaa41e766ba8f596341b96e93ce34f9a43e7d24bf0a0eaf0561" +dependencies = [ + "aes-soft", + "aesni", + "cipher", +] + +[[package]] +name = "aes-gcm" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5278b5fabbb9bd46e24aa69b2fdea62c99088e0a950a9be40e3e0101298f88da" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + +[[package]] +name = "aes-soft" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be14c7498ea50828a38d0e24a765ed2effe92a705885b57d029cd67d45744072" +dependencies = [ + "cipher", + "opaque-debug", +] + +[[package]] +name = "aesni" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea2e11f5e94c2f7d386164cc2aa1f97823fed6f259e486940a71c174dd01b0ce" +dependencies = [ + "cipher", + "opaque-debug", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -64,6 +118,96 @@ dependencies = [ "tokio", ] +[[package]] +name = "async-executor" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b0c4a4f319e45986f347ee47fef8bf5e81c9abc3f6f58dc2391439f30df65f0" +dependencies = [ + "async-lock", + "async-task", + "concurrent-queue", + "fastrand 2.0.1", + "futures-lite", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1b6f5d7df27bd294849f8eec66ecfc63d11814df7a4f5d74168a2394467b776" +dependencies = [ + "async-channel", + "async-executor", + "async-io", + "async-lock", + "blocking", + "futures-lite", + "once_cell", +] + +[[package]] +name = "async-io" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" +dependencies = [ + "async-lock", + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-lite", + "log", + "parking", + "polling", + "rustix 0.37.27", + "slab", + "socket2 0.4.10", + "waker-fn", +] + +[[package]] +name = "async-lock" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-std" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" +dependencies = [ + "async-channel", + "async-global-executor", + "async-io", + "async-lock", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4eb2cdb97421e01129ccb49169d8279ed21e829929144f4a22a6e54ac549ca1" + [[package]] name = "async-trait" version = "0.1.77" @@ -75,6 +219,12 @@ dependencies = [ "syn 2.0.49", ] +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + [[package]] name = "autocfg" version = "1.1.0" @@ -96,18 +246,61 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base-x" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270" + [[package]] name = "base64" version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "blocking" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c36a4d0d48574b3dd360b4b7d95cc651d2b6557b6402848a27d4b228a473e2a" +dependencies = [ + "async-channel", + "async-lock", + "async-task", + "fastrand 2.0.1", + "futures-io", + "futures-lite", + "piper", + "tracing", +] + +[[package]] +name = "bumpalo" +version = "3.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b" + [[package]] name = "bytes" version = "1.5.0" @@ -129,6 +322,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cipher" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f8e7987cbd042a63249497f41aed09f8e65add917ea6566effbc56578d6801" +dependencies = [ + "generic-array", +] + [[package]] name = "concurrent-queue" version = "2.4.0" @@ -138,12 +340,69 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "const_fn" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935" + +[[package]] +name = "cookie" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951" +dependencies = [ + "aes-gcm", + "base64", + "hkdf", + "hmac", + "percent-encoding", + "rand 0.8.5", + "sha2", + "time", + "version_check", +] + +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "cpuid-bool" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb25d077389e53838a8158c8e99174c5a9d902dee4904320db714f3c653ffba" + [[package]] name = "crossbeam-utils" version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" +[[package]] +name = "crypto-mac" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff07008ec701e8028e2ceb8f83f0e4274ee62bd2dbdc4fefff2e9a91824081a" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] +name = "ctr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb4a30d54f7443bf3d6191dcd486aca19e67cb3c49fa7a06a319966346707e7f" +dependencies = [ + "cipher", +] + [[package]] name = "curl" version = "0.4.46" @@ -155,7 +414,7 @@ dependencies = [ "openssl-probe", "openssl-sys", "schannel", - "socket2", + "socket2 0.5.5", "windows-sys 0.52.0", ] @@ -167,7 +426,8 @@ dependencies = [ "curl", "derive-deref-rs", "futures", - "http", + "http 1.0.0", + "http-types", "log", "tempfile", "test-case", @@ -222,6 +482,21 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "discard" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" + [[package]] name = "equivalent" version = "1.0.1" @@ -384,6 +659,16 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.1.16" @@ -395,12 +680,45 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1", ] +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", +] + +[[package]] +name = "ghash" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97304e4cd182c3846f7575ced3890c53012ce534ad9114046b0a9e00bb30a375" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "gimli" version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "gloo-timers" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "h2" version = "0.3.24" @@ -412,7 +730,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.11", "indexmap", "slab", "tokio", @@ -432,6 +750,26 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd" +[[package]] +name = "hkdf" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51ab2f639c231793c5f6114bdb9bbe50a7dbbfcd7c7c6bd8475dec2d991e964f" +dependencies = [ + "digest", + "hmac", +] + +[[package]] +name = "hmac" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1441c6b1e930e2817404b5046f1f989899143a12bf92de603b69f4e0aee1e15" +dependencies = [ + "crypto-mac", + "digest", +] + [[package]] name = "http" version = "0.2.11" @@ -443,6 +781,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.6" @@ -450,7 +799,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http", + "http 0.2.11", "pin-project-lite", ] @@ -462,12 +811,14 @@ checksum = "6e9b187a72d63adbfba487f48095306ac823049cb504ee195541e91c7775f5ad" dependencies = [ "anyhow", "async-channel", + "async-std", "base64", + "cookie", "futures-lite", - "http", + "http 0.2.11", "infer", "pin-project-lite", - "rand", + "rand 0.7.3", "serde", "serde_json", "serde_qs", @@ -498,13 +849,13 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.11", "http-body", "httparse", "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.5.5", "tokio", "tower-service", "tracing", @@ -546,12 +897,41 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "io-lifetimes" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "itoa" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "js-sys" +version = "0.3.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "libc" version = "0.2.153" @@ -570,6 +950,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linux-raw-sys" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" + [[package]] name = "linux-raw-sys" version = "0.4.13" @@ -581,6 +967,9 @@ name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +dependencies = [ + "value-bag", +] [[package]] name = "memchr" @@ -633,6 +1022,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "openssl-probe" version = "0.1.5" @@ -675,18 +1070,62 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" +dependencies = [ + "atomic-waker", + "fastrand 2.0.1", + "futures-io", +] + [[package]] name = "pkg-config" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "polling" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" +dependencies = [ + "autocfg", + "bitflags 1.3.2", + "cfg-if", + "concurrent-queue", + "libc", + "log", + "pin-project-lite", + "windows-sys 0.48.0", +] + +[[package]] +name = "polyval" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eebcc4aa140b9abd2bc40d9c3f7ccec842679cd79045ac3a7ac698c1a064b7cd" +dependencies = [ + "cpuid-bool", + "opaque-debug", + "universal-hash", +] + [[package]] name = "ppv-lite86" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-hack" +version = "0.5.20+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" + [[package]] name = "proc-macro2" version = "1.0.78" @@ -711,13 +1150,24 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ - "getrandom", + "getrandom 0.1.16", "libc", - "rand_chacha", - "rand_core", + "rand_chacha 0.2.2", + "rand_core 0.5.1", "rand_hc", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + [[package]] name = "rand_chacha" version = "0.2.2" @@ -725,7 +1175,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", ] [[package]] @@ -734,7 +1194,16 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom", + "getrandom 0.1.16", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.12", ] [[package]] @@ -743,7 +1212,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" dependencies = [ - "rand_core", + "rand_core 0.5.1", ] [[package]] @@ -787,16 +1256,39 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "0.37.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2" +dependencies = [ + "bitflags 1.3.2", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys 0.3.8", + "windows-sys 0.48.0", +] + [[package]] name = "rustix" version = "0.38.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" dependencies = [ - "bitflags", + "bitflags 2.4.2", "errno", "libc", - "linux-raw-sys", + "linux-raw-sys 0.4.13", "windows-sys 0.52.0", ] @@ -815,6 +1307,21 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "serde" version = "1.0.196" @@ -869,6 +1376,34 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1da05c97445caa12d05e848c4a4fcbbea29e748ac28f7e80e9b010392063770" +dependencies = [ + "sha1_smol", +] + +[[package]] +name = "sha1_smol" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" + +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer", + "cfg-if", + "cpufeatures", + "digest", + "opaque-debug", +] + [[package]] name = "slab" version = "0.4.9" @@ -878,6 +1413,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.5" @@ -888,6 +1433,70 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "standback" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e113fb6f3de07a243d434a56ec6f186dfd51cb08448239fe7bcae73f87ff28ff" +dependencies = [ + "version_check", +] + +[[package]] +name = "stdweb" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5" +dependencies = [ + "discard", + "rustc_version", + "stdweb-derive", + "stdweb-internal-macros", + "stdweb-internal-runtime", + "wasm-bindgen", +] + +[[package]] +name = "stdweb-derive" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "serde_derive", + "syn 1.0.109", +] + +[[package]] +name = "stdweb-internal-macros" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11" +dependencies = [ + "base-x", + "proc-macro2", + "quote", + "serde", + "serde_derive", + "serde_json", + "sha1", + "syn 1.0.109", +] + +[[package]] +name = "stdweb-internal-runtime" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" + +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + [[package]] name = "syn" version = "1.0.109" @@ -918,7 +1527,7 @@ checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67" dependencies = [ "cfg-if", "fastrand 2.0.1", - "rustix", + "rustix 0.38.31", "windows-sys 0.52.0", ] @@ -975,6 +1584,44 @@ dependencies = [ "syn 2.0.49", ] +[[package]] +name = "time" +version = "0.2.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4752a97f8eebd6854ff91f1c1824cd6160626ac4bd44287f7f4ea2035a02a242" +dependencies = [ + "const_fn", + "libc", + "standback", + "stdweb", + "time-macros", + "version_check", + "winapi", +] + +[[package]] +name = "time-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "957e9c6e26f12cb6d0dd7fc776bb67a706312e7299aed74c8dd5b17ebb27e2f1" +dependencies = [ + "proc-macro-hack", + "time-macros-impl", +] + +[[package]] +name = "time-macros-impl" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3c141a1b43194f3f56a1411225df8646c55781d5f26db825b3d98507eb482f" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "standback", + "syn 1.0.109", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1001,7 +1648,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", - "socket2", + "socket2 0.5.5", "tokio-macros", "windows-sys 0.48.0", ] @@ -1062,6 +1709,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-bidi" version = "0.3.15" @@ -1083,6 +1736,16 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "universal-hash" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05" +dependencies = [ + "generic-array", + "subtle", +] + [[package]] name = "url" version = "2.5.0" @@ -1095,12 +1758,24 @@ dependencies = [ "serde", ] +[[package]] +name = "value-bag" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a72e1902dde2bd6441347de2b70b7f5d59bf157c6c62f0c44572607a1d55bbe" + [[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "waker-fn" version = "1.1.1" @@ -1128,6 +1803,104 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.49", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877b9c3f61ceea0e56331985743b13f3d25c406a7098d45180fb5f09bc19ed97" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.49", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" + +[[package]] +name = "web-sys" +version = "0.3.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96565907687f7aceb35bc5fc03770a8a0471d82e479f25832f54a0e3f4b28446" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[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-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 6c375a7..da59704 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,8 @@ readme = "README.md" async-curl = "0.3" curl = "0.4" derive-deref-rs = "0.1" -http = "0.2" +http = "1.0" +http-types = "2.12.0" log = "0.4" thiserror = "1.0" tokio = { version = "1.36", features = ["rt"] } diff --git a/examples/asynchronous.rs b/examples/asynchronous.rs index 6244581..611800a 100644 --- a/examples/asynchronous.rs +++ b/examples/asynchronous.rs @@ -1,8 +1,7 @@ use async_curl::actor::CurlActor; -use curl_http_client::{collector::Collector, http_client::HttpClient, request::HttpRequest}; +use curl_http_client::{collector::Collector, http_client::HttpClient}; use futures::future; -use http::{HeaderMap, Method}; -use url::Url; +use http::{Method, Request}; #[tokio::main(flavor = "current_thread")] async fn main() { @@ -16,12 +15,11 @@ async fn main() { let handle = tokio::spawn(async move { let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: Url::parse("https://www.rust-lang.org/").unwrap(), - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri("https://www.rust-lang.org/") + .method(Method::GET) + .body(None) + .unwrap(); let response = HttpClient::new(collector) .request(request) diff --git a/examples/download.rs b/examples/download.rs index 5a8727c..072f098 100644 --- a/examples/download.rs +++ b/examples/download.rs @@ -4,10 +4,8 @@ use async_curl::actor::CurlActor; use curl_http_client::{ collector::{Collector, FileInfo}, http_client::HttpClient, - request::HttpRequest, }; -use http::{HeaderMap, Method}; -use url::Url; +use http::{Method, Request}; #[tokio::main(flavor = "current_thread")] async fn main() { @@ -15,12 +13,11 @@ async fn main() { let collector = Collector::File(FileInfo::path(PathBuf::from(""))); - let request = HttpRequest { - url: Url::parse("").unwrap(), - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri("") + .method(Method::GET) + .body(None) + .unwrap(); let response = HttpClient::new(collector) .request(request) diff --git a/examples/get.rs b/examples/get.rs index 73ceaa1..1f6cae3 100644 --- a/examples/get.rs +++ b/examples/get.rs @@ -1,19 +1,17 @@ use async_curl::actor::CurlActor; -use curl_http_client::{collector::Collector, http_client::HttpClient, request::HttpRequest}; -use http::{HeaderMap, Method}; -use url::Url; +use curl_http_client::{collector::Collector, http_client::HttpClient}; +use http::{Method, Request}; #[tokio::main(flavor = "current_thread")] async fn main() { let actor = CurlActor::new(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: Url::parse("").unwrap(), - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri("") + .method(Method::GET) + .body(None) + .unwrap(); let response = HttpClient::new(collector) .request(request) diff --git a/examples/post.rs b/examples/post.rs index cc2b860..c7f33e8 100644 --- a/examples/post.rs +++ b/examples/post.rs @@ -1,19 +1,17 @@ use async_curl::actor::CurlActor; -use curl_http_client::{collector::Collector, http_client::HttpClient, request::HttpRequest}; -use http::{HeaderMap, Method}; -use url::Url; +use curl_http_client::{collector::Collector, http_client::HttpClient}; +use http::{Method, Request}; #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { let actor = CurlActor::new(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: Url::parse("").unwrap(), - method: Method::POST, - headers: HeaderMap::new(), - body: Some("test body".as_bytes().to_vec()), - }; + let request = Request::builder() + .uri("") + .method(Method::POST) + .body(Some("test body".as_bytes().to_vec())) + .unwrap(); let response = HttpClient::new(collector) .request(request) diff --git a/examples/resume_download.rs b/examples/resume_download.rs index 13b9473..b04a233 100644 --- a/examples/resume_download.rs +++ b/examples/resume_download.rs @@ -5,10 +5,8 @@ use async_curl::actor::CurlActor; use curl_http_client::{ collector::{Collector, FileInfo}, http_client::{BytesOffset, HttpClient}, - request::HttpRequest, }; -use http::{HeaderMap, Method}; -use url::Url; +use http::{Method, Request}; #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { @@ -17,12 +15,11 @@ async fn main() -> Result<(), Box> { let collector = Collector::File(FileInfo::path(save_to.clone())); let partial_download_file_size = fs::metadata(save_to.as_path())?.len() as usize; - let request = HttpRequest { - url: Url::parse("").unwrap(), - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri("") + .method(Method::GET) + .body(None) + .unwrap(); let response = HttpClient::new(collector) .resume_from(BytesOffset::from(partial_download_file_size)) diff --git a/examples/upload.rs b/examples/upload.rs index 2702bec..1fa0909 100644 --- a/examples/upload.rs +++ b/examples/upload.rs @@ -4,10 +4,8 @@ use async_curl::actor::CurlActor; use curl_http_client::{ collector::{Collector, FileInfo}, http_client::{FileSize, HttpClient}, - request::HttpRequest, }; -use http::{HeaderMap, Method}; -use url::Url; +use http::{Method, Request}; #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { @@ -17,12 +15,11 @@ async fn main() -> Result<(), Box> { let actor = CurlActor::new(); let collector = Collector::File(FileInfo::path(file_to_be_uploaded)); - let request = HttpRequest { - url: Url::parse("").unwrap(), - method: Method::PUT, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri("") + .method(Method::PUT) + .body(None) + .unwrap(); let response = HttpClient::new(collector) .upload_file_size(FileSize::from(file_size)) diff --git a/src/http_client.rs b/src/http_client.rs index 7c19c37..a5a3ea5 100644 --- a/src/http_client.rs +++ b/src/http_client.rs @@ -5,13 +5,11 @@ use curl::easy::{Auth, Easy2, Handler, HttpVersion, ProxyType, SslVersion, TimeC use derive_deref_rs::Deref; use http::{ header::{CONTENT_LENGTH, CONTENT_TYPE}, - HeaderMap, HeaderValue, Method, StatusCode, + HeaderMap, HeaderValue, Method, Request, Response, }; use log::trace; -use crate::{ - collector::ExtendedHandler, error::Error, request::HttpRequest, response::HttpResponse, -}; +use crate::{collector::ExtendedHandler, error::Error}; /// Proceed to building state pub struct Build; @@ -79,14 +77,17 @@ where /// /// The HttpRequest can be customized by the caller by setting the Url, Method Type, /// Headers and the Body. - pub fn request(mut self, request: HttpRequest) -> Result> { - self.easy.url(&request.url.to_string()[..]).map_err(|e| { - trace!("{:?}", e); - Error::Curl(e) - })?; + pub fn request(mut self, request: Request>>) -> Result> { + self.easy + .url(request.uri().to_string().as_str()) + .map_err(|e| { + trace!("{:?}", e); + Error::Curl(e) + })?; let mut headers = curl::easy::List::new(); - request.headers.iter().try_for_each(|(name, value)| { + + request.headers().iter().try_for_each(|(name, value)| { headers .append(&format!( "{}: {}", @@ -108,15 +109,16 @@ where Error::Curl(e) })?; - match request.method { + match *request.method() { Method::POST => { self.easy.post(true).map_err(Error::Curl)?; - if let Some(body) = request.body { + + if let Some(body) = request.body() { self.easy.post_field_size(body.len() as u64).map_err(|e| { trace!("{:?}", e); Error::Curl(e) })?; - self.easy.post_fields_copy(body.as_slice()).map_err(|e| { + self.easy.post_fields_copy(body).map_err(|e| { trace!("{:?}", e); Error::Curl(e) })?; @@ -827,7 +829,7 @@ where } /// This will perform the curl operation asynchronously. - pub async fn perform(self) -> Result> { + pub async fn perform(self) -> Result>>, Error> { let easy = self.send_request().await?; let (data, headers) = easy.get_ref().get_response_body_and_headers(); @@ -875,14 +877,14 @@ where response_header }; - Ok(HttpResponse { - status_code: StatusCode::from_u16(status_code).map_err(|err| { - trace!("{:?}", err); - Error::Http(err.to_string()) - })?, - headers: response_header, - body: data, - }) + let mut response = Response::builder(); + for (name, value) in &response_header { + response = response.header(name, value); + } + + response = response.status(status_code); + + response.body(data).map_err(|e| Error::Http(e.to_string())) } } @@ -903,7 +905,7 @@ where } /// This will perform the curl operation synchronously. - pub fn perform(self) -> Result> { + pub fn perform(self) -> Result>>, Error> { let easy = self.send_request()?; let (data, headers) = easy.get_ref().get_response_body_and_headers(); @@ -951,14 +953,14 @@ where response_header }; - Ok(HttpResponse { - status_code: StatusCode::from_u16(status_code).map_err(|err| { - trace!("{:?}", err); - Error::Http(err.to_string()) - })?, - headers: response_header, - body: data, - }) + let mut response = Response::builder(); + for (name, value) in &response_header { + response = response.header(name, value); + } + + response = response.status(status_code); + + response.body(data).map_err(|e| Error::Http(e.to_string())) } } /// A strong type unit when setting download speed and upload speed diff --git a/src/lib.rs b/src/lib.rs index 3ca60f5..e2ea02f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -439,8 +439,6 @@ pub mod collector; pub mod error; pub mod http_client; -pub mod request; -pub mod response; pub mod dep { pub use curl; diff --git a/src/request.rs b/src/request.rs deleted file mode 100644 index 62e717f..0000000 --- a/src/request.rs +++ /dev/null @@ -1,17 +0,0 @@ -use http::HeaderMap; -use url::Url; - -/// An HTTP request. -#[derive(Clone, Debug)] -pub struct HttpRequest { - // These are all owned values so that the request can safely be passed between - // threads. - /// URL to which the HTTP request is being made. - pub url: Url, - /// HTTP request method for this request. - pub method: http::method::Method, - /// HTTP request headers to send. - pub headers: HeaderMap, - /// HTTP request body (typically for POST requests only). - pub body: Option>, -} diff --git a/src/response.rs b/src/response.rs deleted file mode 100644 index 7375841..0000000 --- a/src/response.rs +++ /dev/null @@ -1,12 +0,0 @@ -use http::{HeaderMap, StatusCode}; - -/// An HTTP response. -#[derive(Clone, Debug)] -pub struct HttpResponse { - /// HTTP status code returned by the server. - pub status_code: StatusCode, - /// HTTP response headers returned by the server. - pub headers: HeaderMap, - /// HTTP response body returned by the server. - pub body: Option>, -} diff --git a/src/test/asynchronous.rs b/src/test/asynchronous.rs index b9adaf8..1046183 100644 --- a/src/test/asynchronous.rs +++ b/src/test/asynchronous.rs @@ -1,11 +1,10 @@ use async_curl::actor::CurlActor; use futures::future; -use http::{HeaderMap, Method, StatusCode}; +use http::{Method, Request, StatusCode}; use url::Url; use crate::collector::Collector; use crate::http_client::HttpClient; -use crate::request::HttpRequest; use crate::test::test_setup::{setup_test_environment, MockResponder, ResponderType}; #[tokio::test] @@ -16,12 +15,12 @@ async fn test_across_multiple_threads() { let curl = CurlActor::new(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + const NUM_CONCURRENT: usize = 100; let mut handles = Vec::new(); @@ -38,8 +37,11 @@ async fn test_across_multiple_threads() { .await .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body.unwrap(), "test body".as_bytes().to_vec()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + *response.body().as_ref().unwrap(), + "test body".as_bytes().to_vec() + ); }); handles.push(handle); } diff --git a/src/test/download.rs b/src/test/download.rs index cef2b7b..8b3955f 100644 --- a/src/test/download.rs +++ b/src/test/download.rs @@ -1,14 +1,13 @@ use std::fs; use async_curl::actor::CurlActor; -use http::{HeaderMap, Method, StatusCode}; +use http::{Method, Request, StatusCode}; use test_case::test_case; use tokio::sync::mpsc::channel; use url::Url; use crate::collector::{Collector, FileInfo}; use crate::http_client::{BytesOffset, BytesPerSec, HttpClient}; -use crate::request::HttpRequest; use crate::test::test_setup::{setup_test_environment, MockResponder, ResponderType}; #[tokio::test] @@ -20,12 +19,12 @@ async fn test_download() { let save_to = tempdir.path().join("downloaded_file.jpg"); let actor = CurlActor::new(); let collector = Collector::File(FileInfo::path(save_to.clone())); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -35,10 +34,10 @@ async fn test_download() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); assert_eq!(fs::read(save_to).unwrap(), include_bytes!("sample.jpg")); - assert!(!response.headers.is_empty()); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -50,12 +49,12 @@ async fn test_download_with_speed_control() { let save_to = tempdir.path().join("downloaded_file.jpg"); let actor = CurlActor::new(); let collector = Collector::File(FileInfo::path(save_to.clone())); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .download_speed(BytesPerSec::from(40000000)) .unwrap() @@ -67,10 +66,10 @@ async fn test_download_with_speed_control() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); assert_eq!(fs::read(save_to).unwrap(), include_bytes!("sample.jpg")); - assert!(!response.headers.is_empty()); + assert!(!response.headers().is_empty()); } #[test_case(4500, StatusCode::PARTIAL_CONTENT; "Offset 4500 bytes")] @@ -91,12 +90,12 @@ async fn test_resume_download(offset: usize, expected_status_code: StatusCode) { let actor = CurlActor::new(); let collector = Collector::File(FileInfo::path(save_to.clone())); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .resume_from(BytesOffset::from(partial_file_size)) .unwrap() @@ -108,10 +107,10 @@ async fn test_resume_download(offset: usize, expected_status_code: StatusCode) { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, expected_status_code); - assert_eq!(response.body, None); + assert_eq!(response.status(), expected_status_code); + assert_eq!(*response.body(), None); assert_eq!(fs::read(save_to).unwrap(), include_bytes!("sample.jpg")); - assert!(!response.headers.is_empty()); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -128,12 +127,11 @@ async fn test_download_with_transfer_speed_sender() { let file_info = FileInfo::path(save_to.clone()).with_transfer_speed_sender(tx); let collector = Collector::File(file_info); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); let handle = tokio::spawn(async move { while let Some(speed) = rx.recv().await { @@ -152,10 +150,10 @@ async fn test_download_with_transfer_speed_sender() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); assert_eq!(fs::read(save_to).unwrap(), include_bytes!("sample.jpg")); - assert!(!response.headers.is_empty()); + assert!(!response.headers().is_empty()); handle.abort(); } @@ -169,12 +167,12 @@ async fn test_download_with_headers() { let save_to = tempdir.path().join("downloaded_file.jpg"); let actor = CurlActor::new(); let collector = Collector::FileAndHeaders(FileInfo::path(save_to.clone()), Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -184,8 +182,8 @@ async fn test_download_with_headers() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); assert_eq!(fs::read(save_to).unwrap(), include_bytes!("sample.jpg")); - assert!(!response.headers.is_empty()); + assert!(!response.headers().is_empty()); } diff --git a/src/test/get.rs b/src/test/get.rs index cca5adc..5d62f05 100644 --- a/src/test/get.rs +++ b/src/test/get.rs @@ -1,10 +1,9 @@ use async_curl::actor::CurlActor; -use http::{HeaderMap, Method, StatusCode}; +use http::{Method, Request, StatusCode}; use url::Url; use crate::collector::Collector; use crate::http_client::HttpClient; -use crate::request::HttpRequest; use crate::test::test_setup::{setup_test_environment, MockResponder, ResponderType}; #[tokio::test] @@ -15,12 +14,12 @@ async fn test_get() { let actor = CurlActor::new(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -30,9 +29,12 @@ async fn test_get() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body.unwrap(), "test body".as_bytes().to_vec()); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + *response.body().as_ref().unwrap(), + "test body".as_bytes().to_vec() + ); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -43,12 +45,12 @@ async fn test_get_with_headers() { let actor = CurlActor::new(); let collector = Collector::RamAndHeaders(Vec::new(), Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -58,9 +60,12 @@ async fn test_get_with_headers() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body.unwrap(), "test body".as_bytes().to_vec()); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + *response.body().as_ref().unwrap(), + "test body".as_bytes().to_vec() + ); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -70,12 +75,12 @@ async fn test_get_sync() { let target_url = Url::parse(format!("{}/test", server.uri()).as_str()).unwrap(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -84,7 +89,10 @@ async fn test_get_sync() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body.unwrap(), "test body".as_bytes().to_vec()); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + *response.body().as_ref().unwrap(), + "test body".as_bytes().to_vec() + ); + assert!(!response.headers().is_empty()); } diff --git a/src/test/headers.rs b/src/test/headers.rs index e499473..93b9449 100644 --- a/src/test/headers.rs +++ b/src/test/headers.rs @@ -1,12 +1,11 @@ use std::fs; use async_curl::actor::CurlActor; -use http::{HeaderMap, Method}; +use http::{Method, Request}; use url::Url; use crate::collector::{Collector, ExtendedHandler, FileInfo}; use crate::http_client::HttpClient; -use crate::request::HttpRequest; use crate::test::test_setup::{setup_test_environment, MockResponder, ResponderType}; #[tokio::test] @@ -17,12 +16,12 @@ async fn test_with_complete_headers_ram_and_header() { let actor = CurlActor::new(); let collector = Collector::RamAndHeaders(Vec::new(), Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -51,12 +50,12 @@ async fn test_with_complete_headers_file_and_headers() { let save_to = tempdir.path().join("downloaded_file.jpg"); let actor = CurlActor::new(); let collector = Collector::FileAndHeaders(FileInfo::path(save_to.clone()), Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -85,12 +84,12 @@ async fn test_with_complete_headers_ram() { let actor = CurlActor::new(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -119,12 +118,12 @@ async fn test_with_complete_headers_file() { let save_to = tempdir.path().join("downloaded_file.jpg"); let actor = CurlActor::new(); let collector = Collector::File(FileInfo::path(save_to.clone())); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -152,12 +151,12 @@ async fn test_with_complete_headers_ram_and_header_sync() { let target_url = Url::parse(format!("{}/test", server.uri()).as_str()).unwrap(); let collector = Collector::RamAndHeaders(Vec::new(), Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::GET, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::GET) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() diff --git a/src/test/post.rs b/src/test/post.rs index 0c89ccf..2728427 100644 --- a/src/test/post.rs +++ b/src/test/post.rs @@ -1,10 +1,9 @@ use async_curl::actor::CurlActor; -use http::{HeaderMap, Method, StatusCode}; +use http::{Method, Request, StatusCode}; use url::Url; use crate::collector::Collector; use crate::http_client::HttpClient; -use crate::request::HttpRequest; use crate::test::test_setup::{setup_test_environment, MockResponder, ResponderType}; #[tokio::test] @@ -15,12 +14,12 @@ async fn test_post() { let actor = CurlActor::new(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::POST, - headers: HeaderMap::new(), - body: Some("test body".as_bytes().to_vec()), - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::POST) + .body(Some("test body".as_bytes().to_vec())) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -30,9 +29,10 @@ async fn test_post() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body.unwrap().len(), 0); - assert!(!response.headers.is_empty()); + + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), Some(Vec::new())); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -43,12 +43,12 @@ async fn test_post_with_headers() { let actor = CurlActor::new(); let collector = Collector::RamAndHeaders(Vec::new(), Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::POST, - headers: HeaderMap::new(), - body: Some("test body".as_bytes().to_vec()), - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::POST) + .body(Some("test body".as_bytes().to_vec())) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -58,9 +58,9 @@ async fn test_post_with_headers() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body.unwrap().len(), 0); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), Some(Vec::new())); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -70,12 +70,12 @@ async fn test_post_sync() { let target_url = Url::parse(format!("{}/test", server.uri()).as_str()).unwrap(); let collector = Collector::Ram(Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::POST, - headers: HeaderMap::new(), - body: Some("test body".as_bytes().to_vec()), - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::POST) + .body(Some("test body".as_bytes().to_vec())) + .unwrap(); + let response = HttpClient::new(collector) .request(request) .unwrap() @@ -84,7 +84,7 @@ async fn test_post_sync() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body.unwrap().len(), 0); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), Some(Vec::new())); + assert!(!response.headers().is_empty()); } diff --git a/src/test/test_setup.rs b/src/test/test_setup.rs index 6e5831b..d2200a4 100644 --- a/src/test/test_setup.rs +++ b/src/test/test_setup.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use http::StatusCode; +use http_types::StatusCode; use tempfile::TempDir; use wiremock::{ http::{HeaderName, HeaderValue, HeaderValues, Method}, @@ -45,7 +45,7 @@ impl Respond for MockResponder { ); println!("Content-Range: {}", content_range); - ResponseTemplate::new(StatusCode::PARTIAL_CONTENT) + ResponseTemplate::new(StatusCode::PartialContent) .append_header( HeaderName::from_str("Content-Range").unwrap(), HeaderValue::from_str(content_range.as_str()).unwrap(), @@ -61,28 +61,28 @@ impl Respond for MockResponder { .set_body_bytes(&mock_file[offset..]) } else { let contents = include_bytes!("sample.jpg"); - ResponseTemplate::new(StatusCode::OK).set_body_bytes(contents.as_slice()) + ResponseTemplate::new(StatusCode::Ok).set_body_bytes(contents.as_slice()) } } ResponderType::Body(body) => { - ResponseTemplate::new(StatusCode::OK).set_body_bytes(body.as_slice()) + ResponseTemplate::new(StatusCode::Ok).set_body_bytes(body.as_slice()) } }, Method::Post => match &self.responder { - ResponderType::File => ResponseTemplate::new(StatusCode::OK), + ResponderType::File => ResponseTemplate::new(StatusCode::Ok), ResponderType::Body(body) => { assert_eq!(*body, request.body); - ResponseTemplate::new(StatusCode::OK) + ResponseTemplate::new(StatusCode::Ok) } }, Method::Put => match &self.responder { ResponderType::File => { assert_eq!(include_bytes!("sample.jpg").to_vec(), request.body); - ResponseTemplate::new(StatusCode::OK) + ResponseTemplate::new(StatusCode::Ok) } ResponderType::Body(body) => { assert_eq!(*body, request.body); - ResponseTemplate::new(StatusCode::OK) + ResponseTemplate::new(StatusCode::Ok) } }, _ => { diff --git a/src/test/upload.rs b/src/test/upload.rs index 49eeea7..0f095f7 100644 --- a/src/test/upload.rs +++ b/src/test/upload.rs @@ -1,13 +1,12 @@ use std::fs; use async_curl::actor::CurlActor; -use http::{HeaderMap, Method, StatusCode}; +use http::{Method, Request, StatusCode}; use tokio::sync::mpsc::channel; use url::Url; use crate::collector::{Collector, FileInfo}; use crate::http_client::{BytesPerSec, FileSize, HttpClient}; -use crate::request::HttpRequest; use crate::test::test_setup::{setup_test_environment, MockResponder, ResponderType}; #[tokio::test] @@ -22,12 +21,12 @@ async fn test_upload() { let actor = CurlActor::new(); let collector = Collector::File(FileInfo::path(to_be_uploaded)); - let request = HttpRequest { - url: target_url, - method: Method::PUT, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::PUT) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .upload_file_size(FileSize::from(file_size)) .unwrap() @@ -39,9 +38,9 @@ async fn test_upload() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -56,12 +55,12 @@ async fn test_upload_with_speed_control() { let actor = CurlActor::new(); let collector = Collector::File(FileInfo::path(to_be_uploaded)); - let request = HttpRequest { - url: target_url, - method: Method::PUT, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::PUT) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .upload_file_size(FileSize::from(file_size)) .unwrap() @@ -75,9 +74,9 @@ async fn test_upload_with_speed_control() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -97,12 +96,11 @@ async fn test_upload_with_transfer_speed_sender() { let file_info = FileInfo::path(to_be_uploaded).with_transfer_speed_sender(tx); let collector = Collector::File(file_info); - let request = HttpRequest { - url: target_url, - method: Method::PUT, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::PUT) + .body(None) + .unwrap(); let handle = tokio::spawn(async move { while let Some(speed) = rx.recv().await { @@ -123,9 +121,9 @@ async fn test_upload_with_transfer_speed_sender() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); + assert!(!response.headers().is_empty()); handle.abort(); } @@ -142,12 +140,12 @@ async fn test_upload_with_headers() { let actor = CurlActor::new(); let collector = Collector::FileAndHeaders(FileInfo::path(to_be_uploaded), Vec::new()); - let request = HttpRequest { - url: target_url, - method: Method::PUT, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::PUT) + .body(None) + .unwrap(); + let response = HttpClient::new(collector) .upload_file_size(FileSize::from(file_size)) .unwrap() @@ -159,9 +157,9 @@ async fn test_upload_with_headers() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); + assert!(!response.headers().is_empty()); } #[tokio::test] @@ -175,12 +173,11 @@ async fn test_upload_sync() { let file_size = fs::metadata(to_be_uploaded.as_path()).unwrap().len() as usize; let collector = Collector::File(FileInfo::path(to_be_uploaded)); - let request = HttpRequest { - url: target_url, - method: Method::PUT, - headers: HeaderMap::new(), - body: None, - }; + let request = Request::builder() + .uri(target_url.as_str()) + .method(Method::PUT) + .body(None) + .unwrap(); let response = HttpClient::new(collector) .upload_file_size(FileSize::from(file_size)) .unwrap() @@ -191,7 +188,7 @@ async fn test_upload_sync() { .unwrap(); println!("Response: {:?}", response); - assert_eq!(response.status_code, StatusCode::OK); - assert_eq!(response.body, None); - assert!(!response.headers.is_empty()); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(*response.body(), None); + assert!(!response.headers().is_empty()); }