2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-11-16 05:09:13 +03:00
|
|
|
|
|
|
|
#ifndef nsStandardURL_h__
|
|
|
|
#define nsStandardURL_h__
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsString.h"
|
2001-11-16 05:09:13 +03:00
|
|
|
#include "nsISerializable.h"
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsIFileURL.h"
|
|
|
|
#include "nsIStandardURL.h"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsIObserver.h"
|
2001-11-16 05:09:13 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2003-07-16 23:09:00 +04:00
|
|
|
#include "nsURLHelper.h"
|
2006-03-15 07:59:42 +03:00
|
|
|
#include "nsIClassInfo.h"
|
2012-02-20 07:51:48 +04:00
|
|
|
#include "nsISizeOf.h"
|
2012-06-06 07:18:25 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-04-20 19:46:12 +03:00
|
|
|
#include "mozilla/LinkedList.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2015-09-11 09:44:00 +03:00
|
|
|
#include "nsISensitiveInfoHiddenURI.h"
|
2017-11-20 19:11:30 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2009-06-10 16:51:03 +04:00
|
|
|
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
# define DEBUG_DUMP_URLS_AT_SHUTDOWN
|
|
|
|
#endif
|
2001-11-16 05:09:13 +03:00
|
|
|
|
|
|
|
class nsIBinaryInputStream;
|
|
|
|
class nsIBinaryOutputStream;
|
2002-03-06 10:48:55 +03:00
|
|
|
class nsIIDNService;
|
2004-12-07 16:21:32 +03:00
|
|
|
class nsIPrefBranch;
|
2013-09-22 07:04:57 +04:00
|
|
|
class nsIFile;
|
|
|
|
class nsIURLParser;
|
2001-11-16 05:09:13 +03:00
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace mozilla {
|
2017-08-02 14:43:30 +03:00
|
|
|
class Encoding;
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace net {
|
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// standard URL implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsStandardURL : public nsIFileURL,
|
|
|
|
public nsIStandardURL,
|
|
|
|
public nsISerializable,
|
2002-05-03 07:01:32 +04:00
|
|
|
public nsIClassInfo,
|
2012-02-20 07:51:48 +04:00
|
|
|
public nsISizeOf,
|
2015-09-11 09:44:00 +03:00
|
|
|
public nsISensitiveInfoHiddenURI
|
2017-04-20 19:46:12 +03:00
|
|
|
#ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
|
|
|
|
,
|
|
|
|
public LinkedListElement<nsStandardURL>
|
|
|
|
#endif
|
2001-11-16 05:09:13 +03:00
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
protected:
|
|
|
|
virtual ~nsStandardURL();
|
2018-03-21 01:23:31 +03:00
|
|
|
explicit nsStandardURL(bool aSupportsFileURL = false, bool aTrackURL = true);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
public:
|
2018-03-20 17:51:17 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2001-11-16 05:09:13 +03:00
|
|
|
NS_DECL_NSIURI
|
|
|
|
NS_DECL_NSIURL
|
|
|
|
NS_DECL_NSIFILEURL
|
|
|
|
NS_DECL_NSISTANDARDURL
|
|
|
|
NS_DECL_NSISERIALIZABLE
|
2002-05-03 07:01:32 +04:00
|
|
|
NS_DECL_NSICLASSINFO
|
2015-09-11 09:44:00 +03:00
|
|
|
NS_DECL_NSISENSITIVEINFOHIDDENURI
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-02-20 07:51:48 +04:00
|
|
|
// nsISizeOf
|
2016-05-19 05:02:57 +03:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
static void InitGlobalObjects();
|
|
|
|
static void ShutdownGlobalObjects();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 11:41:25 +03:00
|
|
|
public: /* internal -- HPUX compiler can't handle this being private */
|
2002-03-06 10:48:55 +03:00
|
|
|
//
|
2001-11-16 05:09:13 +03:00
|
|
|
// location and length of an url segment relative to mSpec
|
2002-03-06 10:48:55 +03:00
|
|
|
//
|
2001-11-16 05:09:13 +03:00
|
|
|
struct URLSegment {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mPos;
|
|
|
|
int32_t mLen;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
URLSegment() : mPos(0), mLen(-1) {}
|
2012-08-22 19:56:38 +04:00
|
|
|
URLSegment(uint32_t pos, int32_t len) : mPos(pos), mLen(len) {}
|
2018-04-30 19:46:04 +03:00
|
|
|
URLSegment(const URLSegment &aCopy) = default;
|
2001-11-16 05:09:13 +03:00
|
|
|
void Reset() {
|
|
|
|
mPos = 0;
|
|
|
|
mLen = -1;
|
|
|
|
}
|
2011-09-10 13:27:29 +04:00
|
|
|
// Merge another segment following this one to it if they're contiguous
|
|
|
|
// Assumes we have something like "foo;bar" where this object is 'foo' and
|
|
|
|
// right is 'bar'.
|
|
|
|
void Merge(const nsCString &spec, const char separator,
|
|
|
|
const URLSegment &right) {
|
|
|
|
if (mLen >= 0 && *(spec.get() + mPos + mLen) == separator &&
|
|
|
|
mPos + mLen + 1 == right.mPos) {
|
|
|
|
mLen += 1 + right.mLen;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2001-11-16 05:09:13 +03:00
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
//
|
|
|
|
// URL segment encoder : performs charset conversion and URL escaping.
|
|
|
|
//
|
|
|
|
class nsSegmentEncoder {
|
|
|
|
public:
|
2017-08-02 14:43:30 +03:00
|
|
|
explicit nsSegmentEncoder(const Encoding *encoding = nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
// Encode the given segment if necessary, and return the length of
|
|
|
|
// the encoded segment. The encoded segment is appended to |buf|
|
|
|
|
// if and only if encoding is required.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t EncodeSegmentCount(const char *str, const URLSegment &segment,
|
2017-06-20 12:19:05 +03:00
|
|
|
int16_t mask, nsCString &buf, bool &appended,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t extraLen = 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
// Encode the given string if necessary, and return a reference to
|
|
|
|
// the encoded string. Returns a reference to |buf| if encoding
|
|
|
|
// is required. Otherwise, a reference to |str| is returned.
|
2017-06-20 12:18:17 +03:00
|
|
|
const nsACString &EncodeSegment(const nsACString &str, int16_t mask,
|
2017-06-20 12:19:05 +03:00
|
|
|
nsCString &buf);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
private:
|
2017-04-27 13:27:03 +03:00
|
|
|
const Encoding *mEncoding;
|
2002-03-06 10:48:55 +03:00
|
|
|
};
|
|
|
|
friend class nsSegmentEncoder;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-20 12:19:52 +03:00
|
|
|
static nsresult NormalizeIPv4(const nsACString &host, nsCString &result);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-12-02 09:59:34 +03:00
|
|
|
protected:
|
2011-05-22 05:12:45 +04:00
|
|
|
// enum used in a few places to specify how .ref attribute should be handled
|
|
|
|
enum RefHandlingEnum { eIgnoreRef, eHonorRef, eReplaceRef };
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
// Helper to share code between Equals and EqualsExceptRef
|
|
|
|
// NOTE: *not* virtual, because no one needs to override this so far...
|
|
|
|
nsresult EqualsInternal(nsIURI *unknownOther, RefHandlingEnum refHandlingMode,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *result);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-12-02 09:59:34 +03:00
|
|
|
virtual nsStandardURL *StartClone();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
// Helper to share code between Clone methods.
|
|
|
|
nsresult CloneInternal(RefHandlingEnum aRefHandlingMode,
|
2016-07-27 01:38:46 +03:00
|
|
|
const nsACString &newRef, nsIURI **aClone);
|
2014-08-11 13:14:03 +04:00
|
|
|
// Helper method that copies member variables from the source StandardURL
|
2017-07-11 20:09:10 +03:00
|
|
|
// if copyCached = true, it will also copy mFile and mDisplayHost
|
2014-08-11 13:14:03 +04:00
|
|
|
nsresult CopyMembers(nsStandardURL *source, RefHandlingEnum mode,
|
|
|
|
const nsACString &newRef, bool copyCached = false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-12-03 07:19:48 +03:00
|
|
|
// Helper for subclass implementation of GetFile(). Subclasses that map
|
|
|
|
// URIs to files in a special way should implement this method. It should
|
|
|
|
// ensure that our mFile is initialized, if it's possible.
|
2009-10-08 22:19:07 +04:00
|
|
|
// returns NS_ERROR_NO_INTERFACE if the url does not map to a file
|
2004-12-03 07:19:48 +03:00
|
|
|
virtual nsresult EnsureFile();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-06-14 14:05:43 +03:00
|
|
|
virtual nsresult Clone(nsIURI **aURI);
|
2018-02-27 02:07:48 +03:00
|
|
|
virtual nsresult SetSpecInternal(const nsACString &input);
|
|
|
|
virtual nsresult SetScheme(const nsACString &input);
|
|
|
|
virtual nsresult SetUserPass(const nsACString &input);
|
|
|
|
virtual nsresult SetUsername(const nsACString &input);
|
|
|
|
virtual nsresult SetPassword(const nsACString &input);
|
|
|
|
virtual nsresult SetHostPort(const nsACString &aValue);
|
|
|
|
virtual nsresult SetHost(const nsACString &input);
|
|
|
|
virtual nsresult SetPort(int32_t port);
|
|
|
|
virtual nsresult SetPathQueryRef(const nsACString &input);
|
|
|
|
virtual nsresult SetRef(const nsACString &input);
|
|
|
|
virtual nsresult SetFilePath(const nsACString &input);
|
|
|
|
virtual nsresult SetQuery(const nsACString &input);
|
|
|
|
virtual nsresult SetQueryWithEncoding(const nsACString &input,
|
|
|
|
const Encoding *encoding);
|
2018-03-05 06:14:44 +03:00
|
|
|
bool Deserialize(const mozilla::ipc::URIParams &);
|
2018-03-19 22:22:32 +03:00
|
|
|
nsresult ReadPrivate(nsIObjectInputStream *stream);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 11:41:25 +03:00
|
|
|
private:
|
2018-01-22 22:20:18 +03:00
|
|
|
nsresult Init(uint32_t urlType, int32_t defaultPort, const nsACString &spec,
|
|
|
|
const char *charset, nsIURI *baseURI);
|
|
|
|
nsresult SetDefaultPort(int32_t aNewDefaultPort);
|
2018-02-26 07:24:09 +03:00
|
|
|
nsresult SetFile(nsIFile *file);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-31 05:06:58 +03:00
|
|
|
nsresult SetFileNameInternal(const nsACString &input);
|
|
|
|
nsresult SetFileBaseNameInternal(const nsACString &input);
|
|
|
|
nsresult SetFileExtensionInternal(const nsACString &input);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t Port() { return mPort == -1 ? mDefaultPort : mPort; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-02-18 06:24:34 +03:00
|
|
|
void ReplacePortInSpec(int32_t aNewPort);
|
2001-11-16 05:09:13 +03:00
|
|
|
void Clear();
|
2011-09-29 10:19:26 +04:00
|
|
|
void InvalidateCache(bool invalidateCachedFile = true);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-03 22:15:30 +03:00
|
|
|
bool ValidIPv6orHostname(const char *host, uint32_t aLen);
|
2016-08-09 04:18:00 +03:00
|
|
|
static bool IsValidOfBase(unsigned char c, const uint32_t base);
|
2017-06-20 12:19:52 +03:00
|
|
|
nsresult NormalizeIDN(const nsACString &host, nsCString &result);
|
2017-08-01 01:51:48 +03:00
|
|
|
nsresult CheckIfHostIsAscii();
|
2003-07-16 23:09:00 +04:00
|
|
|
void CoalescePath(netCoalesceFlags coalesceFlag, char *path);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-05-10 11:52:19 +03:00
|
|
|
uint32_t AppendSegmentToBuf(char *, uint32_t, const char *,
|
|
|
|
const URLSegment &input, URLSegment &output,
|
|
|
|
const nsCString *esc = nullptr,
|
|
|
|
bool useEsc = false, int32_t *diff = nullptr);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t AppendToBuf(char *, uint32_t, const char *, uint32_t);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-08-02 14:43:30 +03:00
|
|
|
nsresult BuildNormalizedSpec(const char *spec, const Encoding *encoding);
|
|
|
|
nsresult SetSpecWithEncoding(const nsACString &input,
|
|
|
|
const Encoding *encoding);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool SegmentIs(const URLSegment &s1, const char *val,
|
|
|
|
bool ignoreCase = false);
|
|
|
|
bool SegmentIs(const char *spec, const URLSegment &s1, const char *val,
|
|
|
|
bool ignoreCase = false);
|
|
|
|
bool SegmentIs(const URLSegment &s1, const char *val, const URLSegment &s2,
|
|
|
|
bool ignoreCase = false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t ReplaceSegment(uint32_t pos, uint32_t len, const char *val,
|
|
|
|
uint32_t valLen);
|
|
|
|
int32_t ReplaceSegment(uint32_t pos, uint32_t len, const nsACString &val);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult ParseURL(const char *spec, int32_t specLen);
|
|
|
|
nsresult ParsePath(const char *spec, uint32_t pathPos, int32_t pathLen = -1);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
char *AppendToSubstring(uint32_t pos, int32_t len, const char *tail);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
// dependent substring helpers
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsDependentCSubstring Segment(uint32_t pos, int32_t len); // see below
|
2004-02-19 05:44:03 +03:00
|
|
|
const nsDependentCSubstring Segment(const URLSegment &s) {
|
|
|
|
return Segment(s.mPos, s.mLen);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
// dependent substring getters
|
2004-02-19 05:44:03 +03:00
|
|
|
const nsDependentCSubstring Prepath(); // see below
|
|
|
|
const nsDependentCSubstring Scheme() { return Segment(mScheme); }
|
2011-09-29 10:19:26 +04:00
|
|
|
const nsDependentCSubstring Userpass(bool includeDelim = false); // see below
|
2004-02-19 05:44:03 +03:00
|
|
|
const nsDependentCSubstring Username() { return Segment(mUsername); }
|
|
|
|
const nsDependentCSubstring Password() { return Segment(mPassword); }
|
|
|
|
const nsDependentCSubstring Hostport(); // see below
|
|
|
|
const nsDependentCSubstring Host(); // see below
|
|
|
|
const nsDependentCSubstring Path() { return Segment(mPath); }
|
|
|
|
const nsDependentCSubstring Filepath() { return Segment(mFilepath); }
|
|
|
|
const nsDependentCSubstring Directory() { return Segment(mDirectory); }
|
|
|
|
const nsDependentCSubstring Filename(); // see below
|
|
|
|
const nsDependentCSubstring Basename() { return Segment(mBasename); }
|
|
|
|
const nsDependentCSubstring Extension() { return Segment(mExtension); }
|
|
|
|
const nsDependentCSubstring Query() { return Segment(mQuery); }
|
|
|
|
const nsDependentCSubstring Ref() { return Segment(mRef); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
// shift the URLSegments to the right by diff
|
2016-05-10 11:52:19 +03:00
|
|
|
void ShiftFromAuthority(int32_t diff);
|
|
|
|
void ShiftFromUsername(int32_t diff);
|
|
|
|
void ShiftFromPassword(int32_t diff);
|
|
|
|
void ShiftFromHost(int32_t diff);
|
|
|
|
void ShiftFromPath(int32_t diff);
|
|
|
|
void ShiftFromFilepath(int32_t diff);
|
|
|
|
void ShiftFromDirectory(int32_t diff);
|
|
|
|
void ShiftFromBasename(int32_t diff);
|
|
|
|
void ShiftFromExtension(int32_t diff);
|
|
|
|
void ShiftFromQuery(int32_t diff);
|
|
|
|
void ShiftFromRef(int32_t diff);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
// fastload helper functions
|
|
|
|
nsresult ReadSegment(nsIBinaryInputStream *, URLSegment &);
|
|
|
|
nsresult WriteSegment(nsIBinaryOutputStream *, const URLSegment &);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-06-10 15:53:54 +04:00
|
|
|
void FindHostLimit(nsACString::const_iterator &aStart,
|
|
|
|
nsACString::const_iterator &aEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
// mSpec contains the normalized version of the URL spec (UTF-8 encoded).
|
2001-11-16 05:09:13 +03:00
|
|
|
nsCString mSpec;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mDefaultPort;
|
|
|
|
int32_t mPort;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
// url parts (relative to mSpec)
|
|
|
|
URLSegment mScheme;
|
|
|
|
URLSegment mAuthority;
|
|
|
|
URLSegment mUsername;
|
|
|
|
URLSegment mPassword;
|
2002-03-06 10:48:55 +03:00
|
|
|
URLSegment mHost;
|
2001-11-16 05:09:13 +03:00
|
|
|
URLSegment mPath;
|
2002-03-06 10:48:55 +03:00
|
|
|
URLSegment mFilepath;
|
2001-11-16 05:09:13 +03:00
|
|
|
URLSegment mDirectory;
|
|
|
|
URLSegment mBasename;
|
|
|
|
URLSegment mExtension;
|
|
|
|
URLSegment mQuery;
|
|
|
|
URLSegment mRef;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURLParser> mParser;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-12-03 07:19:48 +03:00
|
|
|
// mFile is protected so subclasses can access it directly
|
|
|
|
protected:
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIFile> mFile; // cached result for nsIFileURL::GetFile
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-12-03 07:19:48 +03:00
|
|
|
private:
|
2017-07-11 20:09:10 +03:00
|
|
|
// cached result for nsIURI::GetDisplayHost
|
|
|
|
nsCString mDisplayHost;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
enum { eEncoding_Unknown, eEncoding_ASCII, eEncoding_UTF8 };
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mURLType : 2; // nsIStandardURL::URLTYPE_xxx
|
|
|
|
uint32_t mSupportsFileURL : 1; // QI to nsIFileURL?
|
2017-07-11 20:09:10 +03:00
|
|
|
uint32_t mCheckedIfHostA : 1; // If set to true, it means either that
|
|
|
|
// mDisplayHost has a been initialized, or
|
|
|
|
// that the hostname is not punycode
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
// global objects. don't use COMPtr as its destructor will cause a
|
|
|
|
// coredump if we leak it.
|
2005-04-27 05:32:36 +04:00
|
|
|
static nsIIDNService *gIDN;
|
2017-06-13 18:23:11 +03:00
|
|
|
static const char gHostLimitDigits[];
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gInitialized;
|
2017-07-11 20:10:10 +03:00
|
|
|
static bool gPunycodeHost;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2009-06-10 16:51:03 +04:00
|
|
|
public:
|
|
|
|
#ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
|
|
|
|
void PrintSpec() const { printf(" %s\n", mSpec.get()); }
|
|
|
|
#endif
|
2017-11-20 19:11:30 +03:00
|
|
|
|
|
|
|
public:
|
2018-01-24 22:33:06 +03:00
|
|
|
// We make this implementation a template so that we can avoid writing
|
|
|
|
// the same code for SubstitutingURL (which extends nsStandardURL)
|
|
|
|
template <class T>
|
|
|
|
class TemplatedMutator : public nsIURIMutator,
|
|
|
|
public BaseURIMutator<T>,
|
2018-01-24 01:13:04 +03:00
|
|
|
public nsIStandardURLMutator,
|
2018-01-31 04:32:27 +03:00
|
|
|
public nsIURLMutator,
|
2018-02-01 00:40:45 +03:00
|
|
|
public nsIFileURLMutator,
|
2018-03-19 22:22:32 +03:00
|
|
|
public nsISerializable {
|
2018-01-24 22:33:06 +03:00
|
|
|
NS_FORWARD_SAFE_NSIURISETTERS_RET(BaseURIMutator<T>::mURI)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 22:33:06 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD
|
|
|
|
Deserialize(const mozilla::ipc::URIParams &aParams) override {
|
|
|
|
return BaseURIMutator<T>::InitFromIPCParams(aParams);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-19 22:22:32 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
Write(nsIObjectOutputStream *aOutputStream) override {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
|
2018-03-19 22:22:32 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 22:33:06 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD Read(nsIObjectInputStream *aStream) override {
|
|
|
|
return BaseURIMutator<T>::InitFromInputStream(aStream);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 22:33:06 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD Finalize(nsIURI **aURI) override {
|
|
|
|
BaseURIMutator<T>::mURI.forget(aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 22:33:06 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD SetSpec(const nsACString &aSpec,
|
|
|
|
nsIURIMutator **aMutator) override {
|
|
|
|
if (aMutator) {
|
|
|
|
nsCOMPtr<nsIURIMutator> mutator = this;
|
|
|
|
mutator.forget(aMutator);
|
|
|
|
}
|
|
|
|
return BaseURIMutator<T>::InitFromSpec(aSpec);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 01:13:04 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD Init(uint32_t aURLType, int32_t aDefaultPort,
|
|
|
|
const nsACString &aSpec, const char *aCharset,
|
|
|
|
nsIURI *aBaseURI,
|
|
|
|
nsIURIMutator **aMutator) override {
|
|
|
|
if (aMutator) {
|
|
|
|
nsCOMPtr<nsIURIMutator> mutator = this;
|
|
|
|
mutator.forget(aMutator);
|
|
|
|
}
|
2018-01-24 22:33:06 +03:00
|
|
|
RefPtr<T> uri;
|
|
|
|
if (BaseURIMutator<T>::mURI) {
|
|
|
|
// We don't need a new URI object if we already have one
|
|
|
|
BaseURIMutator<T>::mURI.swap(uri);
|
2018-01-24 01:13:04 +03:00
|
|
|
} else {
|
2018-03-20 14:52:43 +03:00
|
|
|
uri = Create();
|
2018-01-24 01:13:04 +03:00
|
|
|
}
|
|
|
|
nsresult rv =
|
|
|
|
uri->Init(aURLType, aDefaultPort, aSpec, aCharset, aBaseURI);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2018-02-16 18:31:43 +03:00
|
|
|
BaseURIMutator<T>::mURI = uri.forget();
|
2018-01-24 01:13:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 01:13:04 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHODIMP
|
|
|
|
SetDefaultPort(int32_t aNewDefaultPort, nsIURIMutator **aMutator) override {
|
2018-01-24 22:33:06 +03:00
|
|
|
if (!BaseURIMutator<T>::mURI) {
|
2018-01-24 01:13:04 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aMutator) {
|
|
|
|
nsCOMPtr<nsIURIMutator> mutator = this;
|
|
|
|
mutator.forget(aMutator);
|
|
|
|
}
|
2018-01-24 22:33:06 +03:00
|
|
|
return BaseURIMutator<T>::mURI->SetDefaultPort(aNewDefaultPort);
|
2018-01-24 01:13:04 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-31 04:32:27 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD SetFileName(const nsACString &aFileName,
|
|
|
|
nsIURIMutator **aMutator) override {
|
|
|
|
if (!BaseURIMutator<T>::mURI) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aMutator) {
|
|
|
|
nsCOMPtr<nsIURIMutator> mutator = this;
|
|
|
|
mutator.forget(aMutator);
|
|
|
|
}
|
2018-01-31 05:06:58 +03:00
|
|
|
return BaseURIMutator<T>::mURI->SetFileNameInternal(aFileName);
|
2018-01-31 04:32:27 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-31 04:32:27 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD SetFileBaseName(const nsACString &aFileBaseName,
|
|
|
|
nsIURIMutator **aMutator) override {
|
|
|
|
if (!BaseURIMutator<T>::mURI) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aMutator) {
|
|
|
|
nsCOMPtr<nsIURIMutator> mutator = this;
|
|
|
|
mutator.forget(aMutator);
|
|
|
|
}
|
2018-01-31 05:06:58 +03:00
|
|
|
return BaseURIMutator<T>::mURI->SetFileBaseNameInternal(aFileBaseName);
|
2018-01-31 04:32:27 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-31 04:32:27 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD SetFileExtension(
|
|
|
|
const nsACString &aFileExtension, nsIURIMutator **aMutator) override {
|
|
|
|
if (!BaseURIMutator<T>::mURI) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aMutator) {
|
|
|
|
nsCOMPtr<nsIURIMutator> mutator = this;
|
|
|
|
mutator.forget(aMutator);
|
|
|
|
}
|
2018-01-31 05:06:58 +03:00
|
|
|
return BaseURIMutator<T>::mURI->SetFileExtensionInternal(aFileExtension);
|
2018-01-31 04:32:27 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-20 14:52:43 +03:00
|
|
|
T *Create() override { return new T(mMarkedFileURL); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-20 14:52:43 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD MarkFileURL() override {
|
|
|
|
mMarkedFileURL = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-01 00:40:45 +03:00
|
|
|
MOZ_MUST_USE NS_IMETHOD SetFile(nsIFile *aFile) override {
|
|
|
|
RefPtr<T> uri;
|
|
|
|
if (BaseURIMutator<T>::mURI) {
|
|
|
|
// We don't need a new URI object if we already have one
|
|
|
|
BaseURIMutator<T>::mURI.swap(uri);
|
|
|
|
} else {
|
2018-04-30 19:46:04 +03:00
|
|
|
uri = new T(/* aSupportsFileURL = */ true);
|
2018-02-01 00:40:45 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-01 00:40:45 +03:00
|
|
|
nsresult rv = uri->SetFile(aFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2018-01-31 05:06:58 +03:00
|
|
|
BaseURIMutator<T>::mURI.swap(uri);
|
2018-02-01 00:40:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-06-14 11:19:07 +03:00
|
|
|
explicit TemplatedMutator() : mMarkedFileURL(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~TemplatedMutator() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-20 14:52:43 +03:00
|
|
|
bool mMarkedFileURL = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 22:33:06 +03:00
|
|
|
friend T;
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 22:33:06 +03:00
|
|
|
class Mutator final : public TemplatedMutator<nsStandardURL> {
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
public:
|
|
|
|
explicit Mutator() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-24 22:33:06 +03:00
|
|
|
private:
|
|
|
|
virtual ~Mutator() = default;
|
2017-11-20 19:11:30 +03:00
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-27 02:07:48 +03:00
|
|
|
friend BaseURIMutator<nsStandardURL>;
|
2001-11-16 05:09:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NS_THIS_STANDARDURL_IMPL_CID \
|
|
|
|
{ /* b8e3e97b-1ccd-4b45-af5a-79596770f5d7 */ \
|
|
|
|
0xb8e3e97b, 0x1ccd, 0x4b45, { \
|
|
|
|
0xaf, 0x5a, 0x79, 0x59, 0x67, 0x70, 0xf5, 0xd7 \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Dependent substring getters
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
inline const nsDependentCSubstring nsStandardURL::Segment(uint32_t pos,
|
|
|
|
int32_t len) {
|
2002-03-06 10:48:55 +03:00
|
|
|
if (len < 0) {
|
|
|
|
pos = 0;
|
|
|
|
len = 0;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
return Substring(mSpec, pos, uint32_t(len));
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline const nsDependentCSubstring nsStandardURL::Prepath() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = 0;
|
2002-03-06 10:48:55 +03:00
|
|
|
if (mAuthority.mLen >= 0) len = mAuthority.mPos + mAuthority.mLen;
|
|
|
|
return Substring(mSpec, 0, len);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
inline const nsDependentCSubstring nsStandardURL::Userpass(bool includeDelim) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t pos = 0, len = 0;
|
2018-11-14 22:10:42 +03:00
|
|
|
if (mUsername.mLen > 0 || mPassword.mLen > 0) {
|
|
|
|
if (mUsername.mLen > 0) {
|
|
|
|
pos = mUsername.mPos;
|
|
|
|
len = mUsername.mLen;
|
|
|
|
if (mPassword.mLen >= 0) {
|
|
|
|
len += (mPassword.mLen + 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pos = mPassword.mPos - 1;
|
|
|
|
len = mPassword.mLen + 1;
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
if (includeDelim) len++;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
return Substring(mSpec, pos, len);
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline const nsDependentCSubstring nsStandardURL::Hostport() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t pos = 0, len = 0;
|
2002-03-06 10:48:55 +03:00
|
|
|
if (mAuthority.mLen > 0) {
|
|
|
|
pos = mHost.mPos;
|
|
|
|
len = mAuthority.mPos + mAuthority.mLen - pos;
|
|
|
|
}
|
|
|
|
return Substring(mSpec, pos, len);
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline const nsDependentCSubstring nsStandardURL::Host() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t pos = 0, len = 0;
|
2002-03-06 10:48:55 +03:00
|
|
|
if (mHost.mLen > 0) {
|
|
|
|
pos = mHost.mPos;
|
|
|
|
len = mHost.mLen;
|
2002-05-16 00:07:48 +04:00
|
|
|
if (mSpec.CharAt(pos) == '[' && mSpec.CharAt(pos + len - 1) == ']') {
|
2002-03-06 10:48:55 +03:00
|
|
|
pos++;
|
|
|
|
len -= 2;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
return Substring(mSpec, pos, len);
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline const nsDependentCSubstring nsStandardURL::Filename() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t pos = 0, len = 0;
|
2002-03-06 10:48:55 +03:00
|
|
|
// if there is no basename, then there can be no extension
|
|
|
|
if (mBasename.mLen > 0) {
|
|
|
|
pos = mBasename.mPos;
|
|
|
|
len = mBasename.mLen;
|
|
|
|
if (mExtension.mLen >= 0) len += (mExtension.mLen + 1);
|
|
|
|
}
|
|
|
|
return Substring(mSpec, pos, len);
|
|
|
|
}
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
#endif // nsStandardURL_h__
|