HttpRequest.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __HTTP_REQUEST_H__
  23. #define __HTTP_REQUEST_H__
  24. #include <string>
  25. #include <vector>
  26. #include "base/CCRef.h"
  27. #include "base/ccMacros.h"
  28. /**
  29. * @addtogroup network
  30. * @{
  31. */
  32. NS_CC_BEGIN
  33. namespace network {
  34. class HttpClient;
  35. class HttpResponse;
  36. typedef std::function<void(HttpClient* client, HttpResponse* response)> ccHttpRequestCallback;
  37. typedef void (cocos2d::Ref::*SEL_HttpResponse)(HttpClient* client, HttpResponse* response);
  38. #define httpresponse_selector(_SELECTOR) (cocos2d::network::SEL_HttpResponse)(&_SELECTOR)
  39. /**
  40. * Defines the object which users must packed for HttpClient::send(HttpRequest*) method.
  41. * Please refer to tests/test-cpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample
  42. * @since v2.0.2
  43. *
  44. * @lua NA
  45. */
  46. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  47. #ifdef DELETE
  48. #undef DELETE
  49. #endif
  50. #endif
  51. class CC_DLL HttpRequest : public Ref
  52. {
  53. public:
  54. /**
  55. * The HttpRequest type enum used in the HttpRequest::setRequestType.
  56. */
  57. enum class Type
  58. {
  59. GET,
  60. POST,
  61. PUT,
  62. DELETE,
  63. UNKNOWN,
  64. };
  65. /**
  66. * Constructor.
  67. * Because HttpRequest object will be used between UI thread and network thread,
  68. requestObj->autorelease() is forbidden to avoid crashes in AutoreleasePool
  69. new/retain/release still works, which means you need to release it manually
  70. Please refer to HttpRequestTest.cpp to find its usage.
  71. */
  72. HttpRequest()
  73. : _requestType(Type::UNKNOWN)
  74. , _pTarget(nullptr)
  75. , _pSelector(nullptr)
  76. , _pCallback(nullptr)
  77. , _pUserData(nullptr)
  78. {
  79. }
  80. /** Destructor. */
  81. virtual ~HttpRequest()
  82. {
  83. if (_pTarget)
  84. {
  85. _pTarget->release();
  86. }
  87. }
  88. /**
  89. * Override autorelease method to avoid developers to call it.
  90. * If this function was called, it would trigger assert in debug mode
  91. *
  92. * @return Ref* always return nullptr.
  93. */
  94. Ref* autorelease()
  95. {
  96. CCASSERT(false, "HttpResponse is used between network thread and ui thread \
  97. therefore, autorelease is forbidden here");
  98. return nullptr;
  99. }
  100. // setter/getters for properties
  101. /**
  102. * Set request type of HttpRequest object before being sent,now it support the enum value of HttpRequest::Type.
  103. *
  104. * @param type the request type.
  105. */
  106. void setRequestType(Type type)
  107. {
  108. _requestType = type;
  109. }
  110. /**
  111. * Get the request type of HttpRequest object.
  112. *
  113. * @return HttpRequest::Type.
  114. */
  115. Type getRequestType() const
  116. {
  117. return _requestType;
  118. }
  119. /**
  120. * Set the url address of HttpRequest object.
  121. * The url value could be like these: "http://httpbin.org/ip" or "https://httpbin.org/get"
  122. *
  123. * @param url the string object.
  124. */
  125. void setUrl(const std::string& url)
  126. {
  127. _url = url;
  128. }
  129. /**
  130. * Get the url address of HttpRequest object.
  131. *
  132. * @return const char* the pointer of _url.
  133. */
  134. const char* getUrl() const
  135. {
  136. return _url.c_str();
  137. }
  138. /**
  139. * Set the request data of HttpRequest object.
  140. *
  141. * @param buffer the buffer of request data, it support binary data.
  142. * @param len the size of request data.
  143. */
  144. void setRequestData(const char* buffer, size_t len)
  145. {
  146. _requestData.assign(buffer, buffer + len);
  147. }
  148. /**
  149. * Get the request data pointer of HttpRequest object.
  150. *
  151. * @return char* the request data pointer.
  152. */
  153. char* getRequestData()
  154. {
  155. if(!_requestData.empty())
  156. return _requestData.data();
  157. return nullptr;
  158. }
  159. /**
  160. * Get the size of request data
  161. *
  162. * @return ssize_t the size of request data
  163. */
  164. ssize_t getRequestDataSize() const
  165. {
  166. return _requestData.size();
  167. }
  168. /**
  169. * Set a string tag to identify your request.
  170. * This tag can be found in HttpResponse->getHttpRequest->getTag().
  171. *
  172. * @param tag the string object.
  173. */
  174. void setTag(const std::string& tag)
  175. {
  176. _tag = tag;
  177. }
  178. /**
  179. * Get the string tag to identify the request.
  180. * The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback.
  181. *
  182. * @return const char* the pointer of _tag
  183. */
  184. const char* getTag() const
  185. {
  186. return _tag.c_str();
  187. }
  188. /**
  189. * Set user-customed data of HttpRequest object.
  190. * You can attach a customed data in each request, and get it back in response callback.
  191. * But you need to new/delete the data pointer manually.
  192. *
  193. * @param pUserData the string pointer
  194. */
  195. void setUserData(void* pUserData)
  196. {
  197. _pUserData = pUserData;
  198. }
  199. /**
  200. * Get the user-customed data pointer which were pre-setted.
  201. * Don't forget to delete it. HttpClient/HttpResponse/HttpRequest will do nothing with this pointer.
  202. *
  203. * @return void* the pointer of user-customed data.
  204. */
  205. void* getUserData() const
  206. {
  207. return _pUserData;
  208. }
  209. /**
  210. * Set the target and related callback selector.
  211. * When response come back, it would call (pTarget->*pSelector) to process something.
  212. *
  213. * @param pTarget the target object pointer.
  214. * @param pSelector the callback function.
  215. */
  216. CC_DEPRECATED_ATTRIBUTE void setResponseCallback(Ref* pTarget, SEL_CallFuncND pSelector)
  217. {
  218. doSetResponseCallback(pTarget, (SEL_HttpResponse)pSelector);
  219. }
  220. /**
  221. * Set the target and related callback selector of HttpRequest object.
  222. * When response come back, we would call (pTarget->*pSelector) to process response data.
  223. *
  224. * @param pTarget the target object pointer.
  225. * @param pSelector the SEL_HttpResponse function.
  226. */
  227. void setResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
  228. {
  229. doSetResponseCallback(pTarget, pSelector);
  230. }
  231. /**
  232. * Set response callback function of HttpRequest object.
  233. * When response come back, we would call _pCallback to process response data.
  234. *
  235. * @param callback the ccHttpRequestCallback function.
  236. */
  237. void setResponseCallback(const ccHttpRequestCallback& callback)
  238. {
  239. _pCallback = callback;
  240. }
  241. /**
  242. * Get the target of callback selector function, mainly used by HttpClient.
  243. *
  244. * @return Ref* the target of callback selector function
  245. */
  246. Ref* getTarget() const
  247. {
  248. return _pTarget;
  249. }
  250. /**
  251. * This sub class is just for migration SEL_CallFuncND to SEL_HttpResponse,someday this way will be removed.
  252. *
  253. * @lua NA
  254. */
  255. class _prxy
  256. {
  257. public:
  258. /** Constructor. */
  259. _prxy( SEL_HttpResponse cb ) :_cb(cb) {}
  260. /** Destructor. */
  261. ~_prxy(){};
  262. operator SEL_HttpResponse() const { return _cb; }
  263. CC_DEPRECATED_ATTRIBUTE operator SEL_CallFuncND() const { return (SEL_CallFuncND) _cb; }
  264. protected:
  265. SEL_HttpResponse _cb;
  266. };
  267. /**
  268. * Get _prxy object by the _pSelector.
  269. *
  270. * @return _prxy the _prxy object
  271. */
  272. _prxy getSelector() const
  273. {
  274. return _prxy(_pSelector);
  275. }
  276. /**
  277. * Get ccHttpRequestCallback callback function.
  278. *
  279. * @return const ccHttpRequestCallback& ccHttpRequestCallback callback function.
  280. */
  281. const ccHttpRequestCallback& getCallback() const
  282. {
  283. return _pCallback;
  284. }
  285. /**
  286. * Set custom-defined headers.
  287. *
  288. * @param headers The string vector of custom-defined headers.
  289. */
  290. void setHeaders(const std::vector<std::string>& headers)
  291. {
  292. _headers = headers;
  293. }
  294. /**
  295. * Get custom headers.
  296. *
  297. * @return std::vector<std::string> the string vector of custom-defined headers.
  298. */
  299. std::vector<std::string> getHeaders() const
  300. {
  301. return _headers;
  302. }
  303. private:
  304. void doSetResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
  305. {
  306. if (_pTarget)
  307. {
  308. _pTarget->release();
  309. }
  310. _pTarget = pTarget;
  311. _pSelector = pSelector;
  312. if (_pTarget)
  313. {
  314. _pTarget->retain();
  315. }
  316. }
  317. protected:
  318. // properties
  319. Type _requestType; /// kHttpRequestGet, kHttpRequestPost or other enums
  320. std::string _url; /// target url that this request is sent to
  321. std::vector<char> _requestData; /// used for POST
  322. std::string _tag; /// user defined tag, to identify different requests in response callback
  323. Ref* _pTarget; /// callback target of pSelector function
  324. SEL_HttpResponse _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(HttpClient *sender, HttpResponse * response)
  325. ccHttpRequestCallback _pCallback; /// C++11 style callbacks
  326. void* _pUserData; /// You can add your customed data here
  327. std::vector<std::string> _headers; /// custom http headers
  328. };
  329. }
  330. NS_CC_END
  331. // end group
  332. /// @}
  333. #endif //__HTTP_REQUEST_H__