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
use async_trait::async_trait;
use base64::encode;
use bytes::{Bytes, BytesMut};
use chrono::prelude::*;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use dyn_clone::DynClone;
use futures::future::join_all;
use hmac::{Hmac, Mac};
use reqwest::{
    header::{self, HeaderMap, HeaderName, HeaderValue},
    Client, Method, Request, Response, Url,
};
use rustc_serialize::hex::ToHex;
use sha2::Sha256 as sha2_256;
use std::fmt;
use url::form_urlencoded;

use super::canal::{Canal, PoolType};
use crate::blocking::{AuthType, Handler};
use crate::error::Error;
use crate::tokio_async::traits::{DataPool, S3Folder};
use crate::utils::{
    s3object_list_xml_parser, upload_id_xml_parser, S3Convert, S3Object, UrlStyle, DEFAULT_REGION,
};

type UTCTime = DateTime<Utc>;

pub trait Authorizer: Send + Sync + DynClone + fmt::Debug {
    /// This method will setup the header and put the authorize string
    fn authorize(&self, _request: &mut Request, _now: &UTCTime) {
        unimplemented!()
    }

    /// This method will be called once the resource change the region stored
    fn update_region(&mut self, _region: String) {}
}

dyn_clone::clone_trait_object!(Authorizer);

#[derive(Clone, Debug)]
pub struct PublicAuthorizer {}

impl Authorizer for PublicAuthorizer {
    fn authorize(&self, _requests: &mut Request, _now: &UTCTime) {}
}

#[derive(Clone, Debug)]
pub struct V2Authorizer {
    pub access_key: String,
    pub secret_key: String,
    pub auth_str: String,
    pub special_header_prefix: String,
}

#[allow(dead_code)]
impl V2Authorizer {
    /// new V2 Authorizer compatible with AWS and CEPH
    pub fn new(access_key: String, secret_key: String) -> Self {
        V2Authorizer {
            access_key,
            secret_key,
            auth_str: "AWS".to_string(),
            special_header_prefix: "x-amz".to_string(),
        }
    }
    /// Setup the Auth string, if you are using customized S3
    /// Default is "AWS"
    pub fn auth_str(mut self, auth_str: String) -> Self {
        self.auth_str = auth_str;
        self
    }

    /// Setup the Special header prefix, if you are using customized S3
    /// Default is "x-amz"
    pub fn special_header_prefix(mut self, special_header_prefix: String) -> Self {
        self.special_header_prefix = special_header_prefix;
        self
    }
}

impl Authorizer for V2Authorizer {
    fn authorize(&self, request: &mut Request, _now: &UTCTime) {
        let authorize_string = format!(
            "{} {}:{}",
            self.auth_str,
            self.access_key,
            <Request as V2Signature>::sign(request, &self.secret_key)
        );
        let headers = request.headers_mut();
        headers.insert(header::AUTHORIZATION, authorize_string.parse().unwrap());
    }
}

#[derive(Clone, Debug)]
pub struct V4Authorizer {
    pub access_key: String,
    pub secret_key: String,
    pub region: String,
    pub service: String,
    pub action: String,
    pub auth_str: String,
    pub special_header_prefix: String,
}

#[allow(dead_code)]
impl V4Authorizer {
    /// new V4 Authorizer for AWS and CEPH
    pub fn new(access_key: String, secret_key: String, region: String) -> Self {
        V4Authorizer {
            access_key,
            secret_key,
            region,
            service: "s3".to_string(),
            action: "aws4_request".to_string(),
            auth_str: "AWS4-HMAC-SHA256".to_string(),
            special_header_prefix: "x-amz".to_string(),
        }
    }
    /// Default is "us-east-1"
    pub fn region(mut self, region: String) -> Self {
        self.region = region;
        self
    }
    /// Default is "s3"
    pub fn service(mut self, service: String) -> Self {
        self.service = service;
        self
    }
    /// Default is "aws4_request"
    pub fn action(mut self, action: String) -> Self {
        self.action = action;
        self
    }
    /// Setup the Auth string, if you are using customized S3
    /// Default is "AWS4-HMAC-SHA256"
    pub fn auth_str(mut self, auth_str: String) -> Self {
        self.auth_str = auth_str;
        self
    }

