HttpRequest.h 10 KB

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