nsViewSourceHandler.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* vim:set ts=4 sw=4 sts=4 et: */
  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. #include "nsViewSourceHandler.h"
  7. #include "nsViewSourceChannel.h"
  8. #include "nsNetUtil.h"
  9. #include "nsSimpleNestedURI.h"
  10. #define VIEW_SOURCE "view-source"
  11. namespace mozilla {
  12. namespace net {
  13. ////////////////////////////////////////////////////////////////////////////////
  14. NS_IMPL_ISUPPORTS(nsViewSourceHandler, nsIProtocolHandler)
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // nsIProtocolHandler methods:
  17. NS_IMETHODIMP
  18. nsViewSourceHandler::GetScheme(nsACString &result)
  19. {
  20. result.AssignLiteral(VIEW_SOURCE);
  21. return NS_OK;
  22. }
  23. NS_IMETHODIMP
  24. nsViewSourceHandler::GetDefaultPort(int32_t *result)
  25. {
  26. *result = -1;
  27. return NS_OK;
  28. }
  29. NS_IMETHODIMP
  30. nsViewSourceHandler::GetProtocolFlags(uint32_t *result)
  31. {
  32. *result = URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD |
  33. URI_NON_PERSISTABLE;
  34. return NS_OK;
  35. }
  36. NS_IMETHODIMP
  37. nsViewSourceHandler::NewURI(const nsACString &aSpec,
  38. const char *aCharset,
  39. nsIURI *aBaseURI,
  40. nsIURI **aResult)
  41. {
  42. *aResult = nullptr;
  43. // Extract inner URL and normalize to ASCII. This is done to properly
  44. // support IDN in cases like "view-source:http://www.szalagavató.hu/"
  45. int32_t colon = aSpec.FindChar(':');
  46. if (colon == kNotFound)
  47. return NS_ERROR_MALFORMED_URI;
  48. nsCOMPtr<nsIURI> innerURI;
  49. nsresult rv = NS_NewURI(getter_AddRefs(innerURI),
  50. Substring(aSpec, colon + 1), aCharset, aBaseURI);
  51. if (NS_FAILED(rv))
  52. return rv;
  53. nsAutoCString asciiSpec;
  54. rv = innerURI->GetAsciiSpec(asciiSpec);
  55. if (NS_FAILED(rv))
  56. return rv;
  57. // put back our scheme and construct a simple-uri wrapper
  58. asciiSpec.Insert(VIEW_SOURCE ":", 0);
  59. // We can't swap() from an RefPtr<nsSimpleNestedURI> to an nsIURI**,
  60. // sadly.
  61. nsSimpleNestedURI* ourURI = new nsSimpleNestedURI(innerURI);
  62. nsCOMPtr<nsIURI> uri = ourURI;
  63. if (!uri)
  64. return NS_ERROR_OUT_OF_MEMORY;
  65. rv = ourURI->SetSpec(asciiSpec);
  66. if (NS_FAILED(rv))
  67. return rv;
  68. // Make the URI immutable so it's impossible to get it out of sync
  69. // with its inner URI.
  70. ourURI->SetMutable(false);
  71. uri.swap(*aResult);
  72. return rv;
  73. }
  74. NS_IMETHODIMP
  75. nsViewSourceHandler::NewChannel2(nsIURI* uri,
  76. nsILoadInfo* aLoadInfo,
  77. nsIChannel** result)
  78. {
  79. NS_ENSURE_ARG_POINTER(uri);
  80. nsViewSourceChannel *channel = new nsViewSourceChannel();
  81. if (!channel)
  82. return NS_ERROR_OUT_OF_MEMORY;
  83. NS_ADDREF(channel);
  84. nsresult rv = channel->Init(uri);
  85. if (NS_FAILED(rv)) {
  86. NS_RELEASE(channel);
  87. return rv;
  88. }
  89. // set the loadInfo on the new channel
  90. rv = channel->SetLoadInfo(aLoadInfo);
  91. if (NS_FAILED(rv)) {
  92. NS_RELEASE(channel);
  93. return rv;
  94. }
  95. *result = static_cast<nsIViewSourceChannel*>(channel);
  96. return NS_OK;
  97. }
  98. NS_IMETHODIMP
  99. nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
  100. {
  101. return NewChannel2(uri, nullptr, result);
  102. }
  103. nsresult
  104. nsViewSourceHandler::NewSrcdocChannel(nsIURI *aURI,
  105. nsIURI *aBaseURI,
  106. const nsAString &aSrcdoc,
  107. nsILoadInfo* aLoadInfo,
  108. nsIChannel** outChannel)
  109. {
  110. NS_ENSURE_ARG_POINTER(aURI);
  111. RefPtr<nsViewSourceChannel> channel = new nsViewSourceChannel();
  112. nsresult rv = channel->InitSrcdoc(aURI, aBaseURI, aSrcdoc, aLoadInfo);
  113. if (NS_FAILED(rv)) {
  114. return rv;
  115. }
  116. *outChannel = static_cast<nsIViewSourceChannel*>(channel.forget().take());
  117. return NS_OK;
  118. }
  119. NS_IMETHODIMP
  120. nsViewSourceHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
  121. {
  122. // don't override anything.
  123. *_retval = false;
  124. return NS_OK;
  125. }
  126. nsViewSourceHandler::nsViewSourceHandler()
  127. {
  128. gInstance = this;
  129. }
  130. nsViewSourceHandler::~nsViewSourceHandler()
  131. {
  132. gInstance = nullptr;
  133. }
  134. nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr;
  135. nsViewSourceHandler*
  136. nsViewSourceHandler::GetInstance()
  137. {
  138. return gInstance;
  139. }
  140. } // namespace net
  141. } // namespace mozilla