    /// Setup the Special header prefix, if you are using customized S3
    /// Default is "x-amz"
    pub fn special_header_prefix(mut self, special_header_prefix: String) -> Self {
        self.special_header_prefix = special_header_prefix;
        self
    }
}

impl Authorizer for V4Authorizer {
    fn authorize(&self, request: &mut Request, now: &UTCTime) {
        let SignatureInfo {
            signed_headers,
            signature,
        } = <Request as V4Signature>::sign(
            request,
            &self.auth_str,
            now,
            &self.secret_key,
            &self.region,
            &self.service,
            &self.action,
        );
        let authorize_string = format!(
            "{} Credential={}/{}/{}/{}/{}, SignedHeaders={}, Signature={}",
            self.auth_str,
            self.access_key,
            now.format("%Y%m%d").to_string(),
            self.region,
            self.service,
            self.action,
            signed_headers,
            signature
        );
        let headers = request.headers_mut();
        headers.insert(header::AUTHORIZATION, authorize_string.parse().unwrap());
    }
    fn update_region(&mut self, region: String) {
        self.region = region;
    }
}
#[derive(Clone, Debug)]
pub struct S3Pool {
    pub host: String,
    /// To use https or not, please note that integrity is secured by S3 protocol.
    /// If the confidentiality is not under concerned, the http is good.
    pub secure: bool,
    /// Default will be Path style,
    /// because Virtual hosted URLs may be supported for non-SSL requests only.
    pub url_style: UrlStyle,

    /// The part size for multipart, default disabled.
    /// If Some the pull/push will check out the object size first and do mulitpart
    /// If None download and upload will be in one part
    pub part_size: Option<usize>,

    client: Client,

    pub authorizer: Box<dyn Authorizer>,

    objects: Vec<S3Object>,
    start_after: Option<String>,
}

impl S3Pool {
    pub fn bucket(self, bucket_name: &str) -> Canal {
        Canal {
            up_pool: Some(Box::new(self)),
            down_pool: None,
            upstream_object: Some(bucket_name.into()),
            downstream_object: None,
            default: PoolType::UpPool,
        }
    }

    pub fn resource(self, s3_object: S3Object) -> Canal {
        Canal {
            up_pool: Some(Box::new(self)),
            down_pool: None,
            upstream_object: Some(s3_object),
            downstream_object: None,
            default: PoolType::UpPool,
        }
    }

    pub fn new(host: String) -> Self {
        S3Pool {
            host,
            secure: false,
            url_style: UrlStyle::PATH,
            client: Client::new(),
            authorizer: Box::new(PublicAuthorizer {}),
            part_size: None,
            objects: Vec::with_capacity(1000),
            start_after: None,
        }
    }

    pub fn aws_v2(mut self, access_key: String, secret_key: String) -> Self {
        self.authorizer = Box::new(V2Authorizer::new(access_key, secret_key));
        self.url_style = UrlStyle::PATH;
        self
    }

    pub fn aws_v4(mut self, access_key: String, secret_key: String, region: String) -> Self {
        self.authorizer = Box::new(V4Authorizer::new(access_key, secret_key, region));
        self.url_style = UrlStyle::HOST;
        self
    }

    pub fn endpoint_and_virturalhost(&self, desc: S3Object) -> (String, Option<String>) {
        let ((host, uri), virturalhost) = match self.url_style {
            UrlStyle::PATH => (desc.path_style_links(self.host.clone()), None),
            UrlStyle::HOST => {
                let (host, uri) = desc.virtural_host_style_links(self.host.clone());
                ((host.clone(), uri), Some(host))
            }
        };
        if self.secure {
            (format!("https://{}{}", host, uri), virturalhost)
        } else {
            (format!("http://{}{}", host, uri), virturalhost)
        }
    }

