llcorehttpoperation.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * @file llcorehttpoperation.cpp
  3. * @brief Definitions for internal classes based on HttpOperation
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llcorehttpoperation.h"
  28. #include "llcorehttphandler.h"
  29. #include "llcorehttpinternal.h"
  30. #include "llcorehttpreplyqueue.h"
  31. #include "llcorehttprequest.h"
  32. #include "llcorehttprequestqueue.h"
  33. #include "llcorehttpresponse.h"
  34. #include "llcorehttpservice.h"
  35. #include "lltimer.h"
  36. namespace LLCore
  37. {
  38. // ==================================
  39. // HttpOperation
  40. // ==================================
  41. // Statics
  42. HttpOperation::handleMap_t HttpOperation::sHandleMap;
  43. LLCoreInt::HttpMutex HttpOperation::sOpMutex;
  44. HttpOperation::HttpOperation()
  45. : std::enable_shared_from_this<HttpOperation>(),
  46. mReqPolicy(HttpRequest::DEFAULT_POLICY_ID),
  47. mTracing(HTTP_TRACE_OFF),
  48. mMyHandle(LLCORE_HTTP_HANDLE_INVALID)
  49. {
  50. mMetricCreated = LLTimer::totalTime();
  51. }
  52. HttpOperation::~HttpOperation()
  53. {
  54. destroyHandle();
  55. mReplyQueue.reset();
  56. mUserHandler.reset();
  57. }
  58. void HttpOperation::setReplyPath(HttpReplyQueue::ptr_t reply_queue,
  59. HttpHandler::ptr_t user_handler)
  60. {
  61. mReplyQueue.swap(reply_queue);
  62. mUserHandler.swap(user_handler);
  63. }
  64. void HttpOperation::stageFromRequest(HttpService*)
  65. {
  66. // Default implementation should never be called. This indicates an
  67. // operation making a transition that is not defined.
  68. llerrs << "Default stageFromRequest method may not be called." << llendl;
  69. }
  70. void HttpOperation::stageFromReady(HttpService*)
  71. {
  72. // Default implementation should never be called. This indicates an
  73. // operation making a transition that is not defined.
  74. llerrs << "Default stageFromReady method may not be called." << llendl;
  75. }
  76. void HttpOperation::stageFromActive(HttpService*)
  77. {
  78. // Default implementation should never be called. This indicates an
  79. // operation making a transition that is not defined.
  80. llerrs << "Default stageFromActive method may not be called." << llendl;
  81. }
  82. void HttpOperation::visitNotifier(HttpRequest*)
  83. {
  84. if (mUserHandler)
  85. {
  86. HttpResponse* response = new HttpResponse();
  87. response->setStatus(mStatus);
  88. mUserHandler->onCompleted(getHandle(), response);
  89. response->release();
  90. }
  91. }
  92. HttpStatus HttpOperation::cancel()
  93. {
  94. HttpStatus status;
  95. return status;
  96. }
  97. // Handle methods
  98. HttpHandle HttpOperation::getHandle()
  99. {
  100. return mMyHandle != LLCORE_HTTP_HANDLE_INVALID ? mMyHandle : createHandle();
  101. }
  102. HttpHandle HttpOperation::createHandle()
  103. {
  104. HttpHandle handle = static_cast<HttpHandle>(this);
  105. {
  106. LLCoreInt::HttpScopedLock lock(sOpMutex);
  107. sHandleMap.emplace(handle, shared_from_this());
  108. mMyHandle = handle;
  109. }
  110. return mMyHandle;
  111. }
  112. void HttpOperation::destroyHandle()
  113. {
  114. if (mMyHandle != LLCORE_HTTP_HANDLE_INVALID)
  115. {
  116. LLCoreInt::HttpScopedLock lock(sOpMutex);
  117. handleMap_t::iterator it = sHandleMap.find(mMyHandle);
  118. if (it != sHandleMap.end())
  119. {
  120. sHandleMap.erase(it);
  121. }
  122. }
  123. }
  124. //static
  125. HttpOperation::ptr_t HttpOperation::findByHandle(HttpHandle handle)
  126. {
  127. if (handle == LLCORE_HTTP_HANDLE_INVALID)
  128. {
  129. return ptr_t();
  130. }
  131. wptr_t weak;
  132. {
  133. LLCoreInt::HttpScopedLock lock(sOpMutex);
  134. handleMap_t::iterator it = sHandleMap.find(handle);
  135. if (it == sHandleMap.end())
  136. {
  137. llwarns << "Could not find operation for handle " << handle
  138. << llendl;
  139. return ptr_t();
  140. }
  141. weak = it->second;
  142. }
  143. if (!weak.expired())
  144. {
  145. return weak.lock();
  146. }
  147. return ptr_t();
  148. }
  149. void HttpOperation::addAsReply()
  150. {
  151. if (mTracing > HTTP_TRACE_OFF)
  152. {
  153. llinfos << "TRACE, ToReplyQueue, Handle: " << getHandle() << llendl;
  154. }
  155. if (mReplyQueue)
  156. {
  157. HttpOperation::ptr_t op = shared_from_this();
  158. mReplyQueue->addOp(op);
  159. }
  160. }
  161. // ==================================
  162. // HttpOpStop
  163. // ==================================
  164. HttpOpStop::HttpOpStop()
  165. : HttpOperation()
  166. {
  167. }
  168. void HttpOpStop::stageFromRequest(HttpService* service)
  169. {
  170. if (service)
  171. {
  172. // Do operations
  173. service->stopRequested();
  174. // Prepare response if needed
  175. addAsReply();
  176. }
  177. else
  178. {
  179. llwarns << "NULL service passed" << llendl;
  180. llassert(false);
  181. }
  182. }
  183. // ==================================
  184. // HttpOpNull
  185. // ==================================
  186. HttpOpNull::HttpOpNull()
  187. : HttpOperation()
  188. {
  189. }
  190. void HttpOpNull::stageFromRequest(HttpService*)
  191. {
  192. // Perform op/ Nothing to perform. This does not fall into the libcurl
  193. // ready/active queues, it just bounces over to the reply queue directly.
  194. // Prepare response if needed
  195. addAsReply();
  196. }
  197. } // End namespace LLCore