tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

166 lignes · 4499 octetsBrut · Historique
1/// Connection settings, from a `postgres://` URL (issue gabla108/louise#145).
2///
3/// The password is never in an error message or `Debug` output.
4
5import std/bytes
6import std/http.{url_decode}
7import std/list
8import std/result
9import std/string
10
11/// How TLS is wanted, as PostgreSQL's `sslmode` (`prefer` is the default,
12/// like libpq).
13@derive Eq Debug
14pub type TlsMode
15{ /// No TLS.
16 Disable
17 /// TLS if the server offers it, plain otherwise (the default).
18, Prefer
19 /// TLS, or no connection.
20, Require
21}
22
23/// A connection's settings.
24pub type Config
25{ Config(
26 host: String
27 , port: Int
28 , user: String
29 , database: String
30 , password: Option<String>
31 , tls: TlsMode
32 , application: String
33 )
34}
35
36/// Defaults: `localhost:5432`, user and database "postgres", no
37/// password, `Prefer`, application "louise".
38pub fn defaults :: -> Config
39{
40 Config("localhost", 5432, "postgres", "postgres", None, Prefer, "louise")
41}
42
43/// `postgres://user:password@host:port/database?sslmode=require` (the
44/// `postgresql://` spelling works too; every part is percent-decoded).
45/// The error never quotes the URL: it may hold a password.
46pub fn parse :: url: String -> Result<Config, String>
47{
48 use rest <-
49 result.try
50 match strip_scheme url
51 { Some(rest) -> Ok(rest), None -> Error("not a postgres:// URL") }
52 let (main, query) = split_at_first rest "?"
53 let (auth, host_part) = split_auth main
54 let (user, password) = split_at_first auth ":"
55 let (authority, database) = split_at_first host_part "/"
56 let (host, port_text) = split_at_first authority ":"
57 use port <-
58 result.try
59 if port_text == ""
60 {
61 Ok(5432)
62 }
63 else
64 {
65 string.to_int (url_decode port_text)
66 |> result.map_error fn _ { "the port is not a number" }
67 }
68 use tls <- result.try (tls_mode (query_param query "sslmode"))
69 let user = if user == "" { "postgres" } else { url_decode user }
70 let application = match query_param query "application_name"
71 { Some(name) -> url_decode name, None -> "louise" }
72 Ok( Config( if host == "" { "localhost" } else { url_decode host }
73 , port
74 , user
75 , if database == "" { user } else { url_decode database }
76 , if password == "" { None } else { Some(url_decode password) }
77 , tls
78 , application
79 )
80 )
81}
82
83/// The auth part and the host part; without a `@`, all of it is the host
84/// part.
85fn split_auth :: main: String -> (String, String)
86{
87 match split_one main "@" { Some(pair) -> pair, None -> ("", main) }
88}
89
90/// The URL after its scheme (`postgres://` or `postgresql://`), if it has
91/// one.
92fn strip_scheme :: url: String -> Option<String>
93{
94 if string.starts_with url "postgresql://"
95 {
96 Some(drop_start url 13)
97 }
98 else if string.starts_with url "postgres://"
99 {
100 Some(drop_start url 11)
101 }
102 else
103 {
104 None
105 }
106}
107
108/// `s` without its first `n` characters (the scheme is ASCII).
109fn drop_start :: s: String n: Int -> String
110{
111 let b = bytes.from_string s
112 match bytes.to_string (bytes.slice b n (bytes.length b))
113 { Ok(rest) -> rest, Error(_) -> s }
114}
115
116/// The part before the first `sep`, and after it ("" without one).
117fn split_at_first :: s: String sep: String -> (String, String)
118{
119 match split_one s sep { Some(pair) -> pair, None -> (s, "") }
120}
121
122/// `Some((before, after))` splitting at the first `sep`, or `None`.
123fn split_one :: s: String sep: String -> Option<(String, String)>
124{
125 match string.split s sep
126 { [first, second, ..rest] -> Some((first, string.join [second, ..rest] sep))
127 , _ -> None
128 }
129}
130
131/// The query's `name` parameter, if any.
132fn query_param :: query: String name: String -> Option<String>
133{
134 match query
135 { "" -> None
136 , _ ->
137 {
138 let pairs =
139 list.map (string.split query "&") fn p { split_at_first p "=" }
140 match list.filter pairs fn p { p.0 == name }
141 { [(_, value), ..] -> Some(value), [] -> None }
142 }
143 }
144}
145
146/// The TLS mode of an `sslmode` value; `""` (none given) is `Prefer`.
147fn tls_mode :: mode: Option<String> -> Result<TlsMode, String>
148{
149 match mode
150 { None -> Ok(Prefer)
151 , Some("disable") -> Ok(Disable)
152 , Some("prefer") -> Ok(Prefer)
153 , Some("require") -> Ok(Require)
154 , Some
155 ( other
156 ) -> if other == "allow" || other == "verify-ca" || other == "verify-full"
157 {
158 Error( "sslmode {other} is not supported: the driver verifies certificates (use `require`)"
159 )
160 }
161 else
162 {
163 Error("sslmode must be `disable`, `prefer` or `require`")
164 }
165 }
166}