Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 169 additions & 19 deletions splitflap-doc/constructs.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ particular resource (e.g., a page or file) within the tagging entity.
The tag URI scheme is formalized in @hyperlink["https://datatracker.ietf.org/doc/html/rfc4151"]{RFC
4151}.

@defproc[(mint-tag-uri [authority (or/c dns-domain? email-address?)]
@defproc[(mint-tag-uri [authority tag-authority?]
[date tag-entity-date?]
[specific tag-specific-string?])
tag-uri?]{

Returns a @tech{tag URI} struct for use as a unique identifier in a @racket[feed-item],
@racket[feed], @racket[@episode] or @racket[podcast].

The @racket[_authority] must be a domain name or email address in lowercase. (See
@racket[tag-authority?].)

The @racket[_date] must be any date on which you had ownership or assignment of the domain or
email address at 00:00 UTC (the start of the day). (See @racket[tag-entity-date?].)

Expand All @@ -72,6 +75,8 @@ characters are allowed here.
(mint-tag-uri "rclib.example.com" "2012-04-01" "Marian'sBlog")
(mint-tag-uri "diveintomark.example.com" "2003" "3.2397")]

@history[#:changed "1.4" @elem{The @racket[_authority] must now satisfy @racket[tag-authority?].}]

}

@defproc[(tag-uri->string [tag tag-uri?]) non-empty-string?]{
Expand Down Expand Up @@ -108,6 +113,31 @@ Returns @racket[#t] if the @racket[tag-uri->string] representation of @racket[_t

}

@defproc[(tag-authority? [v any/c]) boolean?]{

Returns @racket[#t] if @racket[_v] can be used as the @tech{authority} in a @tech{tag URI}: a
@racket[dns-domain?] or @racket[email-address?] that also meets these conditions:

@itemlist[

@item{It contains no uppercase letters. RFC 4151 recommends lowercase, and the @W3CFeedValidator[]
rejects tag URIs that do not follow this recommendation.}

@item{If it is an email address, the part before the @litchar{@"@"} includes only @litchar{a–z},
@litchar{0–9}, or characters in the set @litchar{-._}, as RFC 4151 requires.}

]

@examples[#:eval mod-constructs
(tag-authority? "rclib.example.com")
(tag-authority? "marian@rclib.example.com")
(tag-authority? "RCLib.example.com")
(tag-authority? "marian+blog@rclib.example.com")]

@history[#:added "1.4"]

}

@defproc[(tag-entity-date? [str string?]) boolean?]{

Returns @racket[#t] if @racket[_str] is a string of the form @racket{YYYY[-MM[-DD]]} --- that is, an
Expand Down Expand Up @@ -296,7 +326,8 @@ A hash table mapping file extensions (in lowercase symbol form) to MIME types.
(hash-ref mime-types-by-ext 'epub)]

@history[#:changed "1.3" @elem{Now a plain hash rather than a
@tech[#:doc '(lib "scribblings/reference/reference.scrbl")]{promise}.}]
@tech[#:doc '(lib "scribblings/reference/reference.scrbl")]{promise}.}
#:changed "1.4" @elem{The table is loaded the first time it is used.}]

}

Expand All @@ -317,7 +348,8 @@ in @racket[mime-types-by-ext], @racket[#f] otherwise. This function does not acc
@defproc[(dns-domain? [v any/c]) boolean?]{

Returns @racket[#t] if @racket[_v] is a string whose entire contents are a valid DNS domain
according to @hyperlink["https://datatracker.ietf.org/doc/html/rfc1035"]{RFC 1035}:
according to @hyperlink["https://datatracker.ietf.org/doc/html/rfc1035"]{RFC 1035}, as updated by
@hyperlink["https://datatracker.ietf.org/doc/html/rfc1123#section-2.1"]{RFC 1123}:

@itemlist[

Expand All @@ -326,10 +358,19 @@ according to @hyperlink["https://datatracker.ietf.org/doc/html/rfc1035"]{RFC 103
@item{Each label must consist of only the characters @litchar{A–Z}, @litchar{a–z}, @litchar{0–9}, or
@litchar{-}.}

@item{Labels may not start with a digit or a hyphen, and may not end in a hyphen.}
@item{Labels may not start or end with a hyphen.}

@item{Labels with @litchar{--} in the third and fourth positions must be valid @tech{A-labels} (see
@secref["idn"]). If any A-label contains right-to-left text, every label must satisfy the Bidi rule
of @hyperlink["https://datatracker.ietf.org/doc/html/rfc5893"]{RFC 5893}.}

@item{No individual label may be longer than 63 bytes (including an extra byte for a length header),
and the entire domain may not be longer than 255 bytes.}
@item{The last label may not consist entirely of digits (see
@hyperlink["https://datatracker.ietf.org/doc/html/rfc3696#section-2"]{RFC 3696}), so IPv4 addresses
never qualify.}

@item{No individual label may be longer than 63 bytes, and the entire domain may not be longer than
253 bytes. (RFC 1035 sets the limit at 255 bytes, but that count includes a length byte before each
label and a zero byte for the root label.)}

]

Expand All @@ -338,27 +379,36 @@ and the entire domain may not be longer than 255 bytes.}
(dns-domain? "rclib.org")
(dns-domain? "a.b.c.d.e-f")
(dns-domain? "a.b1000.com")
(dns-domain? "1.example.com")
(dns-domain? "192.0.2.16")
(dns-domain? "xn--bcher-kva.example")
(dns-domain? "bücher.example")
code:blank
(define longest-valid-label (make-string 62 #\a))
(define longest-valid-label (make-string 63 #\a))
(define longest-valid-domain
(string-append longest-valid-label "." (code:comment @#,elem{63 bytes (including length header)})
longest-valid-label "." (code:comment @#,elem{126})
longest-valid-label "." (code:comment @#,elem{189})
longest-valid-label "." (code:comment @#,elem{252})
"aa")) (code:comment @#,elem{255 bytes})
(string-append longest-valid-label "."
longest-valid-label "."
longest-valid-label "."
(make-string 61 #\a)))
(string-length longest-valid-domain)
code:blank
(dns-domain? longest-valid-label)
(dns-domain? longest-valid-domain)
(dns-domain? (string-append longest-valid-label "a"))
(dns-domain? (string-append longest-valid-domain "a"))]

@history[#:changed "1.4" @elem{Labels may now start with a digit. The length limits are now 63 bytes
per label and 253 bytes overall (previously 62 and 254). The empty string no longer qualifies.
Labels with @litchar{--} in the third and fourth positions must now be valid A-labels.}]
}

@defproc[(valid-url-string? [v any/c]) boolean?]{

Returns @racket[#t] if @racket[_v] is a “valid URL” for use in feeds. For this library’s purposes, a
valid URL is one which, when parsed with @racket[string->url], includes a valid @tt{scheme} part
(e.g. @racket{http://}), and in which the host is a @racket[dns-domain?] (and not, say, an IP
address).
address). The URL must also contain only ASCII characters; use @racket[url-string->ascii] to convert
an internationalized URL.

@examples[#:eval mod-constructs
(valid-url-string? "http://rclib.example.com")
Expand All @@ -373,7 +423,11 @@ address).
code:blank
(code:line (code:comment @#,elem{Valid URLs but not allowed by this library for use in feeds}))
(code:line (valid-url-string? "ldap://[2001:db8::7]/c=GB?objectClass?one") (code:comment @#,elem{Host is not a DNS domain}))
(code:line (valid-url-string? "telnet://192.0.2.16:80/") (code:comment @#,elem{ditto}))]
(code:line (valid-url-string? "telnet://192.0.2.16:80/") (code:comment @#,elem{ditto}))
(code:line (valid-url-string? "https://bücher.example/") (code:comment @#,elem{not ASCII; see url-string->ascii}))]

@history[#:changed "1.4" @elem{Follows the changes to @racket[dns-domain?]. In particular, URLs with
an empty host no longer qualify. URLs must now contain only ASCII characters.}]

}

Expand Down Expand Up @@ -415,26 +469,35 @@ common-sense subset of RFC 5322:

@item{Must be in the format @racketvalfont{@nonterm{local-part}@litchar{@"@"}@nonterm{domain}}}

@item{The @nonterm{local-part} must be no longer than 65 bytes and only include @litchar{a–z},
@litchar{A–Z}, @litchar{0–9}, or characters in the set @litchar|{!#$%&'*+/=?^_‘{|}~-.}|.}
@item{The @nonterm{local-part} must be no longer than 64 bytes and only include @litchar{a–z},
@litchar{A–Z}, @litchar{0–9}, or characters in the set @litchar|{!#$%&'*+/=?^_`{|}~-.}|.}

@item{The @nonterm{local-part} may not start or end with @litchar{.}, and may not contain two
@litchar{.} characters in a row.}

@item{The @nonterm{domain} must be valid according to @racket[dns-domain?].}

@item{The entire email address must be no longer than 255 bytes.}
@item{The entire email address must be no longer than 254 bytes.}

]

@examples[#:eval mod-constructs
(email-address? "test-email.with+symbol@example.com")
(email-address? "#!$%&'*+-/=?^_{}|~@example.com")
(email-address? "marian..paroo@example.com")
code:blank
(code:comment @#,elem{See also dns-domain? which applies to everything after the @"@" sign})
(email-address? "email@123.123.123.123")
(email-address? "λ@example.com")]

@history[#:changed "1.4" @elem{Fixed a bug that accepted any local part containing at least one
allowed character and rejected local parts with no lowercase letters. The allowed characters now
include @litchar{`} (as in RFC 5322) instead of @litchar{‘}. Local parts may no longer start or end
with a period or contain two periods in a row.}]

}

@defproc[(validate-email-address [addr string?]) boolean?]{
@defproc[(validate-email-address [addr string?]) string?]{

Returns @racket[_addr] if it is a valid email address (according to the same rules as for
@racket[email-address?]); otherwise, an exception is raised whose message explains the reason the
Expand All @@ -445,8 +508,95 @@ address is invalid.
(validate-email-address "@")
(validate-email-address "me@myself@example.com")
(validate-email-address ".marian@rclib.example.com")
(validate-email-address "marian..paroo@rclib.example.com")
(validate-email-address "λ@example.com")
(validate-email-address "lambda@1.example.com")]
(validate-email-address "lambda@example..com")]

@history[#:changed "1.4" @elem{Now accepts and rejects exactly the same addresses as
@racket[email-address?]. Previously it rejected local parts containing uppercase letters.}]

}

@subsection[#:tag "idn"]{Internationalized domain names}

Domain names, URLs and email addresses in feeds must be ASCII. Use the functions in this section to
convert internationalized domain names to their ASCII form, in which each label that contains
non-ASCII characters is replaced by its @deftech{A-label}: the prefix @litchar{xn--} followed by the
@hyperlink["https://datatracker.ietf.org/doc/html/rfc3492"]{Punycode} encoding of the label (so
@racket{bücher} becomes @racket{xn--bcher-kva}).

Splitflap follows @hyperlink["https://datatracker.ietf.org/doc/html/rfc5891"]{IDNA2008} strictly:

@itemlist[

@item{Each label must pass the IDNA2008 registration checks: it must be in Unicode Normalization
Form C, may not start with a combining mark or contain characters that IDNA2008 disallows, and must
satisfy the contextual rules of @hyperlink["https://datatracker.ietf.org/doc/html/rfc5892"]{RFC
5892}. If any label contains right-to-left characters, every label must satisfy the Bidi rule of
@hyperlink["https://datatracker.ietf.org/doc/html/rfc5893"]{RFC 5893}.}

@item{Nothing is mapped: for example, uppercase letters are not converted to lowercase, and the
ideographic full stop @litchar{。} is not treated as a label separator. When a domain name fails only
because it needs mapping of this kind, the exception message suggests the mapped form.}

@item{Character properties come from IANA’s
@hyperlink["https://www.iana.org/assignments/idna-tables-12.0.0/idna-tables-12.0.0.xhtml"]{IDNA
tables for Unicode 12.0.0}, the most recent version IANA has published. Characters added to Unicode
after version 12.0.0 are rejected.}

@item{Labels that are already ASCII are never changed (but they are still checked).}

]

These rules ensure that a given domain name always converts to the same A-labels. This matters for
@tech{tag URIs}, which must never change once published: the older IDNA2003 standard and the
“transitional” processing in @hyperlink["https://www.unicode.org/reports/tr46/"]{UTS #46} convert
@racket{faß.de} to @racket{fass.de}, for example, while IDNA2008 produces @racket{xn--fa-hia.de}.

@defproc[(domain->ascii [domain string?]) dns-domain?]{

Returns the ASCII form of @racket[_domain], replacing each label that contains non-ASCII characters
with its @tech{A-label}. An exception is raised if @racket[_domain] is not a valid domain name.

@examples[#:eval mod-constructs
(domain->ascii "bücher.example")
(domain->ascii "faß.de")
(domain->ascii "例え.テスト")
(domain->ascii "rclib.example.com")
(eval:error (domain->ascii "Bücher.example"))]

@history[#:added "1.4"]

}

@defproc[(url-string->ascii [url string?]) valid-url-string?]{

Converts @racket[_url] to a URL that contains only ASCII characters, following
@hyperlink["https://datatracker.ietf.org/doc/html/rfc3987#section-3.1"]{RFC 3987}: the host is
converted with @racket[domain->ascii], and all other non-ASCII characters are percent-encoded as
UTF-8. ASCII characters are never changed. An exception is raised if the result is not a
@racket[valid-url-string?].

@examples[#:eval mod-constructs
(url-string->ascii "https://bücher.example/straße?q=ü")
(url-string->ascii "https://rclib.example.com/my%20file.html")]

@history[#:added "1.4"]

}

@defproc[(email-address->ascii [addr string?]) email-address?]{

Returns @racket[_addr] with its domain converted by @racket[domain->ascii]. The local part (before
the @litchar{@"@"}) must already be ASCII: internationalized local parts have no ASCII form
(@hyperlink["https://datatracker.ietf.org/doc/html/rfc6530"]{RFC 6530}), and the @Atom1.0[] spec
does not allow them. An exception is raised if the result is not an @racket[email-address?].

@examples[#:eval mod-constructs
(email-address->ascii "marian@bücher.example")
(eval:error (email-address->ascii "marían@example.com"))]

@history[#:added "1.4"]

}

Expand Down
48 changes: 39 additions & 9 deletions splitflap-doc/notes.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,49 @@ Splitflap can be considered stable. No backward-incompatible changes are planned
See Splitflap’s @hyperlink["https://github.com/otherjoel/splitflap/issues"]{issues tracker on Github}
for any known problems with the library, or to report problems.

@subsection{Non-ASCII email addresses and domains}
@subsection{Non-ASCII email addresses}

The @Atom1.0[] and @RSS2.0[] specs do not contemplate or allow for the use of non-ASCII characters
in email addresses or domain names, which is a defect considering that non-English alphabets are in
widespread use for both of these things.

At this early stage I have chosen to enforce the standards as written, for consistency’s sake. I do
plan to add a parameter which would cause @racket[dns-domain?] and @racket[email-address?] to
validate strings according to some alternative scheme that allows for non-ASCII characters. In order
to do this correctly, though, I need to educate myself about any standards that exist in this area.
Splitflap can convert internationalized domain names and URLs to their ASCII form (see
@secref["idn"]), but it cannot include an email address whose local part (the part before the
@litchar{@"@"}) contains non-ASCII characters. Such addresses have no ASCII form
(@hyperlink["https://datatracker.ietf.org/doc/html/rfc6530"]{RFC 6530}), and the @Atom1.0[] spec
requires email addresses to conform to RFC 2822, which allows only ASCII.

@section{Version History}

@subsection{Version 1.4}

@itemlist[

@item{@racket[dns-domain?] now allows labels that start with a digit (per RFC 1123) and labels of up
to 63 bytes; limits domains to 253 bytes; and no longer accepts the empty string. As a result,
@racket[valid-url-string?] no longer accepts URLs with an empty host.}

@item{@racket[email-address?] now checks the entire local part (previously one allowed character was
enough), and it now applies exactly the same rules as @racket[validate-email-address]. Both accept
uppercase letters and @litchar{`}, and both reject local parts that start or end with a period or
contain two periods in a row.}

@item{Added @racket[tag-authority?]. @racket[mint-tag-uri] now requires a lowercase authority, and
allows only @litchar{a–z}, @litchar{0–9}, @litchar{-}, @litchar{.} and @litchar{_} before the
@litchar{@"@"} of an email authority, per RFC 4151. The @W3CFeedValidator[] rejects Atom feeds whose
tag URIs use other authorities.}

@item{Added @racket[domain->ascii], @racket[url-string->ascii] and @racket[email-address->ascii],
which convert internationalized domain names to ASCII according to IDNA2008 (see @secref["idn"]).
@racket[url-string->ascii] also percent-encodes any other non-ASCII characters, following RFC 3987.
@racket[email-address->ascii] converts only the domain, because a non-ASCII local part has no ASCII
form.}

@item{@racket[dns-domain?] now requires labels with @litchar{--} in the third and fourth positions
to be valid A-labels, and @racket[valid-url-string?] now rejects URLs that contain non-ASCII
characters.}

@item{The MIME type table (@racket[mime-types-by-ext]) is now loaded the first time it is used,
rather than whenever Splitflap is loaded.}

]

@subsection{Version 1.3}

@itemlist[#:style 'compact
Expand Down
10 changes: 7 additions & 3 deletions splitflap-lib/constructs.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@

(contract-out
[url-domain (-> valid-url-string? dns-domain?)]
[url-join (-> valid-url-string? relative-path? valid-url-string?)])
[url-join (-> valid-url-string? relative-path? valid-url-string?)]
[domain->ascii (-> string? dns-domain?)]
[url-string->ascii (-> string? valid-url-string?)]
[email-address->ascii (-> string? email-address?)])

; Tag URIs:
tag-authority?
tag-entity-date?
tag-uri?
tag-specific-string?
(contract-out
[mint-tag-uri (-> (or/c dns-domain? email-address?) tag-entity-date? tag-specific-string? tag-uri?)]
[mint-tag-uri (-> tag-authority? tag-entity-date? tag-specific-string? tag-uri?)]
[append-specific (-> tag-uri? tag-specific-string? tag-uri?)]
[tag-uri->string (->* (tag-uri?) (#:specific tag-specific-string?) string?)]
[tag=? (-> tag-uri? tag-uri? boolean?)]
Expand All @@ -45,8 +49,8 @@
; Enclosures and MIME types
(struct-out enclosure)
file->enclosure
mime-types-by-ext
(contract-out
[mime-types-by-ext (hash/c symbol? string? #:immutable #t)]
[path/string->mime-type (-> path-string? (or/c string? #f))])

; Language codes
Expand Down
2 changes: 1 addition & 1 deletion splitflap-lib/info.rkt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#lang info

(define collection "splitflap")
(define version "1.3")
(define version "1.4")

(define deps '(["base" #:version "8.1"]
"gregor-lib"))
Expand Down
Loading