forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMozURL.h
139 lines (125 loc) · 5.06 KB
/
MozURL.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* 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/. */
#ifndef mozURL_h__
#define mozURL_h__
#include "mozilla/UniquePtr.h"
extern "C" {
struct rusturl;
}
namespace mozilla {
namespace net {
// This class provides a thread-safe, immutable URL parser.
// As long as there is RefPtr to the object, you may use it on any thread.
// The constructor is private. One can instantiate the object by
// calling the Init() method as such:
//
// RefPtr<MozURL> url;
// nsAutoCString href("http://example.com/path?query#ref");
// nsresult rv = MozURL::Init(getter_AddRefs(url), href);
// if (NS_SUCCEEDED(rv)) { /* use url */ }
//
// When changing the URL is needed, you need to call the Mutate() method.
// This gives you a Mutator object, on which you can perform setter operations.
// Calling Finalize() on the Mutator will result in a new MozURL and a status
// code. If any of the setter operations failed, it will be reflected in the
// status code, and a null MozURL.
//
// Note: In the case of a domain name containing non-ascii characters,
// GetSpec and GetHostname will return the IDNA(punycode) version of the host.
// Also note that for now, MozURL only supports the UTF-8 charset.
class MozURL final
{
public:
static nsresult Init(MozURL** aURL, const nsACString& aSpec,
const MozURL* aBaseURL = nullptr);
nsresult GetScheme(nsACString& aScheme);
nsresult GetSpec(nsACString& aSpec);
nsresult GetUsername(nsACString& aUser);
nsresult GetPassword(nsACString& aPassword);
// Will return the hostname of URL. If the hostname is an IPv6 address,
// it will be enclosed in square brackets, such as `[::1]`
nsresult GetHostname(nsACString& aHost);
// If the URL's port number is equal to the default port, will only return the
// hostname, otherwise it will return a string of the form `{host}:{port}`
// See: https://url.spec.whatwg.org/#default-port
nsresult GetHostPort(nsACString& aHostPort);
// Will return the port number, if specified, or -1
nsresult GetPort(int32_t* aPort);
nsresult GetFilePath(nsACString& aPath);
nsresult GetQuery(nsACString& aQuery);
nsresult GetRef(nsACString& aRef);
nsresult GetOrigin(nsACString& aOrigin);
private:
explicit MozURL(rusturl* rawPtr)
: mURL(rawPtr)
{
}
virtual ~MozURL() {}
struct FreeRustURL
{
void operator()(rusturl* aPtr);
};
mozilla::UniquePtr<rusturl, FreeRustURL> mURL;
public:
class MOZ_STACK_CLASS Mutator
{
public:
// Calling this method will result in the creation of a new MozURL that
// adopts the mutator's mURL.
// If any of the setters failed with an error code, that error code will be
// returned here. It will also return an error code if Finalize is called
// more than once on the Mutator.
nsresult Finalize(MozURL** aURL);
// These setter methods will return a reference to `this` so that you may
// chain setter operations as such:
//
// RefPtr<MozURL> url2;
// nsresult rv = url->Mutate().SetHostname(NS_LITERAL_CSTRING("newhost"))
// .SetFilePath(NS_LITERAL_CSTRING("new/file/path"))
// .Finalize(getter_AddRefs(url2));
// if (NS_SUCCEEDED(rv)) { /* use url2 */ }
Mutator& SetScheme(const nsACString& aScheme);
Mutator& SetUsername(const nsACString& aUser);
Mutator& SetPassword(const nsACString& aPassword);
Mutator& SetHostname(const nsACString& aHost);
Mutator& SetHostPort(const nsACString& aHostPort);
Mutator& SetFilePath(const nsACString& aPath);
Mutator& SetQuery(const nsACString& aQuery);
Mutator& SetRef(const nsACString& aRef);
Mutator& SetPort(int32_t aPort);
// This method returns the status code of the setter operations.
// If any of the setters failed, it will return the code of the first error
// that occured. If none of the setters failed, it will return NS_OK.
// This method is useful to avoid doing expensive operations when the result
// would not be used because an error occurred. For example:
//
// RefPtr<MozURL> url2;
// MozURL::Mutator mut = url->Mutate();
// mut.SetScheme("!@#$"); // this would fail
// if (NS_SUCCEDED(mut.GetStatus())) {
// nsAutoCString host(ExpensiveComputing());
// rv = mut.SetHostname(host).Finalize(getter_AddRefs(url2));
// }
// if (NS_SUCCEEDED(rv)) { /* use url2 */ }
nsresult GetStatus() { return mStatus; }
private:
explicit Mutator(MozURL* url);
mozilla::UniquePtr<rusturl, FreeRustURL> mURL;
bool mFinalized;
nsresult mStatus;
friend class MozURL;
};
Mutator Mutate() { return Mutator(this); }
// These are used to avoid inheriting from nsISupports
public:
NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
NS_IMETHOD_(MozExternalRefCountType) Release(void);
typedef mozilla::TrueType HasThreadSafeRefCnt;
protected:
::mozilla::ThreadSafeAutoRefCnt mRefCnt;
NS_DECL_OWNINGTHREAD
};
} // namespace net
} // namespace mozilla
#endif // mozURL_h__