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
//! APIs used to send HTTP Responses for HTTP-triggered serverless functions.

use super::error::{Error, HttpError};
use super::stream::{BaseStream, WriteStream};
use bytes::Bytes;
use http::header::{HeaderMap, HeaderName, HeaderValue};
use http::{Response, StatusCode, Version};
use num_traits::FromPrimitive;

#[link(wasm_import_module = "http_response")]
extern "C" {
    fn host_response_send(
        status: u32,
        version_addr: *const u8,
        version_len: u32,
        headers_addr: *const u8,
        headers_len: u32,
        body_addr: *const u8,
        body_len: u32,
        err_ptr: *mut i32,
    ) -> i32;

    fn host_response_send_streaming(
        status: u32,
        version_addr: *const u8,
        version_len: u32,
        headers_addr: *const u8,
        headers_len: u32,
        sd_ptr: *mut u32,
        err_ptr: *mut i32,
    ) -> i32;
}

/// An HTTP response which may include body, headers, and status code.
///
/// Each serverless function can send only one response to the client.
///
/// ## Sending HTTP Response to client
///
/// - [`HttpResponse::send()`]
/// - [`HttpResponse::send_using_standard_http_lib()`]
///
/// Normally, these methods do not need to be used explicity. EDJX sample
/// [`Rust Serverless Application`](https://github.com/edjx/edjfunction-example-rust)
/// code file (`lib.rs`) uses one of these methods to process Responses and relay to the client.
/// 
/// If HttpResponse needs to be sent using Streams, then use [`HttpResponse::send_streaming()`].
///
///  ```
/// #[no_mangle]
/// pub fn init() {
///   let req = match HttpRequest::from_client();
///
///   let res = crate::serverless_function::serverless(req.unwrap());
///   
///   match res.send() {
///       Ok(x) => x,
///       Err(e) => {
///           error!("{}", e.to_string().as_str());
///       }
///   };
/// }
/// ```
/// Developers need to change only the above `init()` function in `lib.rs` when the HTTP Response type needs
/// to be modified or sent using Streams.
///
/// ## Construction of HTTP Responses
///
/// - [`HttpResponse::new()`]
/// - [`HttpResponse::from()`]
///
/// For interoperability with other Rust libraries, [`HttpResponse`] can be initiated using
/// [`http`], which is crate's [`http::Response`] type along with the [`HttpResponse::send_using_standard_http_lib()`]
/// function passing [`http::Response<Option<Bytes>>`] as parameters.
///
/// ## Builder-style Methods
///
/// [`HttpResponse`] can be used as a
/// [builder](https://doc.rust-lang.org/1.0.0/style/ownership/builders.html), allowing responses to
/// be constructed and used through method chaining. Methods with the `set_` name prefix, such as
/// [`set_header()`][`Self::set_text()`], return `Self` to allow chaining. The builder style is
/// typically most useful when constructing and using a response in a single expression. For
/// example:
///
/// ```no_run
/// use edjx::HttpResponse;
/// HttpResponse::new()
///     .set_header("edjx-header".parse().unwrap(), "hello-there".parse().unwrap())
///     .set_status(StatusCode::OK)
///     .send();
/// ```
pub struct HttpResponse {
    parts: Parts,
    body: Option<Vec<u8>>,
}

/// Component parts of `HttpResponse`
///
/// The HTTP response head consists of a status, version, and a set of
/// header fields.
#[doc(hidden)]
struct Parts {
    status: StatusCode,
    version: Version,
    headers: HeaderMap,
}

#[doc(hidden)]
impl Parts {
    /// Creates a new default instance of `Parts` with status code `200 OK`, version as [`HTTP/1.1`] and no headers.
    fn new() -> Parts {
        Parts {
            status: StatusCode::OK,
            version: Version::HTTP_11,
            headers: HeaderMap::new(),
        }
    }
}

impl HttpResponse {
    /// Creates a client [`HttpResponse`].
    ///
    /// The response is created with status code `200 OK`, no headers, and an empty body.
    pub fn new() -> HttpResponse {
        HttpResponse {
            body: None,
            parts: Parts::new(),
        }
    }