    pub fn init_headers(
        &self,
        headers: &mut HeaderMap,
        now: &UTCTime,
        virturalhost: Option<String>,
    ) {
        headers.insert(
            header::DATE,
            HeaderValue::from_str(now.to_rfc2822().as_str()).unwrap(),
        );
        headers.insert(
            header::USER_AGENT,
            HeaderValue::from_static("Rust S3 Handler"),
        );
        if let Some(virtural_host) = virturalhost {
            headers.insert(header::HOST, HeaderValue::from_str(&virtural_host).unwrap());
        } else {
            headers.insert(header::HOST, HeaderValue::from_str(&self.host).unwrap());
        }
    }

    fn handle_list_response(&mut self, body: String) -> Result<(), Error> {
        self.objects = s3object_list_xml_parser(&body)?;
        // TODO
        // parse start_after
        Ok(())
    }

    pub fn part_size(mut self, s: usize) -> Self {
        self.part_size = Some(s);
        self
    }

    /// Init multipart upload session, and return `multipart_id`
    async fn init_multipart_upload(
        &self,
        url: String,
        virturalhost: Option<String>,
    ) -> Result<String, Error> {
        let url = format!("{}?uploads", url);
        let mut request = self.client.post(&url).build()?;

        let now = Utc::now();
        self.init_headers(request.headers_mut(), &now, virturalhost);
        self.authorizer.authorize(&mut request, &now);

        let r = self.client.execute(request).await?;

        Ok(upload_id_xml_parser(&r.text().await?)?)
    }

    async fn generate_part_upload_requests(
        &self,
        desc: S3Object,
        multipart_id: &str,
        part_size: usize,
        object: Bytes,
    ) -> Result<Vec<Result<Response, reqwest::Error>>, Error> {
        let mut part_number = 0;
        let mut start = 0;
        let mut req_list = vec![];
        while start < object.len() {
            part_number += 1;
            let end = if start + part_size >= object.len() {
                object.len()
            } else {
                start + part_size
            };
            let (endpoint, virtural_host) = self.endpoint_and_virturalhost(desc.clone());
            let url = format!(
                "{}?uploadId={}&partNumber={}",
                endpoint, multipart_id, part_number
            );

            let mut request = self
                .client
                .put(&url)
                .body(object.slice(start..end))
                .build()?;

            let now = Utc::now();
            self.init_headers(request.headers_mut(), &now, virtural_host);
            self.authorizer.authorize(&mut request, &now);
            req_list.push(self.client.execute(request));
            start += part_size
        }
        Ok(join_all(req_list).await)
    }

    async fn complete_multi_part_upload(
        &self,
        reqs: Vec<Result<Response, reqwest::Error>>,
        desc: S3Object,
        multipart_id: &str,
    ) -> Result<Response, Error> {
        let mut content = format!("<CompleteMultipartUpload>");
        for (idx, res) in reqs.into_iter().enumerate() {
            let r = res?;
            let etag = r.headers()[reqwest::header::ETAG]
                .to_str()
                .expect("unexpected etag from server");

            content.push_str(&format!(
                "<Part><PartNumber>{}</PartNumber><ETag>{}</ETag></Part>",
                idx + 1,
                etag
            ));
        }
        content.push_str(&format!("</CompleteMultipartUpload>"));
        let (endpoint, virturalhost) = self.endpoint_and_virturalhost(desc);
        let url = format!("{}?uploadId={}", endpoint, multipart_id);
        let mut request = self.client.post(&url).body(content.into_bytes()).build()?;
        let now = Utc::now();
        self.init_headers(request.headers_mut(), &now, virturalhost);
        self.authorizer.authorize(&mut request, &now);
        let r = self.client.execute(request).await?;
        Ok(r)
    }

