llappcorehttp.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @file llappcorehttp.h
  3. * @brief Singleton initialization/shutdown class for llcorehttp library
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewergpl$
  6. *
  7. * Copyright (c) 2012, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef _LL_APP_COREHTTP_H_
  33. #define _LL_APP_COREHTTP_H_
  34. #include "boost/bind.hpp"
  35. #include "boost/signals2.hpp"
  36. #include "llcorehttphandler.h"
  37. #include "llcorehttprequest.h"
  38. #include "llcorehttpresponse.h"
  39. // This class manages the lifecyle of the core http library. Slightly different
  40. // style than traditional code but reflects the use of handler classes and
  41. // light-weight interface object instances of the new libraries. To be used as
  42. // a singleton and static construction is fine.
  43. class LLAppCoreHttp final : public LLCore::HttpHandler
  44. {
  45. protected:
  46. LOG_CLASS(LLAppCoreHttp);
  47. public:
  48. static constexpr long PIPELINING_DEPTH = 8L;
  49. typedef LLCore::HttpRequest::policy_t policy_t;
  50. enum EAppPolicy
  51. {
  52. // Catchall policy class. Not used yet but will have a generous
  53. // concurrency limit. Deep queueing possible by having a chatty HTTP
  54. // user.
  55. //
  56. // Destination: anywhere
  57. // Protocol: http: or https:
  58. // Transfer size: KB-MB
  59. // Long poll: no
  60. // Concurrency: high
  61. // Request rate: unknown
  62. // Pipelined: no
  63. AP_DEFAULT,
  64. // Texture fetching policy class. Used to download textures via
  65. // capability or SSB service. Deep queueing of requests. Do not share.
  66. // GET requests only.
  67. //
  68. // Destination: simhost:12046 & {bake-texture,cdn}:80
  69. // Protocol: http:
  70. // Transfer size: KB-MB
  71. // Long poll: no
  72. // Concurrency: high
  73. // Request rate: high
  74. // Pipelined: yes
  75. AP_TEXTURE,
  76. // Legacy mesh fetching policy class. Used to download textures via the
  77. // "GetMesh"' capability. Do not share.
  78. //
  79. // Destination: simhost:12046
  80. // Protocol: http:
  81. // Transfer size: KB-MB
  82. // Long poll: no
  83. // Concurrency: dangerously high
  84. // Request rate: high
  85. // Pipelined: no
  86. AP_MESH1,
  87. // New mesh fetching policy class. Used to download textures via the
  88. // "GetMesh2" capability. Used when fetch request (typically one LOD)
  89. // is 'small', currently defined as 2MB. Very deeply queued. Do not
  90. // share. GET requests only.
  91. //
  92. // Destination: simhost:12046 & cdn:80
  93. // Protocol: http:
  94. // Transfer size: KB-MB
  95. // Long poll: no
  96. // Concurrency: high
  97. // Request rate: high
  98. // Pipelined: yes
  99. AP_MESH2,
  100. // Large mesh fetching policy class. Used to download textures via
  101. // "GetMesh" or "GetMesh2" capabilities. Used when fetch request is not
  102. // small to avoid head-of-line problem when large requests block a
  103. // sequence of small, fast requests. Can be shared with similar traffic
  104. // that can wait for longish stalls (default timeout 600s).
  105. //
  106. // Destination: simhost:12046 & cdn:80
  107. // Protocol: http:
  108. // Transfer size: MB
  109. // Long poll: no
  110. // Concurrency: low
  111. // Request rate: low
  112. // Pipelined: no
  113. AP_LARGE_MESH,
  114. // Asset download policy class. Used to fetch assets.
  115. //
  116. // Destination: cdn:80
  117. // Protocol: https:
  118. // Transfer size: KB-MB
  119. // Long poll: no
  120. // Concurrency: high
  121. // Request rate: high
  122. // Pipelined: yes
  123. AP_ASSETS,
  124. // Asset upload policy class. Used to store assets (mesh only at the
  125. // moment) via changeable URL. Responses may take some time (default
  126. // timeout 240s).
  127. //
  128. // Destination: simhost:12043
  129. // Protocol: https:
  130. // Transfer size: KB-MB
  131. // Long poll: no
  132. // Concurrency: low
  133. // Request rate: low
  134. // Pipelined: no
  135. AP_UPLOADS,
  136. // Long-poll-type HTTP requests. Not bound by a connection limit.
  137. // Requests will typically hang around for a long time (~30s). Only
  138. // shareable with other long-poll requests.
  139. //
  140. // Destination: simhost:12043
  141. // Protocol: https:
  142. // Transfer size: KB
  143. // Long poll: yes
  144. // Concurrency: unlimited but low in practice
  145. // Request rate: low
  146. // Pipelined: no
  147. AP_LONG_POLL,
  148. // Inventory operations (really Capabilities-related operations). Mix
  149. // of high-priority and low-priority operations.
  150. //
  151. // Destination: simhost:12043
  152. // Protocol: https:
  153. // Transfer size: KB-MB
  154. // Long poll: no
  155. // Concurrency: high
  156. // Request rate: high
  157. // Pipelined: no
  158. AP_INVENTORY,
  159. AP_REPORTING = AP_INVENTORY, // Piggy-back on inventory
  160. // Material resource requests and puts.
  161. //
  162. // Destination: simhost:12043
  163. // Protocol: https:
  164. // Transfer size: KB
  165. // Long poll: no
  166. // Concurrency: low
  167. // Request rate: low
  168. // Pipelined: no
  169. AP_MATERIALS,
  170. // Appearance resource requests and puts.
  171. //
  172. // Destination: simhost:12043
  173. // Protocol: https:
  174. // Transfer size: KB
  175. // Long poll: no
  176. // Concurrency: mid
  177. // Request rate: low
  178. // Pipelined: yes
  179. AP_AGENT,
  180. AP_COUNT // Must be last
  181. };
  182. LLAppCoreHttp();
  183. ~LLAppCoreHttp();
  184. // Initialize the LLCore::HTTP library creating service classes
  185. // and starting the servicing thread. Caller is expected to do
  186. // other initializations (SSL mutex, thread hash function) appropriate
  187. // for the application.
  188. void init();
  189. // Request that the servicing thread stop servicing requests,
  190. // release resource references and stop. Request is asynchronous
  191. // and @see cleanup() will perform a limited wait loop for this
  192. // request to stop the thread.
  193. void requestStop();
  194. // Terminate LLCore::HTTP library services. Caller is expected
  195. // to have made a best-effort to shutdown the servicing thread
  196. // by issuing a requestThreadStop() and waiting for completion
  197. // notification that the stop has completed.
  198. void cleanup();
  199. // Notification when the stop request is complete.
  200. LL_INLINE void onCompleted(LLCore::HttpHandle handle,
  201. LLCore::HttpResponse* response) override
  202. {
  203. mStopped = true;
  204. }
  205. // Retrieve a policy class identifier for desired application function.
  206. LL_INLINE policy_t getPolicy(EAppPolicy policy) const
  207. {
  208. return mHttpClasses[policy].mPolicy;
  209. }
  210. LL_INLINE bool isPipelined(EAppPolicy policy) const
  211. {
  212. return mHttpClasses[policy].mPipelined;
  213. }
  214. bool isPipeliningOn();
  215. // Apply initial or new settings from the environment.
  216. void refreshSettings(bool initial = false);
  217. #if LL_CURL_BUG
  218. // HACK: to work around libcurl bugs that sometimes cause the HTTP pipeline
  219. // to return corrupted data... The idea of that hack is to temporarily turn
  220. // off pipelining when we detect an issue, and automatically turn it back
  221. // on a minute later, with fresh pipelined connections, once the old ones
  222. // have been closed.
  223. void setPipelinedTempOff(bool turn_off = true);
  224. void checkPipelinedTempOff();
  225. #endif
  226. private:
  227. static LLCore::HttpStatus sslVerify(const std::string& uri,
  228. const LLCore::HttpHandler::ptr_t& handler,
  229. void* userdata);
  230. private:
  231. // PODish container for per-class settings and state.
  232. struct HttpClass
  233. {
  234. public:
  235. HttpClass();
  236. public:
  237. // Policy class id for the class:
  238. policy_t mPolicy;
  239. U32 mConnLimit;
  240. bool mPipelined;
  241. // Signal to global setting that affect this class (if any):
  242. boost::signals2::connection mSettingsSignal;
  243. };
  244. HttpClass mHttpClasses[AP_COUNT];
  245. // Request queue to issue shutdowns:
  246. LLCore::HttpRequest* mRequest;
  247. LLCore::HttpHandle mStopHandle;
  248. F64 mStopRequested;
  249. // Signals to global settings that affect us:
  250. boost::signals2::connection mPipelinedSignal;
  251. boost::signals2::connection mOSPipelinedSignal;
  252. #if LL_CURL_BUG
  253. // When to restart HTTP pipelining after it got temporarily turned off
  254. F32 mRestartPipelined;
  255. bool mPipelinedTempOff;
  256. #endif
  257. bool mPipelined; // Global setting
  258. bool mStopped;
  259. };
  260. #endif // _LL_APP_COREHTTP_H_