ureq 3.5.1

Simple, safe HTTP client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
use std::fmt;
use std::io::{self, Read, Write};
use std::net::SocketAddr;

use crate::Error;
use crate::proxy::{Proxy, ProxyProtocol};
use crate::util::{AuthorityExt, UriExt};

use super::chain::Either;

use super::tcp::{self, TcpTransport};
use super::time::Instant;
use super::{ConnectionDetails, Connector, LazyBuffers, NextTimeout, ResolvedSocketAddrs};
use super::{Transport, TransportAdapter};

/// Connector for SOCKS proxies.
///
/// Requires the **socks-proxy** feature.
///
/// The connector looks at the proxy settings in [`proxy`](crate::config::ConfigBuilder::proxy) to
/// determine whether to attempt a proxy connection or not.
#[derive(Default)]
pub struct SocksConnector(());

impl<In: Transport> Connector<In> for SocksConnector {
    type Out = Either<In, TcpTransport>;

    fn connect(
        &self,
        details: &ConnectionDetails,
        chained: Option<In>,
    ) -> Result<Option<Self::Out>, Error> {
        Self::connect_with(details, chained, |addrs, now, timeout| {
            let stream = tcp::try_connect(
                addrs,
                now,
                timeout,
                details.current_time.clone(),
                details.config,
            )?;
            let buffers = LazyBuffers::new(
                details.config.input_buffer_size(),
                details.config.output_buffer_size(),
            );
            let transport = TcpTransport::new(stream, buffers);
            (details.register_shutdown)(transport.shutdown_handle())?;
            Ok(transport)
        })
    }
}

impl SocksConnector {
    fn connect_with<In: Transport, Out: Transport>(
        details: &ConnectionDetails,
        chained: Option<In>,
        connect: impl FnOnce(&ResolvedSocketAddrs, Instant, NextTimeout) -> Result<Out, Error>,
    ) -> Result<Option<Either<In, Out>>, Error> {
        let proxy = match details.config.proxy() {
            Some(v) if v.protocol().is_socks() => v,
            // If there is no proxy configured, or it isn't a SOCKS proxy, use whatever is chained.
            _ => {
                trace!("SOCKS not configured");
                return Ok(chained.map(Either::A));
            }
        };

        if chained.is_some() {
            trace!("Skip");
            return Ok(chained.map(Either::A));
        }

        // Check if this host is not supposed to be proxied. A direct connection
        // does not need the proxy, so do this before resolving it.
        if proxy.is_no_proxy(details.uri) {
            return Ok(None);
        }

        let target = Target::new(details, proxy)?;

        // Keep all setup steps within the budget that started before this connector.
        let timeout_at = |now: Instant| {
            let mut timeout = details.timeout;
            if !timeout.after.is_not_happening() {
                let elapsed = now.duration_since(details.now);
                timeout.after = timeout.after.saturating_sub(*elapsed).into();
            }
            timeout.check()
        };

        let proxy_addrs = details.resolver.resolve(
            proxy.uri(),
            details.config,
            timeout_at((details.current_time)())?,
        )?;

        // Connect to the proxy like TcpConnector connects to a server, within the
        // connect timeout and trying each resolved address.
        let now = (details.current_time)();
        let transport = connect(&proxy_addrs, now, timeout_at(now)?)?;

        // The adapter deducts elapsed time between handshake reads and writes,
        // while TcpTransport also enforces the per-read and per-write limits.
        let mut w = TransportAdapter::new(transport, details.current_time.clone());
        w.set_timeout(timeout_at((details.current_time)())?);

        trace!("Try connect {} -> {:?}", proxy.protocol(), target);
        handshake(&mut w, proxy, target)?;
        debug!("{} connected -> {:?}", proxy.protocol(), target);

        Ok(Some(Either::B(w.into_inner())))
    }
}

/// The address the proxy is asked to connect to.
#[derive(Debug, Clone, Copy)]
enum Target<'a> {
    Ip(SocketAddr),
    Domain(&'a str, u16),
}

impl<'a> Target<'a> {
    fn new(details: &ConnectionDetails<'a>, proxy: &Proxy) -> Result<Self, Error> {
        if proxy.resolve_target() {
            // The target is already resolved by run().
            let addr = details.addrs.first().ok_or(Error::HostNotFound)?;
            return Ok(Target::Ip(*addr));
        }

        // Do not resolve the target locally, instead pass (host, port)
        // to the proxy and let it resolve. An IP address is passed as one.
        let (_, port) = details.uri.host_port();
        // unwrap is ok because run() checks ensure_valid_url().
        let host = details.uri.authority().unwrap().host_bare();

        match host.parse() {
            Ok(ip) => Ok(Target::Ip(SocketAddr::new(ip, port))),
            Err(_) => Ok(Target::Domain(host, port)),
        }
    }

    fn port(&self) -> u16 {
        match self {
            Target::Ip(addr) => addr.port(),
            Target::Domain(_, port) => *port,
        }
    }
}

