/
germanubis
/
sqlx
ΠΠ±Π·ΠΎΡ
ΠΠΎΠΊΡΠΌΠ΅Π½ΡΠ°ΡΠΈΡ
ΠΠΎΠΉΡΠΈ
/
germanubis
/
sqlx
ΠΠΎΠ΄
ΠΠ°ΠΏΡΠΎΡΡ
0
ΠΠ°Π΄Π°ΡΠΈ
ΠΠΈΠΊΠΈ
ΠΠ°ΠΊΠ΅ΡΡ
0
Π Π΅Π»ΠΈΠ·Ρ
0
ΠΠ½Π°Π»ΠΈΡΠΈΠΊΠ°
ΠΠ΅Π·ΠΎΠΏΠ°ΡΠ½ΠΎΡΡΡ
v0.8.2
sqlx-postgres/src/message/sasl.rs
69 ΡΡΡΠΎΠΊ
2 KB
Austin Bonander
refactor(postgres): make better use of traits to improve protocol handling
24 Π°Π²Π³ 2024, 09:39
24 Π°Π²Π³ 2024, 09:39
53766e4
ΠΠΎΠ΄
ΠΠ²ΡΠΎΡΡΡΠ²ΠΎ
Π ΡΡΠΌ ΠΊΠΎΠ΄?
use crate::io::BufMutExt; use crate::message::{FrontendMessage, FrontendMessageFormat}; use sqlx_core::Error; use std::num::Saturating; pub struct SaslInitialResponse<'a> { pub response: &'a str, pub plus: bool, } impl SaslInitialResponse<'_> { #[inline(always)] fn selected_mechanism(&self) -> &'static str { if self.plus { "SCRAM-SHA-256-PLUS" } else { "SCRAM-SHA-256" } } } impl FrontendMessage for SaslInitialResponse<'_> { const FORMAT: FrontendMessageFormat = FrontendMessageFormat::PasswordPolymorphic; #[inline(always)] fn body_size_hint(&self) -> Saturating<usize> { let mut size = Saturating(0); size += self.selected_mechanism().len(); size += 1; // NUL terminator size += 4; // response_len size += self.response.len(); size } fn encode_body(&self, buf: &mut Vec<u8>) -> Result<(), Error> { // name of the SASL authentication mechanism that the client selected buf.put_str_nul(self.selected_mechanism()); let response_len = i32::try_from(self.response.len()).map_err(|_| { err_protocol!( "SASL Initial Response length too long for protcol: {}", self.response.len() ) })?; buf.extend_from_slice(&response_len.to_be_bytes()); buf.extend_from_slice(self.response.as_bytes()); Ok(()) } } pub struct SaslResponse<'a>(pub &'a str); impl FrontendMessage for SaslResponse<'_> { const FORMAT: FrontendMessageFormat = FrontendMessageFormat::PasswordPolymorphic; fn body_size_hint(&self) -> Saturating<usize> { Saturating(self.0.len()) } fn encode_body(&self, buf: &mut Vec<u8>) -> Result<(), Error> { buf.extend(self.0.as_bytes()); Ok(()) } }