Debian Codegraph @

examples/std-https-v1_0-rustls.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #![cfg(feature = "std")]
2
3 use std::{
4 env,
5 io::{stdin, stdout, Write as _},
6 net::TcpStream,
7 sync::Arc,
8 };
9
10 use io_stream::{
11 coroutines::{
12 read::{ReadStream, ReadStreamResult},
13 write::{WriteStream, WriteStreamResult},
14 },
15 runtimes::std::handle,
16 };
17 use memchr::memmem;
18 use rustls::{ClientConfig, ClientConnection, StreamOwned};
19 use rustls_platform_verifier::ConfigVerifierExt;
20 use url::Url;
21
22 fn main() {
23 env_logger::init();
24
25 let url: Url = match env::var("URL") {
26 Ok(url) => url.parse().unwrap(),
27 Err(_) => read_line("URL?").parse().unwrap(),
28 };
29
30 let mut stream = connect(&url);
31
32 let request = format!(
33 "GET {} HTTP/1.0\r\nHost: {}:{}\r\n\r\n",
34 url.path(),
35 url.host_str().unwrap(),
36 url.port_or_known_default().unwrap(),
37 );
38
39 println!("request: {request:?}");
40
41 let mut arg = None;
42 let mut write = WriteStream::new(request.into_bytes());
43
44 loop {
45 match write.resume(arg) {
46 WriteStreamResult::Ok(_) => break,
47 WriteStreamResult::Err(err) => panic!("{err}"),
48 WriteStreamResult::Eof => panic!("reached unexpected EOF"),
49 WriteStreamResult::Io(io) => arg = Some(handle(&mut stream, io).unwrap()),
50 }
51 }
52
53 let mut response = Vec::new();
54
55 loop {
56 let mut arg = None;
57 let mut read = ReadStream::new();
58
59 let output = loop {
60 match read.resume(arg) {
61 ReadStreamResult::Ok(output) => break output,
62 ReadStreamResult::Err(err) => panic!("{err}"),
63 ReadStreamResult::Eof => panic!("reached unexpected EOF"),
64 ReadStreamResult::Io(io) => arg = Some(handle(&mut stream, io).unwrap()),
65 }
66 };
67
68 let bytes = output.bytes();
69
70 match memmem::find(bytes, &[b'\r', b'\n', b'\r', b'\n']) {
71 None => {
72 response.extend(bytes);
73 continue;
74 }
75 Some(n) => {
76 response.extend(&bytes[..n]);
77 break;
78 }
79 }
80 }
81
82 println!("----------------");
83 println!("{}", String::from_utf8_lossy(&response));
84 println!("----------------");
85 }
86
87 fn read_line(prompt: &str) -> String {
88 print!("{prompt} ");
89 stdout().flush().unwrap();
90
91 let mut line = String::new();
92 stdin().read_line(&mut line).unwrap();
93
94 line.trim().to_owned()
95 }
96
97 trait StreamExt: std::io::Read + std::io::Write {}
98 impl<T: std::io::Read + std::io::Write> StreamExt for T {}
99
100 fn connect(url: &Url) -> Box<dyn StreamExt> {
101 let domain = url.domain().unwrap();
102
103 if url.scheme().eq_ignore_ascii_case("https") {
•104 let config = ClientConfig::with_platform_verifier()
105 .expect("Failed to load TLS certificates")
106 .into();
107 let server_name = domain.to_string().try_into().unwrap();
•108 let conn = ClientConnection::new(config, server_name).unwrap();
109 let tcp = TcpStream::connect((domain.to_string(), 443)).unwrap();
110 let tls = StreamOwned::new(conn, tcp);
111 Box::new(tls)
112 } else {
113 let tcp = TcpStream::connect((domain.to_string(), 80)).unwrap();
114 Box::new(tcp)
115 }
116 }