tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PeerIdentity.cpp (1989B)


      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 "nsNetCID.h"
     12 #include "nsNetUtil.h"
     13 #include "nsServiceManagerUtils.h"
     14 
     15 namespace mozilla {
     16 
     17 bool PeerIdentity::Equals(const PeerIdentity& aOther) const {
     18  return Equals(aOther.mPeerIdentity);
     19 }
     20 
     21 bool PeerIdentity::Equals(const nsAString& aOtherString) const {
     22  nsString user;
     23  GetUser(mPeerIdentity, user);
     24  nsString otherUser;
     25  GetUser(aOtherString, otherUser);
     26  if (user != otherUser) {
     27    return false;
     28  }
     29 
     30  nsString host;
     31  GetHost(mPeerIdentity, host);
     32  nsString otherHost;
     33  GetHost(aOtherString, otherHost);
     34 
     35  nsCString normHost;
     36  GetNormalizedHost(host, normHost);
     37  nsCString normOtherHost;
     38  GetNormalizedHost(otherHost, normOtherHost);
     39  return normHost == normOtherHost;
     40 }
     41 
     42 /* static */
     43 void PeerIdentity::GetUser(const nsAString& aPeerIdentity, nsAString& aUser) {
     44  int32_t at = aPeerIdentity.FindChar('@');
     45  if (at >= 0) {
     46    aUser = Substring(aPeerIdentity, 0, at);
     47  } else {
     48    aUser.Truncate();
     49  }
     50 }
     51 
     52 /* static */
     53 void PeerIdentity::GetHost(const nsAString& aPeerIdentity, nsAString& aHost) {
     54  int32_t at = aPeerIdentity.FindChar('@');
     55  if (at >= 0) {
     56    aHost = Substring(aPeerIdentity, at + 1);
     57  } else {
     58    aHost = aPeerIdentity;
     59  }
     60 }
     61 
     62 /* static */
     63 void PeerIdentity::GetNormalizedHost(const nsAString& aHost,
     64                                     nsACString& aNormalizedHost) {
     65  const nsCString chost = NS_ConvertUTF16toUTF8(aHost);
     66  DebugOnly<nsresult> rv = NS_DomainToASCII(chost, aNormalizedHost);
     67  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
     68                       "Failed to convert UTF-8 host to ASCII");
     69 }
     70 
     71 } /* namespace mozilla */