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
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::str::from_utf8;
use std::sync::Arc;
use std::time::{Duration, Instant};

use dirs;
use failure::Error;
use futures::future::{self, result};
use futures::Stream;
use http::header::CONTENT_TYPE;
use http::request::Builder;
use hyper::client::connect::Connect;
use hyper::client::HttpConnector;
use hyper::rt::Future;
use hyper::{self, Body, Client, Request, Response, StatusCode};
#[cfg(feature = "openssl")]
use hyper_openssl::HttpsConnector;
use hyper_tls;
#[cfg(unix)]
use hyperlocal::UnixConnector;
use native_tls::{Certificate, Identity, TlsConnector};
#[cfg(feature = "openssl")]
use openssl::ssl::SslConnector;
#[cfg(feature = "openssl")]
use openssl::ssl::{SslFiletype, SslMethod};
use tokio::timer::Timeout;
use tokio_codec::FramedRead;

use container::LogOutput;
use either::EitherResponse;
use errors::{
    DockerResponseBadParameterError, DockerResponseConflictError, DockerResponseNotFoundError,
    DockerResponseNotModifiedError, DockerResponseServerError, JsonDataError,
};
#[cfg(windows)]
use named_pipe::NamedPipeConnector;
use read::{JsonLineDecoder, LineDecoder, StreamReader};
use uri::Uri;

use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use serde_json;

/// The default `DOCKER_SOCKET` address that we will try to connect to.
#[cfg(unix)]
pub const DEFAULT_SOCKET: &'static str = "unix:///var/run/docker.sock";

/// The default `DOCKER_NAMED_PIPE` address that a windows client will try to connect to.
#[cfg(windows)]
pub const DEFAULT_NAMED_PIPE: &'static str = "npipe:////./pipe/docker_engine";

/// The default `DOCKER_HOST` address that we will try to connect to.
pub const DEFAULT_DOCKER_HOST: &'static str = "tcp://localhost:2375";

// Default number of threads for the connection pool, when using HTTP or HTTPS.
const DEFAULT_NUM_THREADS: usize = 1;

/// Default timeout for all requests is 2 minutes.
const DEFAULT_TIMEOUT: u64 = 120;

pub(crate) const TRUE_STR: &'static str = "true";
pub(crate) const FALSE_STR: &'static str = "false";

/// The default directory in which to look for our Docker certificate
/// files.
pub fn default_cert_path() -> Result<PathBuf, Error> {
    use errors::NoCertPathError;

    let from_env = env::var("DOCKER_CERT_PATH").or_else(|_| env::var("DOCKER_CONFIG"));
    if let Ok(ref path) = from_env {
        Ok(Path::new(path).to_owned())
    } else {
        let home = dirs::home_dir().ok_or_else(|| NoCertPathError {})?;
        Ok(home.join(".docker"))
    }
}

#[derive(Debug, Clone)]
pub(crate) enum ClientType {
    #[cfg(unix)]
    Unix,
    Http,
    SSL,
    #[cfg(windows)]
    NamedPipe,
}

/// ---
///
/// # Docker
///
/// The main interface for calling the Docker API. Construct a new Docker instance using one of the
/// connect methods:
///  - [`Docker::connect_with_http_defaults`](struct.Docker.html#method.connect_with_http_defaults)
///  - [`Docker::connect_with_named_pipe_defaults`](struct.Docker.html#method.connect_with_pipe_defaults)
///  - [`Docker::connect_with_ssl_defaults`](struct.Docker.html#method.connect_with_ssl_defaults)
///  - [`Docker::connect_with_unix_defaults`](struct.Docker.html#method.connect_with_unix_defaults)
///  - [`Docker::connect_with_tls_defaults`](struct.Docker.html#method.connect_with_tls_defaults)
#[derive(Debug)]
pub struct Docker<C> {
    pub(crate) client: Arc<Client<C>>,
    pub(crate) client_type: ClientType,
    pub(crate) client_addr: String,
    pub(crate) client_timeout: u64,
}

impl<C> Clone for Docker<C> {
    fn clone(&self) -> Docker<C> {
        Docker {
            client: self.client.clone(),
            client_type: self.client_type.clone(),
            client_addr: self.client_addr.clone(),
            client_timeout: self.client_timeout,
        }
    }
}

