3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/xmpp/jid.h"
35 #include "talk/xmpp/constants.h"
36 #include "webrtc/base/common.h"
37 #include "webrtc/base/logging.h"
44 Jid::Jid(const std::string& jid_string) {
45 if (jid_string.empty())
48 // First find the slash and slice off that part
49 size_t slash = jid_string.find('/');
50 resource_name_ = (slash == std::string::npos ? STR_EMPTY :
51 jid_string.substr(slash + 1));
53 // Now look for the node
54 size_t at = jid_string.find('@');
56 if (at < slash && at != std::string::npos) {
57 node_name_ = jid_string.substr(0, at);
58 domain_begin = at + 1;
63 // Now take what is left as the domain
64 size_t domain_length = (slash == std::string::npos) ?
65 (jid_string.length() - domain_begin) : (slash - domain_begin);
66 domain_name_ = jid_string.substr(domain_begin, domain_length);
71 Jid::Jid(const std::string& node_name,
72 const std::string& domain_name,
73 const std::string& resource_name)
74 : node_name_(node_name),
75 domain_name_(domain_name),
76 resource_name_(resource_name) {
80 void Jid::ValidateOrReset() {
85 node_name_ = PrepNode(node_name_, &valid_node);
86 domain_name_ = PrepDomain(domain_name_, &valid_domain);
87 resource_name_ = PrepResource(resource_name_, &valid_resource);
89 if (!valid_node || !valid_domain || !valid_resource) {
92 resource_name_.clear();
96 std::string Jid::Str() const {
102 if (!node_name_.empty())
103 ret = node_name_ + "@";
105 ASSERT(domain_name_ != STR_EMPTY);
108 if (!resource_name_.empty())
109 ret += "/" + resource_name_;
117 bool Jid::IsEmpty() const {
118 return (node_name_.empty() && domain_name_.empty() &&
119 resource_name_.empty());
122 bool Jid::IsValid() const {
123 return !domain_name_.empty();
126 bool Jid::IsBare() const {
128 LOG(LS_VERBOSE) << "Warning: Calling IsBare() on the empty jid.";
131 return IsValid() && resource_name_.empty();
134 bool Jid::IsFull() const {
135 return IsValid() && !resource_name_.empty();
138 Jid Jid::BareJid() const {
143 return Jid(node_name_, domain_name_, STR_EMPTY);
146 bool Jid::BareEquals(const Jid& other) const {
147 return other.node_name_ == node_name_ &&
148 other.domain_name_ == domain_name_;
151 void Jid::CopyFrom(const Jid& jid) {
152 this->node_name_ = jid.node_name_;
153 this->domain_name_ = jid.domain_name_;
154 this->resource_name_ = jid.resource_name_;
157 bool Jid::operator==(const Jid& other) const {
158 return other.node_name_ == node_name_ &&
159 other.domain_name_ == domain_name_ &&
160 other.resource_name_ == resource_name_;
163 int Jid::Compare(const Jid& other) const {
165 compare_result = node_name_.compare(other.node_name_);
166 if (0 != compare_result)
167 return compare_result;
168 compare_result = domain_name_.compare(other.domain_name_);
169 if (0 != compare_result)
170 return compare_result;
171 compare_result = resource_name_.compare(other.resource_name_);
172 return compare_result;
175 // --- JID parsing code: ---
177 // Checks and normalizes the node part of a JID.
178 std::string Jid::PrepNode(const std::string& node, bool* valid) {
182 for (std::string::const_iterator i = node.begin(); i < node.end(); ++i) {
183 bool char_valid = true;
184 unsigned char ch = *i;
186 result += PrepNodeAscii(ch, &char_valid);
189 // TODO: implement the correct stringprep protocol for these
190 result += tolower(ch);
197 if (result.length() > 1023) {
205 // Returns the appropriate mapping for an ASCII character in a node.
206 char Jid::PrepNodeAscii(char ch, bool* valid) {
209 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
210 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
211 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
212 case 'V': case 'W': case 'X': case 'Y': case 'Z':
213 return (char)(ch + ('a' - 'A'));
215 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
216 case 0x06: case 0x07: case 0x08: case 0x09: case 0x0A: case 0x0B:
217 case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11:
218 case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
219 case ' ': case '&': case '/': case ':': case '<': case '>': case '@':
220 case '\"': case '\'':
231 // Checks and normalizes the resource part of a JID.
232 std::string Jid::PrepResource(const std::string& resource, bool* valid) {
236 for (std::string::const_iterator i = resource.begin();
237 i < resource.end(); ++i) {
238 bool char_valid = true;
239 unsigned char ch = *i;
241 result += PrepResourceAscii(ch, &char_valid);
244 // TODO: implement the correct stringprep protocol for these
249 if (result.length() > 1023) {
256 // Returns the appropriate mapping for an ASCII character in a resource.
257 char Jid::PrepResourceAscii(char ch, bool* valid) {
260 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
261 case 0x06: case 0x07: case 0x08: case 0x09: case 0x0A: case 0x0B:
262 case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11:
263 case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
273 // Checks and normalizes the domain part of a JID.
274 std::string Jid::PrepDomain(const std::string& domain, bool* valid) {
278 // TODO: if the domain contains a ':', then we should parse it
279 // as an IPv6 address rather than giving an error about illegal domain.
280 PrepDomain(domain, &result, valid);
285 if (result.length() > 1023) {
293 // Checks and normalizes an IDNA domain.
294 void Jid::PrepDomain(const std::string& domain, std::string* buf, bool* valid) {
296 std::string::const_iterator last = domain.begin();
297 for (std::string::const_iterator i = domain.begin(); i < domain.end(); ++i) {
298 bool label_valid = true;
302 #if 0 // FIX: This isn't UTF-8-aware.
307 PrepDomainLabel(last, i, buf, &label_valid);
316 PrepDomainLabel(last, domain.end(), buf, valid);
319 // Checks and normalizes a domain label.
320 void Jid::PrepDomainLabel(
321 std::string::const_iterator start, std::string::const_iterator end,
322 std::string* buf, bool* valid) {
325 int start_len = static_cast<int>(buf->length());
326 for (std::string::const_iterator i = start; i < end; ++i) {
327 bool char_valid = true;
328 unsigned char ch = *i;
330 *buf += PrepDomainLabelAscii(ch, &char_valid);
333 // TODO: implement ToASCII for these
341 int count = static_cast<int>(buf->length() - start_len);
345 else if (count > 63) {
349 // Is this check needed? See comment in PrepDomainLabelAscii.
350 if ((*buf)[start_len] == '-') {
353 if ((*buf)[buf->length() - 1] == '-') {
360 // Returns the appropriate mapping for an ASCII character in a domain label.
361 char Jid::PrepDomainLabelAscii(char ch, bool* valid) {
363 // TODO: A literal reading of the spec seems to say that we do
364 // not need to check for these illegal characters (an "internationalized
365 // domain label" runs ToASCII with UseSTD3... set to false). But that
366 // can't be right. We should at least be checking that there are no '/'
367 // or '@' characters in the domain. Perhaps we should see what others
371 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
372 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
373 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
374 case 'V': case 'W': case 'X': case 'Y': case 'Z':
375 return (char)(ch + ('a' - 'A'));
377 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
378 case 0x06: case 0x07: case 0x08: case 0x09: case 0x0A: case 0x0B:
379 case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11:
380 case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
381 case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D:
382 case 0x1E: case 0x1F: case 0x20: case 0x21: case 0x22: case 0x23:
383 case 0x24: case 0x25: case 0x26: case 0x27: case 0x28: case 0x29:
384 case 0x2A: case 0x2B: case 0x2C: case 0x2E: case 0x2F: case 0x3A:
385 case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F: case 0x40:
386 case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F: case 0x60:
387 case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F: