http-client-unix-domain-socket 0.1.1

A simple HTTP (json) client using UNIX domain socket in Rust
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#[cfg(feature = "json")]
use crate::error::ErrorAndResponseJson;
use crate::{Error, error::ErrorAndResponse};
use axum_core::body::Body;
use http_body_util::BodyExt;
use hyper::{
    Method, Request, StatusCode,
    client::conn::http1::{self, SendRequest},
};
use hyper_util::rt::TokioIo;
#[cfg(feature = "json")]
use serde::{Serialize, de::DeserializeOwned};
use std::path::PathBuf;
use tokio::{net::UnixStream, task::JoinHandle};

/// A simple HTTP (json) client using UNIX domain socket in Rust
#[derive(Debug)]
pub struct ClientUnix {
    socket_path: PathBuf,
    sender: SendRequest<Body>,
    join_handle: JoinHandle<Error>,
}

impl ClientUnix {
    /// Create a new HTTP client and try to connect to it.
    ///
    /// # Example
    /// ```rust
    /// use http_client_unix_domain_socket::ClientUnix;
    ///
    /// pub async fn new_client() {
    ///     ClientUnix::try_new("/tmp/unix.socket").await.expect("ClientUnix::try_new");
    /// }
    /// ```
    pub async fn try_new(socket_path: &str) -> Result<Self, Error> {
        let socket_path = PathBuf::from(socket_path);
        ClientUnix::try_connect(socket_path).await
    }

    /// Reconnect to an existing [ClientUnix].
    ///
    /// Sometimes the server to which the client is connected may reboot, causing the client to disconnect. For simplicity, no automatic reconnection is implemented - it must be manually performed by calling this function.
    /// The error will be probably trigger during the [ClientUnix::send_request](or [ClientUnix::send_request_json]) with this error [Error::RequestSend].
    /// # Example
    /// ```rust
    /// use http_client_unix_domain_socket::{ClientUnix, Method, Error, ErrorAndResponse};
    ///
    /// pub async fn reconnect_after_failure() {
    ///     let mut client = ClientUnix::try_new("/tmp/unix.socket").await.expect("ClientUnix::try_new");
    ///     let response_result = client.send_request("/nolanv", Method::GET, &[], None).await;
    ///
    ///     if(matches!(
    ///         response_result.err(),
    ///         Some(ErrorAndResponse::InternalError(Error::RequestSend(e)))
    ///             if e.is_canceled()
    ///     )){
    ///         client = client.try_reconnect().await.expect("client.try_reconnect");
    ///     }
    /// }
    /// ```
    pub async fn try_reconnect(self) -> Result<Self, Error> {
        let socket_path = self.socket_path.clone();
        self.abort().await;
        ClientUnix::try_connect(socket_path).await
    }

    /// Abort the [ClientUnix] connection [JoinHandle].
    ///
    /// Used for stopping the connection [JoinHandle]([tokio::task]), it's also used for [ClientUnix::try_reconnect]. The returned [Error] can be used to know if it was stopped without any error.
    pub async fn abort(self) -> Option<Error> {
        self.join_handle.abort();
        self.join_handle.await.ok()
    }

    async fn try_connect(socket_path: PathBuf) -> Result<Self, Error> {
        let stream = TokioIo::new(
            UnixStream::connect(socket_path.clone())
                .await
                .map_err(Error::SocketConnectionInitiation)?,
        );

        let (sender, connection) = http1::handshake(stream).await.map_err(Error::Handhsake)?;

        let join_handle =
            tokio::task::spawn(
                async move { Error::SocketConnectionClosed(connection.await.err()) },
            );

        Ok(ClientUnix {
            socket_path,
            sender,
            join_handle,
        })
    }