#[cfg(feature = "openssl")]
/// A Docker implementation typed to connect to a secure HTTPS connection using the `openssl`
/// library.
impl Docker<HttpsConnector<HttpConnector>> {
    /// Connect using secure HTTPS using defaults that are signalled by environment variables.
    ///
    /// # Defaults
    ///
    ///  - The connection url is sourced from the `DOCKER_HOST` environment variable.
    ///  - The certificate directory is sourced from the `DOCKER_CERT_PATH` environment variable.
    ///  - Certificates are named `key.pem`, `cert.pem` and `ca.pem` to indicate the private key,
    ///  the server certificate and the certificate chain respectively.
    ///  - The number of threads used for the HTTP connection pool defaults to 1.
    ///  - The request timeout defaults to 2 minutes.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_ssl_defaults().unwrap();
    /// connection.ping()
    ///   .and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_ssl_defaults() -> Result<Docker<HttpsConnector<HttpConnector>>, Error> {
        let cert_path = default_cert_path()?;
        if let Ok(ref host) = env::var("DOCKER_HOST") {
            Docker::connect_with_ssl(
                host,
                &cert_path.join("key.pem"),
                &cert_path.join("cert.pem"),
                &cert_path.join("ca.pem"),
                DEFAULT_NUM_THREADS,
                DEFAULT_TIMEOUT,
            )
        } else {
            Docker::connect_with_ssl(
                DEFAULT_DOCKER_HOST,
                &cert_path.join("key.pem"),
                &cert_path.join("cert.pem"),
                &cert_path.join("ca.pem"),
                DEFAULT_NUM_THREADS,
                DEFAULT_TIMEOUT,
            )
        }
    }

    /// Connect using secure HTTPS.
    ///
    /// # Arguments
    ///
    ///  - `addr`: the connection url.
    ///  - `ssl_key`: the private key path.
    ///  - `ssl_cert`: the server certificate path.
    ///  - `ssl_ca`: the certificate chain path.
    ///  - `num_threads`: the number of threads for the HTTP connection pool.
    ///  - `timeout`: the read/write timeout (seconds) to use for every hyper connection
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use std::path::Path;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_ssl(
    ///     "localhost:2375",
    ///     Path::new("/certs/key.pem"),
    ///     Path::new("/certs/cert.pem"),
    ///     Path::new("/certs/ca.pem"),
    ///     1,
    ///     120).unwrap();
    /// connection.ping()
    ///   .and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_ssl(
        addr: &str,
        ssl_key: &Path,
        ssl_cert: &Path,
        ssl_ca: &Path,
        num_threads: usize,
        timeout: u64,
    ) -> Result<Docker<HttpsConnector<HttpConnector>>, Error> {
        // This ensures that using docker-machine-esque addresses work with Hyper.
        let client_addr = addr.replacen("tcp://", "", 1);

        let mut ssl_connector_builder = SslConnector::builder(SslMethod::tls())?;

        ssl_connector_builder.set_ca_file(ssl_ca)?;
        ssl_connector_builder.set_certificate_file(ssl_cert, SslFiletype::PEM)?;
        ssl_connector_builder.set_private_key_file(ssl_key, SslFiletype::PEM)?;

        let mut http_connector = HttpConnector::new(num_threads);
        http_connector.enforce_http(false);

        let https_connector: HttpsConnector<HttpConnector> =
            HttpsConnector::with_connector(http_connector, ssl_connector_builder)?;

        let client_builder = Client::builder();
        let client = client_builder.build(https_connector);
        let docker = Docker {
            client: Arc::new(client),
            client_type: ClientType::SSL,
            client_addr: client_addr.to_owned(),
            client_timeout: timeout,
        };

        Ok(docker)
    }
}

/// A Docker implementation typed to connect to an unsecure Http connection.
impl Docker<HttpConnector> {
    /// Connect using unsecured HTTP using defaults that are signalled by environment variables.
    ///
    /// # Defaults
    ///
    ///  - The connection url is sourced from the `DOCKER_HOST` environment variable, and defaults
    ///  to `localhost:2375`.
    ///  - The number of threads used for the HTTP connection pool defaults to 1.
    ///  - The request timeout defaults to 2 minutes.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_http_defaults().unwrap();
    /// connection.ping()
    ///   .and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_http_defaults() -> Result<Docker<impl Connect>, Error> {
        let host = env::var("DOCKER_HOST").unwrap_or(DEFAULT_DOCKER_HOST.to_string());
        Docker::connect_with_http(&host, DEFAULT_NUM_THREADS, DEFAULT_TIMEOUT)
    }

    /// Connect using unsecured HTTP.  
    ///
    /// # Arguments
    ///
    ///  - `addr`: connection url including scheme and port.
    ///  - `num_threads`: the number of threads for the HTTP connection pool.
    ///  - `timeout`: the read/write timeout (seconds) to use for every hyper connection
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_http(
    ///                    "http://my-custom-docker-server:2735", 4, 20)
    ///                    .unwrap();
    /// connection.ping()
    ///   .and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_http(
        addr: &str,
        num_threads: usize,
        timeout: u64,
    ) -> Result<Docker<impl Connect>, Error> {
        // This ensures that using docker-machine-esque addresses work with Hyper.
        let client_addr = addr.replacen("tcp://", "", 1);

        let http_connector = HttpConnector::new(num_threads);

        let client_builder = Client::builder();
        let client = client_builder.build(http_connector);
        let docker = Docker {
            client: Arc::new(client),
            client_type: ClientType::Http,
            client_addr: client_addr.to_owned(),
            client_timeout: timeout,
        };

        Ok(docker)
    }
}

