Skip to content

Commit f5ff081

Browse files
authored
Refactored punycode and domain functions (#58)
1 parent 28a65aa commit f5ff081

File tree

16 files changed

+559
-497
lines changed

16 files changed

+559
-497
lines changed

include/skyr/domain/domain.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2018-20 Glyn Matthews.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef SKYR_DOMAIN_DOMAIN_HPP
7+
#define SKYR_DOMAIN_DOMAIN_HPP
8+
9+
/// \file domain.hpp
10+
11+
#include <string>
12+
#include <string_view>
13+
#include <system_error>
14+
#include <tl/expected.hpp>
15+
16+
namespace skyr {
17+
inline namespace v1 {
18+
/// Converts a UTF-8 encoded domain to ASCII using
19+
/// [IDNA processing](https://www.domain.org/reports/tr46/#Processing)
20+
///
21+
/// \param domain A domain
22+
/// \param be_strict Tells the processor to be strict
23+
/// \returns An ASCII domain, or an error
24+
auto domain_to_ascii(
25+
std::string_view domain,
26+
bool be_strict = false) -> tl::expected<std::string, std::error_code>;
27+
28+
/// Converts a UTF-32 encoded domain to ASCII using
29+
/// [IDNA processing](https://www.domain.org/reports/tr46/#Processing)
30+
///
31+
/// \param domain A domain
32+
/// \param be_strict Tells the processor to be strict
33+
/// \returns An ASCII domain, or an error
34+
auto domain_to_ascii(
35+
std::u32string_view domain,
36+
bool be_strict = false) -> tl::expected<std::string, std::error_code>;
37+
} // namespace v1
38+
} // namespace skyr
39+
40+
#endif // SKYR_DOMAIN_DOMAIN_HPP

include/skyr/domain/errors.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020 Glyn Matthews.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef SKYR_DOMAIN_ERRORS_HPP
7+
#define SKYR_DOMAIN_ERRORS_HPP
8+
9+
#include <system_error>
10+
11+
namespace skyr {
12+
inline namespace v1 {
13+
/// \enum domain_errc
14+
/// Enumerates domain processing errors
15+
enum class domain_errc {
16+
/// The domain code point is disallowed
17+
disallowed_code_point = 1,
18+
/// The encoder or decoder received bad input
19+
bad_input,
20+
/// Overflow
21+
overflow,
22+
/// Unicode encoding error
23+
encoding_error,
24+
};
25+
26+
/// Creates a `std::error_code` given a `skyr::domain_errc` value
27+
/// \param error A domain error
28+
/// \returns A `std::error_code` object
29+
auto make_error_code(domain_errc error) noexcept -> std::error_code;
30+
} // namespace v1
31+
} // namespace skyr
32+
33+
namespace std {
34+
template <>
35+
struct is_error_code_enum<skyr::v1::domain_errc> : true_type {};
36+
} // namespace std
37+
38+
#endif //SKYR_DOMAIN_ERRORS_HPP
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
// (See accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6-
#ifndef SKYR_UNICODE_IDNA_HPP
7-
#define SKYR_UNICODE_IDNA_HPP
6+
#ifndef SKYR_DOMAIN_IDNA_HPP
7+
#define SKYR_DOMAIN_IDNA_HPP
88

99
namespace skyr {
1010
inline namespace v1 {
11-
namespace unicode {
11+
namespace domain {
1212
/// \enum idna_status
13-
/// The status values come from the IDNA mapping table in unicode TR46:
13+
/// The status values come from the IDNA mapping table in domain TR46:
1414
///
15-
/// https://unicode.org/reports/tr46/#IDNA_Mapping_Table
15+
/// https://domain.org/reports/tr46/#IDNA_Mapping_Table
1616
///
1717
enum class idna_status {
1818
/// The code point is disallowed
@@ -45,8 +45,8 @@ auto map_idna_status(char32_t code_point) -> idna_status;
4545
/// \return The code point or mapped value, depending on the status of the code
4646
/// point
4747
auto map_idna_code_point(char32_t code_point) -> char32_t;
48-
} // namespace unicode
48+
} // namespace domain
4949
} // namespace v1
5050
} // namespace skyr
5151

52-
#endif // SKYR_UNICODE_IDNA_HPP
52+
#endif // SKYR_DOMAIN_IDNA_HPP

include/skyr/domain/punycode.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020 Glyn Matthews.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef SKYR_DOMAIN_PUNYCODE_HPP
7+
#define SKYR_DOMAIN_PUNYCODE_HPP
8+
9+
#include <string>
10+
#include <string_view>
11+
#include <system_error>
12+
#include <tl/expected.hpp>
13+
14+
namespace skyr {
15+
inline namespace v1 {
16+
/// Performs Punycode encoding based on a reference implementation
17+
/// defined in [RFC 3492](https://tools.ietf.org/html/rfc3492)
18+
///
19+
/// \param input A UTF-8 encoded domain to be encoded
20+
/// \returns The encoded ASCII domain, or an error
21+
auto punycode_encode(
22+
std::string_view input) -> tl::expected<std::string, std::error_code>;
23+
24+
/// Performs Punycode encoding based on a reference implementation
25+
/// defined in [RFC 3492](https://tools.ietf.org/html/rfc3492)
26+
///
27+
/// \param input A UTF-32 encoded domain to be encoded
28+
/// \returns The encoded ASCII domain, or an error
29+
auto punycode_encode(
30+
std::u32string_view input) -> tl::expected<std::string, std::error_code>;
31+
32+
/// Performs Punycode decoding based on a reference implementation
33+
/// defined in [RFC 3492](https://tools.ietf.org/html/rfc3492)
34+
///
35+
/// \param input An ASCII encoded domain to be decoded
36+
/// \returns The decoded UTF-8 domain, or an error
37+
auto punycode_decode(
38+
std::string_view input) -> tl::expected<std::string, std::error_code>;
39+
} // namespace v1
40+
} // namespace skyr
41+
42+
#endif // SKYR_DOMAIN_PUNYCODE_HPP

include/skyr/unicode/domain.hpp

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ configure_file(
1010

1111
set(skyr_SRCS
1212
unicode/errors.cpp
13-
unicode/idna.cpp
14-
unicode/domain.cpp
13+
domain/errors.cpp
14+
domain/domain.cpp
15+
domain/punycode.cpp
16+
domain/idna.cpp
1517
core/url_parser_context.hpp
1618
core/url_parser_context.cpp
1719
core/url_record.cpp
@@ -47,8 +49,9 @@ set(skyr_SRCS
4749
${PROJECT_SOURCE_DIR}/include/skyr/unicode/ranges/transforms/byte_transform.hpp
4850
${PROJECT_SOURCE_DIR}/include/skyr/unicode/ranges/transforms/u16_transform.hpp
4951
${PROJECT_SOURCE_DIR}/include/skyr/unicode/ranges/transforms/u32_transform.hpp
50-
${PROJECT_SOURCE_DIR}/include/skyr/unicode/idna.hpp
51-
${PROJECT_SOURCE_DIR}/include/skyr/unicode/domain.hpp
52+
${PROJECT_SOURCE_DIR}/include/skyr/domain/errors.hpp
53+
${PROJECT_SOURCE_DIR}/include/skyr/domain/idna.hpp
54+
${PROJECT_SOURCE_DIR}/include/skyr/domain/domain.hpp
5255
${PROJECT_SOURCE_DIR}/include/skyr/percent_encoding/errors.hpp
5356
${PROJECT_SOURCE_DIR}/include/skyr/percent_encoding/percent_decode_range.hpp
5457
${PROJECT_SOURCE_DIR}/include/skyr/percent_encoding/percent_encode_range.hpp

src/core/url_parser_context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <locale>
1212
#include <skyr/network/ipv4_address.hpp>
1313
#include <skyr/network/ipv6_address.hpp>
14-
#include <skyr/unicode/domain.hpp>
14+
#include <skyr/domain/domain.hpp>
1515
#include <skyr/percent_encoding/percent_decode_range.hpp>
1616
#include "url_parser_context.hpp"
1717
#include "url_schemes.hpp"
@@ -137,7 +137,7 @@ tl::expected<std::string, url_parse_errc> parse_host(
137137
return tl::make_unexpected(url_parse_errc::cannot_decode_host_point);
138138
}
139139

140-
auto ascii_domain = unicode::domain_to_ascii(domain.value());
140+
auto ascii_domain = domain_to_ascii(domain.value());
141141
if (!ascii_domain) {
142142
return tl::make_unexpected(url_parse_errc::domain_error);
143143
}

0 commit comments

Comments
 (0)