1extern crate percent_encoding;
2extern crate url;
3
4use self::percent_encoding::percent_decode;
5use self::url::{Host, Url};
6use alloc::ffi::CString;
7use core::ffi::CStr;
8use std::collections::HashMap;
9
10use crate::result::{ConnectionError, ConnectionResult};
11
12use mysqlclient_sys::mysql_ssl_mode;
13
14bitflags::bitflags! {
15 #[derive(#[automatically_derived]
impl ::core::clone::Clone for CapabilityFlags {
#[inline]
fn clone(&self) -> CapabilityFlags {
let _:
::core::clone::AssertParamIsClone<<CapabilityFlags as
::bitflags::__private::PublicFlags>::Internal>;
*self
}
}
impl CapabilityFlags {
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_LONG_PASSWORD: Self = Self::from_bits_retain(0x00000001);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_FOUND_ROWS: Self = Self::from_bits_retain(0x00000002);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_LONG_FLAG: Self = Self::from_bits_retain(0x00000004);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_CONNECT_WITH_DB: Self =
Self::from_bits_retain(0x00000008);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_NO_SCHEMA: Self = Self::from_bits_retain(0x00000010);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_COMPRESS: Self = Self::from_bits_retain(0x00000020);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_ODBC: Self = Self::from_bits_retain(0x00000040);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_LOCAL_FILES: Self = Self::from_bits_retain(0x00000080);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_IGNORE_SPACE: Self = Self::from_bits_retain(0x00000100);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_PROTOCOL_41: Self = Self::from_bits_retain(0x00000200);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_INTERACTIVE: Self = Self::from_bits_retain(0x00000400);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_SSL: Self = Self::from_bits_retain(0x00000800);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_IGNORE_SIGPIPE: Self =
Self::from_bits_retain(0x00001000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_TRANSACTIONS: Self = Self::from_bits_retain(0x00002000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_RESERVED: Self = Self::from_bits_retain(0x00004000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_SECURE_CONNECTION: Self =
Self::from_bits_retain(0x00008000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_MULTI_STATEMENTS: Self =
Self::from_bits_retain(0x00010000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_MULTI_RESULTS: Self = Self::from_bits_retain(0x00020000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_PS_MULTI_RESULTS: Self =
Self::from_bits_retain(0x00040000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_PLUGIN_AUTH: Self = Self::from_bits_retain(0x00080000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_CONNECT_ATTRS: Self = Self::from_bits_retain(0x00100000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA: Self =
Self::from_bits_retain(0x00200000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS: Self =
Self::from_bits_retain(0x00400000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_SESSION_TRACK: Self = Self::from_bits_retain(0x00800000);
#[allow(deprecated, non_upper_case_globals,)]
pub const CLIENT_DEPRECATE_EOF: Self = Self::from_bits_retain(0x01000000);
}
impl ::bitflags::Flags for CapabilityFlags {
const FLAGS: &'static [::bitflags::Flag<CapabilityFlags>] =
&[{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_LONG_PASSWORD",
CapabilityFlags::CLIENT_LONG_PASSWORD)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_FOUND_ROWS",
CapabilityFlags::CLIENT_FOUND_ROWS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_LONG_FLAG",
CapabilityFlags::CLIENT_LONG_FLAG)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_CONNECT_WITH_DB",
CapabilityFlags::CLIENT_CONNECT_WITH_DB)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_NO_SCHEMA",
CapabilityFlags::CLIENT_NO_SCHEMA)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_COMPRESS",
CapabilityFlags::CLIENT_COMPRESS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_ODBC",
CapabilityFlags::CLIENT_ODBC)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_LOCAL_FILES",
CapabilityFlags::CLIENT_LOCAL_FILES)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_IGNORE_SPACE",
CapabilityFlags::CLIENT_IGNORE_SPACE)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_PROTOCOL_41",
CapabilityFlags::CLIENT_PROTOCOL_41)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_INTERACTIVE",
CapabilityFlags::CLIENT_INTERACTIVE)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_SSL",
CapabilityFlags::CLIENT_SSL)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_IGNORE_SIGPIPE",
CapabilityFlags::CLIENT_IGNORE_SIGPIPE)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_TRANSACTIONS",
CapabilityFlags::CLIENT_TRANSACTIONS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_RESERVED",
CapabilityFlags::CLIENT_RESERVED)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_SECURE_CONNECTION",
CapabilityFlags::CLIENT_SECURE_CONNECTION)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_MULTI_STATEMENTS",
CapabilityFlags::CLIENT_MULTI_STATEMENTS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_MULTI_RESULTS",
CapabilityFlags::CLIENT_MULTI_RESULTS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_PS_MULTI_RESULTS",
CapabilityFlags::CLIENT_PS_MULTI_RESULTS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_PLUGIN_AUTH",
CapabilityFlags::CLIENT_PLUGIN_AUTH)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_CONNECT_ATTRS",
CapabilityFlags::CLIENT_CONNECT_ATTRS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA",
CapabilityFlags::CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS",
CapabilityFlags::CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_SESSION_TRACK",
CapabilityFlags::CLIENT_SESSION_TRACK)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CLIENT_DEPRECATE_EOF",
CapabilityFlags::CLIENT_DEPRECATE_EOF)
}];
type Bits = u32;
fn bits(&self) -> u32 { CapabilityFlags::bits(self) }
fn from_bits_retain(bits: u32) -> CapabilityFlags {
CapabilityFlags::from_bits_retain(bits)
}
}
#[allow(dead_code, deprecated, unused_doc_comments, unused_attributes,
unused_mut, unused_imports, non_upper_case_globals, clippy ::
assign_op_pattern, clippy :: indexing_slicing, clippy :: same_name_method,
clippy :: iter_without_into_iter,)]
const _: () =
{
#[repr(transparent)]
pub struct InternalBitFlags(u32);
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for InternalBitFlags { }
#[automatically_derived]
impl ::core::clone::Clone for InternalBitFlags {
#[inline]
fn clone(&self) -> InternalBitFlags {
let _: ::core::clone::AssertParamIsClone<u32>;
*self
}
}
#[automatically_derived]
impl ::core::marker::Copy for InternalBitFlags { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for InternalBitFlags { }
#[automatically_derived]
impl ::core::cmp::PartialEq for InternalBitFlags {
#[inline]
fn eq(&self, other: &InternalBitFlags) -> bool {
self.0 == other.0
}
}
#[automatically_derived]
impl ::core::cmp::Eq for InternalBitFlags {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<u32>;
}
}
#[automatically_derived]
impl ::core::cmp::PartialOrd for InternalBitFlags {
#[inline]
fn partial_cmp(&self, other: &InternalBitFlags)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}
#[automatically_derived]
impl ::core::cmp::Ord for InternalBitFlags {
#[inline]
fn cmp(&self, other: &InternalBitFlags) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}
#[automatically_derived]
impl ::core::hash::Hash for InternalBitFlags {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}
impl ::bitflags::__private::PublicFlags for CapabilityFlags {
type Primitive = u32;
type Internal = InternalBitFlags;
}
impl ::bitflags::__private::core::default::Default for
InternalBitFlags {
#[inline]
fn default() -> Self { InternalBitFlags::empty() }
}
impl ::bitflags::__private::core::fmt::Debug for InternalBitFlags {
fn fmt(&self,
f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
-> ::bitflags::__private::core::fmt::Result {
if self.is_empty() {
f.write_fmt(format_args!("{0:#x}",
<u32 as ::bitflags::Bits>::EMPTY))
} else {
::bitflags::__private::core::fmt::Display::fmt(self, f)
}
}
}
impl ::bitflags::__private::core::fmt::Display for InternalBitFlags {
fn fmt(&self,
f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
-> ::bitflags::__private::core::fmt::Result {
::bitflags::parser::to_writer(&CapabilityFlags(*self), f)
}
}
impl ::bitflags::__private::core::str::FromStr for InternalBitFlags {
type Err = ::bitflags::parser::ParseError;
fn from_str(s: &str)
->
::bitflags::__private::core::result::Result<Self,
Self::Err> {
::bitflags::parser::from_str::<CapabilityFlags>(s).map(|flags|
flags.0)
}
}
impl ::bitflags::__private::core::convert::AsRef<u32> for
InternalBitFlags {
fn as_ref(&self) -> &u32 { &self.0 }
}
impl ::bitflags::__private::core::convert::From<u32> for
InternalBitFlags {
fn from(bits: u32) -> Self { Self::from_bits_retain(bits) }
}
#[allow(dead_code, deprecated, unused_attributes)]
impl InternalBitFlags {
#[inline]
pub const fn empty() -> Self {
Self(<u32 as ::bitflags::Bits>::EMPTY)
}
#[inline]
pub const fn all() -> Self {
let mut truncated = <u32 as ::bitflags::Bits>::EMPTY;
let mut i = 0;
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<CapabilityFlags as
::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
let _ = i;
Self(truncated)
}
#[inline]
pub const fn bits(&self) -> u32 { self.0 }
#[inline]
pub const fn from_bits(bits: u32)
-> ::bitflags::__private::core::option::Option<Self> {
let truncated = Self::from_bits_truncate(bits).0;
if truncated == bits {
::bitflags::__private::core::option::Option::Some(Self(bits))
} else { ::bitflags::__private::core::option::Option::None }
}
#[inline]
pub const fn from_bits_truncate(bits: u32) -> Self {
Self(bits & Self::all().0)
}
#[inline]
pub const fn from_bits_retain(bits: u32) -> Self { Self(bits) }
#[inline]
pub fn from_name(name: &str)
-> ::bitflags::__private::core::option::Option<Self> {
{
if name == "CLIENT_LONG_PASSWORD" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_LONG_PASSWORD.bits()));
}
};
;
{
if name == "CLIENT_FOUND_ROWS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_FOUND_ROWS.bits()));
}
};
;
{
if name == "CLIENT_LONG_FLAG" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_LONG_FLAG.bits()));
}
};
;
{
if name == "CLIENT_CONNECT_WITH_DB" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_CONNECT_WITH_DB.bits()));
}
};
;
{
if name == "CLIENT_NO_SCHEMA" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_NO_SCHEMA.bits()));
}
};
;
{
if name == "CLIENT_COMPRESS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_COMPRESS.bits()));
}
};
;
{
if name == "CLIENT_ODBC" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_ODBC.bits()));
}
};
;
{
if name == "CLIENT_LOCAL_FILES" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_LOCAL_FILES.bits()));
}
};
;
{
if name == "CLIENT_IGNORE_SPACE" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_IGNORE_SPACE.bits()));
}
};
;
{
if name == "CLIENT_PROTOCOL_41" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_PROTOCOL_41.bits()));
}
};
;
{
if name == "CLIENT_INTERACTIVE" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_INTERACTIVE.bits()));
}
};
;
{
if name == "CLIENT_SSL" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_SSL.bits()));
}
};
;
{
if name == "CLIENT_IGNORE_SIGPIPE" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_IGNORE_SIGPIPE.bits()));
}
};
;
{
if name == "CLIENT_TRANSACTIONS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_TRANSACTIONS.bits()));
}
};
;
{
if name == "CLIENT_RESERVED" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_RESERVED.bits()));
}
};
;
{
if name == "CLIENT_SECURE_CONNECTION" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_SECURE_CONNECTION.bits()));
}
};
;
{
if name == "CLIENT_MULTI_STATEMENTS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_MULTI_STATEMENTS.bits()));
}
};
;
{
if name == "CLIENT_MULTI_RESULTS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_MULTI_RESULTS.bits()));
}
};
;
{
if name == "CLIENT_PS_MULTI_RESULTS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_PS_MULTI_RESULTS.bits()));
}
};
;
{
if name == "CLIENT_PLUGIN_AUTH" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_PLUGIN_AUTH.bits()));
}
};
;
{
if name == "CLIENT_CONNECT_ATTRS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_CONNECT_ATTRS.bits()));
}
};
;
{
if name == "CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.bits()));
}
};
;
{
if name == "CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS.bits()));
}
};
;
{
if name == "CLIENT_SESSION_TRACK" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_SESSION_TRACK.bits()));
}
};
;
{
if name == "CLIENT_DEPRECATE_EOF" {
return ::bitflags::__private::core::option::Option::Some(Self(CapabilityFlags::CLIENT_DEPRECATE_EOF.bits()));
}
};
;
let _ = name;
::bitflags::__private::core::option::Option::None
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.0 == <u32 as ::bitflags::Bits>::EMPTY
}
#[inline]
pub const fn is_all(&self) -> bool {
Self::all().0 | self.0 == self.0
}
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.0 & other.0 != <u32 as ::bitflags::Bits>::EMPTY
}
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[inline]
pub fn insert(&mut self, other: Self) {
*self = Self(self.0).union(other);
}
#[inline]
pub fn remove(&mut self, other: Self) {
*self = Self(self.0).difference(other);
}
#[inline]
pub fn toggle(&mut self, other: Self) {
*self = Self(self.0).symmetric_difference(other);
}
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
if value { self.insert(other); } else { self.remove(other); }
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0 & other.0)
}
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self(self.0 ^ other.0)
}
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self::from_bits_truncate(!self.0)
}
}
impl ::bitflags::__private::core::fmt::Binary for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::Octal for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::LowerHex for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::UpperHex for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::ops::BitOr for InternalBitFlags {
type Output = Self;
#[inline]
fn bitor(self, other: InternalBitFlags) -> Self {
self.union(other)
}
}
impl ::bitflags::__private::core::ops::BitOrAssign for
InternalBitFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) { self.insert(other); }
}
impl ::bitflags::__private::core::ops::BitXor for InternalBitFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
self.symmetric_difference(other)
}
}
impl ::bitflags::__private::core::ops::BitXorAssign for
InternalBitFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
}
impl ::bitflags::__private::core::ops::BitAnd for InternalBitFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self { self.intersection(other) }
}
impl ::bitflags::__private::core::ops::BitAndAssign for
InternalBitFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
*self =
Self::from_bits_retain(self.bits()).intersection(other);
}
}
impl ::bitflags::__private::core::ops::Sub for InternalBitFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self { self.difference(other) }
}
impl ::bitflags::__private::core::ops::SubAssign for InternalBitFlags
{
#[inline]
fn sub_assign(&mut self, other: Self) { self.remove(other); }
}
impl ::bitflags::__private::core::ops::Not for InternalBitFlags {
type Output = Self;
#[inline]
fn not(self) -> Self { self.complement() }
}
impl ::bitflags::__private::core::iter::Extend<InternalBitFlags> for
InternalBitFlags {
fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(&mut self, iterator: T) {
for item in iterator { self.insert(item) }
}
}
impl ::bitflags::__private::core::iter::FromIterator<InternalBitFlags>
for InternalBitFlags {
fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(iterator: T) -> Self {
use ::bitflags::__private::core::iter::Extend;
let mut result = Self::empty();
result.extend(iterator);
result
}
}
impl InternalBitFlags {
#[inline]
pub const fn iter(&self)
-> ::bitflags::iter::Iter<CapabilityFlags> {
::bitflags::iter::Iter::__private_const_new(<CapabilityFlags
as ::bitflags::Flags>::FLAGS,
CapabilityFlags::from_bits_retain(self.bits()),
CapabilityFlags::from_bits_retain(self.bits()))
}
#[inline]
pub const fn iter_names(&self)
-> ::bitflags::iter::IterNames<CapabilityFlags> {
::bitflags::iter::IterNames::__private_const_new(<CapabilityFlags
as ::bitflags::Flags>::FLAGS,
CapabilityFlags::from_bits_retain(self.bits()),
CapabilityFlags::from_bits_retain(self.bits()))
}
}
impl ::bitflags::__private::core::iter::IntoIterator for
InternalBitFlags {
type Item = CapabilityFlags;
type IntoIter = ::bitflags::iter::Iter<CapabilityFlags>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
impl InternalBitFlags {
#[inline]
pub fn bits_mut(&mut self) -> &mut u32 { &mut self.0 }
}
#[allow(dead_code, deprecated, unused_attributes)]
impl CapabilityFlags {
#[inline]
pub const fn empty() -> Self { Self(InternalBitFlags::empty()) }
#[inline]
pub const fn all() -> Self { Self(InternalBitFlags::all()) }
#[inline]
pub const fn bits(&self) -> u32 { self.0.bits() }
#[inline]
pub const fn from_bits(bits: u32)
-> ::bitflags::__private::core::option::Option<Self> {
match InternalBitFlags::from_bits(bits) {
::bitflags::__private::core::option::Option::Some(bits) =>
::bitflags::__private::core::option::Option::Some(Self(bits)),
::bitflags::__private::core::option::Option::None =>
::bitflags::__private::core::option::Option::None,
}
}
#[inline]
pub const fn from_bits_truncate(bits: u32) -> Self {
Self(InternalBitFlags::from_bits_truncate(bits))
}
#[inline]
pub const fn from_bits_retain(bits: u32) -> Self {
Self(InternalBitFlags::from_bits_retain(bits))
}
#[inline]
pub fn from_name(name: &str)
-> ::bitflags::__private::core::option::Option<Self> {
match InternalBitFlags::from_name(name) {
::bitflags::__private::core::option::Option::Some(bits) =>
::bitflags::__private::core::option::Option::Some(Self(bits)),
::bitflags::__private::core::option::Option::None =>
::bitflags::__private::core::option::Option::None,
}
}
#[inline]
pub const fn is_empty(&self) -> bool { self.0.is_empty() }
#[inline]
pub const fn is_all(&self) -> bool { self.0.is_all() }
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.0.intersects(other.0)
}
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.0.contains(other.0)
}
#[inline]
pub fn insert(&mut self, other: Self) { self.0.insert(other.0) }
#[inline]
pub fn remove(&mut self, other: Self) { self.0.remove(other.0) }
#[inline]
pub fn toggle(&mut self, other: Self) { self.0.toggle(other.0) }
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
self.0.set(other.0, value)
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0.intersection(other.0))
}
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0.union(other.0))
}
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self(self.0.difference(other.0))
}
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self(self.0.symmetric_difference(other.0))
}
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self(self.0.complement())
}
}
impl ::bitflags::__private::core::fmt::Binary for CapabilityFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::Octal for CapabilityFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::LowerHex for CapabilityFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::UpperHex for CapabilityFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::ops::BitOr for CapabilityFlags {
type Output = Self;
#[inline]
fn bitor(self, other: CapabilityFlags) -> Self {
self.union(other)
}
}
impl ::bitflags::__private::core::ops::BitOrAssign for CapabilityFlags
{
#[inline]
fn bitor_assign(&mut self, other: Self) { self.insert(other); }
}
impl ::bitflags::__private::core::ops::BitXor for CapabilityFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
self.symmetric_difference(other)
}
}
impl ::bitflags::__private::core::ops::BitXorAssign for
CapabilityFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
}
impl ::bitflags::__private::core::ops::BitAnd for CapabilityFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self { self.intersection(other) }
}
impl ::bitflags::__private::core::ops::BitAndAssign for
CapabilityFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
*self =
Self::from_bits_retain(self.bits()).intersection(other);
}
}
impl ::bitflags::__private::core::ops::Sub for CapabilityFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self { self.difference(other) }
}
impl ::bitflags::__private::core::ops::SubAssign for CapabilityFlags {
#[inline]
fn sub_assign(&mut self, other: Self) { self.remove(other); }
}
impl ::bitflags::__private::core::ops::Not for CapabilityFlags {
type Output = Self;
#[inline]
fn not(self) -> Self { self.complement() }
}
impl ::bitflags::__private::core::iter::Extend<CapabilityFlags> for
CapabilityFlags {
fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(&mut self, iterator: T) {
for item in iterator { self.insert(item) }
}
}
impl ::bitflags::__private::core::iter::FromIterator<CapabilityFlags>
for CapabilityFlags {
fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(iterator: T) -> Self {
use ::bitflags::__private::core::iter::Extend;
let mut result = Self::empty();
result.extend(iterator);
result
}
}
impl CapabilityFlags {
#[inline]
pub const fn iter(&self)
-> ::bitflags::iter::Iter<CapabilityFlags> {
::bitflags::iter::Iter::__private_const_new(<CapabilityFlags
as ::bitflags::Flags>::FLAGS,
CapabilityFlags::from_bits_retain(self.bits()),
CapabilityFlags::from_bits_retain(self.bits()))
}
#[inline]
pub const fn iter_names(&self)
-> ::bitflags::iter::IterNames<CapabilityFlags> {
::bitflags::iter::IterNames::__private_const_new(<CapabilityFlags
as ::bitflags::Flags>::FLAGS,
CapabilityFlags::from_bits_retain(self.bits()),
CapabilityFlags::from_bits_retain(self.bits()))
}
}
impl ::bitflags::__private::core::iter::IntoIterator for
CapabilityFlags {
type Item = CapabilityFlags;
type IntoIter = ::bitflags::iter::Iter<CapabilityFlags>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
};Clone, #[automatically_derived]
impl ::core::marker::Copy for CapabilityFlags { }Copy)]
16 pub struct CapabilityFlags: u32 {
17 const CLIENT_LONG_PASSWORD = 0x00000001;
18 const CLIENT_FOUND_ROWS = 0x00000002;
19 const CLIENT_LONG_FLAG = 0x00000004;
20 const CLIENT_CONNECT_WITH_DB = 0x00000008;
21 const CLIENT_NO_SCHEMA = 0x00000010;
22 const CLIENT_COMPRESS = 0x00000020;
23 const CLIENT_ODBC = 0x00000040;
24 const CLIENT_LOCAL_FILES = 0x00000080;
25 const CLIENT_IGNORE_SPACE = 0x00000100;
26 const CLIENT_PROTOCOL_41 = 0x00000200;
27 const CLIENT_INTERACTIVE = 0x00000400;
28 const CLIENT_SSL = 0x00000800;
29 const CLIENT_IGNORE_SIGPIPE = 0x00001000;
30 const CLIENT_TRANSACTIONS = 0x00002000;
31 const CLIENT_RESERVED = 0x00004000;
32 const CLIENT_SECURE_CONNECTION = 0x00008000;
33 const CLIENT_MULTI_STATEMENTS = 0x00010000;
34 const CLIENT_MULTI_RESULTS = 0x00020000;
35 const CLIENT_PS_MULTI_RESULTS = 0x00040000;
36 const CLIENT_PLUGIN_AUTH = 0x00080000;
37 const CLIENT_CONNECT_ATTRS = 0x00100000;
38 const CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 0x00200000;
39 const CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS = 0x00400000;
40 const CLIENT_SESSION_TRACK = 0x00800000;
41 const CLIENT_DEPRECATE_EOF = 0x01000000;
42 }
43}
44
45pub(super) struct ConnectionOptions {
46 host: Option<CString>,
47 user: CString,
48 password: Option<CString>,
49 database: Option<CString>,
50 port: Option<u16>,
51 unix_socket: Option<CString>,
52 client_flags: CapabilityFlags,
53 ssl_mode: Option<mysql_ssl_mode>,
54 ssl_ca: Option<CString>,
55 ssl_cert: Option<CString>,
56 ssl_key: Option<CString>,
57}
58
59impl ConnectionOptions {
60 pub(super) fn parse(database_url: &str) -> ConnectionResult<Self> {
61 let url = match Url::parse(database_url) {
62 Ok(url) => url,
63 Err(_) => return Err(connection_url_error()),
64 };
65
66 if url.scheme() != "mysql" {
67 return Err(connection_url_error());
68 }
69
70 if url.path_segments().map(Iterator::count).unwrap_or(0) > 1 {
71 return Err(connection_url_error());
72 }
73
74 let query_pairs = url.query_pairs().into_owned().collect::<HashMap<_, _>>();
75 if query_pairs.contains_key("database") {
76 return Err(connection_url_error());
77 }
78
79 let unix_socket = match query_pairs.get("unix_socket") {
80 Some(v) => Some(CString::new(v.as_bytes())?),
81 _ => None,
82 };
83
84 let ssl_ca = match query_pairs.get("ssl_ca") {
85 Some(v) => Some(CString::new(v.as_bytes())?),
86 _ => None,
87 };
88
89 let ssl_cert = match query_pairs.get("ssl_cert") {
90 Some(v) => Some(CString::new(v.as_bytes())?),
91 _ => None,
92 };
93
94 let ssl_key = match query_pairs.get("ssl_key") {
95 Some(v) => Some(CString::new(v.as_bytes())?),
96 _ => None,
97 };
98
99 let ssl_mode = match query_pairs.get("ssl_mode") {
100 Some(v) => {
101 let ssl_mode = match v.to_lowercase().as_str() {
102 "disabled" => mysql_ssl_mode::SSL_MODE_DISABLED,
103 "preferred" => mysql_ssl_mode::SSL_MODE_PREFERRED,
104 "required" => mysql_ssl_mode::SSL_MODE_REQUIRED,
105 "verify_ca" => mysql_ssl_mode::SSL_MODE_VERIFY_CA,
106 "verify_identity" => mysql_ssl_mode::SSL_MODE_VERIFY_IDENTITY,
107 _ => {
108 let msg = "unknown ssl_mode";
109 return Err(ConnectionError::InvalidConnectionUrl(msg.into()));
110 }
111 };
112 Some(ssl_mode)
113 }
114 _ => None,
115 };
116
117 let host = match url.host() {
118 Some(Host::Ipv6(host)) => Some(CString::new(host.to_string())?),
119 Some(host) if host.to_string() == "localhost" && unix_socket.is_some() => None,
120 Some(host) => Some(CString::new(host.to_string())?),
121 None => None,
122 };
123 let user = decode_into_cstring(url.username())?;
124 let password = match url.password() {
125 Some(password) => Some(decode_into_cstring(password)?),
126 None => None,
127 };
128
129 let database = match url.path_segments().and_then(|mut iter| iter.next()) {
130 Some("") | None => None,
131 Some(segment) => Some(CString::new(segment.as_bytes())?),
132 };
133
134 let client_flags = CapabilityFlags::CLIENT_FOUND_ROWS;
136
137 Ok(ConnectionOptions {
138 host,
139 user,
140 password,
141 database,
142 port: url.port(),
143 client_flags,
144 ssl_mode,
145 unix_socket,
146 ssl_ca,
147 ssl_cert,
148 ssl_key,
149 })
150 }
151
152 pub(super) fn host(&self) -> Option<&CStr> {
153 self.host.as_deref()
154 }
155
156 pub(super) fn user(&self) -> &CStr {
157 &self.user
158 }
159
160 pub(super) fn password(&self) -> Option<&CStr> {
161 self.password.as_deref()
162 }
163
164 pub(super) fn database(&self) -> Option<&CStr> {
165 self.database.as_deref()
166 }
167
168 pub(super) fn port(&self) -> Option<u16> {
169 self.port
170 }
171
172 pub(super) fn unix_socket(&self) -> Option<&CStr> {
173 self.unix_socket.as_deref()
174 }
175
176 pub(super) fn ssl_ca(&self) -> Option<&CStr> {
177 self.ssl_ca.as_deref()
178 }
179
180 pub(super) fn ssl_cert(&self) -> Option<&CStr> {
181 self.ssl_cert.as_deref()
182 }
183
184 pub(super) fn ssl_key(&self) -> Option<&CStr> {
185 self.ssl_key.as_deref()
186 }
187
188 pub(super) fn client_flags(&self) -> CapabilityFlags {
189 self.client_flags
190 }
191
192 pub(super) fn ssl_mode(&self) -> Option<mysql_ssl_mode> {
193 self.ssl_mode
194 }
195}
196
197fn decode_into_cstring(s: &str) -> ConnectionResult<CString> {
198 let decoded = percent_decode(s.as_bytes())
199 .decode_utf8()
200 .map_err(|_| connection_url_error())?;
201 CString::new(decoded.as_bytes()).map_err(Into::into)
202}
203
204fn connection_url_error() -> ConnectionError {
205 let msg = "MySQL connection URLs must be in the form \
206 `mysql://[[user]:[password]@]host[:port][/database][?unix_socket=socket-path]`";
207 ConnectionError::InvalidConnectionUrl(msg.into())
208}
209
210#[cfg(test)]
211mod tests {
212 use super::*;
213
214 #[diesel_test_helper::test]
215 fn urls_with_schemes_other_than_mysql_are_errors() {
216 assert!(ConnectionOptions::parse("postgres://localhost").is_err());
217 assert!(ConnectionOptions::parse("http://localhost").is_err());
218 assert!(ConnectionOptions::parse("file:///tmp/mysql.sock").is_err());
219 assert!(ConnectionOptions::parse("socket:///tmp/mysql.sock").is_err());
220 assert!(ConnectionOptions::parse("mysql://localhost?database=somedb").is_err());
221 assert!(ConnectionOptions::parse("mysql://localhost").is_ok());
222 }
223
224 #[diesel_test_helper::test]
225 fn urls_must_have_zero_or_one_path_segments() {
226 assert!(ConnectionOptions::parse("mysql://localhost/foo/bar").is_err());
227 assert!(ConnectionOptions::parse("mysql://localhost/foo").is_ok());
228 }
229
230 #[diesel_test_helper::test]
231 fn first_path_segment_is_treated_as_database() {
232 let foo_cstr = CString::new("foo").unwrap();
233 let bar_cstr = CString::new("bar").unwrap();
234 assert_eq!(
235 Some(&*foo_cstr),
236 ConnectionOptions::parse("mysql://localhost/foo")
237 .unwrap()
238 .database()
239 );
240 assert_eq!(
241 Some(&*bar_cstr),
242 ConnectionOptions::parse("mysql://localhost/bar")
243 .unwrap()
244 .database()
245 );
246 assert_eq!(
247 None,
248 ConnectionOptions::parse("mysql://localhost")
249 .unwrap()
250 .database()
251 );
252 }
253
254 #[diesel_test_helper::test]
255 fn userinfo_should_be_percent_decode() {
256 use self::percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
257 const USERINFO_ENCODE_SET: &AsciiSet = &CONTROLS
258 .add(b' ')
259 .add(b'"')
260 .add(b'<')
261 .add(b'>')
262 .add(b'`')
263 .add(b'#')
264 .add(b'?')
265 .add(b'{')
266 .add(b'}')
267 .add(b'/')
268 .add(b':')
269 .add(b';')
270 .add(b'=')
271 .add(b'@')
272 .add(b'[')
273 .add(b'\\')
274 .add(b']')
275 .add(b'^')
276 .add(b'|');
277
278 let username = "x#gfuL?4Zuj{n73m}eeJt0";
279 let encoded_username = utf8_percent_encode(username, USERINFO_ENCODE_SET);
280
281 let password = "x/gfuL?4Zuj{n73m}eeJt1";
282 let encoded_password = utf8_percent_encode(password, USERINFO_ENCODE_SET);
283
284 let db_url = format!("mysql://{encoded_username}:{encoded_password}@localhost/bar",);
285 let db_url = Url::parse(&db_url).unwrap();
286
287 let conn_opts = ConnectionOptions::parse(db_url.as_str()).unwrap();
288 let username = CString::new(username.as_bytes()).unwrap();
289 let password = CString::new(password.as_bytes()).unwrap();
290 assert_eq!(username, conn_opts.user);
291 assert_eq!(password, conn_opts.password.unwrap());
292 }
293
294 #[diesel_test_helper::test]
295 fn ipv6_host_not_wrapped_in_brackets() {
296 let host1 = CString::new("::1").unwrap();
297 let host2 = CString::new("2001:db8:85a3::8a2e:370:7334").unwrap();
298
299 assert_eq!(
300 Some(&*host1),
301 ConnectionOptions::parse("mysql://[::1]").unwrap().host()
302 );
303 assert_eq!(
304 Some(&*host2),
305 ConnectionOptions::parse("mysql://[2001:db8:85a3::8a2e:370:7334]")
306 .unwrap()
307 .host()
308 );
309 }
310
311 #[diesel_test_helper::test]
312 fn unix_socket_tests() {
313 let unix_socket = "/var/run/mysqld.sock";
314 let username = "foo";
315 let password = "bar";
316 let db_url = format!("mysql://{username}:{password}@localhost?unix_socket={unix_socket}",);
317 let conn_opts = ConnectionOptions::parse(db_url.as_str()).unwrap();
318 let cstring = |s| CString::new(s).unwrap();
319 assert_eq!(None, conn_opts.host);
320 assert_eq!(None, conn_opts.port);
321 assert_eq!(cstring(username), conn_opts.user);
322 assert_eq!(cstring(password), conn_opts.password.unwrap());
323 assert_eq!(
324 CString::new(unix_socket).unwrap(),
325 conn_opts.unix_socket.unwrap()
326 );
327 }
328
329 #[diesel_test_helper::test]
330 fn ssl_ca_tests() {
331 let ssl_ca = "/etc/ssl/certs/ca-certificates.crt";
332 let username = "foo";
333 let password = "bar";
334 let db_url = format!("mysql://{username}:{password}@localhost?ssl_ca={ssl_ca}",);
335 let conn_opts = ConnectionOptions::parse(db_url.as_str()).unwrap();
336 let cstring = |s| CString::new(s).unwrap();
337 assert_eq!(Some(cstring("localhost")), conn_opts.host);
338 assert_eq!(None, conn_opts.port);
339 assert_eq!(cstring(username), conn_opts.user);
340 assert_eq!(cstring(password), conn_opts.password.unwrap());
341 assert_eq!(CString::new(ssl_ca).unwrap(), conn_opts.ssl_ca.unwrap());
342
343 let url_with_unix_str_and_ssl_ca = format!(
344 "mysql://{username}:{password}@localhost?unix_socket=/var/run/mysqld.sock&ssl_ca={ssl_ca}"
345 );
346
347 let conn_opts2 = ConnectionOptions::parse(url_with_unix_str_and_ssl_ca.as_str()).unwrap();
348 assert_eq!(None, conn_opts2.host);
349 assert_eq!(None, conn_opts2.port);
350 assert_eq!(CString::new(ssl_ca).unwrap(), conn_opts2.ssl_ca.unwrap());
351 }
352
353 #[diesel_test_helper::test]
354 fn ssl_cert_tests() {
355 let ssl_cert = "/etc/ssl/certs/client-cert.crt";
356 let username = "foo";
357 let password = "bar";
358 let db_url = format!("mysql://{username}:{password}@localhost?ssl_cert={ssl_cert}");
359 let conn_opts = ConnectionOptions::parse(db_url.as_str()).unwrap();
360 let cstring = |s| CString::new(s).unwrap();
361 assert_eq!(Some(cstring("localhost")), conn_opts.host);
362 assert_eq!(None, conn_opts.port);
363 assert_eq!(cstring(username), conn_opts.user);
364 assert_eq!(cstring(password), conn_opts.password.unwrap());
365 assert_eq!(CString::new(ssl_cert).unwrap(), conn_opts.ssl_cert.unwrap());
366
367 let url_with_unix_str_and_ssl_cert = format!(
368 "mysql://{username}:{password}@localhost?unix_socket=/var/run/mysqld.sock&ssl_cert={ssl_cert}"
369 );
370
371 let conn_opts2 = ConnectionOptions::parse(url_with_unix_str_and_ssl_cert.as_str()).unwrap();
372 assert_eq!(None, conn_opts2.host);
373 assert_eq!(None, conn_opts2.port);
374 assert_eq!(
375 CString::new(ssl_cert).unwrap(),
376 conn_opts2.ssl_cert.unwrap()
377 );
378 }
379
380 #[diesel_test_helper::test]
381 fn ssl_key_tests() {
382 let ssl_key = "/etc/ssl/certs/client-key.crt";
383 let username = "foo";
384 let password = "bar";
385 let db_url = format!("mysql://{username}:{password}@localhost?ssl_key={ssl_key}");
386 let conn_opts = ConnectionOptions::parse(db_url.as_str()).unwrap();
387 let cstring = |s| CString::new(s).unwrap();
388 assert_eq!(Some(cstring("localhost")), conn_opts.host);
389 assert_eq!(None, conn_opts.port);
390 assert_eq!(cstring(username), conn_opts.user);
391 assert_eq!(cstring(password), conn_opts.password.unwrap());
392 assert_eq!(CString::new(ssl_key).unwrap(), conn_opts.ssl_key.unwrap());
393
394 let url_with_unix_str_and_ssl_key = format!(
395 "mysql://{username}:{password}@localhost?unix_socket=/var/run/mysqld.sock&ssl_key={ssl_key}"
396 );
397
398 let conn_opts2 = ConnectionOptions::parse(url_with_unix_str_and_ssl_key.as_str()).unwrap();
399 assert_eq!(None, conn_opts2.host);
400 assert_eq!(None, conn_opts2.port);
401 assert_eq!(CString::new(ssl_key).unwrap(), conn_opts2.ssl_key.unwrap());
402 }
403
404 #[diesel_test_helper::test]
405 fn ssl_mode() {
406 let ssl_mode = |url| ConnectionOptions::parse(url).unwrap().ssl_mode();
407 assert_eq!(ssl_mode("mysql://localhost"), None);
408 assert_eq!(
409 ssl_mode("mysql://localhost?ssl_mode=disabled"),
410 Some(mysql_ssl_mode::SSL_MODE_DISABLED)
411 );
412 assert_eq!(
413 ssl_mode("mysql://localhost?ssl_mode=PREFERRED"),
414 Some(mysql_ssl_mode::SSL_MODE_PREFERRED)
415 );
416 assert_eq!(
417 ssl_mode("mysql://localhost?ssl_mode=required"),
418 Some(mysql_ssl_mode::SSL_MODE_REQUIRED)
419 );
420 assert_eq!(
421 ssl_mode("mysql://localhost?ssl_mode=VERIFY_CA"),
422 Some(mysql_ssl_mode::SSL_MODE_VERIFY_CA)
423 );
424 assert_eq!(
425 ssl_mode("mysql://localhost?ssl_mode=verify_identity"),
426 Some(mysql_ssl_mode::SSL_MODE_VERIFY_IDENTITY)
427 );
428 }
429}