#[cfg(unix)]
/// A Docker implementation typed to connect to a Unix socket.
impl Docker<UnixConnector> {
    /// Connect using a Unix socket using defaults that are signalled by environment variables.
    ///
    /// # Defaults
    ///
    ///  - The socket location defaults to `/var/run/docker.sock`.
    ///  - The request timeout defaults to 2 minutes.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_unix_defaults().unwrap();
    /// connection.ping().and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_unix_defaults() -> Result<Docker<impl Connect>, Error> {
        Docker::connect_with_unix(DEFAULT_SOCKET, DEFAULT_TIMEOUT)
    }

    /// Connect using a Unix socket.
    ///
    /// # Arguments
    ///
    ///  - `addr`: connection socket path.
    ///  - `timeout`: the read/write timeout (seconds) to use for every hyper connection
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_unix("/var/run/docker.sock", 120).unwrap();
    /// connection.ping().and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_unix(addr: &str, timeout: u64) -> Result<Docker<impl Connect>, Error> {
        let client_addr = addr.replacen("unix://", "", 1);

        let unix_connector = UnixConnector::new();

        let mut client_builder = Client::builder();
        client_builder.keep_alive(false);

        let client = client_builder.build(unix_connector);
        let docker = Docker {
            client: Arc::new(client),
            client_type: ClientType::Unix,
            client_addr: client_addr.to_owned(),
            client_timeout: timeout,
        };

        Ok(docker)
    }
}

#[cfg(windows)]
/// A Docker implementation typed to connect to a Windows Named Pipe, exclusive to the windows
/// target.
impl Docker<NamedPipeConnector> {
    /// Connect using a Windows Named Pipe using defaults that are signalled by environment
    /// variables.
    ///
    /// # Defaults
    ///
    ///  - The socket location defaults to `//./pipe/docker_engine`.
    ///  - The request timeout defaults to 2 minutes.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_named_pipe_defaults().unwrap();
    /// connection.ping().and_then(|_| Ok(println!("Connected!")));
    ///
    /// # }
    /// ```
    pub fn connect_with_named_pipe_defaults() -> Result<Docker<NamedPipeConnector>, Error> {
        Docker::connect_with_named_pipe(DEFAULT_NAMED_PIPE, DEFAULT_TIMEOUT)
    }

    /// Connect using a Windows Named Pipe.
    ///
    /// # Arguments
    ///
    ///  - `addr`: socket location.
    ///  - `timeout`: the read/write timeout (seconds) to use for every hyper connection
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    ///
    /// let connection = Docker::connect_with_named_pipe(
    ///     "//./pipe/docker_engine", 120).unwrap();
    /// connection.ping().and_then(|_| Ok(println!("Connected!")));
    ///
    /// # }
    /// ```
    pub fn connect_with_named_pipe(
        addr: &str,
        timeout: u64,
    ) -> Result<Docker<NamedPipeConnector>, Error> {
        let client_addr = addr.replacen("npipe://", "", 1);

        let named_pipe_connector = NamedPipeConnector::new();

        let mut client_builder = Client::builder();
        client_builder.keep_alive(false);
        client_builder.http1_title_case_headers(true);
        let client = client_builder.build(named_pipe_connector);
        let docker = Docker {
            client: Arc::new(client),
            client_type: ClientType::NamedPipe,
            client_addr: client_addr.to_owned(),
            client_timeout: timeout,
        };

        Ok(docker)
    }
}