    /// Sets the HTTP status code of the client response.
    ///
    /// # Example
    ///
    /// Using the constants from [`http::StatusCode`]:
    ///
    /// ```no_run
    /// use edjx::HttpResponse;
    /// use http::StatusCode;
    ///
    /// let mut resp = HttpResponse::from("not found!").set_status(StatusCode::NOT_FOUND);
    /// resp.send();
    /// ```
    pub fn set_status(mut self, status: StatusCode) -> Self {
        self.parts.status = status;
        return self;
    }

    /// Returns the HTTP status code of the response.
    pub fn get_status(&self) -> StatusCode {
        self.parts.status
    }

    /// Sets the HTTP version of this response. Use constants from [`http::Version`]:
    pub fn set_version(mut self, version: Version) -> Self {
        self.parts.version = version;
        return self;
    }

    /// Returns the HTTP version of this response.
    pub fn get_version(&self) -> Version {
        return self.parts.version;
    }

    /// Sets the given [`&str`] value as the body of the client response.
    ///
    /// Any previous body that was set on the response is discarded.
    pub fn set_text(mut self, text: &str) -> Self {
        self.body = Some(Vec::from(text));
        return self;
    }

    /// Sets the given [`Vec<u8>`] value as the body of the client response.
    ///
    /// Any previous body that was set on the response is discarded.
    pub fn set_vec_bytes(mut self, bytes: Vec<u8>) -> Self {
        self.body = Some(bytes);
        return self;
    }

    /// Sets a response header to the given value, discarding any previous values for the given
    /// header name.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use edjx::HttpResponse;
    ///
    /// let mut resp = HttpResponse::new().set_header("hello".parse().unwrap(), "world!".parse().unwrap());
    ///
    /// let header_map = resp.get_headers();
    /// assert!(header_map.contains_key("hello"));
    /// ```
    pub fn set_header(mut self, header_name: HeaderName, value: HeaderValue) -> Self {
        self.parts.headers.append(header_name, value);
        return self;
    }

    /// Returns the response header map as [`http::header::HeaderMap`].
    pub fn get_headers(&self) -> &HeaderMap {
        return &self.parts.headers;
    }

    /// Returns the response header map as a mutable reference to [`http::header::HeaderMap`].
    pub fn headers_mut(&mut self) -> &mut HeaderMap {
        return &mut self.parts.headers;
    }

    /// Send the response to the client.
    ///
    /// # Error Response
    ///
    /// This method returns an error response of [`HttpError::Unknown`] if another response was sent to the client
    /// by the method [`HttpResponse::send_using_standard_http_lib()`], or even if http connection from client has been closed.
    ///
    /// # Example
    /// ```
    /// #[no_mangle]
    /// pub fn init() {
    ///   let req = match HttpRequest::from_client()
    ///
    ///   let res = crate::serverless_function::serverless(req);
    ///     match res.send() {
    ///         Ok(x) => x,
    ///         Err(e) => {
    ///             error!("{}", e.to_string().as_str());
    ///         }
    ///     };
    /// }
    /// ```
    pub fn send(&self) -> Result<(), HttpError> {
        let headers_vec =
            serde_json::to_vec(&super::utils::convert_to_hashmap(&self.parts.headers)).unwrap();
        let headers_slice = headers_vec.as_slice();
        let status = self.parts.status.as_u16();
        let version: String = super::utils::to_version_string(&self.parts.version);
        let body_len: u32;
        let body_slice;
        let empty_body_vec = Vec::new();
        if self.body.is_none() {
            body_len = 0;
            body_slice = empty_body_vec.as_slice();
        } else {
            body_slice = self.body.as_ref().unwrap().as_slice();
            body_len = body_slice.len() as u32;
        }

        let mut err = 0;
        let resp = unsafe {
            host_response_send(
                status as u32,
                version.as_ptr(),
                version.len() as u32,
                headers_slice.as_ptr(),
                headers_slice.len() as u32,
                body_slice.as_ptr(),
                body_len,
                &mut err,
            )
        };

        if resp < 0 {
            return Err(HttpError::from(Error::from_i32(err).unwrap()));
        }

        Ok(())
    }