    async fn generate_part_download_requests(
        &self,
        desc: S3Object,
        part_size: usize,
    ) -> Result<Vec<Result<Response, reqwest::Error>>, Error> {
        let mut start = 0;
        let mut req_list = vec![];
        while start < desc.size.unwrap() {
            let end = if start + part_size >= desc.size.unwrap() {
                desc.size.unwrap()
            } else {
                start + part_size
            };
            let (url, virturalhost) = self.endpoint_and_virturalhost(desc.clone());

            let mut request = self.client.get(&url).build()?;

            let headers = request.headers_mut();
            headers.insert(
                header::RANGE,
                HeaderValue::from_str(&format!("bytes={}-{}", start, end)).unwrap(),
            );

            let now = Utc::now();
            self.init_headers(headers, &now, virturalhost);
            self.authorizer.authorize(&mut request, &now);
            req_list.push(self.client.execute(request));
            start += part_size
        }
        Ok(join_all(req_list).await)
    }

    async fn complete_multi_part_download(
        &self,
        reqs: Vec<Result<Response, reqwest::Error>>,
    ) -> Result<Bytes, Error> {
        let mut output = BytesMut::with_capacity(0);
        for res in reqs.into_iter() {
            let r = res?;
            // TODO: no copy, check out a way of Bytes -> BytesMut then using unsplit
            output.extend_from_slice(&r.bytes().await?);
        }
        Ok(output.into())
    }
}

impl From<Handler<'_>> for S3Pool {
    fn from(handler: Handler) -> Self {
        let secure = handler.is_secure();
        let Handler {
            host,
            access_key,
            secret_key,
            region,
            auth_type,
            url_style,
            ..
        } = handler;

        let authorizer: Box<dyn Authorizer> = match auth_type {
            AuthType::AWS4 => Box::new(V4Authorizer::new(
                access_key.into(),
                secret_key.into(),
                region.unwrap_or(DEFAULT_REGION.to_string()),
            )),
            AuthType::AWS2 => Box::new(V2Authorizer::new(access_key.into(), secret_key.into())),
        };

        Self {
            host: host.into(),
            secure,
            url_style,
            client: Client::new(),
            authorizer,
            part_size: Some(5242880),
            objects: Vec::with_capacity(1000),
            start_after: None,
        }
    }
}

impl From<&Handler<'_>> for S3Pool {
    fn from(handler: &Handler) -> Self {
        let secure = handler.is_secure();
        let Handler {
            host,
            access_key,
            secret_key,
            region,
            auth_type,
            url_style,
            ..
        } = handler;

        let authorizer: Box<dyn Authorizer> = match auth_type {
            AuthType::AWS4 => Box::new(V4Authorizer::new(
                access_key.to_string(),
                secret_key.to_string(),
                region.clone().unwrap_or(DEFAULT_REGION.to_string()),
            )),
            AuthType::AWS2 => Box::new(V2Authorizer::new(
                access_key.to_string(),
                secret_key.to_string(),
            )),
        };

        Self {
            host: host.to_string(),
            secure,
            url_style: url_style.clone(),
            client: Client::new(),
            authorizer,
            part_size: Some(5242880),
            objects: Vec::with_capacity(1000),
            start_after: None,
        }
    }
}

#[async_trait]
impl DataPool for S3Pool {
    async fn push(&self, desc: S3Object, object: Bytes) -> Result<(), Error> {
        let part_size = self.part_size.unwrap_or_default();
        let _r = if part_size > 0 && part_size < object.len() {
            let (endpoint, virturalhost) = self.endpoint_and_virturalhost(desc.clone());
            let multipart_id = self.init_multipart_upload(endpoint, virturalhost).await?;

            let reqs = self
                .generate_part_upload_requests(desc.clone(), &multipart_id, part_size, object)
                .await?;
            self.complete_multi_part_upload(reqs, desc, &multipart_id)
                .await?
        } else {
            let (endpoint, virturalhost) = self.endpoint_and_virturalhost(desc);
            let mut request = self.client.put(&endpoint).body(object).build()?;

            let now = Utc::now();
            self.init_headers(request.headers_mut(), &now, virturalhost);
            self.authorizer.authorize(&mut request, &now);
            self.client.execute(request).await?
        };
        // TODO validate _r status code
        Ok(())
    }

