| @@ -11,7 +11,6 @@ import std/base64 |
| 11 | 11 | import std/bytes |
| 12 | 12 | import std/dict.{type Dict} |
| 13 | 13 | import std/ffi.{type Resource} |
| 14 | | −import std/int |
| 15 | 14 | import std/list |
| 16 | 15 | import std/net.{type TcpStream} |
| 17 | 16 | import std/option |
| @@ -108,17 +107,17 @@ pub type Socket { |
| 108 | 107 | |
| 109 | 108 | /// Connect and authenticate, without the actor: the socket, for the |
| 110 | 109 | /// connection actor (`connect`) or a listener (`listen`). |
| 111 | | −pub fn connect_socket(config: Config) -> Result<Socket, Error> / <io, random> { |
| 112 | | − use tcp <- result.try(net.connect(config.host, config.port) |> result.map_error(Io)) |
| 113 | | − use conn <- result.try(maybe_tls(tcp, config)) |
| 114 | | − use (buffer, backend) <- result.try(handshake(conn, config)) |
| 115 | | − Ok(Socket(conn, buffer, config.host, config.port, backend)) |
| 110 | +pub fn connect_socket(cfg: Config) -> Result<Socket, Error> / <io, random> { |
| 111 | + use tcp <- result.try(net.connect(cfg.host, cfg.port) |> result.map_error(Io)) |
| 112 | + use conn <- result.try(maybe_tls(tcp, cfg)) |
| 113 | + use (buffer, backend) <- result.try(handshake(conn, cfg)) |
| 114 | + Ok(Socket(conn, buffer, cfg.host, cfg.port, backend)) |
| 116 | 115 | } |
| 117 | 116 | |
| 118 | 117 | /// Connect and authenticate. The connection is an actor; stop it with |
| 119 | 118 | /// `close`. |
| 120 | | −pub fn connect(config: Config) -> Result<Subject<Msg>, Error> / <io, proc, random> { |
| 121 | | − use socket <- result.try(connect_socket(config)) |
| 119 | +pub fn connect(cfg: Config) -> Result<Subject<Msg>, Error> / <io, proc, random> { |
| 120 | + use socket <- result.try(connect_socket(cfg)) |
| 122 | 121 | Ok(connect_over(socket)) |
| 123 | 122 | } |
| 124 | 123 | |
| @@ -134,8 +133,8 @@ fn state_of(socket: Socket) -> State { |
| 134 | 133 | |
| 135 | 134 | /// TLS if the mode wants it: `SslRequest`, then the handshake if the |
| 136 | 135 | /// server agrees. `Require` refuses a plain answer. |
| 137 | | −fn maybe_tls(tcp: Resource<TcpStream>, config: Config) -> Result<Conn, Error> / <io> { |
| 138 | | − match config.tls { |
| 136 | +fn maybe_tls(tcp: Resource<TcpStream>, cfg: Config) -> Result<Conn, Error> / <io> { |
| 137 | + match cfg.tls { |
| 139 | 138 | Disable -> Ok(Tcp(tcp)), |
| 140 | 139 | mode -> { |
| 141 | 140 | use _ <- result.try(net.write(tcp, protocol.encode(protocol.SslRequest)) |> result.map_error(Io)) |
| @@ -145,7 +144,7 @@ fn maybe_tls(tcp: Resource<TcpStream>, config: Config) -> Result<Conn, Error> / |
| 145 | 144 | match protocol.ssl_answer(byte) { |
| 146 | 145 | Some(protocol.Upgrade) -> { |
| 147 | 146 | use tls_config <- result.try(tls.config(tls.defaults()) |> result.map_error(tls_io)) |
| 148 | | − use stream <- result.try(tls.connect(tcp, config.host, tls_config) |> result.map_error(tls_io)) |
| 147 | + use stream <- result.try(tls.connect(tcp, cfg.host, tls_config) |> result.map_error(tls_io)) |
| 149 | 148 | Ok(Tls(stream)) |
| 150 | 149 | }, |
| 151 | 150 | Some(protocol.Plain) -> |
| @@ -168,27 +167,27 @@ fn tls_io(e: tls.TlsError) -> Error { |
| 168 | 167 | /// The startup message, then authenticate until `ReadyForQuery`. The |
| 169 | 168 | /// buffer's leftover bytes come back (the server sent nothing extra in |
| 170 | 169 | /// practice), with the backend key data if it arrived. |
| 171 | | −fn handshake(conn: Conn, config: Config) -> Result<(Bytes, Option<(Int, Int)>), Error> / <io, random> { |
| 172 | | − use _ <- result.try(write_msg(conn, protocol.Startup(config.user, config.database, config.application))) |
| 173 | | − use (buffer, at, backend) <- result.try(auth_loop(conn, bytes.empty(), 0, config, None)) |
| 170 | +fn handshake(conn: Conn, cfg: Config) -> Result<(Bytes, Option<(Int, Int)>), Error> / <io, random> { |
| 171 | + use _ <- result.try(write_msg(conn, protocol.Startup(cfg.user, cfg.database, cfg.application))) |
| 172 | + use (buffer, at, backend) <- result.try(auth_loop(conn, bytes.empty(), 0, cfg, None)) |
| 174 | 173 | Ok((bytes.slice(buffer, at, bytes.length(buffer)), backend)) |
| 175 | 174 | } |
| 176 | 175 | |
| 177 | | −fn auth_loop(conn: Conn, buffer: Bytes, at: Int, config: Config, backend: Option<(Int, Int)>) -> Result<(Bytes, Int, Option<(Int, Int)>), Error> / <io, random> { |
| 176 | +fn auth_loop(conn: Conn, buffer: Bytes, at: Int, cfg: Config, backend: Option<(Int, Int)>) -> Result<(Bytes, Int, Option<(Int, Int)>), Error> / <io, random> { |
| 178 | 177 | use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 179 | 178 | match msg { |
| 180 | | − protocol.AuthenticationOk -> auth_loop(conn, buffer, at, config, backend), |
| 179 | + protocol.AuthenticationOk -> auth_loop(conn, buffer, at, cfg, backend), |
| 181 | 180 | protocol.AuthenticationCleartext -> { |
| 182 | | − use _ <- result.try(cleartext_password(conn, config)) |
| 183 | | − auth_loop(conn, buffer, at, config, backend) |
| 181 | + use _ <- result.try(cleartext_password(conn, cfg)) |
| 182 | + auth_loop(conn, buffer, at, cfg, backend) |
| 184 | 183 | }, |
| 185 | 184 | protocol.AuthenticationSasl(mechanisms) -> { |
| 186 | | − use (buffer, at) <- result.try(scram_exchange(conn, buffer, at, config, mechanisms)) |
| 187 | | − auth_loop(conn, buffer, at, config, backend) |
| 185 | + use (buffer, at) <- result.try(scram_exchange(conn, buffer, at, cfg, mechanisms)) |
| 186 | + auth_loop(conn, buffer, at, cfg, backend) |
| 188 | 187 | }, |
| 189 | | − protocol.ParameterStatus(..) -> auth_loop(conn, buffer, at, config, backend), |
| 190 | | − protocol.BackendKeyData(process, secret) -> auth_loop(conn, buffer, at, config, Some((process, secret))), |
| 191 | | − protocol.NoticeResponse(_) -> auth_loop(conn, buffer, at, config, backend), |
| 188 | + protocol.ParameterStatus(..) -> auth_loop(conn, buffer, at, cfg, backend), |
| 189 | + protocol.BackendKeyData(pid, secret) -> auth_loop(conn, buffer, at, cfg, Some((pid, secret))), |
| 190 | + protocol.NoticeResponse(_) -> auth_loop(conn, buffer, at, cfg, backend), |
| 192 | 191 | protocol.ReadyForQuery(_) -> Ok((buffer, at, backend)), |
| 193 | 192 | protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 194 | 193 | _ -> Error(Driver("an unexpected message during startup")), |
| @@ -196,18 +195,18 @@ fn auth_loop(conn: Conn, buffer: Bytes, at: Int, config: Config, backend: Option |
| 196 | 195 | } |
| 197 | 196 | |
| 198 | 197 | /// A cleartext password, over TLS or to localhost only. |
| 199 | | −fn cleartext_password(conn: Conn, config: Config) -> Result<(), Error> / <io> { |
| 198 | +fn cleartext_password(conn: Conn, cfg: Config) -> Result<(), Error> / <io> { |
| 200 | 199 | let safe = |
| 201 | 200 | match conn { |
| 202 | 201 | Tls(_) -> True, |
| 203 | 202 | Tcp(_) -> |
| 204 | | − config.host == "localhost" || config.host == "127.0.0.1" || config.host == "::1", |
| 203 | + cfg.host == "localhost" || cfg.host == "127.0.0.1" || cfg.host == "::1", |
| 205 | 204 | } |
| 206 | 205 | if !safe { |
| 207 | 206 | Error(Driver("the server wants a cleartext password, which needs TLS or localhost")) |
| 208 | 207 | } else { |
| 209 | 208 | use password <- result.try( |
| 210 | | − option.to_result(config.password, "the server wants a password; the URL has none") |
| 209 | + option.to_result(cfg.password, "the server wants a password; the URL has none") |
| 211 | 210 | |> result.map_error(Driver), |
| 212 | 211 | ) |
| 213 | 212 | write_msg(conn, protocol.Password(password)) |
| @@ -219,14 +218,14 @@ fn scram_exchange( |
| 219 | 218 | conn: Conn, |
| 220 | 219 | buffer: Bytes, |
| 221 | 220 | at: Int, |
| 222 | | − config: Config, |
| 221 | + cfg: Config, |
| 223 | 222 | mechanisms: List<String>, |
| 224 | 223 | ) -> Result<(Bytes, Int), Error> / <io, random> { |
| 225 | 224 | if !list.any(mechanisms, fn(m) { m == "SCRAM-SHA-256" }) { |
| 226 | 225 | Error(Driver("the server offers no SCRAM-SHA-256")) |
| 227 | 226 | } else { |
| 228 | 227 | use password <- result.try( |
| 229 | | − option.to_result(config.password, "the server wants a password; the URL has none") |
| 228 | + option.to_result(cfg.password, "the server wants a password; the URL has none") |
| 230 | 229 | |> result.map_error(Driver), |
| 231 | 230 | ) |
| 232 | 231 | let nonce = base64.encode(random.bytes(18)) |
| @@ -312,14 +311,14 @@ pub type Msg { |
| 312 | 311 | fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 313 | 312 | match msg { |
| 314 | 313 | DoQuery(sql, params, reply) -> { |
| 315 | | − let (result, state) = run_query(state, sql, params) |
| 316 | | − process.send(reply, result) |
| 314 | + let (res, state) = run_query(state, sql, params) |
| 315 | + process.send(reply, res) |
| 317 | 316 | Continue(state) |
| 318 | 317 | }, |
| 319 | 318 | Ping(reply) -> { |
| 320 | | − let (result, buffer, at) = ping(state.conn, state.buffer, state.at) |
| 321 | | − process.send(reply, result) |
| 322 | | − match result { |
| 319 | + let (res, buffer, at) = ping(state.conn, state.buffer, state.at) |
| 320 | + process.send(reply, res) |
| 321 | + match res { |
| 323 | 322 | Ok(()) -> Continue(State(..state, buffer: buffer, at: at)), |
| 324 | 323 | Error(_) -> { |
| 325 | 324 | close_conn(state.conn) |
| @@ -388,8 +387,8 @@ fn run_query(state: State, sql: String, params: List<Value>) -> (Result<QueryRes |
| 388 | 387 | match conn_write(state.conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 389 | 388 | Error(e) -> (Error(e), state), |
| 390 | 389 | Ok(()) -> { |
| 391 | | − let (result, buffer, at) = collect(state.conn, state.buffer, state.at, [], [], "", None) |
| 392 | | − (result, State(..state, buffer: buffer, at: at)) |
| 390 | + let (res, buffer, at) = collect(state.conn, state.buffer, state.at, [], [], "", None) |
| 391 | + (res, State(..state, buffer: buffer, at: at)) |
| 393 | 392 | }, |
| 394 | 393 | } |
| 395 | 394 | }, |
| @@ -586,8 +585,8 @@ pub type Notification { |
| 586 | 585 | /// parked on the socket. Stop it with `process.kill` (a supervisor |
| 587 | 586 | /// restarts it: it re-`LISTEN`s). Quoting: the channel is an identifier, |
| 588 | 587 | /// sent as `LISTEN "channel"`. |
| 589 | | −pub fn listen(config: Config, channel: String, subscriber: Subject<Notification>) -> Result<Pid, Error> / <io, proc, random> { |
| 590 | | − use socket <- result.try(connect_socket(config)) |
| 588 | +pub fn listen(cfg: Config, channel: String, subscriber: Subject<Notification>) -> Result<Pid, Error> / <io, proc, random> { |
| 589 | + use socket <- result.try(connect_socket(cfg)) |
| 591 | 590 | let Socket(conn, buffer, ..) = socket |
| 592 | 591 | use (buffer, at) <- result.try(listen_command(conn, buffer, 0, channel)) |
| 593 | 592 | Ok(process.spawn(fn() { listen_serve(conn, buffer, at, channel, subscriber) })) |