@@ -6,6 +6,7 @@ use gleam_core::{Error, Result};
6
6
use http:: { Request , Response } ;
7
7
8
8
static REQWEST_CLIENT : OnceLock < reqwest:: Client > = OnceLock :: new ( ) ;
9
+ static CERTS_ENV_VAR : & str = "GLEAM_CACERTS_PATH" ;
9
10
10
11
#[ derive( Debug ) ]
11
12
pub struct HttpClient ;
@@ -27,7 +28,7 @@ impl gleam_core::io::HttpClient for HttpClient {
27
28
. try_into ( )
28
29
. expect ( "Unable to convert HTTP request for use by reqwest library" ) ;
29
30
let mut response = REQWEST_CLIENT
30
- . get_or_init ( reqwest :: Client :: new )
31
+ . get_or_init ( init_client )
31
32
. execute ( request)
32
33
. await
33
34
. map_err ( Error :: http) ?;
@@ -42,3 +43,32 @@ impl gleam_core::io::HttpClient for HttpClient {
42
43
. map_err ( Error :: http)
43
44
}
44
45
}
46
+
47
+ fn init_client ( ) -> reqwest:: Client {
48
+ if let Some ( cert) = get_certificate ( ) {
49
+ return reqwest:: Client :: builder ( )
50
+ . add_root_certificate ( cert)
51
+ . build ( )
52
+ . expect ( "Unable to initialize a reqwest HTTP client" ) ;
53
+ } else {
54
+ return reqwest:: Client :: new ( ) ;
55
+ }
56
+ }
57
+
58
+ fn get_certificate ( ) -> Option < reqwest:: Certificate > {
59
+ match std:: env:: var ( CERTS_ENV_VAR ) {
60
+ Ok ( certs_path) => {
61
+ let data = std:: fs:: read ( certs_path) . expect ( & format ! (
62
+ "Unable to read certs file set as `{}`" ,
63
+ CERTS_ENV_VAR
64
+ ) ) ;
65
+ let cert = reqwest:: Certificate :: from_pem ( & data) . expect ( & format ! (
66
+ "Unable to construct a certificate from certs file set as `{}`" ,
67
+ CERTS_ENV_VAR
68
+ ) ) ;
69
+
70
+ Some ( cert)
71
+ }
72
+ _ => None ,
73
+ }
74
+ }
0 commit comments