fn handshake<S: Read + Write>(w: &mut S, proxy: &Proxy, target: Target) -> io::Result<()> {
    match proxy.protocol() {
        ProxyProtocol::Socks4 | ProxyProtocol::Socks4A => {
            if proxy.username().is_some() {
                debug!("SOCKS4 does not support username/password");
            }

            socks4(w, target)
        }

        ProxyProtocol::Socks5 | ProxyProtocol::Socks5h => socks5(w, proxy, target),

        _ => unreachable!(), // HTTP(s) proxies.
    }
}

/// SOCKS4 CONNECT, and SOCKS4A when the target is a host name. The user id is empty.
fn socks4<S: Read + Write>(w: &mut S, target: Target) -> io::Result<()> {
    // Version, command (CONNECT), port.
    let mut request = vec![4, 1];
    request.extend_from_slice(&target.port().to_be_bytes());

    match target {
        Target::Ip(SocketAddr::V4(addr)) => {
            request.extend_from_slice(&addr.ip().octets());
            request.push(0); // user id
        }
        Target::Ip(SocketAddr::V6(_)) => {
            return Err(invalid_input("SOCKS4 does not support IPv6"));
        }
        Target::Domain(host, _) => {
            // SOCKS4A: the address 0.0.0.1 tells the proxy to resolve the host
            // name sent after the user id.
            request.extend_from_slice(&[0, 0, 0, 1]);
            request.push(0); // user id
            request.extend_from_slice(host.as_bytes());
            request.push(0);
        }
    }

    w.write_all(&request)?;

    // Version (0), status, then a port and address that are not used for CONNECT.
    let mut reply = [0; 8];
    w.read_exact(&mut reply)?;

    if reply[0] != 0 {
        return Err(invalid_data("invalid response version"));
    }

    match reply[1] {
        90 => Ok(()),
        91 => Err(io::Error::other("request rejected or failed")),
        92 => Err(permission_denied(
            "request rejected because SOCKS server cannot connect to identd on the client",
        )),
        93 => Err(permission_denied(
            "request rejected because the client program and identd report different user-ids",
        )),
        _ => Err(invalid_data("invalid response code")),
    }
}

/// SOCKS5 CONNECT (RFC 1928), with username/password authentication (RFC 1929)
/// when the proxy has a username.
fn socks5<S: Read + Write>(w: &mut S, proxy: &Proxy, target: Target) -> io::Result<()> {
    let username = proxy.username();

    // Version, then the offered methods. No authentication (0) is always offered,
    // username/password (2) only when there is a username.
    let greeting: &[u8] = if username.is_some() {
        &[5, 2, 2, 0]
    } else {
        &[5, 1, 0]
    };
    w.write_all(greeting)?;

    // Version, then the method the proxy chose.
    let mut reply = [0; 2];
    w.read_exact(&mut reply)?;

    if reply[0] != 5 {
        return Err(invalid_data("invalid response version"));
    }

    match (reply[1], username) {
        (0, _) => {}
        (2, Some(username)) => {
            let password = proxy.password().unwrap_or("");
            authenticate(w, username, password)?;
        }
        (0xff, _) => return Err(io::Error::other("no acceptable auth methods")),
        _ => return Err(io::Error::other("unknown auth method")),
    }

    // Version, command (CONNECT), reserved, address type, address, port.
    let mut request = vec![5, 1, 0];

    match target {
        Target::Ip(SocketAddr::V4(addr)) => {
            request.push(1);
            request.extend_from_slice(&addr.ip().octets());
        }
        Target::Ip(SocketAddr::V6(addr)) => {
            request.push(4);
            request.extend_from_slice(&addr.ip().octets());
        }
        Target::Domain(host, _) => {
            let len =
                u8::try_from(host.len()).map_err(|_| invalid_input("domain name too long"))?;
            request.push(3);
            request.push(len);
            request.extend_from_slice(host.as_bytes());
        }
    }

    request.extend_from_slice(&target.port().to_be_bytes());
    w.write_all(&request)?;

    // Version, then status.
    w.read_exact(&mut reply)?;

    if reply[0] != 5 {
        return Err(invalid_data("invalid response version"));
    }

    if reply[1] != 0 {
        return Err(socks5_failure(reply[1]));
    }

    // Reserved, then the type of the address the proxy bound. The address and
    // port are not used for CONNECT, but must be read past.
    w.read_exact(&mut reply)?;

    if reply[0] != 0 {
        return Err(invalid_data("invalid reserved byte"));
    }

    let len = match reply[1] {
        1 => 4,
        3 => {
            let mut len = [0];
            w.read_exact(&mut len)?;
            len[0] as usize
        }
        4 => 16,
        _ => return Err(io::Error::other("unsupported address type")),
    };

    let mut bound = [0; u8::MAX as usize + 2];
    w.read_exact(&mut bound[..len + 2])?;

    Ok(())
}

/// The error for a SOCKS5 reply other than succeeded (0).
fn socks5_failure(reply: u8) -> io::Error {
    let reason = match reply {
        1 => "general SOCKS server failure",
        2 => "connection not allowed by ruleset",
        3 => "network unreachable",
        4 => "host unreachable",
        5 => "connection refused",
        6 => "TTL expired",
        7 => "command not supported",
        8 => "address kind not supported",
        _ => "unknown error",
    };

    io::Error::other(reason)
}

