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
//! Read-only HTTP request input for HTTP-trigger serverless functions.
//!
//! For executing HTTP Requests, use [`crate::HttpFetch`].

use super::error::{Error, HTTPBodyTooLargeError, HeaderError, HttpError, UriError};
use super::stream::{BaseStream, ReadStream};
use http::Uri;
use http::{header::HeaderName, HeaderMap, Version};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::str;
use std::str::FromStr;
use std::{collections::HashMap, convert::TryFrom};
use url::form_urlencoded;

#[link(wasm_import_module = "request")]
extern "C" {
    fn http_cur_req_get_method() -> i32;
    fn http_cur_req_get_uri(err_code: *mut i32) -> i32;
    fn http_cur_req_get_body(error_code: *mut i32) -> i32;
    fn http_cur_req_get_read_stream(read_sd: *mut u32) -> i32;
    fn http_cur_req_get_all_header_keys(err_code: *mut i32) -> i32;
    fn http_cur_req_get_header_value(
        header_ptr: *const u8,
        header_len: i32,
        err_code: *mut i32,
    ) -> i32;
}

/// Request which may include version, body, headers, method, and URL.
///
#[derive(Debug)]
pub struct HttpRequest {
    version: Version,
    method: HttpMethod,
    uri: Uri,
    headers: HeaderMap,
    body: Option<Vec<u8>>,
    read_stream: Option<ReadStream>,
}

/// Enum containing a number of common HTTP methods.
#[derive(Debug, FromPrimitive, PartialEq)]
pub enum HttpMethod {
    NONE = 0,
    GET = 1,
    HEAD = 2,
    POST = 3,
    PUT = 4,
    DELETE = 5,
    CONNECT = 6,
    OPTIONS = 7,
    TRACE = 8,
    PATCH = 9,
}

impl FromStr for HttpMethod {
    type Err = ();

    fn from_str(input: &str) -> Result<HttpMethod, Self::Err> {
        match input.to_uppercase().as_str() {
            "GET" => Ok(HttpMethod::GET),
            "HEAD" => Ok(HttpMethod::HEAD),
            "POST" => Ok(HttpMethod::POST),
            "PUT" => Ok(HttpMethod::PUT),
            "DELETE" => Ok(HttpMethod::DELETE),
            "CONNECT" => Ok(HttpMethod::CONNECT),
            "OPTIONS" => Ok(HttpMethod::OPTIONS),
            "TRACE" => Ok(HttpMethod::TRACE),
            "PATCH" => Ok(HttpMethod::PATCH),
            _ => Ok(HttpMethod::NONE),
        }
    }
}

impl HttpRequest {
    /// Returns the client request being handled by the serverless function.
    ///
    /// # Panics
    ///
    /// This method panics if the client request has already been retrieved by this method once and the body
    /// has been fetched.
    ///
    /// # Limits Exceeded Errors
    ///
    /// If the request exceeds the limits specified, this method sends an error response
    /// with a corresponding error code for the exceeding limit, so that the same can be
    /// handled explicitly by returning a customized error page.
    ///  
    ///             
    ///  
    /// Normally, this method does not need to be used explicity. EDJX sample
    /// [`Rust Serverless Application`](https://github.com/edjx/edjfunction-example-rust)
    /// code file (lib.rs) uses this method to fetch HTTP Request.
    ///
    /// `lib.rs`:
    ///
    /// ```
    /// #[no_mangle]
    /// pub fn init() {
    ///   let req = HttpRequest::from_client();
    ///   crate::serverless_function::serverless(req.unwrap());
    /// }
    /// ```
    ///
    /// `init()` function in `lib.rs` needs to be changed only if a
    /// mutable reference to the request is required.
    ///
    pub fn from_client() -> Result<HttpRequest, HttpError> {
        let mut headers = HeaderMap::new();
        let header_keys = get_all_header_keys_from_host()?;
        for key in header_keys {
            headers.insert(
                HeaderName::from_str(key.as_str()).unwrap(),
                get_header_value_from_host(key.as_str())?.parse().unwrap(),
            );
        }

        Ok(HttpRequest {
            version: Version::HTTP_11,
            method: get_method_from_host(),
            uri: get_uri_from_host()?,
            headers: headers,
            body: None,
            read_stream: None,
        })
    }

    /// Returns the enum value for HttpMethod.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use edjx::{HttpMethod, HttpRequest};
    /// use std::str::FromStr;
    ///
    /// fn serverless(req: &HttpRequest) {
    ///     let method = req.method();
    ///     assert_eq!(method, &HttpMethod::from_str("get").unwrap());
    /// }
    /// ```
    pub fn method(&self) -> &HttpMethod {
        return &self.method;
    }

    /// Returns the request URI value as [`http::Uri`] object. Use different functions available in
    /// [`http::Uri`] for the corresponding
    /// object, as needed.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use edjx::{HttpRequest};
    ///
    /// fn serverless(req: &HttpRequest) {
    ///     let uri = req.uri();
    ///     assert_eq!(uri.path(), "/foo/bar");
    /// }
    /// ```
    pub fn uri(&self) -> &Uri {
        return &self.uri;
    }

    /// Returns the query string of request as an `Option` of [`&str`].
    pub fn query(&self) -> Option<&str> {
        return self.uri.query();
    }

