Uniform TCP / TLS / SOCK5 Stream - TraitObject, Enum Type, Don't know

Could really use some help. Developing out a HTTP client, Rquest always threw openssl errors that frustrated me, plus I wanted something simple that also resembled PHP's PSR-7 standard.

Anyway, close to being done but problem getting a uniform connection for HTTP, SSL and SOCK5 streams. Here:

Line 134, if I could simply get that to compile uncommented I'd be good to go. Tried trait objects, enum type, and hunreds of attempts but to no avail.

Some examples of what I've played around with:
rust-atlas-http/tls_not_working.rs at master · mdizak/rust-atlas-http · GitHub

None of that compiles, but have played around with both attempts loads to no avail. Always get yelled at by the compiler because creating the TLS stream (line 126 of client.rs) has mutable references to conn and sock, hence have difficult passing it or returning it. Tried Box::new(), including all three within struct of trait object, enum type, etc.

Here's the error I'm getting:

error[E0597]: `sock` does not live long enough
   --> lib/http/src/client.rs:126:57
115 |         let mut sock = self.connect(&uri, &port)?;
    |             -------- binding `sock` declared here
126 |         let mut stream = rustls::Stream::new(&mut conn, &mut sock);
    |                                                         ^^^^^^^^^ borrowed value does not live long enough
134 |         let (version, status, reason, header_lines) = self.read_header(&mut reader, &req)?;
    |                                                                        ----------- cast requires that `sock` is borrowed for `'static`
214 |     }
    |     - `sock` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `atlas-http` (lib) due to 2 previous errors

Any help would be greatly appreciated, as I'd love to get this wrapped up. Plus I just want to know this for future reference. if wanted, test yourself with "cargo add atlas-http", then for example:

    use atlas_http::HttpClient;

    fn main() {
        let mut http = HttpClient::new();
        let res = http.get("https://www.google.com/").unwrap();
        println!("Status: {}", res.status_code());

Any help appreciated, thanks in advance.

have you tried using rustls::StreamOwned instead of Stream?

1 Like

Thanks so much, that worked beautifully. Apologies for not seeing it myself, but blind and the Rust docs aren't overly accessible via screen reader. It's on my todo list to clean those up and have cargo generate a more blind friendly version, but will get to it shortly.

Anyway, that StreamOwned worked beautifully, thanks again.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.