fn authenticate<S: Read + Write>(w: &mut S, username: &str, password: &str) -> io::Result<()> {
    // Each is sent after its length in one byte, and must not be empty.
    let valid = 1..=u8::MAX as usize;

    if !valid.contains(&username.len()) {
        return Err(invalid_input("invalid username"));
    }
    if !valid.contains(&password.len()) {
        return Err(invalid_input("invalid password"));
    }

    // Version, username, password.
    let mut request = vec![1];
    request.push(username.len() as u8);
    request.extend_from_slice(username.as_bytes());
    request.push(password.len() as u8);
    request.extend_from_slice(password.as_bytes());
    w.write_all(&request)?;

    // Version, then status.
    let mut reply = [0; 2];
    w.read_exact(&mut reply)?;

    if reply[0] != 1 {
        return Err(invalid_data("invalid response version"));
    }

    if reply[1] != 0 {
        return Err(permission_denied("password authentication failed"));
    }

    Ok(())
}

fn invalid_input(reason: &'static str) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidInput, reason)
}

fn invalid_data(reason: &'static str) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, reason)
}

fn permission_denied(reason: &'static str) -> io::Error {
    io::Error::new(io::ErrorKind::PermissionDenied, reason)
}

impl fmt::Debug for SocksConnector {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SocksConnector").finish()
    }
}

