TestUpload.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "TestCommon.h"
  6. #include <algorithm>
  7. #ifdef WIN32
  8. #include <windows.h>
  9. #endif
  10. #include "nsIComponentRegistrar.h"
  11. #include "nsIScriptSecurityManager.h"
  12. #include "nsServiceManagerUtils.h"
  13. #include "nsIServiceManager.h"
  14. #include "nsNetUtil.h"
  15. #include "nsIUploadChannel.h"
  16. #include "NetwerkTestLogging.h"
  17. //
  18. // set NSPR_LOG_MODULES=Test:5
  19. //
  20. static PRLogModuleInfo *gTestLog = nullptr;
  21. #define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
  22. //-----------------------------------------------------------------------------
  23. // InputTestConsumer
  24. //-----------------------------------------------------------------------------
  25. class InputTestConsumer : public nsIStreamListener
  26. {
  27. virtual ~InputTestConsumer();
  28. public:
  29. InputTestConsumer();
  30. NS_DECL_ISUPPORTS
  31. NS_DECL_NSIREQUESTOBSERVER
  32. NS_DECL_NSISTREAMLISTENER
  33. };
  34. InputTestConsumer::InputTestConsumer()
  35. {
  36. }
  37. InputTestConsumer::~InputTestConsumer() = default;
  38. NS_IMPL_ISUPPORTS(InputTestConsumer,
  39. nsIStreamListener,
  40. nsIRequestObserver)
  41. NS_IMETHODIMP
  42. InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
  43. {
  44. LOG(("InputTestConsumer::OnStartRequest\n"));
  45. return NS_OK;
  46. }
  47. NS_IMETHODIMP
  48. InputTestConsumer::OnDataAvailable(nsIRequest *request,
  49. nsISupports* context,
  50. nsIInputStream *aIStream,
  51. uint64_t aSourceOffset,
  52. uint32_t aLength)
  53. {
  54. char buf[1025];
  55. uint32_t amt, size;
  56. nsresult rv;
  57. while (aLength) {
  58. size = std::min<uint32_t>(aLength, sizeof(buf));
  59. rv = aIStream->Read(buf, size, &amt);
  60. if (NS_FAILED(rv)) {
  61. NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv),
  62. "The stream should never block.");
  63. return rv;
  64. }
  65. aLength -= amt;
  66. }
  67. return NS_OK;
  68. }
  69. NS_IMETHODIMP
  70. InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
  71. nsresult aStatus)
  72. {
  73. LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus));
  74. QuitPumpingEvents();
  75. return NS_OK;
  76. }
  77. int
  78. main(int argc, char* argv[])
  79. {
  80. if (test_common_init(&argc, &argv) != 0)
  81. return -1;
  82. nsresult rv;
  83. if (argc < 2) {
  84. printf("usage: %s <url> <file-to-upload>\n", argv[0]);
  85. return -1;
  86. }
  87. char* uriSpec = argv[1];
  88. char* fileName = argv[2];
  89. gTestLog = PR_NewLogModule("Test");
  90. {
  91. nsCOMPtr<nsIServiceManager> servMan;
  92. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  93. // first thing to do is create ourselves a stream that
  94. // is to be uploaded.
  95. nsCOMPtr<nsIInputStream> uploadStream;
  96. rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
  97. true,
  98. nsDependentCString(fileName)); // XXX UTF-8
  99. if (NS_FAILED(rv)) return -1;
  100. // create our url.
  101. nsCOMPtr<nsIURI> uri;
  102. rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
  103. if (NS_FAILED(rv)) return -1;
  104. nsCOMPtr<nsIScriptSecurityManager> secman =
  105. do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
  106. if (NS_FAILED(rv)) return -1;
  107. nsCOMPtr<nsIPrincipal> systemPrincipal;
  108. rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
  109. if (NS_FAILED(rv)) return -1;
  110. nsCOMPtr<nsIChannel> channel;
  111. rv = NS_NewChannel(getter_AddRefs(channel),
  112. uri,
  113. systemPrincipal,
  114. nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS,
  115. nsIContentPolicy::TYPE_OTHER);
  116. if (NS_FAILED(rv)) return -1;
  117. // QI and set the upload stream
  118. nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
  119. uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);
  120. // create a dummy listener
  121. InputTestConsumer* listener;
  122. listener = new InputTestConsumer;
  123. if (!listener) {
  124. NS_ERROR("Failed to create a new stream listener!");
  125. return -1;
  126. }
  127. NS_ADDREF(listener);
  128. channel->AsyncOpen2(listener);
  129. PumpEvents();
  130. } // this scopes the nsCOMPtrs
  131. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  132. rv = NS_ShutdownXPCOM(nullptr);
  133. NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
  134. return 0;
  135. }