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
extern crate curl;
extern crate failure;
#[macro_use]
extern crate failure_derive;
extern crate serde;
extern crate serde_json;

// Magic failure::ResultExt which has context method
// and implements for std::result::Result
use failure::{Backtrace, Context, Fail, ResultExt};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::cell::{RefCell, RefMut};
use std::fmt;
use std::io::{Read, Write};

/// Shortcut alias for results of this module.
pub type Result<T> = std::result::Result<T, failure::Error>;

/// A enum represents HTTP methods.
#[derive(PartialEq, Debug)]
pub enum Method {
    Get,
    Head,
    Post,
    Put,
    Delete,
}

impl fmt::Display for Method {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Method::Get => write!(f, "GET"),
            Method::Head => write!(f, "HEAD"),
            Method::Post => write!(f, "POST"),
            Method::Put => write!(f, "PUT"),
            Method::Delete => write!(f, "DELETE"),
        }
    }
}

///
/// A Http client base on curl.
///
pub struct Client {
    shared_handle: RefCell<curl::easy::Easy>,
    base_url: String,
    user_agent: String,
}

impl Client {
    /// Initialize a curl http client based on the **base_url**.
    pub fn new(base_url: &str) -> Client {
        Client {
            shared_handle: RefCell::new(curl::easy::Easy::new()),
            base_url: base_url.to_string(),
            user_agent: "curl-http".to_string(),
        }
    }

    /// Set the User-Agent header. Default is `curl-http`
    pub fn set_user_agent(&mut self, user_agent: &str) {
        self.user_agent = user_agent.to_string();
    }

    /// Make a specific method request.
    pub fn request(&self, method: Method, endpoint: &str) -> Result<Request> {
        let url = format!("{}{}", self.base_url, endpoint);
        let mut handle = self.shared_handle.borrow_mut();
        handle.reset();
        Request::new(handle, method, &url)?.with_user_agent(&self.user_agent)
    }

    /// High level HTTP **GET** method
    pub fn get(&self, endpoint: &str) -> Result<Response> {
        self.request(Method::Get, endpoint)?.send()
    }

    /// High level HTTP **POST** method
    pub fn post<S: Serialize>(&self, endpoint: &str, body: &S) -> Result<Response> {
        self.request(Method::Post, endpoint)?
            .with_json_body(body)?
            .send()
    }

    /// High level HTTP **PUT** method
    pub fn put<S: Serialize>(&self, endpoint: &str, body: &S) -> Result<Response> {
        self.request(Method::Put, endpoint)?
            .with_json_body(body)?
            .send()
    }

    /// High level HTTP **DELETE** method
    pub fn delete(&self, endpoint: &str) -> Result<Response> {
        self.request(Method::Delete, endpoint)?.send()
    }
}

/// The struct represents the HTTP request.
pub struct Request<'a> {
    handle: RefMut<'a, curl::easy::Easy>,
    headers: curl::easy::List,
    url: String,
    body: Option<Vec<u8>>,
}

impl<'a> Request<'a> {
    pub fn new(
        mut handle: RefMut<'a, curl::easy::Easy>,
        method: Method,
        url: &str,
    ) -> Result<Request<'a>> {
        match method {
            Method::Get => handle.get(true)?,
            Method::Head => {
                handle.get(true)?;
                handle.custom_request("HEAD")?;
                handle.nobody(true)?;
            }
            Method::Post => handle.custom_request("POST")?,
            Method::Put => handle.custom_request("PUT")?,
            Method::Delete => handle.custom_request("DELETE")?,
        }