#[cfg(test)]
mod test {
    use std::io::Cursor;
    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, TcpListener, TcpStream};
    use std::sync::{Arc, Mutex};
    use std::thread;
    use std::time::Duration;

    use super::*;
    use crate::config::Config;
    use crate::http::Uri;
    use crate::transport::{Buffers, NextTimeout, TcpConnector};
    use crate::unversioned::resolver::{ResolvedSocketAddrs, Resolver};
    use crate::{Agent, Timeout};

    // Resolves IP addresses only, so no test depends on DNS.
    #[derive(Debug)]
    struct IpOnly;

    impl Resolver for IpOnly {
        fn resolve(
            &self,
            uri: &Uri,
            _: &Config,
            _: NextTimeout,
        ) -> Result<ResolvedSocketAddrs, Error> {
            let (host, port) = uri.host_port();
            let ip: IpAddr = host.parse().map_err(|_| Error::HostNotFound)?;
            let mut addrs = self.empty();
            addrs.push(SocketAddr::new(ip, port));
            Ok(addrs)
        }
    }

    // Advance the shared clock to simulate time spent before and during proxy DNS.
    #[derive(Debug)]
    struct TimedProxyResolver {
        elapsed: Arc<Mutex<Duration>>,
        lookup_time: Duration,
        seen: Mutex<Option<NextTimeout>>,
    }

    impl Resolver for TimedProxyResolver {
        fn resolve(
            &self,
            uri: &Uri,
            config: &Config,
            timeout: NextTimeout,
        ) -> Result<ResolvedSocketAddrs, Error> {
            *self.seen.lock().unwrap() = Some(timeout);
            *self.elapsed.lock().unwrap() += self.lookup_time;
            IpOnly.resolve(uri, config, timeout)
        }
    }

    #[derive(Debug, Default)]
    struct TimedIo {
        connects: Vec<NextTimeout>,
        reads: Vec<NextTimeout>,
        writes: Vec<NextTimeout>,
        replies: std::collections::VecDeque<&'static [u8]>,
        sent: Vec<u8>,
        dropped: bool,
    }

    #[derive(Debug)]
    struct TimedConnector {
        io: Arc<Mutex<TimedIo>>,
        elapsed: Arc<Mutex<Duration>>,
    }

    impl TimedConnector {
        fn new(resolver: &TimedProxyResolver) -> Self {
            Self {
                io: Arc::new(Mutex::new(TimedIo::default())),
                elapsed: resolver.elapsed.clone(),
            }
        }
    }

    impl<In: Transport> Connector<In> for TimedConnector {
        type Out = Either<In, TimedTransport>;

        fn connect(
            &self,
            details: &ConnectionDetails,
            chained: Option<In>,
        ) -> Result<Option<Self::Out>, Error> {
            SocksConnector::connect_with(details, chained, |_, _, timeout| {
                self.io.lock().unwrap().connects.push(timeout);
                Ok(TimedTransport {
                    io: self.io.clone(),
                    elapsed: self.elapsed.clone(),
                    buffers: LazyBuffers::new(4096, 4096),
                })
            })
        }
    }

    #[derive(Debug)]
    struct TimedTransport {
        io: Arc<Mutex<TimedIo>>,
        elapsed: Arc<Mutex<Duration>>,
        buffers: LazyBuffers,
    }

    impl Transport for TimedTransport {
        fn buffers(&mut self) -> &mut dyn Buffers {
            &mut self.buffers
        }

        fn transmit_output(&mut self, amount: usize, timeout: NextTimeout) -> Result<(), Error> {
            let timeout = timeout.for_write().check()?;
            let mut io = self.io.lock().unwrap();
            io.writes.push(timeout);
            io.sent.extend_from_slice(&self.buffers.output()[..amount]);
            Ok(())
        }

        fn await_input(&mut self, timeout: NextTimeout) -> Result<bool, Error> {
            let timeout = timeout.for_read().check()?;
            let mut io = self.io.lock().unwrap();
            io.reads.push(timeout);
            if let Some(reply) = io.replies.pop_front() {
                self.buffers.input_append_buf()[..reply.len()].copy_from_slice(reply);
                self.buffers.input_appended(reply.len());
                return Ok(true);
            }
            // A silent peer consumes its allowance instantly on the simulated clock.
            assert!(
                !timeout.after.is_not_happening(),
                "unscripted read without a timeout"
            );
            *self.elapsed.lock().unwrap() += *timeout.after;
            Err(Error::Timeout(timeout.reason))
        }

        fn is_open(&mut self) -> bool {
            true
        }
    }

    impl Drop for TimedTransport {
        fn drop(&mut self) {
            self.io.lock().unwrap().dropped = true;
        }
    }

    fn connect_with_elapsed(
        proxy: Proxy,
        resolver: &TimedProxyResolver,
        connector: &TimedConnector,
        timeout: NextTimeout,
    ) -> Result<(), Error> {
        let uri: Uri = "http://127.0.0.1:1/".parse().unwrap();
        let config = Agent::config_builder().proxy(Some(proxy)).build();
        let mut addrs = resolver.empty();
        addrs.push("127.0.0.1:1".parse().unwrap());
        let elapsed = resolver.elapsed.clone();
        let start = Instant::now();
        let details = ConnectionDetails {
            uri: &uri,
            addrs,
            config: &config,
            request_level: false,
            resolver,
            now: start,
            timeout,
            current_time: Arc::new(move || start + (*elapsed.lock().unwrap()).into()),
            run_connector: Arc::new(|_| unreachable!()),
            register_shutdown: Arc::new(|_| Ok(())),
        };
        connector.connect(&details, None::<()>).map(|_| ())
    }

    #[test]
    fn proxy_lookup_preserves_remaining_budget() {
        for reason in [Timeout::Global, Timeout::PerCall, Timeout::Connect] {
            let proxy = Proxy::new("socks5h://127.0.0.1:1080").unwrap();
            let resolver = TimedProxyResolver {
                elapsed: Arc::new(Mutex::new(Duration::from_millis(250))),
                lookup_time: Duration::from_millis(500),
                seen: Mutex::new(None),
            };
            let connector = TimedConnector::new(&resolver);
            let timeout = NextTimeout {
                after: Duration::from_secs(1).into(),
                reason,
                per_read: Some(Duration::from_secs(2).into()),
                per_write: Some(Duration::from_secs(3).into()),
            };
            let result = connect_with_elapsed(proxy, &resolver, &connector, timeout);
            assert!(matches!(result, Err(Error::Timeout(actual)) if actual == reason));

            let seen = resolver.seen.lock().unwrap().unwrap();
            assert_eq!(seen.after, Duration::from_millis(750).into());
            assert_eq!(seen.reason, reason);
            assert_eq!(seen.per_read, timeout.per_read);
            assert_eq!(seen.per_write, timeout.per_write);
            let io = connector.io.lock().unwrap();
            assert_eq!(io.connects.len(), 1);
            assert_eq!(io.connects[0].after, Duration::from_millis(250).into());
            assert_eq!(io.reads.len(), 1);
            assert_eq!(io.reads[0].after, Duration::from_millis(250).into());
            assert!(io.dropped, "connection left open");
            assert_eq!(*resolver.elapsed.lock().unwrap(), Duration::from_secs(1));
        }
    }

    #[test]
    fn expired_proxy_lookup_does_not_open_socket() {
        for reason in [Timeout::Global, Timeout::PerCall, Timeout::Connect] {
            let proxy = Proxy::new("socks5h://127.0.0.1:1080").unwrap();
            let resolver = TimedProxyResolver {
                elapsed: Arc::new(Mutex::new(Duration::ZERO)),
                lookup_time: Duration::from_secs(1),
                seen: Mutex::new(None),
            };
            let connector = TimedConnector::new(&resolver);
            let result = connect_with_elapsed(
                proxy,
                &resolver,
                &connector,
                NextTimeout {
                    after: Duration::from_secs(1).into(),
                    reason,
                    per_read: Some(Duration::from_millis(20).into()),
                    ..NextTimeout::default()
                },
            );
            assert!(matches!(result, Err(Error::Timeout(actual)) if actual == reason));
            assert!(connector.io.lock().unwrap().connects.is_empty());
        }
    }

    #[test]
    fn proxy_lookup_does_not_spend_per_read_limit() {
        let proxy = Proxy::new("socks5h://127.0.0.1:1080").unwrap();
        let resolver = TimedProxyResolver {
            elapsed: Arc::new(Mutex::new(Duration::from_millis(250))),
            lookup_time: Duration::from_secs(2),
            seen: Mutex::new(None),
        };
        let connector = TimedConnector::new(&resolver);
        let result = connect_with_elapsed(
            proxy,
            &resolver,
            &connector,
            NextTimeout {
                per_read: Some(Duration::from_millis(100).into()),
                ..NextTimeout::default()
            },
        );
        assert!(matches!(result, Err(Error::Timeout(Timeout::PerRead))));
        let seen = resolver.seen.lock().unwrap().unwrap();
        assert!(seen.after.is_not_happening());
        let io = connector.io.lock().unwrap();
        assert_eq!(io.reads.len(), 1);
        assert_eq!(io.reads[0].after, Duration::from_millis(100).into());
        assert!(io.dropped, "connection left open");
        assert_eq!(
            *resolver.elapsed.lock().unwrap(),
            Duration::from_millis(2350)
        );
    }

    #[cfg(any(feature = "_rustls", feature = "native-tls-no-default"))]
    fn tls_after_socks_preserves_remaining_budget<C>(
        make_tls: impl Fn() -> C,
        tls_config: crate::tls::TlsConfig,
    ) where
        C: Connector<Either<(), TimedTransport>>,
    {
        for reason in [Timeout::Global, Timeout::PerCall, Timeout::Connect] {
            let proxy = Proxy::new("socks5h://127.0.0.1:1080").unwrap();
            let resolver = TimedProxyResolver {
                elapsed: Arc::new(Mutex::new(Duration::ZERO)),
                lookup_time: Duration::from_millis(750),
                seen: Mutex::new(None),
            };
            let connector = TimedConnector::new(&resolver);
            connector
                .io
                .lock()
                .unwrap()
                .replies
                .extend([&[5, 0][..], SUCCEEDED5]);
            let uri: Uri = "https://127.0.0.1/".parse().unwrap();
            let config = Agent::config_builder()
                .proxy(Some(proxy))
                .tls_config(tls_config.clone())
                .build();
            let elapsed = resolver.elapsed.clone();
            let start = Instant::now();
            let details = ConnectionDetails {
                uri: &uri,
                addrs: resolver.empty(),
                config: &config,
                request_level: false,
                resolver: &resolver,
                now: start,
                timeout: NextTimeout {
                    after: Duration::from_secs(1).into(),
                    reason,
                    ..NextTimeout::default()
                },
                current_time: Arc::new(move || start + (*elapsed.lock().unwrap()).into()),
                run_connector: Arc::new(|_| unreachable!()),
                register_shutdown: Arc::new(|_| Ok(())),
            };
            let io = connector.io.clone();
            let result = connector.chain(make_tls()).connect(&details, None::<()>);
            assert!(
                matches!(result, Err(Error::Timeout(actual)) if actual == reason),
                "{reason}: {result:?}"
            );
            let io = io.lock().unwrap();
            assert_eq!(
                io.reads.len(),
                3,
                "two SOCKS replies, then a silent TLS peer"
            );
            for timeout in &io.reads {
                assert_eq!(timeout.after, Duration::from_millis(250).into());
                assert_eq!(timeout.reason, reason);
            }
            assert!(io.sent[13..].starts_with(&[22, 3]), "expected ClientHello");
            assert!(io.dropped, "connection left open");
            assert_eq!(*resolver.elapsed.lock().unwrap(), Duration::from_secs(1));
        }
    }

    #[cfg(feature = "_rustls")]
    #[test]
    fn rustls_after_socks_uses_remaining_budget() {
        tls_after_socks_preserves_remaining_budget(
            crate::transport::RustlsConnector::default,
            crate::tls::TlsConfig::builder()
                .disable_verification(true)
                .unversioned_rustls_crypto_provider(Arc::new(
                    rustls::crypto::aws_lc_rs::default_provider(),
                ))
                .build(),
        );
    }

    #[cfg(feature = "native-tls-no-default")]
    #[test]
    fn native_tls_after_socks_uses_remaining_budget() {
        tls_after_socks_preserves_remaining_budget(
            crate::transport::NativeTlsConnector::default,
            crate::tls::TlsConfig::builder()
                .provider(crate::tls::TlsProvider::NativeTls)
                .disable_verification(true)
                .build(),
        );
    }

    // SOCKS, then plain TCP, without the in-memory transport of the _test feature.
    fn agent(proxy: Proxy) -> Agent {
        let config = Agent::config_builder().proxy(Some(proxy)).build();
        let connector = ().chain(SocksConnector::default()).chain(TcpConnector::default());
        Agent::with_parts(config, connector, IpOnly)
    }

    fn respond_ok(stream: &mut TcpStream) {
        let _ = stream.read(&mut [0; 4096]);
        let response = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok";
        stream.write_all(response).unwrap();
    }

    #[test]
    fn no_proxy_goes_direct_without_resolving_proxy() {
        let server = TcpListener::bind("127.0.0.1:0").unwrap();
        let url = format!("http://{}/", server.local_addr().unwrap());
        thread::spawn(move || respond_ok(&mut server.accept().unwrap().0));

        // IpOnly cannot resolve the proxy, like a proxy name that is not in DNS.
        let proxy = Proxy::builder(ProxyProtocol::Socks5)
            .host("proxy.invalid")
            .no_proxy("127.0.0.1")
            .build()
            .unwrap();

        let mut response = agent(proxy).get(&url).call().unwrap();
        assert_eq!(response.body_mut().read_to_string().unwrap(), "ok");
    }

    #[test]
    fn silent_proxy_times_out() {
        for protocol in ["socks4", "socks4a", "socks5", "socks5h"] {
            let proxy = Proxy::new(&format!("{protocol}://127.0.0.1:1080")).unwrap();
            let resolver = TimedProxyResolver {
                elapsed: Arc::new(Mutex::new(Duration::ZERO)),
                lookup_time: Duration::ZERO,
                seen: Mutex::new(None),
            };
            let connector = TimedConnector::new(&resolver);
            let limit = Duration::from_millis(200);
            let result = connect_with_elapsed(
                proxy,
                &resolver,
                &connector,
                NextTimeout {
                    after: limit.into(),
                    reason: Timeout::Global,
                    ..NextTimeout::default()
                },
            );
            assert!(matches!(result, Err(Error::Timeout(Timeout::Global))));
            assert_eq!(*resolver.elapsed.lock().unwrap(), limit);
            assert!(connector.io.lock().unwrap().dropped, "connection left open");
        }
    }

    #[test]
    fn request_through_proxy() {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let proxy = format!("socks5h://{}", listener.local_addr().unwrap());
        let proxy = Proxy::new(&proxy).unwrap();

        // Answers the handshake, then the request as if it were the target.
        let proxy_side = thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            let mut greeting = [0; 3];
            stream.read_exact(&mut greeting).unwrap();
            stream.write_all(&[5, 0]).unwrap();
            let mut request = [0; 18];
            stream.read_exact(&mut request).unwrap();
            stream.write_all(&[5, 0, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap();
            respond_ok(&mut stream);
            [&greeting[..], &request[..]].concat()
        });

        let mut response = agent(proxy).get("http://example.com/").call().unwrap();
        assert_eq!(response.body_mut().read_to_string().unwrap(), "ok");

        let sent = proxy_side.join().unwrap();
        let expected = [&[5, 1, 0, 5, 1, 0, 3, 11][..], b"example.com", &[0, 80]].concat();
        assert_eq!(sent, expected);
    }

    // Plays the proxy's side of a handshake from memory.
    struct Script {
        replies: Cursor<Vec<u8>>,
        sent: Vec<u8>,
    }

    impl Read for Script {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            // Force fragmented I/O even when a whole reply is available.
            let len = buf.len().min(1);
            self.replies.read(&mut buf[..len])
        }
    }

    impl Write for Script {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            let len = buf.len().min(1);
            self.sent.extend_from_slice(&buf[..len]);
            Ok(len)
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    // Bytes in parts, so that text can be written as text.
    type Parts = &'static [&'static [u8]];

    fn script(proxy: &str, target: Target, replies: Parts) -> (io::Result<()>, Script) {
        let proxy = Proxy::new(proxy).unwrap();
        let mut script = Script {
            replies: Cursor::new(replies.concat()),
            sent: vec![],
        };
        let result = handshake(&mut script, &proxy, target);
        (result, script)
    }

    const IPV4: Target = Target::Ip(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)), 80));
    const IPV6: Target = Target::Ip(SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 80));
    const DOMAIN: Target = Target::Domain("example.com", 443);

    const GRANTED4: &[u8] = &[0, 90, 0, 0, 0, 0, 0, 0];
    const SUCCEEDED5: &[u8] = &[5, 0, 0, 1, 0, 0, 0, 0, 0, 0];

    #[derive(Debug)]
    struct ReadAheadTransport {
        buffers: LazyBuffers,
        replies: Cursor<Vec<u8>>,
        reads: usize,
    }

    impl Transport for ReadAheadTransport {
        fn buffers(&mut self) -> &mut dyn Buffers {
            &mut self.buffers
        }

        fn transmit_output(&mut self, _: usize, timeout: NextTimeout) -> Result<(), Error> {
            timeout.for_write().check()?;
            Ok(())
        }

        fn await_input(&mut self, timeout: NextTimeout) -> Result<bool, Error> {
            timeout.for_read().check()?;
            self.reads += 1;
            let len = self.replies.read(self.buffers.input_append_buf())?;
            self.buffers.input_appended(len);
            Ok(len > 0)
        }

        fn is_open(&mut self) -> bool {
            true
        }
    }

    // rust-socks #17 lost application data read alongside the SOCKS reply when
    // its temporary BufReader was dropped. Exercise actual adapter read-ahead
    // and retain the unread data when handing the transport back to the caller.
    // https://github.com/sfackler/rust-socks/pull/17
    #[test]
    fn handshake_read_ahead_preserves_application_data() {
        let application = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok";
        for (proxy, target, replies) in [
            ("socks4://proxy", IPV4, GRANTED4.to_vec()),
            ("socks4a://proxy", DOMAIN, GRANTED4.to_vec()),
            ("socks5://proxy", IPV4, [&[5, 0][..], SUCCEEDED5].concat()),
            (
                "socks5h://proxy",
                DOMAIN,
                [&[5, 0, 5, 0, 0, 3, 4][..], b"host", &[0, 0]].concat(),
            ),
            (
                "socks5://user:pass@proxy",
                IPV6,
                [&[5, 2, 1, 0, 5, 0, 0, 4][..], &[0; 18]].concat(),
            ),
        ] {
            let proxy = Proxy::new(proxy).unwrap();
            let transport = ReadAheadTransport {
                buffers: LazyBuffers::new(512, 512),
                replies: Cursor::new([replies, application.to_vec()].concat()),
                reads: 0,
            };
            let now = Instant::now();
            let current_time = Arc::new(move || now);
            let mut adapter = TransportAdapter::new(transport, current_time.clone());
            handshake(&mut adapter, &proxy, target).unwrap();

            let transport = adapter.into_inner();
            assert_eq!(
                transport.reads, 1,
                "reply and application data must be read together"
            );
            assert_eq!(transport.buffers.input(), application);

            let mut adapter = TransportAdapter::new(transport, current_time);
            let mut received = vec![0; application.len()];
            adapter.read_exact(&mut received).unwrap();
            assert_eq!(received, application);
            assert_eq!(
                adapter.into_inner().reads,
                1,
                "use the retained input buffer"
            );
        }
    }

    #[test]
    fn handshakes() {
        let cases: &[(&str, Target, Parts, Parts)] = &[
            (
                "socks4://proxy",
                IPV4,
                &[&[4, 1, 0, 80, 10, 0, 0, 1, 0]],
                &[GRANTED4],
            ),
            (
                "socks4a://proxy",
                DOMAIN,
                &[&[4, 1, 1, 187, 0, 0, 0, 1, 0], b"example.com", &[0]],
                &[GRANTED4],
            ),
            (
                // ureq has always sent an empty SOCKS4 user id.
                "socks4://user:pass@proxy",
                IPV4,
                &[&[4, 1, 0, 80, 10, 0, 0, 1, 0]],
                &[GRANTED4],
            ),
            (
                "socks5://proxy",
                IPV4,
                &[&[5, 1, 0], &[5, 1, 0, 1, 10, 0, 0, 1, 0, 80]],
                &[&[5, 0], SUCCEEDED5],
            ),
            (
                // The proxy names the bound address by host name.
                "socks5h://proxy",
                DOMAIN,
                &[&[5, 1, 0], &[5, 1, 0, 3, 11], b"example.com", &[1, 187]],
                &[&[5, 0], &[5, 0, 0, 3, 4], b"host", &[0, 0]],
            ),
            (
                "socks5://user:pass@proxy",
                IPV6,
                &[
                    &[5, 2, 2, 0],
                    &[1, 4],
                    b"user",
                    &[4],
                    b"pass",
                    &[5, 1, 0, 4],
                    &[0; 15], // ::1
                    &[1, 0, 80],
                ],
                &[&[5, 2], &[1, 0], &[5, 0, 0, 4], &[0; 18]],
            ),
            (
                // The proxy may choose no authentication even when offered a password.
                "socks5://user:pass@proxy",
                IPV4,
                &[&[5, 2, 2, 0], &[5, 1, 0, 1, 10, 0, 0, 1, 0, 80]],
                &[&[5, 0], SUCCEEDED5],
            ),
        ];

        for (proxy, target, sent, replies) in cases {
            let proxy_config = Proxy::new(proxy).unwrap();
            let mut script = Script {
                replies: Cursor::new([replies.concat(), b"application data".to_vec()].concat()),
                sent: vec![],
            };
            let result = handshake(&mut script, &proxy_config, *target);
            result.unwrap_or_else(|e| panic!("{proxy}: {e}"));
            assert_eq!(script.sent, sent.concat(), "{proxy}");

            // Consume exactly the reply, leaving application bytes for the caller.
            let mut remaining = Vec::new();
            script.read_to_end(&mut remaining).unwrap();
            assert_eq!(remaining, b"application data", "{proxy}");
        }
    }

    #[test]
    fn handshake_failures() {
        use io::ErrorKind::{InvalidInput, Other, PermissionDenied, UnexpectedEof};

        let cases: &[(&str, Target, Parts, io::ErrorKind, &str)] = &[
            (
                "socks4://proxy",
                IPV4,
                &[&[0, 91, 0, 0, 0, 0, 0, 0]],
                Other,
                "request rejected or failed",
            ),
            (
                "socks4://proxy",
                IPV6,
                &[],
                InvalidInput,
                "SOCKS4 does not support IPv6",
            ),
            (
                "socks5://proxy",
                IPV4,
                &[&[5, 0xff]],
                Other,
                "no acceptable auth methods",
            ),
            (
                // A password was not offered, so the proxy cannot ask for one.
                "socks5://proxy",
                IPV4,
                &[&[5, 2]],
                Other,
                "unknown auth method",
            ),
            (
                "socks5://user:pass@proxy",
                IPV4,
                &[&[5, 2], &[1, 1]],
                PermissionDenied,
                "password authentication failed",
            ),
            (
                "socks5://proxy",
                IPV4,
                &[&[5, 0], &[5, 5, 0, 1, 0, 0, 0, 0, 0, 0]],
                Other,
                "connection refused",
            ),
        ];

        for (proxy, target, replies, kind, reason) in cases {
            let (result, _) = script(proxy, *target, replies);
            let error = result.unwrap_err();
            assert_eq!(error.kind(), *kind, "{proxy}: {error}");
            assert_eq!(error.to_string(), *reason, "{proxy}");
        }

        // A reply cut short is an error, not a connection.
        let (result, _) = script("socks5://proxy", IPV4, &[&[5, 0], &[5, 0, 0, 1, 0, 0]]);
        assert_eq!(result.unwrap_err().kind(), UnexpectedEof);
    }

    #[test]
    fn domain_name_length() {
        let proxy = Proxy::new("socks5h://proxy").unwrap();
        for len in [1, 254, 255, 256] {
            let host = "x".repeat(len);
            let mut script = Script {
                replies: Cursor::new([&[5, 0][..], SUCCEEDED5].concat()),
                sent: vec![],
            };
            let result = handshake(&mut script, &proxy, Target::Domain(&host, 443));
            if len == 256 {
                let error = result.unwrap_err();
                assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
                assert_eq!(error.to_string(), "domain name too long");
                assert_eq!(script.sent, [5, 1, 0]);
            } else {
                result.unwrap();
                assert_eq!(
                    script.sent,
                    [
                        &[5, 1, 0, 5, 1, 0, 3, len as u8][..],
                        host.as_bytes(),
                        &[1, 187]
                    ]
                    .concat()
                );
            }
        }
    }

    #[test]
    fn every_truncated_reply_is_an_error() {
        let domain_reply = [&[5, 0, 0, 3, 255][..], &[b'x'; 255], &[0, 0]].concat();
        let ipv6_reply = [&[5, 0, 0, 4][..], &[0; 18]].concat();
        for (proxy, reply) in [
            ("socks4://proxy", GRANTED4.to_vec()),
            ("socks4a://proxy", GRANTED4.to_vec()),
            ("socks5://proxy", [&[5, 0][..], SUCCEEDED5].concat()),
            ("socks5://proxy", [&[5, 0][..], &domain_reply].concat()),
            ("socks5://proxy", [&[5, 0][..], &ipv6_reply].concat()),
            (
                "socks5://user:pass@proxy",
                [&[5, 2, 1, 0][..], SUCCEEDED5].concat(),
            ),
        ] {
            let proxy = Proxy::new(proxy).unwrap();
            for len in 0..=reply.len() {
                let mut script = Script {
                    replies: Cursor::new(reply[..len].to_vec()),
                    sent: vec![],
                };
                let result = handshake(&mut script, &proxy, IPV4);
                if len == reply.len() {
                    result.unwrap();
                    assert_eq!(script.replies.position(), len as u64);
                } else {
                    let error = result.unwrap_err();
                    assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof, "prefix {len}");
                }
            }
        }
    }

    #[test]
    fn malformed_handshake_replies() {
        use io::ErrorKind::{InvalidData, Other};
        let cases: &[(&str, Parts, io::ErrorKind, &str)] = &[
            (
                "socks4://proxy",
                &[&[4, 90, 0, 0, 0, 0, 0, 0]],
                InvalidData,
                "invalid response version",
            ),
            (
                "socks4://proxy",
                &[&[0, 94, 0, 0, 0, 0, 0, 0]],
                InvalidData,
                "invalid response code",
            ),
            (
                "socks5://proxy",
                &[&[4, 0]],
                InvalidData,
                "invalid response version",
            ),
            (
                "socks5://proxy",
                &[&[5, 0], &[4, 0]],
                InvalidData,
                "invalid response version",
            ),
            (
                "socks5://proxy",
                &[&[5, 0], &[5, 0, 1, 1]],
                InvalidData,
                "invalid reserved byte",
            ),
            (
                "socks5://proxy",
                &[&[5, 0], &[5, 0, 0, 2]],
                Other,
                "unsupported address type",
            ),
            (
                "socks5://user:pass@proxy",
                &[&[5, 2], &[2, 0]],
                InvalidData,
                "invalid response version",
            ),
        ];
        for (proxy, replies, kind, reason) in cases {
            let (result, _) = script(proxy, IPV4, replies);
            let error = result.unwrap_err();
            assert_eq!(error.kind(), *kind, "{proxy}: {error}");
            assert_eq!(error.to_string(), *reason, "{proxy}");
        }
    }

    #[test]
    fn proxy_failure_codes() {
        for (code, reason) in [
            (1, "general SOCKS server failure"),
            (2, "connection not allowed by ruleset"),
            (3, "network unreachable"),
            (4, "host unreachable"),
            (5, "connection refused"),
            (6, "TTL expired"),
            (7, "command not supported"),
            (8, "address kind not supported"),
            (255, "unknown error"),
        ] {
            let mut script = Script {
                replies: Cursor::new(vec![5, 0, 5, code]),
                sent: vec![],
            };
            let proxy = Proxy::new("socks5://proxy").unwrap();
            let error = handshake(&mut script, &proxy, IPV4).unwrap_err();
            assert_eq!(error.kind(), io::ErrorKind::Other);
            assert_eq!(error.to_string(), reason);
        }
        for code in [92, 93] {
            let mut script = Script {
                replies: Cursor::new(vec![0, code, 0, 0, 0, 0, 0, 0]),
                sent: vec![],
            };
            let proxy = Proxy::new("socks4://proxy").unwrap();
            let error = handshake(&mut script, &proxy, IPV4).unwrap_err();
            assert_eq!(error.kind(), io::ErrorKind::PermissionDenied);
        }
    }
}