    async fn pull(&self, mut desc: S3Object) -> Result<Bytes, Error> {
        self.fetch_meta(&mut desc).await?;
        let part_size = self.part_size.unwrap_or_default();
        if part_size > 0 && part_size < desc.size.unwrap_or_default() {
            let reqs = self
                .generate_part_download_requests(desc, part_size)
                .await?;
            let output = self.complete_multi_part_download(reqs).await?;

            Ok(output)
        } else {
            // TODO reuse the client setting and not only the reqest
            let (endpoint, virturalhost) = self.endpoint_and_virturalhost(desc);
            let mut request = Request::new(Method::GET, Url::parse(&endpoint)?);

            let now = Utc::now();
            self.init_headers(request.headers_mut(), &now, virturalhost);
            self.authorizer.authorize(&mut request, &now);

            let r = self.client.execute(request).await?;
            // TODO validate status code
            Ok(r.bytes().await?)
        }
    }

    async fn list(&self, index: Option<S3Object>) -> Result<Box<dyn S3Folder>, Error> {
        let mut pool = self.clone();
        let (endpoint, virturalhost) = self.endpoint_and_virturalhost(index.unwrap_or_default());
        let mut request = Request::new(Method::GET, Url::parse(&endpoint)?);

        let now = Utc::now();
        pool.init_headers(request.headers_mut(), &now, virturalhost);
        pool.authorizer.authorize(&mut request, &now);
        let body = pool.client.execute(request).await?.text().await?;
        pool.handle_list_response(body)?;
        Ok(Box::new(pool))
    }

    async fn remove(&self, desc: S3Object) -> Result<(), Error> {
        let (endpoint, virturalhost) = self.endpoint_and_virturalhost(desc);
        let mut request = Request::new(Method::DELETE, Url::parse(&endpoint)?);

        let now = Utc::now();
        self.init_headers(request.headers_mut(), &now, virturalhost);
        self.authorizer.authorize(&mut request, &now);

        let _r = self.client.execute(request).await?;
        // TODO validate status code
        Ok(())
    }

    fn check_scheme(&self, scheme: &str) -> Result<(), Error> {
        if scheme.to_lowercase() != "s3" {
            Err(Error::SchemeError())
        } else {
            Ok(())
        }
    }

    async fn fetch_meta(&self, desc: &mut S3Object) -> Result<(), Error> {
        let (endpoint, virturalhost) = self.endpoint_and_virturalhost(desc.clone());
        let mut request = self.client.head(&endpoint).build()?;

        let now = Utc::now();
        self.init_headers(request.headers_mut(), &now, virturalhost);
        self.authorizer.authorize(&mut request, &now);

        let r = self.client.execute(request).await?;
        let headers = r.headers();
        desc.etag = if headers.contains_key(reqwest::header::ETAG) {
            Some(
                headers[reqwest::header::ETAG]
                    .to_str()?
                    .to_string()
                    .replace('"', ""),
            )
        } else {
            None
        };
        desc.mtime = if headers.contains_key(HeaderName::from_lowercase(b"last-modified").unwrap())
        {
            Some(
                headers[HeaderName::from_lowercase(b"last-modified").unwrap()]
                    .to_str()?
                    .into(),
            )
        } else {
            None
        };
        desc.size = if headers.contains_key(reqwest::header::CONTENT_LENGTH) {
            Some(
                headers[reqwest::header::CONTENT_LENGTH]
                    .to_str()?
                    .parse::<usize>()
                    .unwrap_or_default(),
            )
        } else {
            None
        };

        // TODO: check out it is correct or not that the storage class is absent here

        Ok(())
    }
}