    /// Send a raw HTTP request.
    ///
    /// The [ClientUnix::send_request] method allows sending an HTTP request without serializing it. This method can be useful when communicating using a format other than JSON, or for endpoints that don’t return responses adhering to the JSON format. [Error] are wrapped in an Enum [ErrorAndResponse] that includes both [ErrorAndResponse::InternalError] and HTTP response [ErrorAndResponse::ResponseUnsuccessful].
    /// # Examples
    /// ## HTTP GET
    /// ```rust
    /// use http_client_unix_domain_socket::{ClientUnix, Method, StatusCode, ErrorAndResponse};
    ///
    /// pub async fn get_hello_world() {
    ///     let mut client = ClientUnix::try_new("/tmp/unix.socket")
    ///         .await
    ///         .expect("ClientUnix::try_new");
    ///
    ///     match client
    ///         .send_request("/nolanv", Method::GET, &vec![("Host", "localhost")], None)
    ///         .await
    ///     {
    ///         Err(ErrorAndResponse::ResponseUnsuccessful(status_code, response)) => {
    ///             assert!(status_code == StatusCode::NOT_FOUND);
    ///             assert!(response == "not found".as_bytes());
    ///         }
    ///
    ///         Ok((status_code, response)) => {
    ///             assert_eq!(status_code, StatusCode::OK);
    ///             assert_eq!(response, "Hello nolanv".as_bytes());
    ///         }
    ///
    ///         Err(_) => panic!("Something went wrong")
    ///     }
    /// }
    /// ```
    /// ## HTTP POST
    /// ```rust
    /// use http_client_unix_domain_socket::{ClientUnix, Method, StatusCode, Body};
    ///
    /// pub async fn post_hello_world() {
    ///     let mut client = ClientUnix::try_new("/tmp/unix.socket")
    ///         .await
    ///         .expect("ClientUnix::try_new");
    ///
    ///     let (status_code, response) = client
    ///         .send_request("/", Method::POST, &[], Some(Body::from("nolanv")))
    ///         .await
    ///         .expect("client.send_request");
    ///
    ///     assert_eq!(status_code, StatusCode::OK);
    ///     assert_eq!(response, "Hello nolanv".as_bytes());
    /// }
    /// ```
    pub async fn send_request(
        &mut self,
        endpoint: &str,
        method: Method,
        headers: &[(&str, &str)],
        body_request: Option<Body>,
    ) -> Result<(StatusCode, Vec<u8>), ErrorAndResponse> {
        let mut request_builder = Request::builder();
        for header in headers {
            request_builder = request_builder.header(header.0, header.1);
        }
        let request = request_builder
            .method(method)
            .uri(format!("http://unix.socket{}", endpoint))
            .body(body_request.unwrap_or(Body::empty()))
            .map_err(|e| ErrorAndResponse::InternalError(Error::RequestBuild(e)))?;

        let response = self
            .sender
            .send_request(request)
            .await
            .map_err(|e| ErrorAndResponse::InternalError(Error::RequestSend(e)))?;

        let status_code = response.status();
        let body_response = response
            .collect()
            .await
            .map_err(|e| ErrorAndResponse::InternalError(Error::ResponseCollect(e)))?
            .to_bytes();

        if !status_code.is_success() {
            return Err(ErrorAndResponse::ResponseUnsuccessful(
                status_code,
                body_response.to_vec(),
            ));
        }
        Ok((status_code, body_response.to_vec()))
    }