/// A Docker implementation typed to connect to a secure HTTPS connection, using the native rust
/// TLS library.
impl Docker<hyper_tls::HttpsConnector<HttpConnector>> {
    /// Connect using secure HTTPS using native TLS and defaults that are signalled by environment
    /// variables.
    ///
    /// # Defaults
    ///
    ///  - The connection url is sourced from the `DOCKER_HOST` environment variable.
    ///  - The certificate directory is sourced from the `DOCKER_CERT_PATH` environment variable.
    ///  - Certificate PKCS #12 archive is named `identity.pfx` and the certificate chain is named `ca.pem`.
    ///  - The password for the PKCS #12 archive defaults to an empty password.
    ///  - The number of threads used for the HTTP connection pool defaults to 1.
    ///  - The request timeout defaults to 2 minutes.
    ///
    ///  # PKCS12
    ///
    ///  PKCS #12 archives can be created with OpenSSL:
    ///
    ///  ```bash
    ///  openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem -certfile
    ///  chain_certs.pem
    ///  ```
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_tls_defaults().unwrap();
    /// connection.ping()
    ///   .and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_tls_defaults(
) -> Result<Docker<hyper_tls::HttpsConnector<HttpConnector>>, Error> {
        let cert_path = default_cert_path()?;
        if let Ok(ref host) = env::var("DOCKER_HOST") {
            Docker::connect_with_tls(
                host,
                &cert_path.join("identity.pfx"),
                &cert_path.join("ca.pem"),
                "",
                DEFAULT_NUM_THREADS,
                DEFAULT_TIMEOUT,
            )
        } else {
            Docker::connect_with_tls(
                DEFAULT_DOCKER_HOST,
                &cert_path.join("identity.pfx"),
                &cert_path.join("ca.pem"),
                "",
                DEFAULT_NUM_THREADS,
                DEFAULT_TIMEOUT,
            )
        }
    }

    /// Connect using secure HTTPS using native TLS.
    ///
    /// # Arguments
    ///
    ///  - `addr`: the connection url.
    ///  - `pkcs12_file`: the PKCS #12 archive.
    ///  - `ca_file`: the certificate chain.
    ///  - `pkcs12_password`: the password to the PKCS #12 archive.
    ///  - `num_threads`: the number of threads for the HTTP connection pool.
    ///  - `timeout`: the read/write timeout (seconds) to use for every hyper connection
    ///
    ///  # PKCS12
    ///
    ///  PKCS #12 archives can be created with OpenSSL:
    ///
    ///  ```bash
    ///  openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem -certfile
    ///  chain_certs.pem
    ///  ```
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use std::path::Path;
    ///
    /// use futures::future::Future;
    ///
    /// let connection = Docker::connect_with_tls(
    ///     "localhost:2375",
    ///     Path::new("/certs/identity.pfx"),
    ///     Path::new("/certs/ca.pem"),
    ///     "my_secret_password",
    ///     1,
    ///     120
    /// ).unwrap();
    /// connection.ping()
    ///   .and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with_tls(
        addr: &str,
        pkcs12_file: &Path,
        ca_file: &Path,
        pkcs12_password: &str,
        num_thread: usize,
        timeout: u64,
    ) -> Result<Docker<hyper_tls::HttpsConnector<HttpConnector>>, Error> {
        let client_addr = addr.replacen("tcp://", "", 1);

        let mut tls_connector_builder = TlsConnector::builder();

        let mut file = File::open(pkcs12_file)?;
        let mut buf = vec![];
        file.read_to_end(&mut buf)?;
        let identity = Identity::from_pkcs12(&buf, pkcs12_password)?;

        let mut file = File::open(ca_file)?;
        let mut buf = vec![];
        file.read_to_end(&mut buf)?;
        let ca = Certificate::from_pem(&buf)?;

        let tls_connector_builder = tls_connector_builder.identity(identity);
        tls_connector_builder.add_root_certificate(ca);

        let mut http_connector = HttpConnector::new(num_thread);
        http_connector.enforce_http(false);

        let https_connector: hyper_tls::HttpsConnector<HttpConnector> =
            hyper_tls::HttpsConnector::from((http_connector, tls_connector_builder.build()?));

        let client_builder = Client::builder();
        let client = client_builder.build(https_connector);
        let docker = Docker {
            client: Arc::new(client),
            client_type: ClientType::SSL,
            client_addr: client_addr.to_owned(),
            client_timeout: timeout,
        };

        Ok(docker)
    }
}