#[async_trait]
impl S3Folder for S3Pool {
    async fn next_object(&mut self) -> Result<Option<S3Object>, Error> {
        // if self.objects.is_empty() && self.start_after.is_some() {
        //     // let mut url = self.client.url.clone();
        //     // url.query_pairs_mut()
        //     //     .append_pair("start-after", &self.start_after.take().unwrap());
        //     // let r = self.client.execute(Request::new(Method::GET, url)).await?;
        //     // self.handle_response(r).await?;
        // }
        if self.objects.is_empty() {
            Ok(None)
        } else {
            Ok(Some(self.objects.remove(0)))
        }
    }
}

pub struct CanonicalHeadersInfo {
    pub signed_headers: String,
    pub canonical_headers: String,
}

pub struct CanonicalRequestInfo {
    pub signed_headers: String,
    pub canonical_request: String,
}

pub trait Canonical {
    fn canonical_headers_info(&self) -> CanonicalHeadersInfo;
    fn canonical_query_string(&self) -> String;
    fn canonical_request_info(&self, payload_hash: &str) -> CanonicalRequestInfo;
}

impl Canonical for Request {
    fn canonical_headers_info(&self) -> CanonicalHeadersInfo {
        let mut canonical_headers = String::new();
        let mut signed_headers = Vec::new();

        let mut headers: Vec<(String, &str)> = self
            .headers()
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or_default()))
            .collect();

        headers.sort_by(|a, b| a.0.to_lowercase().as_str().cmp(b.0.to_lowercase().as_str()));
        for h in headers {
            canonical_headers.push_str(h.0.to_lowercase().as_str());
            canonical_headers.push(':');
            canonical_headers.push_str(h.1.trim());
            canonical_headers.push('\n');
            signed_headers.push(h.0.to_lowercase());
        }
        CanonicalHeadersInfo {
            signed_headers: signed_headers.join(";"),
            canonical_headers,
        }
    }

    fn canonical_query_string(&self) -> String {
        let mut encoded = form_urlencoded::Serializer::new(String::new());
        let mut qs: Vec<(String, String)> = self
            .url()
            .query_pairs()
            .into_iter()
            .map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().to_owned()))
            .collect();

        qs.sort_by(|x, y| x.0.cmp(&y.0));

        for (key, value) in qs {
            encoded.append_pair(&key, &value);
        }

        // There is a `~` in upload id, should be treated in a tricky way.
        //
        // >>>
        // In the concatenated string, period characters (.) are not escaped.
        // RFC 3986 considers the period character an unreserved character,
        // so it is **not** URL encoded.
        // >>>
        //
        // ref:
        // https://docs.aws.amazon.com/general/latest/gr/signature-version-2.html#create-canonical-string
        encoded.finish().replace("%7E", "~")
    }

    fn canonical_request_info(&self, payload_hash: &str) -> CanonicalRequestInfo {
        let CanonicalHeadersInfo {
            signed_headers,
            canonical_headers,
        } = self.canonical_headers_info();
        CanonicalRequestInfo {
            signed_headers: signed_headers.clone(),
            canonical_request: format!(
                "{}\n{}\n{}\n{}\n{}\n{}",
                self.method().as_str(),
                self.url().path(),
                self.canonical_query_string(),
                canonical_headers,
                signed_headers,
                payload_hash
            ),
        }
    }
}

pub trait V2Signature
where
    Self: Canonical,
{
    fn string_to_signed(&self) -> String;
    fn sign(&self, sign_key: &str) -> String;
}

impl V2Signature for Request {
    fn string_to_signed(&self) -> String {
        format!(
            "{}\n\n\n{}\n{}{}",
            self.method().as_str(),
            self.headers().get(header::DATE).unwrap().to_str().unwrap(),
            self.url().path(),
            self.canonical_query_string()
        )
    }
    fn sign(&self, sign_key: &str) -> String {
        encode(&hmacsha1::hmac_sha1(
            sign_key.as_bytes(),
            <Request as V2Signature>::string_to_signed(self).as_bytes(),
        ))
    }
}

