1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=2 sts=2 expandtab
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is the
19 : * Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2008
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com> (Original author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsNullPrincipalURI.h"
41 : #include "nsNetUtil.h"
42 : #include "nsEscape.h"
43 : #include "nsCRT.h"
44 :
45 : ////////////////////////////////////////////////////////////////////////////////
46 : //// nsNullPrincipalURI
47 :
48 9282 : nsNullPrincipalURI::nsNullPrincipalURI(const nsCString &aSpec)
49 : {
50 9282 : PRInt32 dividerPosition = aSpec.FindChar(':');
51 9282 : NS_ASSERTION(dividerPosition != -1, "Malformed URI!");
52 :
53 9282 : PRInt32 n = aSpec.Left(mScheme, dividerPosition);
54 9282 : NS_ASSERTION(n == dividerPosition, "Storing the scheme failed!");
55 :
56 9282 : PRInt32 count = aSpec.Length() - dividerPosition - 1;
57 9282 : n = aSpec.Mid(mPath, dividerPosition + 1, count);
58 9282 : NS_ASSERTION(n == count, "Storing the path failed!");
59 :
60 9282 : ToLowerCase(mScheme);
61 9282 : }
62 :
63 : static NS_DEFINE_CID(kNullPrincipalURIImplementationCID,
64 : NS_NULLPRINCIPALURI_IMPLEMENTATION_CID);
65 :
66 34546 : NS_IMPL_THREADSAFE_ADDREF(nsNullPrincipalURI)
67 43809 : NS_IMPL_THREADSAFE_RELEASE(nsNullPrincipalURI)
68 :
69 28201 : NS_INTERFACE_MAP_BEGIN(nsNullPrincipalURI)
70 28201 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURI)
71 28200 : if (aIID.Equals(kNullPrincipalURIImplementationCID))
72 0 : foundInterface = static_cast<nsIURI *>(this);
73 : else
74 28200 : NS_INTERFACE_MAP_ENTRY(nsIURI)
75 8609 : NS_INTERFACE_MAP_ENTRY(nsISizeOf)
76 8609 : NS_INTERFACE_MAP_END
77 :
78 : ////////////////////////////////////////////////////////////////////////////////
79 : //// nsIURI
80 :
81 : NS_IMETHODIMP
82 2820 : nsNullPrincipalURI::GetAsciiHost(nsACString &_host)
83 : {
84 2820 : _host.Truncate();
85 2820 : return NS_OK;
86 : }
87 :
88 : NS_IMETHODIMP
89 9 : nsNullPrincipalURI::GetAsciiSpec(nsACString &_spec)
90 : {
91 18 : nsCAutoString buffer;
92 9 : (void)GetSpec(buffer);
93 9 : NS_EscapeURL(buffer, esc_OnlyNonASCII | esc_AlwaysCopy, _spec);
94 9 : return NS_OK;
95 : }
96 :
97 : NS_IMETHODIMP
98 0 : nsNullPrincipalURI::GetHost(nsACString &_host)
99 : {
100 0 : _host.Truncate();
101 0 : return NS_OK;
102 : }
103 :
104 : NS_IMETHODIMP
105 0 : nsNullPrincipalURI::SetHost(const nsACString &aHost)
106 : {
107 0 : return NS_ERROR_NOT_IMPLEMENTED;
108 : }
109 :
110 : NS_IMETHODIMP
111 0 : nsNullPrincipalURI::GetHostPort(nsACString &_host)
112 : {
113 0 : return NS_ERROR_NOT_IMPLEMENTED;
114 : }
115 :
116 : NS_IMETHODIMP
117 0 : nsNullPrincipalURI::SetHostPort(const nsACString &aHost)
118 : {
119 0 : return NS_ERROR_NOT_IMPLEMENTED;
120 : }
121 :
122 : NS_IMETHODIMP
123 0 : nsNullPrincipalURI::GetOriginCharset(nsACString &_charset)
124 : {
125 0 : _charset.Truncate();
126 0 : return NS_OK;
127 : }
128 :
129 : NS_IMETHODIMP
130 0 : nsNullPrincipalURI::GetPassword(nsACString &_password)
131 : {
132 0 : return NS_ERROR_NOT_IMPLEMENTED;
133 : }
134 :
135 : NS_IMETHODIMP
136 0 : nsNullPrincipalURI::SetPassword(const nsACString &aPassword)
137 : {
138 0 : return NS_ERROR_NOT_IMPLEMENTED;
139 : }
140 :
141 : NS_IMETHODIMP
142 0 : nsNullPrincipalURI::GetPath(nsACString &_path)
143 : {
144 0 : _path = mPath;
145 0 : return NS_OK;
146 : }
147 :
148 : NS_IMETHODIMP
149 0 : nsNullPrincipalURI::SetPath(const nsACString &aPath)
150 : {
151 0 : return NS_ERROR_NOT_IMPLEMENTED;
152 : }
153 :
154 : NS_IMETHODIMP
155 0 : nsNullPrincipalURI::GetRef(nsACString &_ref)
156 : {
157 0 : _ref.Truncate();
158 0 : return NS_ERROR_NOT_IMPLEMENTED;
159 : }
160 :
161 : NS_IMETHODIMP
162 0 : nsNullPrincipalURI::SetRef(const nsACString &aRef)
163 : {
164 0 : return NS_ERROR_NOT_IMPLEMENTED;
165 : }
166 :
167 : NS_IMETHODIMP
168 0 : nsNullPrincipalURI::GetPrePath(nsACString &_prePath)
169 : {
170 0 : _prePath = mScheme + NS_LITERAL_CSTRING(":");
171 0 : return NS_OK;
172 : }
173 :
174 : NS_IMETHODIMP
175 2820 : nsNullPrincipalURI::GetPort(PRInt32 *_port)
176 : {
177 2820 : return NS_ERROR_NOT_IMPLEMENTED;
178 : }
179 :
180 : NS_IMETHODIMP
181 0 : nsNullPrincipalURI::SetPort(PRInt32 aPort)
182 : {
183 0 : return NS_ERROR_NOT_IMPLEMENTED;
184 : }
185 :
186 : NS_IMETHODIMP
187 2828 : nsNullPrincipalURI::GetScheme(nsACString &_scheme)
188 : {
189 2828 : _scheme = mScheme;
190 2828 : return NS_OK;
191 : }
192 :
193 : NS_IMETHODIMP
194 0 : nsNullPrincipalURI::SetScheme(const nsACString &aScheme)
195 : {
196 0 : return NS_ERROR_NOT_IMPLEMENTED;
197 : }
198 :
199 : NS_IMETHODIMP
200 1167 : nsNullPrincipalURI::GetSpec(nsACString &_spec)
201 : {
202 1167 : _spec = mScheme + NS_LITERAL_CSTRING(":") + mPath;
203 1167 : return NS_OK;
204 : }
205 :
206 : // result may contain unescaped UTF-8 characters
207 : NS_IMETHODIMP
208 0 : nsNullPrincipalURI::GetSpecIgnoringRef(nsACString &result)
209 : {
210 0 : return GetSpec(result);
211 : }
212 :
213 : NS_IMETHODIMP
214 0 : nsNullPrincipalURI::GetHasRef(bool *result)
215 : {
216 0 : *result = false;
217 0 : return NS_OK;
218 : }
219 :
220 : NS_IMETHODIMP
221 0 : nsNullPrincipalURI::SetSpec(const nsACString &aSpec)
222 : {
223 0 : return NS_ERROR_NOT_IMPLEMENTED;
224 : }
225 :
226 : NS_IMETHODIMP
227 0 : nsNullPrincipalURI::GetUsername(nsACString &_username)
228 : {
229 0 : return NS_ERROR_NOT_IMPLEMENTED;
230 : }
231 :
232 : NS_IMETHODIMP
233 0 : nsNullPrincipalURI::SetUsername(const nsACString &aUsername)
234 : {
235 0 : return NS_ERROR_NOT_IMPLEMENTED;
236 : }
237 :
238 : NS_IMETHODIMP
239 0 : nsNullPrincipalURI::GetUserPass(nsACString &_userPass)
240 : {
241 0 : return NS_ERROR_NOT_IMPLEMENTED;
242 : }
243 :
244 : NS_IMETHODIMP
245 0 : nsNullPrincipalURI::SetUserPass(const nsACString &aUserPass)
246 : {
247 0 : return NS_ERROR_NOT_IMPLEMENTED;
248 : }
249 :
250 : NS_IMETHODIMP
251 5206 : nsNullPrincipalURI::Clone(nsIURI **_newURI)
252 : {
253 : nsCOMPtr<nsIURI> uri =
254 15618 : new nsNullPrincipalURI(mScheme + NS_LITERAL_CSTRING(":") + mPath);
255 5206 : uri.forget(_newURI);
256 5206 : return NS_OK;
257 : }
258 :
259 : NS_IMETHODIMP
260 0 : nsNullPrincipalURI::CloneIgnoringRef(nsIURI **_newURI)
261 : {
262 : // GetRef/SetRef not supported by nsNullPrincipalURI, so
263 : // CloneIgnoringRef() is the same as Clone().
264 0 : return Clone(_newURI);
265 : }
266 :
267 : NS_IMETHODIMP
268 0 : nsNullPrincipalURI::Equals(nsIURI *aOther, bool *_equals)
269 : {
270 0 : *_equals = false;
271 : nsNullPrincipalURI *otherURI;
272 : nsresult rv = aOther->QueryInterface(kNullPrincipalURIImplementationCID,
273 0 : (void **)&otherURI);
274 0 : if (NS_SUCCEEDED(rv)) {
275 0 : *_equals = (mScheme == otherURI->mScheme && mPath == otherURI->mPath);
276 0 : NS_RELEASE(otherURI);
277 : }
278 0 : return NS_OK;
279 : }
280 :
281 : NS_IMETHODIMP
282 0 : nsNullPrincipalURI::EqualsExceptRef(nsIURI *aOther, bool *_equals)
283 : {
284 : // GetRef/SetRef not supported by nsNullPrincipalURI, so
285 : // EqualsExceptRef() is the same as Equals().
286 0 : return Equals(aOther, _equals);
287 : }
288 :
289 : NS_IMETHODIMP
290 0 : nsNullPrincipalURI::Resolve(const nsACString &aRelativePath,
291 : nsACString &_resolvedURI)
292 : {
293 0 : _resolvedURI = aRelativePath;
294 0 : return NS_OK;
295 : }
296 :
297 : NS_IMETHODIMP
298 1233 : nsNullPrincipalURI::SchemeIs(const char *aScheme, bool *_schemeIs)
299 : {
300 1233 : *_schemeIs = (0 == nsCRT::strcasecmp(mScheme.get(), aScheme));
301 1233 : return NS_OK;
302 : }
303 :
304 : ////////////////////////////////////////////////////////////////////////////////
305 : //// nsISizeOf
306 :
307 : size_t
308 0 : nsNullPrincipalURI::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
309 : {
310 0 : return mScheme.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
311 0 : mPath.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
312 : }
313 :
314 : size_t
315 0 : nsNullPrincipalURI::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const {
316 0 : return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
317 : }
318 :
|