/src/mozilla-central/netwerk/dns/nsIDNService.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #ifndef nsIDNService_h__ |
7 | | #define nsIDNService_h__ |
8 | | |
9 | | #include "nsIIDNService.h" |
10 | | #include "nsCOMPtr.h" |
11 | | #include "nsIObserver.h" |
12 | | #include "nsUnicodeScriptCodes.h" |
13 | | #include "nsWeakReference.h" |
14 | | |
15 | | #include "unicode/uidna.h" |
16 | | |
17 | | #include "nsString.h" |
18 | | |
19 | | class nsIPrefBranch; |
20 | | |
21 | | //----------------------------------------------------------------------------- |
22 | | // nsIDNService |
23 | | //----------------------------------------------------------------------------- |
24 | | |
25 | | class nsIDNService final : public nsIIDNService, |
26 | | public nsSupportsWeakReference |
27 | | { |
28 | | public: |
29 | | NS_DECL_THREADSAFE_ISUPPORTS |
30 | | NS_DECL_NSIIDNSERVICE |
31 | | |
32 | | nsIDNService(); |
33 | | |
34 | | nsresult Init(); |
35 | | |
36 | | protected: |
37 | | virtual ~nsIDNService(); |
38 | | |
39 | | private: |
40 | | enum stringPrepFlag { |
41 | | eStringPrepForDNS, |
42 | | eStringPrepForUI, |
43 | | eStringPrepIgnoreErrors |
44 | | }; |
45 | | |
46 | | /** |
47 | | * Convert the following characters that must be recognized as label |
48 | | * separators per RFC 3490 to ASCII full stop characters |
49 | | * |
50 | | * U+3002 (ideographic full stop) |
51 | | * U+FF0E (fullwidth full stop) |
52 | | * U+FF61 (halfwidth ideographic full stop) |
53 | | */ |
54 | | void normalizeFullStops(nsAString& s); |
55 | | |
56 | | /** |
57 | | * Convert and encode a DNS label in ACE/punycode. |
58 | | * @param flag |
59 | | * if eStringPrepIgnoreErrors, all non-ASCII labels are |
60 | | * converted to punycode. |
61 | | * if eStringPrepForUI, only labels that are considered safe |
62 | | * for display are converted. |
63 | | * @see isLabelSafe |
64 | | * if eStringPrepForDNS and stringPrep finds an illegal |
65 | | * character, returns NS_FAILURE and out is empty |
66 | | */ |
67 | | nsresult stringPrepAndACE(const nsAString& in, nsACString& out, |
68 | | stringPrepFlag flag); |
69 | | |
70 | | /** |
71 | | * Convert a DNS label using the stringprep profile defined in RFC 3454 |
72 | | */ |
73 | | nsresult stringPrep(const nsAString& in, nsAString& out, stringPrepFlag flag); |
74 | | |
75 | | /** |
76 | | * Decode an ACE-encoded DNS label to UTF-8 |
77 | | * |
78 | | * @param flag |
79 | | * if eStringPrepForUI and the label is not considered safe to |
80 | | * display, the output is the same as the input |
81 | | * @see isLabelSafe |
82 | | */ |
83 | | nsresult decodeACE(const nsACString& in, nsACString& out, |
84 | | stringPrepFlag flag); |
85 | | |
86 | | /** |
87 | | * Convert complete domain names between UTF8 and ACE and vice versa |
88 | | * |
89 | | * @param flag is passed to decodeACE or stringPrepAndACE for each |
90 | | * label individually, so the output may contain some labels in |
91 | | * punycode and some in UTF-8 |
92 | | */ |
93 | | nsresult UTF8toACE(const nsACString& input, nsACString& ace, |
94 | | stringPrepFlag flag); |
95 | | nsresult ACEtoUTF8(const nsACString& input, nsACString& _retval, |
96 | | stringPrepFlag flag); |
97 | | |
98 | | bool isInWhitelist(const nsACString &host); |
99 | | void prefsChanged(const char *pref); |
100 | | |
101 | | static void PrefChanged(const char* aPref, nsIDNService* aSelf) |
102 | 0 | { |
103 | 0 | mozilla::MutexAutoLock lock(aSelf->mLock); |
104 | 0 | aSelf->prefsChanged(aPref); |
105 | 0 | } |
106 | | |
107 | | /** |
108 | | * Determine whether a label is considered safe to display to the user |
109 | | * according to the algorithm defined in UTR 39 and the profile |
110 | | * selected in mRestrictionProfile. |
111 | | * |
112 | | * For the ASCII-only profile, returns false for all labels containing |
113 | | * non-ASCII characters. |
114 | | * |
115 | | * For the other profiles, returns false for labels containing any of |
116 | | * the following: |
117 | | * |
118 | | * Characters in scripts other than the "recommended scripts" and |
119 | | * "aspirational scripts" defined in |
120 | | * http://www.unicode.org/reports/tr31/#Table_Recommended_Scripts |
121 | | * and http://www.unicode.org/reports/tr31/#Aspirational_Use_Scripts |
122 | | * This includes codepoints that are not defined as Unicode |
123 | | * characters |
124 | | * |
125 | | * Illegal combinations of scripts (@see illegalScriptCombo) |
126 | | * |
127 | | * Numbers from more than one different numbering system |
128 | | * |
129 | | * Sequences of the same non-spacing mark |
130 | | * |
131 | | * Both simplified-only and traditional-only Chinese characters |
132 | | * XXX this test was disabled by bug 857481 |
133 | | */ |
134 | | bool isLabelSafe(const nsAString &label); |
135 | | |
136 | | /** |
137 | | * Determine whether a combination of scripts in a single label is |
138 | | * permitted according to the algorithm defined in UTR 39 and the |
139 | | * profile selected in mRestrictionProfile. |
140 | | * |
141 | | * For the "Highly restrictive" profile, all characters in each |
142 | | * identifier must be from a single script, or from the combinations: |
143 | | * Latin + Han + Hiragana + Katakana; |
144 | | * Latin + Han + Bopomofo; or |
145 | | * Latin + Han + Hangul |
146 | | * |
147 | | * For the "Moderately restrictive" profile, Latin is also allowed |
148 | | * with other scripts except Cyrillic and Greek |
149 | | */ |
150 | | bool illegalScriptCombo(mozilla::unicode::Script script, |
151 | | int32_t& savedScript); |
152 | | |
153 | | /** |
154 | | * Convert a DNS label from ASCII to Unicode using IDNA2008 |
155 | | */ |
156 | | nsresult IDNA2008ToUnicode(const nsACString& input, nsAString& output); |
157 | | |
158 | | /** |
159 | | * Convert a DNS label to a normalized form conforming to IDNA2008 |
160 | | */ |
161 | | nsresult IDNA2008StringPrep(const nsAString& input, nsAString& output, |
162 | | stringPrepFlag flag); |
163 | | |
164 | | UIDNA* mIDNA; |
165 | | |
166 | | // We use this mutex to guard access to: |
167 | | // |mIDNBlacklist|, |mShowPunycode|, |mRestrictionProfile|, |
168 | | // |mIDNUseWhitelist|. |
169 | | // |
170 | | // These members can only be updated on the main thread and |
171 | | // read on any thread. Therefore, acquiring the mutex is required |
172 | | // only for threads other than the main thread. |
173 | | mozilla::Mutex mLock; |
174 | | |
175 | | // guarded by mLock |
176 | | nsString mIDNBlacklist; |
177 | | |
178 | | /** |
179 | | * Flag set by the pref network.IDN_show_punycode. When it is true, |
180 | | * IDNs containing non-ASCII characters are always displayed to the |
181 | | * user in punycode |
182 | | * |
183 | | * guarded by mLock |
184 | | */ |
185 | | bool mShowPunycode; |
186 | | |
187 | | /** |
188 | | * Restriction-level Detection profiles defined in UTR 39 |
189 | | * http://www.unicode.org/reports/tr39/#Restriction_Level_Detection, |
190 | | * and selected by the pref network.IDN.restriction_profile |
191 | | */ |
192 | | enum restrictionProfile { |
193 | | eASCIIOnlyProfile, |
194 | | eHighlyRestrictiveProfile, |
195 | | eModeratelyRestrictiveProfile |
196 | | }; |
197 | | // guarded by mLock; |
198 | | restrictionProfile mRestrictionProfile; |
199 | | // guarded by mLock; |
200 | | nsCOMPtr<nsIPrefBranch> mIDNWhitelistPrefBranch; |
201 | | // guarded by mLock |
202 | | bool mIDNUseWhitelist; |
203 | | }; |
204 | | |
205 | | #endif // nsIDNService_h__ |