pub struct RequestHashInfo {
    pub signed_headers: String,
    pub sha256: String,
}

pub struct StringToSignInfo {
    pub signed_headers: String,
    pub string_to_signed: String,
}

pub struct SignatureInfo {
    pub signed_headers: String,
    pub signature: String,
}

pub trait V4Signature
where
    Self: Canonical,
{
    fn string_to_signed(
        &mut self,
        auth_str: &str,
        now: &UTCTime,
        region: &str,
        service: &str,
        action: &str,
    ) -> StringToSignInfo;
    /// calculate hash mac and update header
    fn payload_sha256(&mut self) -> String;
    /// calculate hash mac and update header
    fn request_sha256(&mut self) -> RequestHashInfo;
    fn sign(
        &mut self,
        auth_str: &str,
        now: &UTCTime,
        sign_key: &str,
        region: &str,
        service: &str,
        action: &str,
    ) -> SignatureInfo;
}

impl V4Signature for Request {
    fn string_to_signed(
        &mut self,
        auth_str: &str,
        now: &UTCTime,
        region: &str,
        service: &str,
        action: &str,
    ) -> StringToSignInfo {
        let iso_8601_str = {
            let mut s = now.to_rfc3339();
            s.retain(|c| !['-', ':'].contains(&c));
            format!("{}Z", &s[..15])
        };
        let headers = self.headers_mut();
        headers.insert(
            header::HeaderName::from_static("x-amz-date"),
            HeaderValue::from_str(&iso_8601_str).unwrap(),
        );
        let RequestHashInfo {
            signed_headers,
            sha256,
        } = self.request_sha256();
        StringToSignInfo {
            signed_headers,
            string_to_signed: format!(
                "{}\n{}\n{}/{}/{}/{}\n{}",
                auth_str,
                iso_8601_str,
                &iso_8601_str[..8],
                region,
                service,
                action,
                sha256
            ),
        }
    }

    fn payload_sha256(&mut self) -> String {
        let mut sha = Sha256::new();
        sha.input(
            self.body()
                .map(|b| b.as_bytes())
                .unwrap_or_default()
                .unwrap_or_default(),
        );
        let paload_hash = sha.result_str();
        let headers = self.headers_mut();
        headers.insert(
            header::HeaderName::from_static("x-amz-content-sha256"),
            HeaderValue::from_str(&paload_hash).unwrap(),
        );
        paload_hash
    }

    fn request_sha256(&mut self) -> RequestHashInfo {
        let paload_hash = self.payload_sha256();

        let CanonicalRequestInfo {
            signed_headers,
            canonical_request,
        } = self.canonical_request_info(&paload_hash);

        let mut sha = Sha256::new();
        sha.input_str(canonical_request.as_str());
        RequestHashInfo {
            signed_headers,
            sha256: sha.result_str(),
        }
    }

