/// Connection settings, from a `postgres://` URL (issue gabla108/louise#145). /// /// The password is never in an error message or `Debug` output. import std/bytes import std/http.{url_decode} import std/list import std/result import std/string /// How TLS is wanted, as PostgreSQL's `sslmode` (`prefer` is the default, /// like libpq). @derive Eq Debug pub type TlsMode { /// No TLS. Disable /// TLS if the server offers it, plain otherwise (the default). , Prefer /// TLS, or no connection. , Require } /// A connection's settings. pub type Config { Config( host: String , port: Int , user: String , database: String , password: Option , tls: TlsMode , application: String ) } /// Defaults: `localhost:5432`, user and database "postgres", no /// password, `Prefer`, application "louise". pub fn defaults :: -> Config { Config("localhost", 5432, "postgres", "postgres", None, Prefer, "louise") } /// `postgres://user:password@host:port/database?sslmode=require` (the /// `postgresql://` spelling works too; every part is percent-decoded). /// The error never quotes the URL: it may hold a password. pub fn parse :: url: String -> Result { use rest <- result.try match strip_scheme url { Some(rest) -> Ok(rest), None -> Error("not a postgres:// URL") } let (main, query) = split_at_first rest "?" let (auth, host_part) = split_auth main let (user, password) = split_at_first auth ":" let (authority, database) = split_at_first host_part "/" let (host, port_text) = split_at_first authority ":" use port <- result.try if port_text == "" { Ok(5432) } else { string.to_int (url_decode port_text) |> result.map_error fn _ { "the port is not a number" } } use tls <- result.try (tls_mode (query_param query "sslmode")) let user = if user == "" { "postgres" } else { url_decode user } let application = match query_param query "application_name" { Some(name) -> url_decode name, None -> "louise" } Ok( Config( if host == "" { "localhost" } else { url_decode host } , port , user , if database == "" { user } else { url_decode database } , if password == "" { None } else { Some(url_decode password) } , tls , application ) ) } /// The auth part and the host part; without a `@`, all of it is the host /// part. fn split_auth :: main: String -> (String, String) { match split_one main "@" { Some(pair) -> pair, None -> ("", main) } } /// The URL after its scheme (`postgres://` or `postgresql://`), if it has /// one. fn strip_scheme :: url: String -> Option { if string.starts_with url "postgresql://" { Some(drop_start url 13) } else if string.starts_with url "postgres://" { Some(drop_start url 11) } else { None } } /// `s` without its first `n` characters (the scheme is ASCII). fn drop_start :: s: String n: Int -> String { let b = bytes.from_string s match bytes.to_string (bytes.slice b n (bytes.length b)) { Ok(rest) -> rest, Error(_) -> s } } /// The part before the first `sep`, and after it ("" without one). fn split_at_first :: s: String sep: String -> (String, String) { match split_one s sep { Some(pair) -> pair, None -> (s, "") } } /// `Some((before, after))` splitting at the first `sep`, or `None`. fn split_one :: s: String sep: String -> Option<(String, String)> { match string.split s sep { [first, second, ..rest] -> Some((first, string.join [second, ..rest] sep)) , _ -> None } } /// The query's `name` parameter, if any. fn query_param :: query: String name: String -> Option { match query { "" -> None , _ -> { let pairs = list.map (string.split query "&") fn p { split_at_first p "=" } match list.filter pairs fn p { p.0 == name } { [(_, value), ..] -> Some(value), [] -> None } } } } /// The TLS mode of an `sslmode` value; `""` (none given) is `Prefer`. fn tls_mode :: mode: Option -> Result { match mode { None -> Ok(Prefer) , Some("disable") -> Ok(Disable) , Some("prefer") -> Ok(Prefer) , Some("require") -> Ok(Require) , Some ( other ) -> if other == "allow" || other == "verify-ca" || other == "verify-full" { Error( "sslmode {other} is not supported: the driver verifies certificates (use `require`)" ) } else { Error("sslmode must be `disable`, `prefer` or `require`") } } }