nsSimpleURI.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/DebugOnly.h"
  6. #undef LOG
  7. #include "IPCMessageUtils.h"
  8. #include "nsSimpleURI.h"
  9. #include "nscore.h"
  10. #include "nsString.h"
  11. #include "plstr.h"
  12. #include "nsURLHelper.h"
  13. #include "nsNetCID.h"
  14. #include "nsIObjectInputStream.h"
  15. #include "nsIObjectOutputStream.h"
  16. #include "nsEscape.h"
  17. #include "nsError.h"
  18. #include "nsIIPCSerializableURI.h"
  19. #include "mozilla/MemoryReporting.h"
  20. #include "mozilla/ipc/URIUtils.h"
  21. using namespace mozilla::ipc;
  22. namespace mozilla {
  23. namespace net {
  24. static NS_DEFINE_CID(kThisSimpleURIImplementationCID,
  25. NS_THIS_SIMPLEURI_IMPLEMENTATION_CID);
  26. static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
  27. ////////////////////////////////////////////////////////////////////////////////
  28. // nsSimpleURI methods:
  29. nsSimpleURI::nsSimpleURI()
  30. : mMutable(true)
  31. , mIsRefValid(false)
  32. , mIsQueryValid(false)
  33. {
  34. }
  35. nsSimpleURI::~nsSimpleURI()
  36. {
  37. }
  38. NS_IMPL_ADDREF(nsSimpleURI)
  39. NS_IMPL_RELEASE(nsSimpleURI)
  40. NS_INTERFACE_TABLE_HEAD(nsSimpleURI)
  41. NS_INTERFACE_TABLE(nsSimpleURI, nsIURI, nsISerializable,
  42. nsIClassInfo, nsIMutable, nsIIPCSerializableURI)
  43. NS_INTERFACE_TABLE_TO_MAP_SEGUE
  44. if (aIID.Equals(kThisSimpleURIImplementationCID))
  45. foundInterface = static_cast<nsIURI*>(this);
  46. else
  47. NS_INTERFACE_MAP_ENTRY(nsISizeOf)
  48. NS_INTERFACE_MAP_END
  49. ////////////////////////////////////////////////////////////////////////////////
  50. // nsISerializable methods:
  51. NS_IMETHODIMP
  52. nsSimpleURI::Read(nsIObjectInputStream* aStream)
  53. {
  54. nsresult rv;
  55. bool isMutable;
  56. rv = aStream->ReadBoolean(&isMutable);
  57. if (NS_FAILED(rv)) return rv;
  58. mMutable = isMutable;
  59. rv = aStream->ReadCString(mScheme);
  60. if (NS_FAILED(rv)) return rv;
  61. rv = aStream->ReadCString(mPath);
  62. if (NS_FAILED(rv)) return rv;
  63. bool isRefValid;
  64. rv = aStream->ReadBoolean(&isRefValid);
  65. if (NS_FAILED(rv)) return rv;
  66. mIsRefValid = isRefValid;
  67. if (isRefValid) {
  68. rv = aStream->ReadCString(mRef);
  69. if (NS_FAILED(rv)) return rv;
  70. } else {
  71. mRef.Truncate(); // invariant: mRef should be empty when it's not valid
  72. }
  73. bool isQueryValid;
  74. rv = aStream->ReadBoolean(&isQueryValid);
  75. if (NS_FAILED(rv)) return rv;
  76. mIsQueryValid = isQueryValid;
  77. if (isQueryValid) {
  78. rv = aStream->ReadCString(mQuery);
  79. if (NS_FAILED(rv)) return rv;
  80. } else {
  81. mQuery.Truncate(); // invariant: mQuery should be empty when it's not valid
  82. }
  83. return NS_OK;
  84. }
  85. NS_IMETHODIMP
  86. nsSimpleURI::Write(nsIObjectOutputStream* aStream)
  87. {
  88. nsresult rv;
  89. rv = aStream->WriteBoolean(mMutable);
  90. if (NS_FAILED(rv)) return rv;
  91. rv = aStream->WriteStringZ(mScheme.get());
  92. if (NS_FAILED(rv)) return rv;
  93. rv = aStream->WriteStringZ(mPath.get());
  94. if (NS_FAILED(rv)) return rv;
  95. rv = aStream->WriteBoolean(mIsRefValid);
  96. if (NS_FAILED(rv)) return rv;
  97. if (mIsRefValid) {
  98. rv = aStream->WriteStringZ(mRef.get());
  99. if (NS_FAILED(rv)) return rv;
  100. }
  101. rv = aStream->WriteBoolean(mIsQueryValid);
  102. if (NS_FAILED(rv)) return rv;
  103. if (mIsQueryValid) {
  104. rv = aStream->WriteStringZ(mQuery.get());
  105. if (NS_FAILED(rv)) return rv;
  106. }
  107. return NS_OK;
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // nsIIPCSerializableURI methods:
  111. void
  112. nsSimpleURI::Serialize(URIParams& aParams)
  113. {
  114. SimpleURIParams params;
  115. params.scheme() = mScheme;
  116. params.path() = mPath;
  117. if (mIsRefValid) {
  118. params.ref() = mRef;
  119. } else {
  120. params.ref().SetIsVoid(true);
  121. }
  122. if (mIsQueryValid) {
  123. params.query() = mQuery;
  124. } else {
  125. params.query().SetIsVoid(true);
  126. }
  127. params.isMutable() = mMutable;
  128. aParams = params;
  129. }
  130. bool
  131. nsSimpleURI::Deserialize(const URIParams& aParams)
  132. {
  133. if (aParams.type() != URIParams::TSimpleURIParams) {
  134. NS_ERROR("Received unknown parameters from the other process!");
  135. return false;
  136. }
  137. const SimpleURIParams& params = aParams.get_SimpleURIParams();
  138. mScheme = params.scheme();
  139. mPath = params.path();
  140. if (params.ref().IsVoid()) {
  141. mRef.Truncate();
  142. mIsRefValid = false;
  143. } else {
  144. mRef = params.ref();
  145. mIsRefValid = true;
  146. }
  147. if (params.query().IsVoid()) {
  148. mQuery.Truncate();
  149. mIsQueryValid = false;
  150. } else {
  151. mQuery = params.query();
  152. mIsQueryValid = true;
  153. }
  154. mMutable = params.isMutable();
  155. return true;
  156. }
  157. ////////////////////////////////////////////////////////////////////////////////
  158. // nsIURI methods:
  159. NS_IMETHODIMP
  160. nsSimpleURI::GetSpec(nsACString &result)
  161. {
  162. if (!result.Assign(mScheme, fallible) ||
  163. !result.Append(NS_LITERAL_CSTRING(":"), fallible) ||
  164. !result.Append(mPath, fallible)) {
  165. return NS_ERROR_OUT_OF_MEMORY;
  166. }
  167. if (mIsQueryValid) {
  168. if (!result.Append(NS_LITERAL_CSTRING("?"), fallible) ||
  169. !result.Append(mQuery, fallible)) {
  170. return NS_ERROR_OUT_OF_MEMORY;
  171. }
  172. } else {
  173. MOZ_ASSERT(mQuery.IsEmpty(), "mIsQueryValid/mQuery invariant broken");
  174. }
  175. if (mIsRefValid) {
  176. if (!result.Append(NS_LITERAL_CSTRING("#"), fallible) ||
  177. !result.Append(mRef, fallible)) {
  178. return NS_ERROR_OUT_OF_MEMORY;
  179. }
  180. } else {
  181. MOZ_ASSERT(mRef.IsEmpty(), "mIsRefValid/mRef invariant broken");
  182. }
  183. return NS_OK;
  184. }
  185. // result may contain unescaped UTF-8 characters
  186. NS_IMETHODIMP
  187. nsSimpleURI::GetSpecIgnoringRef(nsACString &result)
  188. {
  189. result = mScheme + NS_LITERAL_CSTRING(":") + mPath;
  190. if (mIsQueryValid) {
  191. result += NS_LITERAL_CSTRING("?") + mQuery;
  192. }
  193. return NS_OK;
  194. }
  195. NS_IMETHODIMP
  196. nsSimpleURI::GetHasRef(bool *result)
  197. {
  198. *result = mIsRefValid;
  199. return NS_OK;
  200. }
  201. NS_IMETHODIMP
  202. nsSimpleURI::SetSpec(const nsACString &aSpec)
  203. {
  204. NS_ENSURE_STATE(mMutable);
  205. // filter out unexpected chars "\r\n\t" if necessary
  206. nsAutoCString filteredSpec;
  207. net_FilterURIString(aSpec, filteredSpec);
  208. // nsSimpleURI currently restricts the charset to US-ASCII
  209. nsAutoCString spec;
  210. nsresult rv = NS_EscapeURL(filteredSpec, esc_OnlyNonASCII, spec, fallible);
  211. if (NS_FAILED(rv)) {
  212. return rv;
  213. }
  214. int32_t colonPos = spec.FindChar(':');
  215. if (colonPos < 0 || !net_IsValidScheme(spec.get(), colonPos))
  216. return NS_ERROR_MALFORMED_URI;
  217. mScheme.Truncate();
  218. DebugOnly<int32_t> n = spec.Left(mScheme, colonPos);
  219. NS_ASSERTION(n == colonPos, "Left failed");
  220. ToLowerCase(mScheme);
  221. // This sets mPath, mQuery and mRef.
  222. return SetPath(Substring(spec, colonPos + 1));
  223. }
  224. NS_IMETHODIMP
  225. nsSimpleURI::GetScheme(nsACString &result)
  226. {
  227. result = mScheme;
  228. return NS_OK;
  229. }
  230. NS_IMETHODIMP
  231. nsSimpleURI::SetScheme(const nsACString &scheme)
  232. {
  233. NS_ENSURE_STATE(mMutable);
  234. const nsPromiseFlatCString &flat = PromiseFlatCString(scheme);
  235. if (!net_IsValidScheme(flat)) {
  236. NS_WARNING("the given url scheme contains invalid characters");
  237. return NS_ERROR_MALFORMED_URI;
  238. }
  239. mScheme = scheme;
  240. ToLowerCase(mScheme);
  241. return NS_OK;
  242. }
  243. NS_IMETHODIMP
  244. nsSimpleURI::GetPrePath(nsACString &result)
  245. {
  246. result = mScheme + NS_LITERAL_CSTRING(":");
  247. return NS_OK;
  248. }
  249. NS_IMETHODIMP
  250. nsSimpleURI::GetUserPass(nsACString &result)
  251. {
  252. return NS_ERROR_FAILURE;
  253. }
  254. NS_IMETHODIMP
  255. nsSimpleURI::SetUserPass(const nsACString &userPass)
  256. {
  257. return NS_ERROR_FAILURE;
  258. }
  259. NS_IMETHODIMP
  260. nsSimpleURI::GetUsername(nsACString &result)
  261. {
  262. return NS_ERROR_FAILURE;
  263. }
  264. NS_IMETHODIMP
  265. nsSimpleURI::SetUsername(const nsACString &userName)
  266. {
  267. NS_ENSURE_STATE(mMutable);
  268. return NS_ERROR_FAILURE;
  269. }
  270. NS_IMETHODIMP
  271. nsSimpleURI::GetPassword(nsACString &result)
  272. {
  273. return NS_ERROR_FAILURE;
  274. }
  275. NS_IMETHODIMP
  276. nsSimpleURI::SetPassword(const nsACString &password)
  277. {
  278. NS_ENSURE_STATE(mMutable);
  279. return NS_ERROR_FAILURE;
  280. }
  281. NS_IMETHODIMP
  282. nsSimpleURI::GetHostPort(nsACString &result)
  283. {
  284. // Note: Audit all callers before changing this to return an empty
  285. // string -- CAPS and UI code may depend on this throwing.
  286. // Note: If this is changed, change GetAsciiHostPort as well.
  287. return NS_ERROR_FAILURE;
  288. }
  289. NS_IMETHODIMP
  290. nsSimpleURI::SetHostPort(const nsACString &result)
  291. {
  292. NS_ENSURE_STATE(mMutable);
  293. return NS_ERROR_FAILURE;
  294. }
  295. NS_IMETHODIMP
  296. nsSimpleURI::SetHostAndPort(const nsACString &result)
  297. {
  298. NS_ENSURE_STATE(mMutable);
  299. return NS_ERROR_FAILURE;
  300. }
  301. NS_IMETHODIMP
  302. nsSimpleURI::GetHost(nsACString &result)
  303. {
  304. // Note: Audit all callers before changing this to return an empty
  305. // string -- CAPS and UI code depend on this throwing.
  306. return NS_ERROR_FAILURE;
  307. }
  308. NS_IMETHODIMP
  309. nsSimpleURI::SetHost(const nsACString &host)
  310. {
  311. NS_ENSURE_STATE(mMutable);
  312. return NS_ERROR_FAILURE;
  313. }
  314. NS_IMETHODIMP
  315. nsSimpleURI::GetPort(int32_t *result)
  316. {
  317. // Note: Audit all callers before changing this to return an empty
  318. // string -- CAPS and UI code may depend on this throwing.
  319. return NS_ERROR_FAILURE;
  320. }
  321. NS_IMETHODIMP
  322. nsSimpleURI::SetPort(int32_t port)
  323. {
  324. NS_ENSURE_STATE(mMutable);
  325. return NS_ERROR_FAILURE;
  326. }
  327. NS_IMETHODIMP
  328. nsSimpleURI::GetPath(nsACString &result)
  329. {
  330. result = mPath;
  331. if (mIsQueryValid) {
  332. result += NS_LITERAL_CSTRING("?") + mQuery;
  333. }
  334. if (mIsRefValid) {
  335. result += NS_LITERAL_CSTRING("#") + mRef;
  336. }
  337. return NS_OK;
  338. }
  339. NS_IMETHODIMP
  340. nsSimpleURI::SetPath(const nsACString &aPath)
  341. {
  342. NS_ENSURE_STATE(mMutable);
  343. nsAutoCString path;
  344. if (!path.Assign(aPath, fallible)) {
  345. return NS_ERROR_OUT_OF_MEMORY;
  346. }
  347. int32_t queryPos = path.FindChar('?');
  348. int32_t hashPos = path.FindChar('#');
  349. if (queryPos != kNotFound && hashPos != kNotFound && hashPos < queryPos) {
  350. queryPos = kNotFound;
  351. }
  352. nsAutoCString query;
  353. if (queryPos != kNotFound) {
  354. query.Assign(Substring(path, queryPos));
  355. path.Truncate(queryPos);
  356. }
  357. nsAutoCString hash;
  358. if (hashPos != kNotFound) {
  359. if (query.IsEmpty()) {
  360. hash.Assign(Substring(path, hashPos));
  361. path.Truncate(hashPos);
  362. } else {
  363. // We have to search the hash character in the query
  364. hashPos = query.FindChar('#');
  365. hash.Assign(Substring(query, hashPos));
  366. query.Truncate(hashPos);
  367. }
  368. }
  369. mIsQueryValid = false;
  370. mQuery.Truncate();
  371. mIsRefValid = false;
  372. mRef.Truncate();
  373. // The path
  374. if (!mPath.Assign(path, fallible)) {
  375. return NS_ERROR_OUT_OF_MEMORY;
  376. }
  377. nsresult rv = SetQuery(query);
  378. if (NS_FAILED(rv)) {
  379. return rv;
  380. }
  381. return SetRef(hash);
  382. }
  383. NS_IMETHODIMP
  384. nsSimpleURI::GetRef(nsACString &result)
  385. {
  386. if (!mIsRefValid) {
  387. MOZ_ASSERT(mRef.IsEmpty(), "mIsRefValid/mRef invariant broken");
  388. result.Truncate();
  389. } else {
  390. result = mRef;
  391. }
  392. return NS_OK;
  393. }
  394. // NOTE: SetRef("") removes our ref, whereas SetRef("#") sets it to the empty
  395. // string (and will result in .spec and .path having a terminal #).
  396. NS_IMETHODIMP
  397. nsSimpleURI::SetRef(const nsACString &aRef)
  398. {
  399. NS_ENSURE_STATE(mMutable);
  400. nsAutoCString ref;
  401. nsresult rv = NS_EscapeURL(aRef, esc_OnlyNonASCII, ref, fallible);
  402. if (NS_FAILED(rv)) {
  403. return rv;
  404. }
  405. if (ref.IsEmpty()) {
  406. // Empty string means to remove ref completely.
  407. mIsRefValid = false;
  408. mRef.Truncate(); // invariant: mRef should be empty when it's not valid
  409. return NS_OK;
  410. }
  411. mIsRefValid = true;
  412. // Gracefully skip initial hash
  413. if (ref[0] == '#') {
  414. mRef = Substring(ref, 1);
  415. } else {
  416. mRef = ref;
  417. }
  418. return NS_OK;
  419. }
  420. NS_IMETHODIMP
  421. nsSimpleURI::Equals(nsIURI* other, bool *result)
  422. {
  423. return EqualsInternal(other, eHonorRef, result);
  424. }
  425. NS_IMETHODIMP
  426. nsSimpleURI::EqualsExceptRef(nsIURI* other, bool *result)
  427. {
  428. return EqualsInternal(other, eIgnoreRef, result);
  429. }
  430. /* virtual */ nsresult
  431. nsSimpleURI::EqualsInternal(nsIURI* other,
  432. nsSimpleURI::RefHandlingEnum refHandlingMode,
  433. bool* result)
  434. {
  435. NS_ENSURE_ARG_POINTER(other);
  436. NS_PRECONDITION(result, "null pointer");
  437. RefPtr<nsSimpleURI> otherUri;
  438. nsresult rv = other->QueryInterface(kThisSimpleURIImplementationCID,
  439. getter_AddRefs(otherUri));
  440. if (NS_FAILED(rv)) {
  441. *result = false;
  442. return NS_OK;
  443. }
  444. *result = EqualsInternal(otherUri, refHandlingMode);
  445. return NS_OK;
  446. }
  447. bool
  448. nsSimpleURI::EqualsInternal(nsSimpleURI* otherUri, RefHandlingEnum refHandlingMode)
  449. {
  450. bool result = (mScheme == otherUri->mScheme &&
  451. mPath == otherUri->mPath);
  452. if (result) {
  453. result = (mIsQueryValid == otherUri->mIsQueryValid &&
  454. (!mIsQueryValid || mQuery == otherUri->mQuery));
  455. }
  456. if (result && refHandlingMode == eHonorRef) {
  457. result = (mIsRefValid == otherUri->mIsRefValid &&
  458. (!mIsRefValid || mRef == otherUri->mRef));
  459. }
  460. return result;
  461. }
  462. NS_IMETHODIMP
  463. nsSimpleURI::SchemeIs(const char *i_Scheme, bool *o_Equals)
  464. {
  465. NS_ENSURE_ARG_POINTER(o_Equals);
  466. if (!i_Scheme) return NS_ERROR_NULL_POINTER;
  467. const char *this_scheme = mScheme.get();
  468. // mScheme is guaranteed to be lower case.
  469. if (*i_Scheme == *this_scheme || *i_Scheme == (*this_scheme - ('a' - 'A')) ) {
  470. *o_Equals = PL_strcasecmp(this_scheme, i_Scheme) ? false : true;
  471. } else {
  472. *o_Equals = false;
  473. }
  474. return NS_OK;
  475. }
  476. /* virtual */ nsSimpleURI*
  477. nsSimpleURI::StartClone(nsSimpleURI::RefHandlingEnum refHandlingMode,
  478. const nsACString& newRef)
  479. {
  480. nsSimpleURI* url = new nsSimpleURI();
  481. SetRefOnClone(url, refHandlingMode, newRef);
  482. return url;
  483. }
  484. /* virtual */ void
  485. nsSimpleURI::SetRefOnClone(nsSimpleURI* url,
  486. nsSimpleURI::RefHandlingEnum refHandlingMode,
  487. const nsACString& newRef)
  488. {
  489. if (refHandlingMode == eHonorRef) {
  490. url->mRef = mRef;
  491. url->mIsRefValid = mIsRefValid;
  492. } else if (refHandlingMode == eReplaceRef) {
  493. url->SetRef(newRef);
  494. }
  495. }
  496. NS_IMETHODIMP
  497. nsSimpleURI::Clone(nsIURI** result)
  498. {
  499. return CloneInternal(eHonorRef, EmptyCString(), result);
  500. }
  501. NS_IMETHODIMP
  502. nsSimpleURI::CloneIgnoringRef(nsIURI** result)
  503. {
  504. return CloneInternal(eIgnoreRef, EmptyCString(), result);
  505. }
  506. NS_IMETHODIMP
  507. nsSimpleURI::CloneWithNewRef(const nsACString &newRef, nsIURI** result)
  508. {
  509. return CloneInternal(eReplaceRef, newRef, result);
  510. }
  511. nsresult
  512. nsSimpleURI::CloneInternal(nsSimpleURI::RefHandlingEnum refHandlingMode,
  513. const nsACString &newRef,
  514. nsIURI** result)
  515. {
  516. RefPtr<nsSimpleURI> url = StartClone(refHandlingMode, newRef);
  517. if (!url)
  518. return NS_ERROR_OUT_OF_MEMORY;
  519. // Note: |url| may well have mMutable false at this point, so
  520. // don't call any setter methods.
  521. url->mScheme = mScheme;
  522. url->mPath = mPath;
  523. url->mIsQueryValid = mIsQueryValid;
  524. if (url->mIsQueryValid) {
  525. url->mQuery = mQuery;
  526. }
  527. url.forget(result);
  528. return NS_OK;
  529. }
  530. NS_IMETHODIMP
  531. nsSimpleURI::Resolve(const nsACString &relativePath, nsACString &result)
  532. {
  533. result = relativePath;
  534. return NS_OK;
  535. }
  536. NS_IMETHODIMP
  537. nsSimpleURI::GetAsciiSpec(nsACString &result)
  538. {
  539. nsAutoCString buf;
  540. nsresult rv = GetSpec(buf);
  541. if (NS_FAILED(rv)) return rv;
  542. return NS_EscapeURL(buf, esc_OnlyNonASCII|esc_AlwaysCopy, result, fallible);
  543. }
  544. NS_IMETHODIMP
  545. nsSimpleURI::GetAsciiHostPort(nsACString &result)
  546. {
  547. // XXX This behavior mimics GetHostPort.
  548. return NS_ERROR_FAILURE;
  549. }
  550. NS_IMETHODIMP
  551. nsSimpleURI::GetAsciiHost(nsACString &result)
  552. {
  553. result.Truncate();
  554. return NS_OK;
  555. }
  556. NS_IMETHODIMP
  557. nsSimpleURI::GetOriginCharset(nsACString &result)
  558. {
  559. result.Truncate();
  560. return NS_OK;
  561. }
  562. //----------------------------------------------------------------------------
  563. // nsSimpleURI::nsIClassInfo
  564. //----------------------------------------------------------------------------
  565. NS_IMETHODIMP
  566. nsSimpleURI::GetInterfaces(uint32_t *count, nsIID * **array)
  567. {
  568. *count = 0;
  569. *array = nullptr;
  570. return NS_OK;
  571. }
  572. NS_IMETHODIMP
  573. nsSimpleURI::GetScriptableHelper(nsIXPCScriptable **_retval)
  574. {
  575. *_retval = nullptr;
  576. return NS_OK;
  577. }
  578. NS_IMETHODIMP
  579. nsSimpleURI::GetContractID(char * *aContractID)
  580. {
  581. // Make sure to modify any subclasses as needed if this ever
  582. // changes.
  583. *aContractID = nullptr;
  584. return NS_OK;
  585. }
  586. NS_IMETHODIMP
  587. nsSimpleURI::GetClassDescription(char * *aClassDescription)
  588. {
  589. *aClassDescription = nullptr;
  590. return NS_OK;
  591. }
  592. NS_IMETHODIMP
  593. nsSimpleURI::GetClassID(nsCID * *aClassID)
  594. {
  595. // Make sure to modify any subclasses as needed if this ever
  596. // changes to not call the virtual GetClassIDNoAlloc.
  597. *aClassID = (nsCID*) moz_xmalloc(sizeof(nsCID));
  598. if (!*aClassID)
  599. return NS_ERROR_OUT_OF_MEMORY;
  600. return GetClassIDNoAlloc(*aClassID);
  601. }
  602. NS_IMETHODIMP
  603. nsSimpleURI::GetFlags(uint32_t *aFlags)
  604. {
  605. *aFlags = nsIClassInfo::MAIN_THREAD_ONLY;
  606. return NS_OK;
  607. }
  608. NS_IMETHODIMP
  609. nsSimpleURI::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
  610. {
  611. *aClassIDNoAlloc = kSimpleURICID;
  612. return NS_OK;
  613. }
  614. //----------------------------------------------------------------------------
  615. // nsSimpleURI::nsISimpleURI
  616. //----------------------------------------------------------------------------
  617. NS_IMETHODIMP
  618. nsSimpleURI::GetMutable(bool *value)
  619. {
  620. *value = mMutable;
  621. return NS_OK;
  622. }
  623. NS_IMETHODIMP
  624. nsSimpleURI::SetMutable(bool value)
  625. {
  626. NS_ENSURE_ARG(mMutable || !value);
  627. mMutable = value;
  628. return NS_OK;
  629. }
  630. //----------------------------------------------------------------------------
  631. // nsSimpleURI::nsISizeOf
  632. //----------------------------------------------------------------------------
  633. size_t
  634. nsSimpleURI::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
  635. {
  636. return mScheme.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
  637. mPath.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
  638. mQuery.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
  639. mRef.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  640. }
  641. size_t
  642. nsSimpleURI::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
  643. return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  644. }
  645. NS_IMETHODIMP
  646. nsSimpleURI::GetFilePath(nsACString& aFilePath)
  647. {
  648. aFilePath = mPath;
  649. return NS_OK;
  650. }
  651. NS_IMETHODIMP
  652. nsSimpleURI::SetFilePath(const nsACString& aFilePath)
  653. {
  654. return NS_ERROR_FAILURE;
  655. }
  656. NS_IMETHODIMP
  657. nsSimpleURI::GetQuery(nsACString& aQuery)
  658. {
  659. if (!mIsQueryValid) {
  660. MOZ_ASSERT(mQuery.IsEmpty(), "mIsQueryValid/mQuery invariant broken");
  661. aQuery.Truncate();
  662. } else {
  663. aQuery = mQuery;
  664. }
  665. return NS_OK;
  666. }
  667. NS_IMETHODIMP
  668. nsSimpleURI::SetQuery(const nsACString& aQuery)
  669. {
  670. NS_ENSURE_STATE(mMutable);
  671. nsAutoCString query;
  672. nsresult rv = NS_EscapeURL(aQuery, esc_OnlyNonASCII, query, fallible);
  673. if (NS_FAILED(rv)) {
  674. return rv;
  675. }
  676. if (query.IsEmpty()) {
  677. // Empty string means to remove query completely.
  678. mIsQueryValid = false;
  679. mQuery.Truncate(); // invariant: mQuery should be empty when it's not valid
  680. return NS_OK;
  681. }
  682. mIsQueryValid = true;
  683. // Gracefully skip initial question mark
  684. if (query[0] == '?') {
  685. mQuery = Substring(query, 1);
  686. } else {
  687. mQuery = query;
  688. }
  689. return NS_OK;
  690. }
  691. } // namespace net
  692. } // namespace mozilla