Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=2 sts=2 expandtab
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "PeerIdentity.h"
8 :
9 : #include "mozilla/DebugOnly.h"
10 : #include "nsCOMPtr.h"
11 : #include "nsIIDNService.h"
12 : #include "nsNetCID.h"
13 : #include "nsServiceManagerUtils.h"
14 :
15 : namespace mozilla {
16 :
17 : bool
18 0 : PeerIdentity::Equals(const PeerIdentity& aOther) const
19 : {
20 0 : return Equals(aOther.mPeerIdentity);
21 : }
22 :
23 : bool
24 0 : PeerIdentity::Equals(const nsAString& aOtherString) const
25 : {
26 0 : nsString user;
27 0 : GetUser(mPeerIdentity, user);
28 0 : nsString otherUser;
29 0 : GetUser(aOtherString, otherUser);
30 0 : if (user != otherUser) {
31 0 : return false;
32 : }
33 :
34 0 : nsString host;
35 0 : GetHost(mPeerIdentity, host);
36 0 : nsString otherHost;
37 0 : GetHost(aOtherString, otherHost);
38 :
39 : nsresult rv;
40 : nsCOMPtr<nsIIDNService> idnService
41 0 : = do_GetService("@mozilla.org/network/idn-service;1", &rv);
42 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
43 0 : return host == otherHost;
44 : }
45 :
46 0 : nsCString normHost;
47 0 : GetNormalizedHost(idnService, host, normHost);
48 0 : nsCString normOtherHost;
49 0 : GetNormalizedHost(idnService, otherHost, normOtherHost);
50 0 : return normHost == normOtherHost;
51 : }
52 :
53 : /* static */ void
54 0 : PeerIdentity::GetUser(const nsAString& aPeerIdentity, nsAString& aUser)
55 : {
56 0 : int32_t at = aPeerIdentity.FindChar('@');
57 0 : if (at >= 0) {
58 0 : aUser = Substring(aPeerIdentity, 0, at);
59 : } else {
60 0 : aUser.Truncate();
61 : }
62 0 : }
63 :
64 : /* static */ void
65 0 : PeerIdentity::GetHost(const nsAString& aPeerIdentity, nsAString& aHost)
66 : {
67 0 : int32_t at = aPeerIdentity.FindChar('@');
68 0 : if (at >= 0) {
69 0 : aHost = Substring(aPeerIdentity, at + 1);
70 : } else {
71 0 : aHost = aPeerIdentity;
72 : }
73 0 : }
74 :
75 : /* static */ void
76 0 : PeerIdentity::GetNormalizedHost(const nsCOMPtr<nsIIDNService>& aIdnService,
77 : const nsAString& aHost,
78 : nsACString& aNormalizedHost)
79 : {
80 0 : const nsCString chost = NS_ConvertUTF16toUTF8(aHost);
81 0 : DebugOnly<nsresult> rv = aIdnService->ConvertUTF8toACE(chost, aNormalizedHost);
82 0 : NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
83 : "Failed to convert UTF-8 host to ASCII");
84 0 : }
85 :
86 : } /* namespace mozilla */
|