impl<C> Docker<C>
where
    C: Connect + Sync + 'static,
{
    /// Connect using a type that implements `hyper::Connect`.
    ///
    /// # Arguments
    ///
    ///  - `connector`: type that implements `hyper::Connect`.
    ///  - `client_addr`: location to connect to.
    ///  - `timeout`: the read/write timeout (seconds) to use for every hyper connection
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # extern crate bollard;
    /// # extern crate futures;
    /// # extern crate yup_hyper_mock;
    /// # fn main () {
    /// use bollard::Docker;
    ///
    /// use futures::future::Future;
    ///
    /// # use yup_hyper_mock::SequentialConnector;
    /// let mut connector = SequentialConnector::default();
    /// connector.content.push(
    ///   "HTTP/1.1 200 OK\r\nServer: mock1\r\nContent-Type: application/json\r\nContent-Length: 0\r\n\r\n".to_string()
    /// );
    /// let connection = Docker::connect_with(connector, String::new(), 5).unwrap();
    /// connection.ping()
    ///   .and_then(|_| Ok(println!("Connected!")));
    /// # }
    /// ```
    pub fn connect_with(
        connector: C,
        client_addr: String,
        timeout: u64,
    ) -> Result<Docker<C>, Error> {
        let client_builder = Client::builder();
        let client = client_builder.build(connector);

        #[cfg(unix)]
        let client_type = ClientType::Unix;
        #[cfg(windows)]
        let client_type = ClientType::NamedPipe;

        let docker = Docker {
            client: Arc::new(client),
            client_type: client_type,
            client_addr,
            client_timeout: timeout,
        };

        Ok(docker)
    }
}

/// ---
/// # DockerChain
///
/// Retains the same API as the [Docker
/// Client](struct.Docker.html), but consumes the instance and returns the
/// instance as part of the response.
///
/// # Examples
///
/// ```rust,norun
/// use bollard::Docker;
/// let docker = Docker::connect_with_http_defaults().unwrap();
/// docker.chain();
/// ```
#[derive(Debug)]
pub struct DockerChain<C> {
    pub(super) inner: Docker<C>,
}

impl<C> Clone for DockerChain<C> {
    fn clone(&self) -> DockerChain<C> {
        DockerChain {
            inner: self.inner.clone(),
        }
    }
}