    /// Returns the optional value for a provided key in query paramters.
    pub fn query_param_by_name(&self, name: &str) -> Option<String> {
        let query_params: HashMap<String, String> = self
            .uri
            .query()
            .map(|v| form_urlencoded::parse(v.as_bytes()).into_owned().collect())
            .unwrap_or_else(HashMap::new);

        match query_params.get(name) {
            Some(param_value) => Some(param_value.to_string()),
            None => None,
        }
    }

    /// Returns the path part of the request URI.
    pub fn path(&self) -> &str {
        return &self.uri.path();
    }

    /// Returns the map of headers in the request as an [`http::HeaderMap`] object.
    /// Use different functions available in [`http::HeaderMap`] for the corresponding
    /// object, as needed.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use edjx::{HttpRequest};
    ///
    /// fn serverless(req: &HttpRequest) {
    ///     let headers = req.headers();
    ///     assert!(headers.get("host").is_some());
    ///     assert_eq!(headers.get("host").unwrap(), &"example.com");
    /// }
    /// ```
    pub fn headers(&self) -> &HeaderMap {
        return &self.headers;
    }

    /// Returns the body associated with the request as a `Result` response of
    /// [`Vec<u8>`].
    ///
    /// # Example
    ///
    /// ```no_run
    /// use edjx::{error, HttpRequest, HttpResponse};
    ///
    /// fn serverless(req: &HttpRequest) {
    ///     let body = match req.body() {
    ///        Ok(body_vec_option) => body_vec_option,
    ///        Err(e) => {
    ///            error!("{}", &e.to_string());
    ///            HttpResponse::new()
    ///                .set_status(StatusCode::BAD_REQUEST)
    ///                .send()
    ///                .unwrap();
    ///            return;
    ///        }
    ///     }
    /// }
    /// ```
    pub fn body(&mut self) -> Result<&Option<Vec<u8>>, HttpError> {
        if self.body.is_none() {
            self.body = Some(get_body_from_host()?);
        }
        return Ok(&self.body);
    }

    /// Opens a read stream to read the request body.
    ///
    /// [`HttpRequest::body`] and [`HttpRequest::get_read_stream`] cannot be used at the same time.
    pub fn get_read_stream(&mut self) -> Result<&mut ReadStream, HttpError> {
        if self.read_stream.is_none() {
            match get_read_stream_from_host() {
                Ok(rs) => {
                    self.read_stream = Some(rs);
                }
                Err(err) => {
                    return Err(err);
                }
            }
        }
        Ok(self.read_stream.as_mut().unwrap())
    }
}

fn get_method_from_host() -> HttpMethod {
    let response = unsafe { http_cur_req_get_method() };

    let method_enum_value: Option<HttpMethod> = FromPrimitive::from_i32(response);
    match method_enum_value {
        Some(v) => v,
        None => HttpMethod::NONE,
    }
}

fn get_uri_from_host() -> Result<Uri, UriError> {
    let mut err = 0;
    let response = unsafe { http_cur_req_get_uri(&mut err) };

    if response > 0 {
        let value = String::from_utf8(super::result::get_result_bytes(response).unwrap()).unwrap();
        value.parse().map_err(|_| UriError::InvalidUri)
    } else {
        Err(UriError::try_from(err).unwrap())
    }
}

fn get_body_from_host() -> Result<Vec<u8>, HTTPBodyTooLargeError> {
    let mut err = 0;
    let response = unsafe { http_cur_req_get_body(&mut err) };

    if response > 0 {
        let result = super::result::get_result_bytes(response);
        Ok(result.unwrap())
    } else if response == 0 {
        Ok(Vec::with_capacity(0))
    } else {
        assert!(err == Error::HTTPTooLargeBody as i32);
        return Err(HTTPBodyTooLargeError());
    }
}

fn get_read_stream_from_host() -> Result<ReadStream, HttpError> {
    let mut read_sd = 0;
    let response = unsafe { http_cur_req_get_read_stream(&mut read_sd) };
    if response < 0 {
        return Err(HttpError::from(Error::from_i32(-1).unwrap()));
    }

    Ok(BaseStream::new(read_sd))
}

fn get_all_header_keys_from_host() -> Result<Vec<String>, HeaderError> {
    let mut err = 0;
    let response = unsafe { http_cur_req_get_all_header_keys(&mut err) };

    if response > 0 {
        let data = super::result::get_result_bytes(response).unwrap();

        let keys_delimited_string = str::from_utf8(&data).unwrap();
        let mut v = Vec::new();
        for header_key in keys_delimited_string.split(",") {
            if header_key.len() > 0 {
                v.push(String::from(header_key))
            }
        }
        Ok(v)
    } else if response == 0 {
        Ok(Vec::with_capacity(0))
    } else {
        assert!(err == Error::HTTPTooLargeHeaderName as i32);
        Err(HeaderError::TooLargeName)
    }
}

fn get_header_value_from_host(header_key: &str) -> Result<String, HeaderError> {
    let mut key = String::from(header_key);
    let key = key.as_mut_str();
    let dest_length = key.len() as i32;
    let dest_pointer = key.as_mut_ptr() as *const u8;
    let mut err = 0;

    let result_size = unsafe { http_cur_req_get_header_value(dest_pointer, dest_length, &mut err) };
    if result_size > 0 {
        Ok(String::from_utf8(super::result::get_result_bytes(result_size).unwrap()).unwrap())
    } else if result_size == 0 {
        Ok(String::with_capacity(0))
    } else {
        Err(HeaderError::from_i32(err).unwrap())
    }
}