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
use std::pin::Pin;

use bytes::Bytes;
use futures::{
  stream::Stream,
  task::{Context, Poll},
};
use log::{error, trace};
use pin_project::pin_project;
use reqwest::{self, Method, StatusCode};

use crate::{
  errors::{Error, Result},
  v2::*,
};

impl Client {
  /// Check if a blob exists.
  pub async fn has_blob(&self, name: &str, digest: &str) -> Result<bool> {
    let url = {
      let ep = format!("{}/v2/{}/blobs/{}", self.base_url, name, digest);
      reqwest::Url::parse(&ep)?
    };

    let res = self.build_reqwest(Method::HEAD, url.clone()).send().await?;

    trace!("Blob HEAD status: {:?}", res.status());

    match res.status() {
      StatusCode::OK => Ok(true),
      _ => Ok(false),
    }
  }

  pub async fn get_blob_response(&self, name: &str, digest: &str) -> Result<BlobResponse> {
    let ep = format!("{}/v2/{}/blobs/{}", self.base_url, name, digest);
    let url = reqwest::Url::parse(&ep)?;

    let resp = self.build_reqwest(Method::GET, url.clone()).send().await?;

    let status = resp.status();
    trace!("GET {} status: {}", resp.url(), status);

    match resp.error_for_status_ref() {
      Ok(_) => {
        if let Some(len) = resp.content_length() {
          trace!("Receiving a blob with {} bytes", len);
        } else {
          trace!("Receiving a blob");
        }
        Ok(BlobResponse::new(resp, ContentDigest::try_new(digest)?))
      }
      Err(_) if status.is_client_error() => Err(Error::Client { status }),
      Err(_) if status.is_server_error() => Err(Error::Server { status }),
      Err(_) => {
        error!("Received unexpected HTTP status '{}'", status);
        Err(Error::UnexpectedHttpStatus(status))
      }
    }
  }

  /// Retrieve blob.
  pub async fn get_blob(&self, name: &str, digest: &str) -> Result<Vec<u8>> {
    self.get_blob_response(name, digest).await?.bytes().await
  }

  /// Retrieve blob stream.
  pub async fn get_blob_stream(&self, name: &str, digest: &str) -> Result<impl Stream<Item = Result<Vec<u8>>>> {
    Ok(self.get_blob_response(name, digest).await?.stream())
  }
}

#[derive(Debug)]
pub struct BlobResponse {
  resp: reqwest::Response,
  digest: ContentDigest,
}

impl BlobResponse {
  fn new(resp: reqwest::Response, digest: ContentDigest) -> Self {
    Self { resp, digest }
  }

  /// Get size of the blob.
  /// This method can be useful to render progress bar when downloading a blob.
  pub fn size(&self) -> Option<u64> {
    self.resp.content_length()
  }

  /// Retrieve content of the blob.
  pub async fn bytes(self) -> Result<Vec<u8>> {
    let blob = self.resp.bytes().await?.to_vec();

    let mut digest = self.digest;
    digest.update(&blob);
    digest.verify()?;

    Ok(blob)
  }

  /// Get bytes stream of the blob.
  pub fn stream(self) -> impl Stream<Item = Result<Vec<u8>>> {
    BlobStream::new(self.resp.bytes_stream(), self.digest)
  }
}

#[pin_project]
struct BlobStream<S>
where
  S: Stream<Item = reqwest::Result<Bytes>>,
{
  #[pin]
  stream: S,
  #[pin]
  digest: Option<ContentDigest>,
}

impl<S> BlobStream<S>
where
  S: Stream<Item = reqwest::Result<Bytes>> + Unpin,
{
  fn new(stream: S, digest: ContentDigest) -> Self {
    Self {
      stream,
      digest: Some(digest),
    }
  }
}

impl<S> Stream for BlobStream<S>
where
  S: Stream<Item = reqwest::Result<Bytes>> + Unpin,
{
  type Item = Result<Vec<u8>>;

  fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
    let mut this = self.project();
    match this.stream.poll_next(cx) {
      Poll::Ready(Some(chunk_res)) => {
        let mut digest = match this.digest.as_pin_mut() {
          Some(digest) => digest,
          None => return Poll::Ready(None),
        };
        let chunk = chunk_res?;
        digest.update(&chunk);
        Poll::Ready(Some(Ok(chunk.to_vec())))
      }
      Poll::Ready(None) => match this.digest.take() {
        Some(digest) => match digest.verify() {
          Ok(()) => Poll::Ready(None),
          Err(err) => Poll::Ready(Some(Err(err.into()))),
        },
        None => Poll::Ready(None),
      },
      Poll::Pending => Poll::Pending,
    }
  }
}