impl<C> Docker<C>
where
    C: Connect + Sync + 'static,
{
    /// Create a chain of docker commands, useful to calling the API in a sequential manner.
    ///
    /// # Examples
    ///
    /// ```rust,norun
    /// use bollard::Docker;
    /// let docker = Docker::connect_with_http_defaults().unwrap();
    /// docker.chain();
    /// ```
    pub fn chain(self) -> DockerChain<C> {
        DockerChain { inner: self }
    }

    pub(crate) fn process_into_value<T>(
        &self,
        req: Result<Request<Body>, Error>,
    ) -> impl Future<Item = T, Error = Error>
    where
        T: DeserializeOwned,
    {
        self.process_request(req)
            .and_then(Docker::<C>::decode_response)
    }

    pub(crate) fn process_into_stream<T>(
        &self,
        req: Result<Request<Body>, Error>,
    ) -> impl Stream<Item = T, Error = Error>
    where
        T: DeserializeOwned,
    {
        self.process_request(req)
            .into_stream()
            .map(Docker::<C>::decode_into_stream::<T>)
            .flatten()
    }

    pub(crate) fn process_into_stream_string(
        &self,
        req: Result<Request<Body>, Error>,
    ) -> impl Stream<Item = LogOutput, Error = Error> {
        self.process_request(req)
            .into_stream()
            .map(Docker::<C>::decode_into_stream_string)
            .flatten()
    }

    pub(crate) fn process_into_unit(
        &self,
        req: Result<Request<Body>, Error>,
    ) -> impl Future<Item = (), Error = Error> {
        self.process_request(req).and_then(|_| Ok(()))
    }

    pub(crate) fn transpose_option<T>(
        option: Option<Result<T, Error>>,
    ) -> Result<Option<T>, Error> {
        match option {
            Some(Ok(x)) => Ok(Some(x)),
            Some(Err(e)) => Err(e),
            None => Ok(None),
        }
    }

    pub(crate) fn serialize_payload<S>(body: Option<S>) -> Result<Body, Error>
    where
        S: Serialize,
    {
        match body.map(|inst| serde_json::to_string(&inst)) {
            Some(Ok(res)) => Ok(Some(res)),
            Some(Err(e)) => Err(e),
            None => Ok(None),
        }.map_err(|e| e.into())
            .map(|payload| {
                debug!("{}", payload.clone().unwrap_or_else(String::new));
                payload
                    .map(|content| content.into())
                    .unwrap_or(Body::empty())
            })
    }

    fn process_request(
        &self,
        req: Result<Request<Body>, Error>,
    ) -> impl Future<Item = Response<Body>, Error = Error> {
        let client = self.client.clone();
        let timeout = self.client_timeout;

        result(req)
            .and_then(move |request| Docker::execute_request(client, request, timeout))
            .and_then(|response| {
                let status = response.status();
                match status {
                    // Status code 200 - 299
                    s if s.is_success() => EitherResponse::A(future::ok(response)),

                    // Status code 304: Not Modified
                    StatusCode::NOT_MODIFIED => {
                        EitherResponse::F(Docker::<C>::decode_into_string(response).and_then(
                            |message| Err(DockerResponseNotModifiedError { message }.into()),
                        ))
                    }

                    // Status code 409: Conflict
                    StatusCode::CONFLICT => {
                        EitherResponse::E(Docker::<C>::decode_into_string(response).and_then(
                            |message| Err(DockerResponseConflictError { message }.into()),
                        ))
                    }

                    // Status code 400: Bad request
                    StatusCode::BAD_REQUEST => {
                        EitherResponse::D(Docker::<C>::decode_into_string(response).and_then(
                            |message| Err(DockerResponseBadParameterError { message }.into()),
                        ))
                    }

                    // Status code 404: Not Found
                    StatusCode::NOT_FOUND => {
                        EitherResponse::C(Docker::<C>::decode_into_string(response).and_then(
                            |message| Err(DockerResponseNotFoundError { message }.into()),
                        ))
                    }

                    // All other status codes
                    _ => EitherResponse::B(Docker::<C>::decode_into_string(response).and_then(
                        move |message| {
                            Err(DockerResponseServerError {
                                status_code: status.as_u16(),
                                message,
                            }.into())
                        },
                    )),
                }
            })
    }

    pub(crate) fn build_request<O, K, V>(
        &self,
        path: &str,
        builder: &mut Builder,
        query: Result<Option<O>, Error>,
        payload: Result<Body, Error>,
    ) -> Result<Request<Body>, Error>
    where
        O: IntoIterator,
        O::Item: ::std::borrow::Borrow<(K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        query
            .and_then(|q| payload.map(|body| (q, body)))
            .and_then(|(q, body)| {
                let uri = Uri::parse(&self.client_addr, &self.client_type, path, q)?;
                let request_uri: hyper::Uri = uri.into();
                Ok(builder
                    .uri(request_uri)
                    .header(CONTENT_TYPE, "application/json")
                    .body(body)?)
            })
    }

    fn execute_request(
        client: Arc<Client<C>>,
        request: Request<Body>,
        timeout: u64,
    ) -> impl Future<Item = Response<Body>, Error = Error> {
        let now = Instant::now();

        Timeout::new_at(client.request(request), now + Duration::from_secs(timeout))
            .map_err(|e| e.into())
    }

    fn decode_into_stream<T>(res: Response<Body>) -> impl Stream<Item = T, Error = Error>
    where
        T: DeserializeOwned,
    {
        FramedRead::new(
            StreamReader::new(res.into_body().from_err()),
            JsonLineDecoder::new(),
        )
    }

    fn decode_into_stream_string(
        res: Response<Body>,
    ) -> impl Stream<Item = LogOutput, Error = Error> {
        FramedRead::new(
            StreamReader::new(res.into_body().from_err()),
            LineDecoder::new(),
        ).map_err(|e| e)
    }

    fn decode_into_string(response: Response<Body>) -> impl Future<Item = String, Error = Error> {
        response
            .into_body()
            .concat2()
            .map_err(|e| e.into())
            .and_then(|body| from_utf8(&body).map(|x| x.to_owned()).map_err(|e| e.into()))
    }

    fn decode_response<T>(response: Response<Body>) -> impl Future<Item = T, Error = Error>
    where
        T: DeserializeOwned,
    {
        Docker::<C>::decode_into_string(response).and_then(|contents| {
            debug!("Decoded into string: {}", &contents);
            serde_json::from_str::<T>(&contents).map_err(|e| {
                if e.is_data() {
                    JsonDataError {
                        message: e.to_string(),
                        column: e.column(),
                        contents: contents.to_owned(),
                    }.into()
                } else {
                    e.into()
                }
            })
        })
    }
}