    /// Sends the EDJX HTTP Function's response to the client using streaming.
    /// This method returns a [`WriteStream`], which can be used to stream data.
    ///
    /// `HttpResponse::send_streaming()` and `HttpResponse::send()` cannot be used at the same time.
    ///
    /// # Error Response
    ///
    /// This method returns an error response of [`HttpError::Unknown`] if another response was sent to the client
    /// by the method [`HttpResponse::send_using_standard_http_lib()`], or even if http connection from client has been closed.
    ///
    /// # Example
    /// ```
    /// use edjx::{BaseStream, HttpRequest, HttpResponse};
    ///
    /// let mut http_resp = HttpResponse::new()
    ///     .set_header("ServerlessStream".parse().unwrap(), "EDJX".parse().unwrap());
    /// let mut write_stream = http_resp.send_streaming().unwrap();
    ///
    /// write_stream.write_chunk_text("text chunk").unwrap();
    /// write_stream.write_chunk_binary(Vec::from("binary chunk")).unwrap();
    /// write_stream.close().unwrap();
    ///
    /// ```
    pub fn send_streaming(&mut self) -> Result<WriteStream, HttpError> {
        let headers_vec =
            serde_json::to_vec(&super::utils::convert_to_hashmap(&self.parts.headers)).unwrap();
        let headers_slice = headers_vec.as_slice();
        let status = self.parts.status.as_u16();
        let version: String = super::utils::to_version_string(&self.parts.version);

        let mut err = 0;
        let mut sd: u32 = 0;
        let resp = unsafe {
            host_response_send_streaming(
                status as u32,
                version.as_ptr(),
                version.len() as u32,
                headers_slice.as_ptr(),
                headers_slice.len() as u32,
                &mut sd,
                &mut err,
            )
        };

        if resp < 0 {
            return Err(HttpError::from(Error::from_i32(err).unwrap()));
        }

        Ok(BaseStream::new(sd))
    }

    /// Send the response to the client using [`http::Response`].
    ///
    ///
    /// # Error Response
    ///
    /// This method returns an error response of [`HttpError::Unknown`] if another response was sent.
    ///
    /// # Example
    ///
    /// Sending a response using [`http::Response`]:
    ///
    /// ```no_run
    /// use edjx::HttpResponse;
    /// use http::{Response, StatusCode};
    ///
    /// let mut response = Response::builder();
    /// response.header("Foo", "Bar").status(StatusCode::OK);
    /// HttpResponse::send_using_standard_http_lib(response.body(()));
    ///
    /// ```
    pub fn send_using_standard_http_lib(res: Response<Option<Bytes>>) -> Result<(), HttpError> {
        let headers_vec =
            serde_json::to_vec(&super::utils::convert_to_hashmap(&res.headers())).unwrap();
        let headers_slice = headers_vec.as_slice();
        let status = res.status().as_u16();
        let version: String = super::utils::to_version_string(&res.version());
        let body = match res.body() {
            None => Default::default(),
            Some(body) => body.as_ref(),
        };
        let mut err = 0;
        let resp = unsafe {
            host_response_send(
                status as u32,
                version.as_ptr(),
                version.len() as u32,
                headers_slice.as_ptr(),
                headers_slice.len() as u32,
                body.as_ptr(),
                body.len() as u32,
                &mut err,
            )
        };

        if resp < 0 {
            return Err(HttpError::from(Error::from_i32(err).unwrap()));
        }

        Ok(())
    }
}

impl From<String> for HttpResponse {
    #[inline]
    fn from(s: String) -> HttpResponse {
        HttpResponse {
            body: Some(Vec::from(s)),
            parts: Parts::new(),
        }
    }
}

impl From<&str> for HttpResponse {
    #[inline]
    fn from(s: &str) -> HttpResponse {
        HttpResponse {
            body: Some(Vec::from(s)),
            parts: Parts::new(),
        }
    }
}

impl From<Vec<u8>> for HttpResponse {
    #[inline]
    fn from(v: Vec<u8>) -> HttpResponse {
        HttpResponse {
            body: Some(v),
            parts: Parts::new(),
        }
    }
}