Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F49509190
validation.rs
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
validation.rs
View Options
// -------------------------------------------------------------
// Alkane :: Services :: Validation
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Project: Nasqueron
// License: BSD-2-Clause
// Description: Input validation for site names
// -------------------------------------------------------------
use
idna
::
domain_to_ascii_strict
;
/// Determines whether a site name has valid hostname syntax.
///
/// Internationalized domain names can be supplied as Unicode or punycode.
/// Validation uses their ASCII representation for DNS label and length limits,
/// but the original spelling remains the site name used for filesystem paths.
pub
fn
is_valid_site_name
(
site_name
:
&
str
)
->
bool
{
if
site_name
.
is_empty
()
||
site_name
.
ends_with
(
'.'
)
||
site_name
.
contains
(
'/'
)
||
site_name
.
contains
(
'\\'
)
{
return
false
;
}
domain_to_ascii_strict
(
site_name
).
is_ok_and
(
|
ascii_site_name
|
ascii_site_name
.
len
()
<=
253
)
}
// -------------------------------------------------------------
// Tests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[test]
fn
accepts_ascii_hostnames_and_idns
()
{
assert!
(
is_valid_site_name
(
"foo.example.org"
));
assert!
(
is_valid_site_name
(
"EXAMPLE.org"
));
assert!
(
is_valid_site_name
(
"bücher.example"
));
assert!
(
is_valid_site_name
(
"xn--bcher-kva.example"
));
}
#[test]
fn
rejects_invalid_hostname_syntax
()
{
assert!
(
!
is_valid_site_name
(
""
));
assert!
(
!
is_valid_site_name
(
"example..org"
));
assert!
(
!
is_valid_site_name
(
"-example.org"
));
assert!
(
!
is_valid_site_name
(
"example-.org"
));
assert!
(
!
is_valid_site_name
(
"example_org"
));
assert!
(
!
is_valid_site_name
(
"example.org."
));
assert!
(
!
is_valid_site_name
(
"hello world.example"
));
assert!
(
!
is_valid_site_name
(
"../example.org"
));
assert!
(
!
is_valid_site_name
(
"/example.org"
));
assert!
(
!
is_valid_site_name
(
"example.org/../foo"
));
assert!
(
!
is_valid_site_name
(
r"example.org\foo"
));
assert!
(
!
is_valid_site_name
(
&
format!
(
"{}.org"
,
"a"
.
repeat
(
64
))));
assert!
(
!
is_valid_site_name
(
&
"a"
.
repeat
(
254
)));
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Sep 14, 16:30 (4 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4026759
Default Alt Text
validation.rs (2 KB)
Attached To
Mode
rALK Alkane
Attached
Detach File
Event Timeline
Log In to Comment