From 03b0e9b59d877f20a00afe2593cd9a008395fd81 Mon Sep 17 00:00:00 2001 From: Nathan Shaaban Date: Fri, 1 Oct 2021 22:21:43 +0000 Subject: [PATCH 1/4] feat: switch to tungstenite-rs --- Cargo.lock | 440 ++++---------------- engineio/Cargo.toml | 2 +- engineio/src/client/socket.rs | 33 +- engineio/src/error.rs | 4 +- engineio/src/header.rs | 16 +- engineio/src/transport.rs | 10 +- engineio/src/transports/mod.rs | 2 - engineio/src/transports/websocket.rs | 134 +++--- engineio/src/transports/websocket_secure.rs | 198 --------- 9 files changed, 178 insertions(+), 661 deletions(-) delete mode 100644 engineio/src/transports/websocket_secure.rs diff --git a/Cargo.lock b/Cargo.lock index 3c1e6d69..7b3879d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,37 +52,12 @@ dependencies = [ "winapi", ] -[[package]] -name = "autocfg" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" - [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" -[[package]] -name = "base64" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" -dependencies = [ - "byteorder", - "safemem", -] - -[[package]] -name = "base64" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" -dependencies = [ - "byteorder", -] - [[package]] name = "base64" version = "0.13.0" @@ -97,25 +72,13 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "block-buffer" -version = "0.7.3" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ - "block-padding", - "byte-tools", - "byteorder", "generic-array", ] -[[package]] -name = "block-padding" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" -dependencies = [ - "byte-tools", -] - [[package]] name = "bstr" version = "0.2.16" @@ -140,12 +103,6 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cfd4a5b862cb6bfcaf4b9803e887a22393dd5c1ac9e2d1e131d516047eabd01" -[[package]] -name = "byte-tools" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" - [[package]] name = "byteorder" version = "1.4.3" @@ -278,15 +235,6 @@ dependencies = [ "vec_map", ] -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -dependencies = [ - "bitflags", -] - [[package]] name = "core-foundation" version = "0.9.1" @@ -317,6 +265,15 @@ dependencies = [ "serde_json", ] +[[package]] +name = "cpufeatures" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.2.1" @@ -471,9 +428,9 @@ dependencies = [ [[package]] name = "digest" -version = "0.8.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" dependencies = [ "generic-array", ] @@ -493,12 +450,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "fake-simd" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" - [[package]] name = "fallible-iterator" version = "0.2.0" @@ -545,15 +496,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" dependencies = [ "matches", - "percent-encoding 2.1.0", + "percent-encoding", ] -[[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" - [[package]] name = "futures-channel" version = "0.3.17" @@ -593,7 +538,7 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481" dependencies = [ - "autocfg 1.0.1", + "autocfg", "futures-core", "futures-io", "futures-task", @@ -605,11 +550,12 @@ dependencies = [ [[package]] name = "generic-array" -version = "0.12.4" +version = "0.14.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" dependencies = [ "typenum", + "version_check", ] [[package]] @@ -643,10 +589,10 @@ dependencies = [ "bitflags", "libc", "libgit2-sys", - "log 0.4.14", + "log", "openssl-probe", "openssl-sys", - "url 2.2.2", + "url", ] [[package]] @@ -754,25 +700,6 @@ dependencies = [ "serde", ] -[[package]] -name = "hyper" -version = "0.10.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a0652d9a2609a968c14be1a9ea00bf4b1d64e2e1f53a1b51b6fff3a6e829273" -dependencies = [ - "base64 0.9.3", - "httparse", - "language-tags", - "log 0.3.9", - "mime 0.2.6", - "num_cpus", - "time", - "traitobject", - "typeable", - "unicase", - "url 1.7.2", -] - [[package]] name = "hyper" version = "0.14.12" @@ -804,23 +731,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" dependencies = [ "bytes", - "hyper 0.14.12", + "hyper", "native-tls", "tokio", "tokio-native-tls", ] -[[package]] -name = "idna" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" -dependencies = [ - "matches", - "unicode-bidi", - "unicode-normalization", -] - [[package]] name = "idna" version = "0.2.3" @@ -838,7 +754,7 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" dependencies = [ - "autocfg 1.0.1", + "autocfg", "hashbrown", "serde", ] @@ -882,12 +798,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "language-tags" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" - [[package]] name = "lazy_static" version = "1.4.0" @@ -940,15 +850,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "log" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" -dependencies = [ - "log 0.4.14", -] - [[package]] name = "log" version = "0.4.14" @@ -1001,16 +902,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" dependencies = [ - "autocfg 1.0.1", -] - -[[package]] -name = "mime" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" -dependencies = [ - "log 0.3.9", + "autocfg", ] [[package]] @@ -1026,7 +918,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" dependencies = [ "adler", - "autocfg 1.0.1", + "autocfg", ] [[package]] @@ -1036,7 +928,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c2bdb6314ec10835cd3293dd268473a835c02b7b352e788be788b3c6ca6bb16" dependencies = [ "libc", - "log 0.4.14", + "log", "miow", "ntapi", "winapi", @@ -1059,7 +951,7 @@ checksum = "48ba9f7719b5a0f42f338907614285fb5fd70e53858141f69898a1fb7203b24d" dependencies = [ "lazy_static", "libc", - "log 0.4.14", + "log", "openssl", "openssl-probe", "openssl-sys", @@ -1097,7 +989,7 @@ version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" dependencies = [ - "autocfg 1.0.1", + "autocfg", "num-traits", ] @@ -1107,7 +999,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" dependencies = [ - "autocfg 1.0.1", + "autocfg", ] [[package]] @@ -1143,9 +1035,9 @@ checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" [[package]] name = "opaque-debug" -version = "0.2.3" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "openssl" @@ -1173,19 +1065,13 @@ version = "0.9.66" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1996d2d305e561b70d1ee0c53f1542833f4e1ac6ce9a6708b6ff2738ca67dc82" dependencies = [ - "autocfg 1.0.1", + "autocfg", "cc", "libc", "pkg-config", "vcpkg", ] -[[package]] -name = "percent-encoding" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" - [[package]] name = "percent-encoding" version = "2.1.0" @@ -1286,25 +1172,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" -dependencies = [ - "autocfg 0.1.7", - "libc", - "rand_chacha 0.1.1", - "rand_core 0.4.2", - "rand_hc 0.1.0", - "rand_isaac", - "rand_jitter", - "rand_os", - "rand_pcg", - "rand_xorshift", - "winapi", -] - [[package]] name = "rand" version = "0.8.4" @@ -1312,19 +1179,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" dependencies = [ "libc", - "rand_chacha 0.3.1", - "rand_core 0.6.3", - "rand_hc 0.3.1", -] - -[[package]] -name = "rand_chacha" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" -dependencies = [ - "autocfg 0.1.7", - "rand_core 0.3.1", + "rand_chacha", + "rand_core", + "rand_hc", ] [[package]] @@ -1334,24 +1191,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core 0.6.3", -] - -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -dependencies = [ - "rand_core 0.4.2", + "rand_core", ] -[[package]] -name = "rand_core" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" - [[package]] name = "rand_core" version = "0.6.3" @@ -1361,75 +1203,13 @@ dependencies = [ "getrandom", ] -[[package]] -name = "rand_hc" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" -dependencies = [ - "rand_core 0.3.1", -] - [[package]] name = "rand_hc" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" dependencies = [ - "rand_core 0.6.3", -] - -[[package]] -name = "rand_isaac" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rand_jitter" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" -dependencies = [ - "libc", - "rand_core 0.4.2", - "winapi", -] - -[[package]] -name = "rand_os" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" -dependencies = [ - "cloudabi", - "fuchsia-cprng", - "libc", - "rand_core 0.4.2", - "rdrand", - "winapi", -] - -[[package]] -name = "rand_pcg" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" -dependencies = [ - "autocfg 0.1.7", - "rand_core 0.4.2", -] - -[[package]] -name = "rand_xorshift" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" -dependencies = [ - "rand_core 0.3.1", + "rand_core", ] [[package]] @@ -1438,7 +1218,7 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" dependencies = [ - "autocfg 1.0.1", + "autocfg", "crossbeam-deque", "either", "rayon-core", @@ -1457,15 +1237,6 @@ dependencies = [ "num_cpus", ] -[[package]] -name = "rdrand" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" -dependencies = [ - "rand_core 0.3.1", -] - [[package]] name = "redox_syscall" version = "0.2.10" @@ -1516,28 +1287,28 @@ version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "246e9f61b9bb77df069a947682be06e31ac43ea37862e244a69f177694ea6d22" dependencies = [ - "base64 0.13.0", + "base64", "bytes", "encoding_rs", "futures-core", "futures-util", "http", "http-body", - "hyper 0.14.12", + "hyper", "hyper-tls", "ipnet", "js-sys", "lazy_static", - "log 0.4.14", - "mime 0.3.16", + "log", + "mime", "native-tls", - "percent-encoding 2.1.0", + "percent-encoding", "pin-project-lite", "serde", "serde_urlencoded", "tokio", "tokio-native-tls", - "url 2.2.2", + "url", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", @@ -1549,7 +1320,7 @@ name = "rust_engineio" version = "0.3.0-alpha.1" dependencies = [ "adler32", - "base64 0.13.0", + "base64", "bytes", "cargo-tarpaulin", "criterion", @@ -1559,8 +1330,8 @@ dependencies = [ "serde", "serde_json", "thiserror", - "url 2.2.2", - "websocket", + "tungstenite", + "url", ] [[package]] @@ -1568,18 +1339,18 @@ name = "rust_socketio" version = "0.3.0-alpha.1" dependencies = [ "adler32", - "base64 0.13.0", + "base64", "byte", "bytes", "cargo-tarpaulin", "crossbeam-utils", "native-tls", - "rand 0.8.4", + "rand", "regex", "rust_engineio", "serde_json", "thiserror", - "url 2.2.2", + "url", ] [[package]] @@ -1603,12 +1374,6 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" -[[package]] -name = "safemem" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" - [[package]] name = "same-file" version = "1.0.6" @@ -1721,13 +1486,14 @@ dependencies = [ [[package]] name = "sha-1" -version = "0.8.2" +version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ "block-buffer", + "cfg-if", + "cpufeatures", "digest", - "fake-simd", "opaque-debug", ] @@ -1793,7 +1559,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ "cfg-if", "libc", - "rand 0.8.4", + "rand", "redox_syscall", "remove_dir_all", "winapi", @@ -1878,7 +1644,7 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4efe6fc2395938c8155973d7be49fe8d03a843726e285e100a8a383cc0154ce" dependencies = [ - "autocfg 1.0.1", + "autocfg", "bytes", "libc", "memchr", @@ -1907,7 +1673,7 @@ dependencies = [ "bytes", "futures-core", "futures-sink", - "log 0.4.14", + "log", "pin-project-lite", "tokio", ] @@ -1954,7 +1720,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6923477a48e41c1951f1999ef8bb5a3023eb723ceadafe78ffb65dc366761e3" dependencies = [ "lazy_static", - "log 0.4.14", + "log", "tracing-core", ] @@ -1977,12 +1743,6 @@ dependencies = [ "tracing-log", ] -[[package]] -name = "traitobject" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" - [[package]] name = "try-lock" version = "0.2.3" @@ -1990,10 +1750,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] -name = "typeable" -version = "0.1.2" +name = "tungstenite" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" +checksum = "983d40747bce878d2fb67d910dcb8bd3eca2b2358540c3cc1b98c027407a3ae3" +dependencies = [ + "base64", + "byteorder", + "bytes", + "http", + "httparse", + "log", + "native-tls", + "rand", + "sha-1", + "thiserror", + "url", + "utf-8", +] [[package]] name = "typenum" @@ -2001,15 +1775,6 @@ version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec" -[[package]] -name = "unicase" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33" -dependencies = [ - "version_check", -] - [[package]] name = "unicode-bidi" version = "0.3.6" @@ -2037,17 +1802,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" -[[package]] -name = "url" -version = "1.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" -dependencies = [ - "idna 0.1.5", - "matches", - "percent-encoding 1.0.1", -] - [[package]] name = "url" version = "2.2.2" @@ -2055,11 +1809,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" dependencies = [ "form_urlencoded", - "idna 0.2.3", + "idna", "matches", - "percent-encoding 2.1.0", + "percent-encoding", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "vcpkg" version = "0.2.15" @@ -2074,9 +1834,9 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "version_check" -version = "0.1.5" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" [[package]] name = "walkdir" @@ -2095,7 +1855,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" dependencies = [ - "log 0.4.14", + "log", "try-lock", ] @@ -2125,7 +1885,7 @@ checksum = "f34c405b4f0658583dba0c1c7c9b694f3cac32655db463b56c254a1c75269523" dependencies = [ "bumpalo", "lazy_static", - "log 0.4.14", + "log", "proc-macro2", "quote", "syn", @@ -2183,34 +1943,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "websocket" -version = "0.26.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "723abe6b75286edc51d8ecabb38a2353f62a9e9b0588998b59111474f1dcd637" -dependencies = [ - "hyper 0.10.16", - "native-tls", - "rand 0.6.5", - "unicase", - "url 1.7.2", - "websocket-base", -] - -[[package]] -name = "websocket-base" -version = "0.26.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "403f3fd505ff930da84156389639932955fb09705b3dccd1a3d60c8e7ff62776" -dependencies = [ - "base64 0.10.1", - "bitflags", - "byteorder", - "native-tls", - "rand 0.6.5", - "sha-1", -] - [[package]] name = "winapi" version = "0.3.9" diff --git a/engineio/Cargo.toml b/engineio/Cargo.toml index 21f26d50..da843c68 100644 --- a/engineio/Cargo.toml +++ b/engineio/Cargo.toml @@ -18,8 +18,8 @@ reqwest = { version = "0.11.0", features = ["blocking", "native-tls"] } adler32 = "1.2.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -websocket = { version = "0.26.2", features = ["sync-ssl"], default-features = false } thiserror = "1.0" +tungstenite = { version = "0.15.0", features = ["native-tls"] } native-tls = "0.2.7" url = "2.2.2" diff --git a/engineio/src/client/socket.rs b/engineio/src/client/socket.rs index 8e09d7c0..3f37f67e 100644 --- a/engineio/src/client/socket.rs +++ b/engineio/src/client/socket.rs @@ -5,7 +5,7 @@ use crate::transport::Transport; use crate::error::{Error, Result}; use crate::header::HeaderMap; use crate::packet::{HandshakePacket, Packet, PacketId}; -use crate::transports::{PollingTransport, WebsocketSecureTransport, WebsocketTransport}; +use crate::transports::{PollingTransport, WebsocketTransport}; use crate::ENGINE_IO_VERSION; use bytes::Bytes; use native_tls::TlsConnector; @@ -197,13 +197,13 @@ impl SocketBuilder { /// Build socket with only a websocket transport pub fn build_websocket(mut self) -> Result { - // SAFETY: Already a Url - let url = websocket::client::Url::parse(&self.url.to_string())?; + let url = self.url.clone(); match url.scheme() { - "http" | "ws" => { + "http" | "ws" | "https" | "wss" => { let transport = WebsocketTransport::new( url, + None, self.headers .clone() .map(|headers| headers.try_into().unwrap()), @@ -227,31 +227,6 @@ impl SocketBuilder { ), }) } - "https" | "wss" => { - let transport = WebsocketSecureTransport::new( - url, - self.tls_config.clone(), - self.headers.clone().map(|v| v.try_into().unwrap()), - )?; - if self.handshake.is_some() { - transport.upgrade()?; - } else { - self.handshake_with_transport(&transport)?; - } - // NOTE: Although self.url contains the sid, it does not propagate to the transport - // SAFETY: handshake function called previously. - Ok(Socket { - socket: InnerSocket::new( - transport.into(), - self.handshake.unwrap(), - self.on_close, - self.on_data, - self.on_error, - self.on_open, - self.on_packet, - ), - }) - } _ => Err(Error::InvalidUrlScheme(url.scheme().to_string())), } } diff --git a/engineio/src/error.rs b/engineio/src/error.rs index c5e9d560..d8627444 100644 --- a/engineio/src/error.rs +++ b/engineio/src/error.rs @@ -5,7 +5,7 @@ use std::io::Error as IoError; use std::str::Utf8Error; use thiserror::Error; use url::ParseError as UrlParseError; -use websocket::{client::ParseError, WebSocketError}; +use tungstenite::Error as WebSocketError; /// Enumeration of all possible errors in the `socket.io` context. #[derive(Error, Debug)] @@ -42,8 +42,6 @@ pub enum Error { InvalidPoisonedLock(), #[error("Got a websocket error: {0}")] IncompleteResponseFromWebsocket(#[from] WebSocketError), - #[error("Error while parsing the url for the websocket connection: {0}")] - InvalidWebsocketURL(#[from] ParseError), #[error("Got an IO-Error: {0}")] IncompleteIo(#[from] IoError), #[error("Server did not allow upgrading to websockets")] diff --git a/engineio/src/header.rs b/engineio/src/header.rs index 4decec87..65bd51dd 100644 --- a/engineio/src/header.rs +++ b/engineio/src/header.rs @@ -1,13 +1,9 @@ use crate::Error; use bytes::Bytes; -use reqwest::header::{ - HeaderMap as ReqwestHeaderMap, HeaderName as ReqwestHeaderName, - HeaderValue as ReqwestHeaderValue, -}; +use reqwest::header::{HeaderMap as ReqwestHeaderMap, HeaderName as ReqwestHeaderName, HeaderValue as ReqwestHeaderValue}; use std::collections::HashMap; use std::convert::TryFrom; use std::str::FromStr; -use websocket::header::Headers as WebSocketHeaderMap; #[derive(Eq, PartialEq, Hash, Debug, Clone)] pub struct HeaderName { @@ -103,16 +99,6 @@ impl TryFrom for ReqwestHeaderMap { } } -impl From for WebSocketHeaderMap { - fn from(headers: HeaderMap) -> Self { - let mut output = WebSocketHeaderMap::new(); - for (key, val) in headers { - output.append_raw(key.to_string(), val.inner[..].to_vec()); - } - output - } -} - impl IntoIterator for HeaderMap { type Item = (HeaderName, HeaderValue); type IntoIter = IntoIter; diff --git a/engineio/src/transport.rs b/engineio/src/transport.rs index 39ff5c91..c863d73c 100644 --- a/engineio/src/transport.rs +++ b/engineio/src/transport.rs @@ -1,4 +1,4 @@ -use super::transports::{PollingTransport, WebsocketSecureTransport, WebsocketTransport}; +use super::transports::{PollingTransport, WebsocketTransport}; use crate::error::Result; use adler32::adler32; use bytes::Bytes; @@ -35,7 +35,6 @@ pub trait Transport { #[derive(Debug)] pub enum TransportType { Polling(PollingTransport), - WebsocketSecure(WebsocketSecureTransport), Websocket(WebsocketTransport), } @@ -45,12 +44,6 @@ impl From for TransportType { } } -impl From for TransportType { - fn from(transport: WebsocketSecureTransport) -> Self { - TransportType::WebsocketSecure(transport) - } -} - impl From for TransportType { fn from(transport: WebsocketTransport) -> Self { TransportType::Websocket(transport) @@ -62,7 +55,6 @@ impl TransportType { match self { TransportType::Polling(transport) => transport, TransportType::Websocket(transport) => transport, - TransportType::WebsocketSecure(transport) => transport, } } } diff --git a/engineio/src/transports/mod.rs b/engineio/src/transports/mod.rs index 8f17c5eb..e264b9c6 100644 --- a/engineio/src/transports/mod.rs +++ b/engineio/src/transports/mod.rs @@ -1,7 +1,5 @@ mod polling; mod websocket; -mod websocket_secure; pub use self::polling::PollingTransport; pub use self::websocket::WebsocketTransport; -pub use self::websocket_secure::WebsocketSecureTransport; diff --git a/engineio/src/transports/websocket.rs b/engineio/src/transports/websocket.rs index 5d3ff8d9..ea9f8518 100644 --- a/engineio/src/transports/websocket.rs +++ b/engineio/src/transports/websocket.rs @@ -1,69 +1,96 @@ -use crate::error::{Error, Result}; +use crate::error::Error; +use crate::error::Result; +use crate::header::HeaderMap; use crate::packet::Packet; use crate::packet::PacketId; use crate::transport::Transport; use bytes::{BufMut, Bytes, BytesMut}; +use native_tls::TlsConnector; +use tungstenite::Message; +use tungstenite::client_tls_with_config; +use tungstenite::connect; +use url::Url; +use tungstenite::WebSocket; +use tungstenite::stream::MaybeTlsStream; use std::borrow::Cow; +use std::convert::TryFrom; +use std::convert::TryInto; +use std::net::TcpStream; use std::str::from_utf8; use std::sync::{Arc, Mutex, RwLock}; -use websocket::{ - client::Url, dataframe::Opcode, header::Headers, receiver::Reader, sync::stream::TcpStream, - sync::Writer, ws::dataframe::DataFrame, ClientBuilder as WsClientBuilder, Message, -}; +use tungstenite::client::IntoClientRequest; +use tungstenite::Connector::NativeTls; #[derive(Clone)] pub struct WebsocketTransport { - sender: Arc>>, - receiver: Arc>>, + client: Arc>>>, base_url: Arc>, } impl WebsocketTransport { - /// Creates an instance of `WebsocketTransport`. - pub fn new(base_url: Url, headers: Option) -> Result { + /// Creates an instance of `WebsocketSecureTransport`. + pub fn new( + base_url: Url, + tls_config: Option, + headers: Option, + ) -> Result { let mut url = base_url; + match url.scheme() { + "http" => url.set_scheme("ws").unwrap(), + "https" => url.set_scheme("wss").unwrap(), + _ => () + }; url.query_pairs_mut().append_pair("transport", "websocket"); - url.set_scheme("ws").unwrap(); - let mut client_builder = WsClientBuilder::new(url[..].as_ref())?; + let mut request = url.clone().into_client_request()?; if let Some(headers) = headers { - client_builder = client_builder.custom_headers(&headers); + let output_headers = request.headers_mut(); + for (key, value) in headers.into_iter() { + output_headers.append(reqwest::header::HeaderName::try_from(key)?, value.try_into()?); + } } - let client = client_builder.connect_insecure()?; - - client.set_nonblocking(false)?; - - let (receiver, sender) = client.split()?; + let (client, _) = match tls_config { + None => { + connect(request)? + }, + Some(connector) => { + let stream = TcpStream::connect(url.socket_addrs(|| None)?[0])?; + match client_tls_with_config(request, stream, None, Some(NativeTls(connector))) { + Ok(websocket) => Ok(websocket), + Err(err) => Err(Error::InvalidHandshake(err.to_string())) + }? + } + }; Ok(WebsocketTransport { - sender: Arc::new(Mutex::new(sender)), - receiver: Arc::new(Mutex::new(receiver)), + client: Arc::new(Mutex::new(client)), // SAFTEY: already a URL parsing can't fail - base_url: Arc::new(RwLock::new(url::Url::parse(&url.to_string())?)), + base_url: Arc::new(RwLock::new(url)), }) } /// Sends probe packet to ensure connection is valid, then sends upgrade /// request pub(crate) fn upgrade(&self) -> Result<()> { - let mut sender = self.sender.lock()?; - let mut receiver = self.receiver.lock()?; + let mut client = self.client.lock()?; // send the probe packet, the text `2probe` represents a ping packet with // the content `probe` - sender.send_message(&Message::text(Cow::Borrowed(from_utf8(&Bytes::from( + client.write_message(Message::text(Cow::Borrowed(from_utf8(&Bytes::from( Packet::new(PacketId::Ping, Bytes::from("probe")), ))?)))?; // expect to receive a probe packet - let message = receiver.recv_message()?; - if message.take_payload() != Bytes::from(Packet::new(PacketId::Pong, Bytes::from("probe"))) + let message = client.read_message()?; + let payload = message.into_data(); + if Packet::try_from(Bytes::from(payload))? + != Packet::new(PacketId::Pong, Bytes::from("probe")) { return Err(Error::InvalidPacket()); } // finally send the upgrade request. the payload `5` stands for an upgrade // packet without any payload - sender.send_message(&Message::text(Cow::Borrowed(from_utf8(&Bytes::from( + client.write_message(Message::text(Cow::Borrowed(from_utf8(&Bytes::from( Packet::new(PacketId::Upgrade, Bytes::from("")), ))?)))?; @@ -73,32 +100,34 @@ impl WebsocketTransport { impl Transport for WebsocketTransport { fn emit(&self, data: Bytes, is_binary_att: bool) -> Result<()> { - let mut sender = self.sender.lock()?; - let message = if is_binary_att { Message::binary(Cow::Borrowed(data.as_ref())) } else { Message::text(Cow::Borrowed(std::str::from_utf8(data.as_ref())?)) }; - sender.send_message(&message)?; + + let mut writer = self.client.lock()?; + writer.write_message(message)?; + drop(writer); Ok(()) } fn poll(&self) -> Result { - let mut receiver = self.receiver.lock()?; - - // if this is a binary payload, we mark it as a message - let received_df = receiver.recv_dataframe()?; - match received_df.opcode { - Opcode::Binary => { - let mut message = BytesMut::with_capacity(received_df.data.len() + 1); + let mut receiver = self.client.lock()?; + + let message = receiver.read_message()?; + drop(receiver); + match message { + Message::Binary(binary) => { + let mut message = BytesMut::with_capacity(binary.len() + 1); + // Prepend the message ID for consistency. message.put_u8(PacketId::Message as u8); - message.put(received_df.take_payload().as_ref()); + message.put(binary.as_ref()); Ok(message.freeze()) } - _ => Ok(Bytes::from(received_df.take_payload())), + _ => Ok(Bytes::from(message.into_data())), } } @@ -114,7 +143,7 @@ impl Transport for WebsocketTransport { { url.query_pairs_mut().append_pair("transport", "websocket"); } - url.set_scheme("ws").unwrap(); + url.set_scheme("wss").unwrap(); *self.base_url.write()? = url; Ok(()) } @@ -123,7 +152,7 @@ impl Transport for WebsocketTransport { impl std::fmt::Debug for WebsocketTransport { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.write_fmt(format_args!( - "WebsocketTransport(base_url: {:?})", + "WebsocketSecureTransport(base_url: {:?})", self.base_url(), )) } @@ -134,28 +163,30 @@ mod test { use super::*; use crate::ENGINE_IO_VERSION; use std::str::FromStr; - fn new() -> Result { - let url = crate::test::engine_io_server()?.to_string() + let url = crate::test::engine_io_server_secure()?.to_string() + "engine.io/?EIO=" + &ENGINE_IO_VERSION.to_string(); - WebsocketTransport::new(Url::from_str(&url[..])?, None) + WebsocketTransport::new( + Url::from_str(&url[..])?, + Some(crate::test::tls_connector()?), + None, + ) } - #[test] - fn websocket_transport_base_url() -> Result<()> { + fn websocket_secure_transport_base_url() -> Result<()> { let transport = new()?; - let mut url = crate::test::engine_io_server()?; + let mut url = crate::test::engine_io_server_secure()?; url.set_path("/engine.io/"); url.query_pairs_mut() .append_pair("EIO", &ENGINE_IO_VERSION.to_string()) .append_pair("transport", "websocket"); - url.set_scheme("ws").unwrap(); + url.set_scheme("wss").unwrap(); assert_eq!(transport.base_url()?.to_string(), url.to_string()); transport.set_base_url(reqwest::Url::parse("https://127.0.0.1")?)?; assert_eq!( transport.base_url()?.to_string(), - "ws://127.0.0.1/?transport=websocket" + "wss://127.0.0.1/?transport=websocket" ); assert_ne!(transport.base_url()?.to_string(), url.to_string()); @@ -164,7 +195,7 @@ mod test { )?)?; assert_eq!( transport.base_url()?.to_string(), - "ws://127.0.0.1/?transport=websocket" + "wss://127.0.0.1/?transport=websocket" ); assert_ne!(transport.base_url()?.to_string(), url.to_string()); Ok(()) @@ -175,7 +206,10 @@ mod test { let transport = new()?; assert_eq!( format!("{:?}", transport), - format!("WebsocketTransport(base_url: {:?})", transport.base_url()) + format!( + "WebsocketSecureTransport(base_url: {:?})", + transport.base_url() + ) ); Ok(()) } diff --git a/engineio/src/transports/websocket_secure.rs b/engineio/src/transports/websocket_secure.rs deleted file mode 100644 index 3b3a94cb..00000000 --- a/engineio/src/transports/websocket_secure.rs +++ /dev/null @@ -1,198 +0,0 @@ -use crate::error::Error; -use crate::error::Result; -use crate::packet::Packet; -use crate::packet::PacketId; -use crate::transport::Transport; -use bytes::{BufMut, Bytes, BytesMut}; -use native_tls::TlsConnector; -use std::borrow::Cow; -use std::convert::TryFrom; -use std::str::from_utf8; -use std::sync::{Arc, Mutex, RwLock}; -use websocket::{ - client::sync::Client as WsClient, - client::Url, - dataframe::Opcode, - header::Headers, - sync::stream::{TcpStream, TlsStream}, - ws::dataframe::DataFrame, - ClientBuilder as WsClientBuilder, Message, -}; - -#[derive(Clone)] -pub struct WebsocketSecureTransport { - client: Arc>>>, - base_url: Arc>, -} - -impl WebsocketSecureTransport { - /// Creates an instance of `WebsocketSecureTransport`. - pub fn new( - base_url: Url, - tls_config: Option, - headers: Option, - ) -> Result { - let mut url = base_url; - url.query_pairs_mut().append_pair("transport", "websocket"); - url.set_scheme("wss").unwrap(); - let mut client_builder = WsClientBuilder::new(url[..].as_ref())?; - if let Some(headers) = headers { - client_builder = client_builder.custom_headers(&headers); - } - let client = client_builder.connect_secure(tls_config)?; - - client.set_nonblocking(false)?; - - Ok(WebsocketSecureTransport { - client: Arc::new(Mutex::new(client)), - // SAFTEY: already a URL parsing can't fail - base_url: Arc::new(RwLock::new(url::Url::parse(&url.to_string())?)), - }) - } - - /// Sends probe packet to ensure connection is valid, then sends upgrade - /// request - pub(crate) fn upgrade(&self) -> Result<()> { - let mut client = self.client.lock()?; - - // send the probe packet, the text `2probe` represents a ping packet with - // the content `probe` - client.send_message(&Message::text(Cow::Borrowed(from_utf8(&Bytes::from( - Packet::new(PacketId::Ping, Bytes::from("probe")), - ))?)))?; - - // expect to receive a probe packet - let message = client.recv_message()?; - let payload = message.take_payload(); - if Packet::try_from(Bytes::from(payload))? - != Packet::new(PacketId::Pong, Bytes::from("probe")) - { - return Err(Error::InvalidPacket()); - } - - // finally send the upgrade request. the payload `5` stands for an upgrade - // packet without any payload - client.send_message(&Message::text(Cow::Borrowed(from_utf8(&Bytes::from( - Packet::new(PacketId::Upgrade, Bytes::from("")), - ))?)))?; - - Ok(()) - } -} - -impl Transport for WebsocketSecureTransport { - fn emit(&self, data: Bytes, is_binary_att: bool) -> Result<()> { - let message = if is_binary_att { - Message::binary(Cow::Borrowed(data.as_ref())) - } else { - Message::text(Cow::Borrowed(std::str::from_utf8(data.as_ref())?)) - }; - - let mut writer = self.client.lock()?; - writer.send_message(&message)?; - drop(writer); - - Ok(()) - } - - fn poll(&self) -> Result { - let mut receiver = self.client.lock()?; - - // if this is a binary payload, we mark it as a message - let received_df = receiver.recv_dataframe()?; - drop(receiver); - match received_df.opcode { - Opcode::Binary => { - let mut message = BytesMut::with_capacity(received_df.data.len() + 1); - message.put_u8(PacketId::Message as u8); - message.put(received_df.take_payload().as_ref()); - - Ok(message.freeze()) - } - _ => Ok(Bytes::from(received_df.take_payload())), - } - } - - fn base_url(&self) -> Result { - Ok(self.base_url.read()?.clone()) - } - - fn set_base_url(&self, url: url::Url) -> Result<()> { - let mut url = url; - if !url - .query_pairs() - .any(|(k, v)| k == "transport" && v == "websocket") - { - url.query_pairs_mut().append_pair("transport", "websocket"); - } - url.set_scheme("wss").unwrap(); - *self.base_url.write()? = url; - Ok(()) - } -} - -impl std::fmt::Debug for WebsocketSecureTransport { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.write_fmt(format_args!( - "WebsocketSecureTransport(base_url: {:?})", - self.base_url(), - )) - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::ENGINE_IO_VERSION; - use std::str::FromStr; - fn new() -> Result { - let url = crate::test::engine_io_server_secure()?.to_string() - + "engine.io/?EIO=" - + &ENGINE_IO_VERSION.to_string(); - WebsocketSecureTransport::new( - Url::from_str(&url[..])?, - Some(crate::test::tls_connector()?), - None, - ) - } - #[test] - fn websocket_secure_transport_base_url() -> Result<()> { - let transport = new()?; - let mut url = crate::test::engine_io_server_secure()?; - url.set_path("/engine.io/"); - url.query_pairs_mut() - .append_pair("EIO", &ENGINE_IO_VERSION.to_string()) - .append_pair("transport", "websocket"); - url.set_scheme("wss").unwrap(); - assert_eq!(transport.base_url()?.to_string(), url.to_string()); - transport.set_base_url(reqwest::Url::parse("https://127.0.0.1")?)?; - assert_eq!( - transport.base_url()?.to_string(), - "wss://127.0.0.1/?transport=websocket" - ); - assert_ne!(transport.base_url()?.to_string(), url.to_string()); - - transport.set_base_url(reqwest::Url::parse( - "http://127.0.0.1/?transport=websocket", - )?)?; - assert_eq!( - transport.base_url()?.to_string(), - "wss://127.0.0.1/?transport=websocket" - ); - assert_ne!(transport.base_url()?.to_string(), url.to_string()); - Ok(()) - } - - #[test] - fn websocket_secure_debug() -> Result<()> { - let transport = new()?; - assert_eq!( - format!("{:?}", transport), - format!( - "WebsocketSecureTransport(base_url: {:?})", - transport.base_url() - ) - ); - Ok(()) - } -} From 87b7e0d08123700ce28a56b36755affc3e1e64ce Mon Sep 17 00:00:00 2001 From: Nathan Shaaban Date: Fri, 1 Oct 2021 22:26:08 +0000 Subject: [PATCH 2/4] docs: update changelog --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83253617..985afe74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,11 @@ The format is based on [Keep a Changelog], and this project adheres to ## _[Unreleased]_ -_nothing new to show for… yet!_ +### Changes + +* Switch Websocket library to tungstenite-rs + * Removed InvalidWebsocketURL + * Changed inner type of WebSocketError to tungstenite-rs' ## [0.3.0-alpha] - _Refactoring_ From e48a7060a47e99f6ea4e963ff5cf62911f1b04a7 Mon Sep 17 00:00:00 2001 From: Nathan Shaaban Date: Fri, 1 Oct 2021 22:31:53 +0000 Subject: [PATCH 3/4] refactor: format --- engineio/src/error.rs | 2 +- engineio/src/header.rs | 5 ++++- engineio/src/transports/websocket.rs | 25 +++++++++++++------------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/engineio/src/error.rs b/engineio/src/error.rs index d8627444..8eae990a 100644 --- a/engineio/src/error.rs +++ b/engineio/src/error.rs @@ -4,8 +4,8 @@ use serde_json::Error as JsonError; use std::io::Error as IoError; use std::str::Utf8Error; use thiserror::Error; -use url::ParseError as UrlParseError; use tungstenite::Error as WebSocketError; +use url::ParseError as UrlParseError; /// Enumeration of all possible errors in the `socket.io` context. #[derive(Error, Debug)] diff --git a/engineio/src/header.rs b/engineio/src/header.rs index 65bd51dd..f42fd5b6 100644 --- a/engineio/src/header.rs +++ b/engineio/src/header.rs @@ -1,6 +1,9 @@ use crate::Error; use bytes::Bytes; -use reqwest::header::{HeaderMap as ReqwestHeaderMap, HeaderName as ReqwestHeaderName, HeaderValue as ReqwestHeaderValue}; +use reqwest::header::{ + HeaderMap as ReqwestHeaderMap, HeaderName as ReqwestHeaderName, + HeaderValue as ReqwestHeaderValue, +}; use std::collections::HashMap; use std::convert::TryFrom; use std::str::FromStr; diff --git a/engineio/src/transports/websocket.rs b/engineio/src/transports/websocket.rs index ea9f8518..7ee48686 100644 --- a/engineio/src/transports/websocket.rs +++ b/engineio/src/transports/websocket.rs @@ -6,12 +6,6 @@ use crate::packet::PacketId; use crate::transport::Transport; use bytes::{BufMut, Bytes, BytesMut}; use native_tls::TlsConnector; -use tungstenite::Message; -use tungstenite::client_tls_with_config; -use tungstenite::connect; -use url::Url; -use tungstenite::WebSocket; -use tungstenite::stream::MaybeTlsStream; use std::borrow::Cow; use std::convert::TryFrom; use std::convert::TryInto; @@ -19,7 +13,13 @@ use std::net::TcpStream; use std::str::from_utf8; use std::sync::{Arc, Mutex, RwLock}; use tungstenite::client::IntoClientRequest; +use tungstenite::client_tls_with_config; +use tungstenite::connect; +use tungstenite::stream::MaybeTlsStream; use tungstenite::Connector::NativeTls; +use tungstenite::Message; +use tungstenite::WebSocket; +use url::Url; #[derive(Clone)] pub struct WebsocketTransport { @@ -38,25 +38,26 @@ impl WebsocketTransport { match url.scheme() { "http" => url.set_scheme("ws").unwrap(), "https" => url.set_scheme("wss").unwrap(), - _ => () + _ => (), }; url.query_pairs_mut().append_pair("transport", "websocket"); let mut request = url.clone().into_client_request()?; if let Some(headers) = headers { let output_headers = request.headers_mut(); for (key, value) in headers.into_iter() { - output_headers.append(reqwest::header::HeaderName::try_from(key)?, value.try_into()?); + output_headers.append( + reqwest::header::HeaderName::try_from(key)?, + value.try_into()?, + ); } } let (client, _) = match tls_config { - None => { - connect(request)? - }, + None => connect(request)?, Some(connector) => { let stream = TcpStream::connect(url.socket_addrs(|| None)?[0])?; match client_tls_with_config(request, stream, None, Some(NativeTls(connector))) { Ok(websocket) => Ok(websocket), - Err(err) => Err(Error::InvalidHandshake(err.to_string())) + Err(err) => Err(Error::InvalidHandshake(err.to_string())), }? } }; From b84b5d989cfb839e4490090211d4a5d2c5dcd3e5 Mon Sep 17 00:00:00 2001 From: Nathan Shaaban Date: Fri, 1 Oct 2021 22:41:19 +0000 Subject: [PATCH 4/4] fix: tls_config in builder --- engineio/src/client/socket.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engineio/src/client/socket.rs b/engineio/src/client/socket.rs index 3f37f67e..9b92b338 100644 --- a/engineio/src/client/socket.rs +++ b/engineio/src/client/socket.rs @@ -203,7 +203,7 @@ impl SocketBuilder { "http" | "ws" | "https" | "wss" => { let transport = WebsocketTransport::new( url, - None, + self.tls_config.clone(), self.headers .clone() .map(|headers| headers.try_into().unwrap()),