    fn sign(
        &mut self,
        auth_str: &str,
        now: &UTCTime,
        sign_key: &str,
        region: &str,
        service: &str,
        action: &str,
    ) -> SignatureInfo {
        let StringToSignInfo {
            signed_headers,
            string_to_signed,
        } = <Request as V4Signature>::string_to_signed(
            self, auth_str, now, region, service, action,
        );
        let time_str = {
            let mut s = now.to_rfc3339();
            s.retain(|c| !['-', ':'].contains(&c));
            &s[..8].to_string()
        };

        let mut key: String = auth_str.split("-").next().unwrap_or_default().to_string();
        key.push_str(sign_key);

        let mut mac = Hmac::<sha2_256>::new(key.as_str().as_bytes());
        mac.input(time_str.as_bytes());
        let result = mac.result();
        let code_bytes = result.code();

        let mut mac1 = Hmac::<sha2_256>::new(code_bytes);
        mac1.input(region.as_bytes());
        let result1 = mac1.result();
        let code_bytes1 = result1.code();

        let mut mac2 = Hmac::<sha2_256>::new(code_bytes1);
        mac2.input(service.as_bytes());
        let result2 = mac2.result();
        let code_bytes2 = result2.code();

        let mut mac3 = Hmac::<sha2_256>::new(code_bytes2);
        mac3.input(action.as_bytes());
        let result3 = mac3.result();
        let code_bytes3 = result3.code();

        let mut mac4 = Hmac::<sha2_256>::new(code_bytes3);
        mac4.input(string_to_signed.as_bytes());
        let result4 = mac4.result();
        let code_bytes4 = result4.code();

        SignatureInfo {
            signed_headers,
            signature: code_bytes4.to_hex(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::blocking::CredentialConfig;

    #[tokio::test]
    async fn test_handle_list_response() {
        let s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>ant-lab</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>14M</Key><LastModified>2020-01-31T14:58:45.000Z</LastModified><ETag>&quot;8ff43d748637d249d80d6f45e15c7663-3&quot;</ETag><Size>14336000</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>7M</Key><LastModified>2020-11-21T09:50:46.000Z</LastModified><ETag>&quot;cbe4f29b8b099989ae49afc02aa1c618-2&quot;</ETag><Size>7168000</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>7M.json</Key><LastModified>2020-09-19T14:59:23.000Z</LastModified><ETag>&quot;d34bd3f9aff10629ac49353312a42b0f-2&quot;</ETag><Size>7168000</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>get</Key><LastModified>2020-08-11T06:10:11.000Z</LastModified><ETag>&quot;f895d74af5106ce0c3d6cb008fb3b98d&quot;</ETag><Size>304</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>t</Key><LastModified>2020-09-19T15:10:08.000Z</LastModified><ETag>&quot;5050ef3558233dc04b3fac50eff68de1&quot;</ETag><Size>10</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>t.txt</Key><LastModified>2020-09-19T15:04:46.000Z</LastModified><ETag>&quot;5050ef3558233dc04b3fac50eff68de1&quot;</ETag><Size>10</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>test-orig</Key><LastModified>2020-11-21T09:48:29.000Z</LastModified><ETag>&quot;c059dadd468de1835bc99dab6e3b2cee-3&quot;</ETag><Size>11534336</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>test-s3handle</Key><LastModified>2020-11-21T10:09:39.000Z</LastModified><ETag>&quot;5dd39cab1c53c2c77cd352983f9641e1&quot;</ETag><Size>20</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>test.json</Key><LastModified>2020-08-11T09:54:42.000Z</LastModified><ETag>&quot;f895d74af5106ce0c3d6cb008fb3b98d&quot;</ETag><Size>304</Size><Owner><ID>54bbddd7c9c485b696f5b188467d4bec889b83d3862d0a6db526d9d17aadcee2</ID><DisplayName>yanganto</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>";
        let mut pool = S3Pool::new("somewhere.in.the.world".to_string());
        pool.handle_list_response(s.to_string()).unwrap();
        assert!(pool.next_object().await.unwrap().is_some());
    }

    #[test]
    fn test_from_blocking_handle_to_s3_pool() {
        let config = CredentialConfig {
            host: "s3.us-east-1.amazonaws.com".to_string(),
            access_key: "akey".to_string(),
            secret_key: "skey".to_string(),
            user: None,
            region: None,  // default is us-east-1
            s3_type: None, // default will try to config as AWS S3 handler
            secure: None,  // dafault is false, because the integrity protect by HMAC
        };
        let handler = Handler::from(&config);
        let mut pool = S3Pool::from(&handler);
        let s3_pool = S3Pool::new("s3.us-east-1.amazonaws.com".to_string());
        assert_eq!(pool.host, s3_pool.host);

        pool = handler.into();
        let s3_pool = S3Pool::new("s3.us-east-1.amazonaws.com".to_string());
        assert_eq!(pool.host, s3_pool.host);
    }
}