    /// Send JSON HTTP request **(feature = json)**
    ///
    /// Use [ClientUnix::send_request], adding automatically the "Content-Type" header and handling JSON (de)serialization for both the request body and response. This method does not use the same [Error] Enum, enabling typed error responses instead via [ErrorAndResponseJson].
    /// # Examples
    /// ## HTTP POST JSON **(feature = json)**
    /// ```rust
    /// use http_client_unix_domain_socket::{ClientUnix, Method, StatusCode, ErrorAndResponseJson};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Serialize)]
    /// struct NameJson {
    ///     name: String,
    /// }
    ///
    /// #[derive(Deserialize)]
    /// struct HelloJson {
    ///     hello: String,
    /// }
    ///
    /// #[derive(Deserialize, Debug)]
    /// struct ErrorJson {
    ///     msg: String,
    /// }
    ///
    /// pub async fn post_hello_world() {
    ///     let mut client = ClientUnix::try_new("/tmp/unix.socket")
    ///         .await
    ///         .expect("ClientUnix::try_new");
    ///
    ///     match client
    ///         .send_request_json::<NameJson, HelloJson, ErrorJson>(
    ///             "/nolanv",
    ///             Method::POST,
    ///             &vec![("Host", "localhost")],
    ///             Some(&NameJson { name: "nolanv".into() }))
    ///         .await
    ///     {
    ///         Err(ErrorAndResponseJson::ResponseUnsuccessful(status_code, response)) => {
    ///             assert!(status_code == StatusCode::BAD_REQUEST);
    ///             assert!(response.msg == "bad request");
    ///         }
    ///
    ///         Ok((status_code, response)) => {
    ///             assert_eq!(status_code, StatusCode::OK);
    ///             assert_eq!(response.hello, "nolanv");
    ///         }
    ///
    ///         Err(_) => panic!("Something went wrong")
    ///     }
    /// }
    /// ```
    #[cfg(feature = "json")]
    pub async fn send_request_json<IN: Serialize, OUT: DeserializeOwned, ERR: DeserializeOwned>(
        &mut self,
        endpoint: &str,
        method: Method,
        headers: &[(&str, &str)],
        body_request: Option<&IN>,
    ) -> Result<(StatusCode, OUT), ErrorAndResponseJson<ERR>> {
        let mut headers = headers.to_vec();
        headers.push(("Content-Type", "application/json"));

        let body_request = match body_request {
            Some(body_request) => Body::from(
                serde_json::to_vec(body_request)
                    .map_err(|e| ErrorAndResponseJson::InternalError(Error::RequestParsing(e)))?,
            ),
            None => Body::empty(),
        };

        match self
            .send_request(endpoint, method, &headers, Some(body_request))
            .await
        {
            Ok((status_code, response)) => Ok((
                status_code,
                serde_json::from_slice(&response)
                    .map_err(|e| ErrorAndResponseJson::InternalError(Error::ResponseParsing(e)))?,
            )),
            Err(ErrorAndResponse::InternalError(e)) => Err(ErrorAndResponseJson::InternalError(e)),
            Err(ErrorAndResponse::ResponseUnsuccessful(status_code, response)) => {
                Err(ErrorAndResponseJson::ResponseUnsuccessful(
                    status_code,
                    serde_json::from_slice(&response).map_err(|e| {
                        ErrorAndResponseJson::InternalError(Error::ResponseParsing(e))
                    })?,
                ))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_helpers::{server::Server, util::*};
    use hyper::Method;

    #[tokio::test]
    async fn simple_request() {
        let (_, mut client) = make_client_server("simple_request").await;

        let (status_code, response) = client
            .send_request("/nolanv", Method::GET, &[], None)
            .await
            .expect("client.send_request");

        assert_eq!(status_code, StatusCode::OK);
        assert_eq!(response, "Hello nolanv".as_bytes())
    }

    #[tokio::test]
    async fn simple_404_request() {
        let (_, mut client) = make_client_server("simple_404_request").await;

        let result = client
            .send_request("/nolanv/nope", Method::GET, &[], None)
            .await;

        assert!(matches!(
            result.err(),
            Some(ErrorAndResponse::ResponseUnsuccessful(status_code, _))
                if status_code == StatusCode::NOT_FOUND
        ));
    }

    #[tokio::test]
    async fn multiple_request() {
        let (_, mut client) = make_client_server("multiple_request").await;

        for i in 0..20 {
            let (status_code, response) = client
                .send_request(&format!("/nolanv{}", i), Method::GET, &[], None)
                .await
                .expect("client.send_request");

            assert_eq!(status_code, StatusCode::OK);

            assert_eq!(response, format!("Hello nolanv{}", i).as_bytes())
        }
    }

    #[tokio::test]
    async fn server_not_started() {
        let socket_path = make_socket_path_test("client", "server_not_started");

        let client = ClientUnix::try_new(&socket_path).await;
        assert!(matches!(
            client.err(),
            Some(Error::SocketConnectionInitiation(_))
        ));
    }

    #[tokio::test]
    async fn server_stopped() {
        let (server, mut client) = make_client_server("server_stopped").await;
        server.abort().await;

        let response_result = client.send_request("/nolanv", Method::GET, &[], None).await;
        assert!(matches!(
            response_result.err(),
                         Some(ErrorAndResponse::InternalError(Error::RequestSend(e)))
                         if e.is_canceled()
        ));

        let _ = Server::try_new(&make_socket_path_test("client", "server_stopped"))
            .await
            .expect("Server::try_new");
        let mut http_client = client.try_reconnect().await.expect("client.try_reconnect");

        let (status_code, response) = http_client
            .send_request("/nolanv", Method::GET, &[], None)
            .await
            .expect("client.send_request");

        assert_eq!(status_code, StatusCode::OK);
        assert_eq!(response, "Hello nolanv".as_bytes())
    }

    #[tokio::test]
    async fn server_rebooted() {
        let (server, mut client) = make_client_server("server_rebooted").await;
        server.abort().await;

        let _ = Server::try_new(&make_socket_path_test("client", "server_rebooted"))
            .await
            .expect("Server::try_new");

        let response_result = client.send_request("/nolanv", Method::GET, &[], None).await;
        assert!(matches!(
            response_result.err(),
                         Some(ErrorAndResponse::InternalError(Error::RequestSend(e)))
                         if e.is_canceled()
        ));
        let mut http_client = client.try_reconnect().await.expect("client.try_reconnect");

        let (status_code, response) = http_client
            .send_request("/nolanv", Method::GET, &[], None)
            .await
            .expect("client.send_request");

        assert_eq!(status_code, StatusCode::OK);
        assert_eq!(response, "Hello nolanv".as_bytes())
    }
}

#[cfg(feature = "json")]
#[cfg(test)]
mod json_tests {
    use hyper::{Method, StatusCode};
    use serde::{Deserialize, Serialize};
    use serde_json::{Value, json};

    use crate::{error::ErrorAndResponseJson, test_helpers::util::make_client_server};

    #[derive(Deserialize, Debug)]
    struct ErrorJson {
        msg: String,
    }

    #[tokio::test]
    async fn simple_get_request() {
        let (_, mut client) = make_client_server("simple_get_request").await;

        let (status_code, response) = client
            .send_request_json::<(), Value, Value>("/json/nolanv", Method::GET, &[], None)
            .await
            .expect("client.send_request_json");

        assert_eq!(status_code, StatusCode::OK);
        assert_eq!(response.get("hello"), Some(&json!("nolanv")))
    }

    #[tokio::test]
    async fn simple_get_404_request() {
        let (_, mut client) = make_client_server("simple_get_404_request").await;

        let result = client
            .send_request_json::<(), Value, ErrorJson>("/json/nolanv/nop", Method::GET, &[], None)
            .await;

        dbg!(&result);
        assert!(matches!(
            result.err(),
                         Some(ErrorAndResponseJson::ResponseUnsuccessful(status_code, body))
                         if status_code == StatusCode::NOT_FOUND && body.msg == "not found"
        ));
    }

    #[tokio::test]
    async fn simple_post_request() {
        let (_, mut client) = make_client_server("simple_post_request").await;

        #[derive(Serialize)]
        struct NameJson {
            name: String,
        }

        #[derive(Deserialize)]
        struct HelloJson {
            hello: String,
        }

        let (status_code, response) = client
            .send_request_json::<NameJson, HelloJson, Value>(
                "/json",
                Method::POST,
                &[],
                Some(&NameJson {
                    name: "nolanv".into(),
                }),
            )
            .await
            .expect("client.send_request_json");

        assert_eq!(status_code, StatusCode::OK);
        assert_eq!(response.hello, "nolanv")
    }

    #[tokio::test]
    async fn simple_post_bad_request() {
        let (_, mut client) = make_client_server("simple_post_bad_request").await;

        #[derive(Serialize)]
        struct NameBadJson {
            nom: String,
        }

        let result = client
            .send_request_json::<NameBadJson, Value, ErrorJson>(
                "/json",
                Method::POST,
                &[],
                Some(&NameBadJson {
                    nom: "nolanv".into(),
                }),
            )
            .await;

        assert!(matches!(
            result.err(),
                         Some(ErrorAndResponseJson::ResponseUnsuccessful(status_code, body))
                         if status_code == StatusCode::BAD_REQUEST && body.msg == "bad request"
        ));
    }
}