HttpRequest.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "base/CCRef.h"
  25. #include "base/ccMacros.h"
  26. #include <string>
  27. #include <vector>
  28. #include <functional>
  29. /**
  30. * @addtogroup network
  31. * @{
  32. */
  33. NS_CC_BEGIN
  34. namespace network {
  35. class HttpClient;
  36. class HttpResponse;
  37. typedef std::function<void(HttpClient*/* client*/, HttpResponse*/* response*/)> ccHttpRequestCallback;
  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. , _callback(nullptr)
  74. , _userData(nullptr)
  75. , _timeoutInSeconds(10.0f)
  76. {
  77. }
  78. /** Destructor. */
  79. virtual ~HttpRequest()
  80. {
  81. }
  82. /**
  83. * Override autorelease method to avoid developers to call it.
  84. * If this function was called, it would trigger assert in debug mode
  85. *
  86. * @return Ref* always return nullptr.
  87. */
  88. Ref* autorelease()
  89. {
  90. CCASSERT(false, "HttpResponse is used between network thread and ui thread \
  91. therefore, autorelease is forbidden here");
  92. return nullptr;
  93. }
  94. // setter/getters for properties
  95. /**
  96. * Set request type of HttpRequest object before being sent,now it support the enum value of HttpRequest::Type.
  97. *
  98. * @param type the request type.
  99. */
  100. inline void setRequestType(Type type)
  101. {
  102. _requestType = type;
  103. }
  104. /**
  105. * Get the request type of HttpRequest object.
  106. *
  107. * @return HttpRequest::Type.
  108. */
  109. inline Type getRequestType() const
  110. {
  111. return _requestType;
  112. }
  113. /**
  114. * Set the url address of HttpRequest object.
  115. * The url value could be like these: "http://httpbin.org/ip" or "https://httpbin.org/get"
  116. *
  117. * @param url the string object.
  118. */
  119. inline void setUrl(const std::string& url)
  120. {
  121. _url = url;
  122. }
  123. /**
  124. * Get the url address of HttpRequest object.
  125. *
  126. * @return const char* the pointer of _url.
  127. */
  128. inline const char* getUrl() const
  129. {
  130. return _url.c_str();
  131. }
  132. /**
  133. * Set the request data of HttpRequest object.
  134. *
  135. * @param buffer the buffer of request data, it support binary data.
  136. * @param len the size of request data.
  137. */
  138. inline void setRequestData(const char* buffer, size_t len)
  139. {
  140. _requestData.assign(buffer, buffer + len);
  141. }
  142. /**
  143. * Get the request data pointer of HttpRequest object.
  144. *
  145. * @return char* the request data pointer.
  146. */
  147. inline char* getRequestData()
  148. {
  149. if(!_requestData.empty())
  150. return _requestData.data();
  151. return nullptr;
  152. }
  153. /**
  154. * Get the size of request data
  155. *
  156. * @return ssize_t the size of request data
  157. */
  158. inline ssize_t getRequestDataSize() const
  159. {
  160. return _requestData.size();
  161. }
  162. /**
  163. * Set a string tag to identify your request.
  164. * This tag can be found in HttpResponse->getHttpRequest->getTag().
  165. *
  166. * @param tag the string object.
  167. */
  168. inline void setTag(const std::string& tag)
  169. {
  170. _tag = tag;
  171. }
  172. /**
  173. * Get the string tag to identify the request.
  174. * The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback.
  175. *
  176. * @return const char* the pointer of _tag
  177. */
  178. inline const char* getTag() const
  179. {
  180. return _tag.c_str();
  181. }
  182. /**
  183. * Set user-customed data of HttpRequest object.
  184. * You can attach a customed data in each request, and get it back in response callback.
  185. * But you need to new/delete the data pointer manully.
  186. *
  187. * @param userData the string pointer
  188. */
  189. inline void setUserData(void* userData)
  190. {
  191. _userData = userData;
  192. }
  193. /**
  194. * Get the user-customed data pointer which were pre-setted.
  195. * Don't forget to delete it. HttpClient/HttpResponse/HttpRequest will do nothing with this pointer.
  196. *
  197. * @return void* the pointer of user-customed data.
  198. */
  199. inline void* getUserData() const
  200. {
  201. return _userData;
  202. }
  203. /**
  204. * Set response callback function of HttpRequest object.
  205. * When response come back, we would call _pCallback to process response data.
  206. *
  207. * @param callback the ccHttpRequestCallback function.
  208. */
  209. inline void setResponseCallback(const ccHttpRequestCallback& callback)
  210. {
  211. _callback = callback;
  212. }
  213. /**
  214. * Get ccHttpRequestCallback callback function.
  215. *
  216. * @return const ccHttpRequestCallback& ccHttpRequestCallback callback function.
  217. */
  218. inline const ccHttpRequestCallback& getResponseCallback() const
  219. {
  220. return _callback;
  221. }
  222. /**
  223. * Set custom-defined headers.
  224. *
  225. * @param pHeaders the string vector of custom-defined headers.
  226. */
  227. inline void setHeaders(const std::vector<std::string>& headers)
  228. {
  229. _headers = headers;
  230. }
  231. /**
  232. * Get custom headers.
  233. *
  234. * @return std::vector<std::string> the string vector of custom-defined headers.
  235. */
  236. inline std::vector<std::string> getHeaders() const
  237. {
  238. return _headers;
  239. }
  240. inline void setTimeout(float timeoutInSeconds)
  241. {
  242. _timeoutInSeconds = timeoutInSeconds;
  243. }
  244. inline float getTimeout() const
  245. {
  246. return _timeoutInSeconds;
  247. }
  248. protected:
  249. // properties
  250. Type _requestType; /// kHttpRequestGet, kHttpRequestPost or other enums
  251. std::string _url; /// target url that this request is sent to
  252. std::vector<char> _requestData; /// used for POST
  253. std::string _tag; /// user defined tag, to identify different requests in response callback
  254. ccHttpRequestCallback _callback; /// C++11 style callbacks
  255. void* _userData; /// You can add your customed data here
  256. std::vector<std::string> _headers; /// custom http headers
  257. float _timeoutInSeconds;
  258. };
  259. }
  260. NS_CC_END
  261. // end group
  262. /// @}
  263. #endif //__HTTP_REQUEST_H__