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
use std::collections::HashMap;
use std::io::{Read, Cursor};

use bucket::Bucket;
use chrono::{DateTime, UTC};
use command::Command;

use crypto::digest::Digest;
use crypto::hmac::Hmac;
use crypto::mac::Mac;
use crypto::sha2::Sha256;
use curl::easy::{Easy, List, ReadError};
use error::S3Result;
use hex::ToHex;
use signing;
use url::Url;

use EMPTY_PAYLOAD_SHA;
use LONG_DATE;


/// Collection of HTTP headers sent to S3 service, in key/value format.
pub type Headers = HashMap<String, String>;

/// Collection of HTTP query parameters sent to S3 service, in key/value
/// format.
pub type Query = HashMap<String, String>;


// Temporary structure for making a request
pub struct Request<'a> {
    pub bucket: &'a Bucket,
    pub path: &'a str,
    pub command: Command<'a>,
    pub datetime: DateTime<UTC>,
}

impl<'a> Request<'a> {
    pub fn new<'b>(bucket: &'b Bucket, path: &'b str, command: Command<'b>) -> Request<'b> {
        Request {
            bucket: bucket,
            path: path,
            command: command,
            datetime: UTC::now(),
        }
    }

    fn url(&self) -> Url {
        let mut url_str = String::from("https://");
        url_str.push_str(self.bucket.host());
        url_str.push_str("/");
        url_str.push_str(self.bucket.name());
        if !self.path.starts_with('/') {
            url_str.push_str("/");
        }
        url_str.push_str(&signing::uri_encode(self.path, false));

        // Since every part of this URL is either pre-encoded or statically
        // generated, there's really no way this should fail.
        let mut url = Url::parse(&url_str).expect("static URL parsing");

        for (key, value) in self.bucket.extra_query.iter() {
            url.query_pairs_mut().append_pair(key, value);
        }

        if let Command::List { prefix, delimiter } = self.command {
            let mut query_pairs = url.query_pairs_mut();
            delimiter.map(|d| query_pairs.append_pair("delimiter", d));
            query_pairs.append_pair("prefix", prefix);
            query_pairs.append_pair("list-type", "2");
        }

        url
    }

    fn content_length(&self) -> usize {
        match self.command {
            Command::Put { content, .. } => content.len(),
            _ => 0,
        }
    }

    fn content_type(&self) -> String {
        match self.command {
            Command::Put { content_type, .. } => content_type.into(),
            _ => "text/plain".into(),
        }
    }

    fn sha256(&self) -> String {
        match self.command {
            Command::Put { content, .. } => {
                let mut sha = Sha256::new();
                sha.input(content);
                sha.result_str()
            }
            _ => EMPTY_PAYLOAD_SHA.into(),
        }
    }

    fn long_date(&self) -> String {
        self.datetime.format(LONG_DATE).to_string()
    }

    fn canonical_request(&self, headers: &Headers) -> String {
        signing::canonical_request(self.command.http_verb(),
                                   &self.url(),
                                   headers,
                                   &self.sha256())
    }

    fn string_to_sign(&self, request: &str) -> String {
        signing::string_to_sign(&self.datetime, self.bucket.region(), &request)
    }

    fn signing_key(&self) -> Vec<u8> {
        signing::signing_key(&self.datetime,
                             self.bucket.secret_key(),
                             self.bucket.region(),
                             "s3")
    }

    fn authorization(&self, headers: &Headers) -> String {
        let canonical_request = self.canonical_request(headers);
        let string_to_sign = self.string_to_sign(&canonical_request);
        let mut hmac = Hmac::new(Sha256::new(), &self.signing_key());
        hmac.input(string_to_sign.as_bytes());
        let signature = hmac.result().code().to_hex();
        let signed_header = signing::signed_header_string(headers);
        signing::authorization_header(self.bucket.access_key(),
                                      &self.datetime,
                                      self.bucket.region(),
                                      &signed_header,
                                      &signature)
    }

    fn headers(&self) -> S3Result<Headers> {
        // Generate this once, but it's used in more than one place.
        let sha256 = self.sha256();

        // Start with extra_headers, that way our headers replace anything with
        // the same name.
        let mut headers: Headers = self.bucket.extra_headers.clone();
        headers.insert("Host".into(), self.bucket.host().into());
        headers.insert("Content-Length".into(), self.content_length().to_string());
        headers.insert("Content-Type".into(), self.content_type());
        headers.insert("X-Amz-Content-Sha256".into(), sha256.clone());
        headers.insert("X-Amz-Date".into(), self.long_date());

        self.bucket.credentials().token.as_ref().map(|token| {
            headers.insert("X-Amz-Security-Token".into(), token.clone());
        });

        // This must be last, as it signs the other headers
        let authorization = self.authorization(&headers);
        headers.insert("Authorization".into(), authorization);

        // The format of RFC2822 is somewhat malleable, so including it in
        // signed headers can cause signature mismatches. We do include the
        // X-Amz-Date header, so requests are still properly limited to a date
        // range and can't be used again e.g. reply attacks. Adding this header
        // after the generation of the Authorization header leaves it out of
        // the signed headers.
        headers.insert("Date".into(), self.datetime.to_rfc2822());

        Ok(headers)
    }

    fn load_content(&self, handle: &mut Easy) -> S3Result<Cursor<&[u8]>> {
        if let Command::Put { content, .. } = self.command {
            try!(handle.put(true));
            try!(handle.post_field_size(content.len() as u64));
            Ok(Cursor::new(content))
        } else {
            Ok(Cursor::new(&[] as &[u8]))
        }
    }

    pub fn execute(&self) -> S3Result<(Vec<u8>, u32)> {
        let mut handle = Easy::new();
        handle.url(self.url().as_str())?;

        // Special handling to load PUT content
        let mut content_cursor = try!(self.load_content(&mut handle));

        // Set GET, PUT, etc
        handle.custom_request(self.command.http_verb())?;

        // Build and set a Curl List of headers
        let mut list = List::new();
        for (key, value) in try!(self.headers()).iter() {
            let header = format!("{}: {}", key, value);
            list.append(&header)?;
        }
        handle.http_headers(list)?;

        // Run the transfer
        let mut dst = Vec::new();
        {
            let mut transfer = handle.transfer();

            transfer.read_function(|buf| content_cursor.read(buf).or(Err(ReadError::Abort)))?;

            transfer.write_function(|data| {
                dst.extend_from_slice(data);
                Ok(data.len())
            })?;

            transfer.perform()?;
        }
        Ok((dst, handle.response_code()?))
    }
}