        Ok(Request {
            handle,
            headers: curl::easy::List::new(),
            url: url.to_string(),
            body: None,
        })
    }

    /// Set the HTTP header.
    pub fn with_header(mut self, key: &str, value: &str) -> Result<Request<'a>> {
        self.headers.append(&format!("{}: {}", key, value))?;
        Ok(self)
    }

    /// Set custom User-Agent.
    pub fn with_user_agent(mut self, ua: &str) -> Result<Request<'a>> {
        self.headers.append(&format!("User-Agent: {}", ua))?;
        Ok(self)
    }

    /// Set custom url arguments or querystring.
    pub fn with_arguments(mut self, args: &str) -> Result<Request<'a>> {
        self.url = format!("{}?{}", self.url, args);
        Ok(self)
    }

    /// Set the JSON request body for the request.
    pub fn with_json_body<S: Serialize>(mut self, body: &S) -> Result<Request<'a>> {
        let mut body_bytes: Vec<u8> = vec![];
        // Serialize json object to bytes
        serde_json::to_writer(&mut body_bytes, &body)
            .context(ErrorKind::InvalidJsonBody)?;

        self.body = Some(body_bytes);
        self.headers.append("Content-Type: application/json")?;
        Ok(self)
    }

    /// Sends the request and reads the response body into the response object.
    pub fn send(mut self) -> Result<Response> {
        self.handle.http_headers(self.headers)?;
        self.handle.url(&self.url)?;

        match self.body {
            Some(ref body) => {
                let mut body: &[u8] = &body[..];
                self.handle.upload(true)?;
                self.handle.in_filesize(body.len() as u64)?;
                handle_request(&mut self.handle, &mut |buffer| {
                    body.read(buffer).unwrap_or(0)
                })
            }
            None => handle_request(&mut self.handle, &mut |_| 0)
        }
    }
}

fn handle_request(
    handle: &mut curl::easy::Easy,
    read: &mut FnMut(&mut [u8]) -> usize) -> Result<Response> {
    let mut response_body = vec![];
    let mut response_headers = vec![];

    {
        let mut handle = handle.transfer();

        handle.read_function(move |buffer| Ok(read(buffer)))?;

        handle.write_function(|data| {
            Ok(match response_body.write_all(data) {
                Ok(_) => data.len(),
                Err(_) => 0,
            })
        })?;

        handle.header_function(|data| {
            response_headers.push(String::from_utf8_lossy(data).into_owned());
            true
        })?;
        handle.perform()?;
    }

    Ok(Response {
        status: handle.response_code()?,
        headers: response_headers,
        body: Some(response_body),
    })
}

/// Type alias for **u32** http status.
pub type HttpStatus = u32;

/// The struct represents the HTTP response.
#[derive(Clone, Debug)]
pub struct Response {
    status: HttpStatus,
    headers: Vec<String>,
    body: Option<Vec<u8>>,
}

impl Response {
    pub fn status(&self) -> HttpStatus {
        self.status
    }

    pub fn failed(&self) -> bool {
        self.status >= 400 && self.status <= 600
    }

    pub fn ok(&self) -> bool {
        !self.failed()
    }

    /// Deserialize the response body into the given type
    pub fn deserialize<T: DeserializeOwned>(&self) -> Result<T> {
        if self.ok() {
            Ok(serde_json::from_reader(match self.body {
                Some(ref body) => body,
                None => &b""[..],
            }).context(ErrorKind::InvalidJson)?)
        } else {
            Err(ErrorKind::RequestFailed.into())
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum ErrorKind {
    #[fail(display = "Request failed")]
    RequestFailed,
    #[fail(display = "Could not serialize value as JSON")]
    InvalidJsonBody,
    #[fail(display = "Could not parse JSON response")]
    InvalidJson,
}

/// Curl http error.
#[derive(Debug)]
pub struct Error {
    inner: Context<ErrorKind>,
}

impl self::Error {
    pub fn kind(&self) -> ErrorKind {
        *self.inner.get_context()
    }
}

impl Fail for self::Error {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl fmt::Display for self::Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl From<ErrorKind> for self::Error {
    fn from(kind: ErrorKind) -> self::Error {
        self::Error { inner: Context::new(kind) }
    }
}

impl From<Context<ErrorKind>> for self::Error {
    fn from(inner: Context<ErrorKind>) -> self::Error {
        self::Error { inner }
    }
}

impl From<curl::Error> for self::Error {
    fn from(error: curl::Error) -> self::Error {
        failure::Error::from(error).context(ErrorKind::RequestFailed).into()
    }
}