Skip to content

Commit cff6927

Browse files
allow arbitrary config params with more efficient repr (#30)
* switch to compressed config * remove some allocs and combine pw and authkeys into a single enum * allow arbitrary options * remove some redundant configs * clean up * more cleanup * replication * fix * fix lints * simplify * dont treat user separately * do not duplicate the encoding of params
1 parent 20031d7 commit cff6927

File tree

10 files changed

+159
-156
lines changed

10 files changed

+159
-156
lines changed

postgres-protocol/src/authentication/sasl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ enum Credentials<const N: usize> {
117117
/// A regular password as a vector of bytes.
118118
Password(Vec<u8>),
119119
/// A precomputed pair of keys.
120-
Keys(Box<ScramKeys<N>>),
120+
Keys(ScramKeys<N>),
121121
}
122122

123123
enum State {
@@ -176,7 +176,7 @@ impl ScramSha256 {
176176

177177
/// Constructs a new instance which will use the provided key pair for authentication.
178178
pub fn new_with_keys(keys: ScramKeys<32>, channel_binding: ChannelBinding) -> ScramSha256 {
179-
let password = Credentials::Keys(keys.into());
179+
let password = Credentials::Keys(keys);
180180
ScramSha256::new_inner(password, channel_binding, nonce())
181181
}
182182

postgres-protocol/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ macro_rules! from_usize {
6868
impl FromUsize for $t {
6969
#[inline]
7070
fn from_usize(x: usize) -> io::Result<$t> {
71-
if x > <$t>::max_value() as usize {
71+
if x > <$t>::MAX as usize {
7272
Err(io::Error::new(
7373
io::ErrorKind::InvalidInput,
7474
"value too large to transmit",

postgres-protocol/src/message/frontend.rs

+60
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,66 @@ where
271271
})
272272
}
273273

274+
#[inline]
275+
pub fn startup_message_cstr(
276+
parameters: &StartupMessageParams,
277+
buf: &mut BytesMut,
278+
) -> io::Result<()> {
279+
write_body(buf, |buf| {
280+
// postgres protocol version 3.0(196608) in bigger-endian
281+
buf.put_i32(0x00_03_00_00);
282+
buf.put_slice(&parameters.params);
283+
buf.put_u8(0);
284+
Ok(())
285+
})
286+
}
287+
288+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
289+
pub struct StartupMessageParams {
290+
params: BytesMut,
291+
}
292+
293+
impl StartupMessageParams {
294+
/// Set parameter's value by its name.
295+
pub fn insert(&mut self, name: &str, value: &str) -> Result<(), io::Error> {
296+
if name.contains('\0') | value.contains('\0') {
297+
return Err(io::Error::new(
298+
io::ErrorKind::InvalidInput,
299+
"string contains embedded null",
300+
));
301+
}
302+
self.params.put(name.as_bytes());
303+
self.params.put(&b"\0"[..]);
304+
self.params.put(value.as_bytes());
305+
self.params.put(&b"\0"[..]);
306+
Ok(())
307+
}
308+
309+
pub fn str_iter(&self) -> impl Iterator<Item = (&str, &str)> {
310+
let params =
311+
std::str::from_utf8(&self.params).expect("should be validated as utf8 already");
312+
StrParamsIter(params)
313+
}
314+
315+
/// Get parameter's value by its name.
316+
pub fn get(&self, name: &str) -> Option<&str> {
317+
self.str_iter().find_map(|(k, v)| (k == name).then_some(v))
318+
}
319+
}
320+
321+
struct StrParamsIter<'a>(&'a str);
322+
323+
impl<'a> Iterator for StrParamsIter<'a> {
324+
type Item = (&'a str, &'a str);
325+
326+
fn next(&mut self) -> Option<Self::Item> {
327+
let (key, r) = self.0.split_once('\0')?;
328+
let (value, r) = r.split_once('\0')?;
329+
self.0 = r;
330+
Some((key, value))
331+
}
332+
}
333+
274334
#[inline]
275335
pub fn sync(buf: &mut BytesMut) {
276336
buf.put_u8(b'S');

postgres-types/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ impl ToSql for IpAddr {
11721172
}
11731173

11741174
fn downcast(len: usize) -> Result<i32, Box<dyn Error + Sync + Send>> {
1175-
if len > i32::max_value() as usize {
1175+
if len > i32::MAX as usize {
11761176
Err("value too large to transmit".into())
11771177
} else {
11781178
Ok(len as i32)

postgres-types/src/special.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use bytes::BytesMut;
22
use postgres_protocol::types;
33
use std::error::Error;
4-
use std::{i32, i64};
54

65
use crate::{FromSql, IsNull, ToSql, Type};
76

postgres/src/config.rs

-24
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ impl Config {
145145
self
146146
}
147147

148-
/// Gets the password to authenticate with, if one has been configured with
149-
/// the `password` method.
150-
pub fn get_password(&self) -> Option<&[u8]> {
151-
self.config.get_password()
152-
}
153-
154148
/// Sets precomputed protocol-specific keys to authenticate with.
155149
/// When set, this option will override `password`.
156150
/// See [`AuthKeys`] for more information.
@@ -159,12 +153,6 @@ impl Config {
159153
self
160154
}
161155

162-
/// Gets precomputed protocol-specific keys to authenticate with.
163-
/// if one has been configured with the `auth_keys` method.
164-
pub fn get_auth_keys(&self) -> Option<AuthKeys> {
165-
self.config.get_auth_keys()
166-
}
167-
168156
/// Sets the name of the database to connect to.
169157
///
170158
/// Defaults to the user.
@@ -185,24 +173,12 @@ impl Config {
185173
self
186174
}
187175

188-
/// Gets the command line options used to configure the server, if the
189-
/// options have been set with the `options` method.
190-
pub fn get_options(&self) -> Option<&str> {
191-
self.config.get_options()
192-
}
193-
194176
/// Sets the value of the `application_name` runtime parameter.
195177
pub fn application_name(&mut self, application_name: &str) -> &mut Config {
196178
self.config.application_name(application_name);
197179
self
198180
}
199181

200-
/// Gets the value of the `application_name` runtime parameter, if it has
201-
/// been set with the `application_name` method.
202-
pub fn get_application_name(&self) -> Option<&str> {
203-
self.config.get_application_name()
204-
}
205-
206182
/// Sets the SSL configuration.
207183
///
208184
/// Defaults to `prefer`.

0 commit comments

Comments
 (0)