HttpResponse.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_RESPONSE__
  23. #define __HTTP_RESPONSE__
  24. #include "network/HttpRequest.h"
  25. /**
  26. * @addtogroup network
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. namespace network {
  31. /**
  32. * @brief defines the object which users will receive at onHttpCompleted(sender, HttpResponse) callback.
  33. * Please refer to samples/TestCpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample.
  34. * @since v2.0.2.
  35. * @lua NA
  36. */
  37. class CC_DLL HttpResponse : public cocos2d::Ref
  38. {
  39. public:
  40. /**
  41. * Constructor, it's used by HttpClient internal, users don't need to create HttpResponse manually.
  42. * @param request the corresponding HttpRequest which leads to this response.
  43. */
  44. HttpResponse(HttpRequest* request)
  45. : _pHttpRequest(request)
  46. , _succeed(false)
  47. , _responseDataString("")
  48. {
  49. if (_pHttpRequest)
  50. {
  51. _pHttpRequest->retain();
  52. }
  53. }
  54. /**
  55. * Destructor, it will be called in HttpClient internal.
  56. * Users don't need to destruct HttpResponse object manually.
  57. */
  58. virtual ~HttpResponse()
  59. {
  60. if (_pHttpRequest)
  61. {
  62. _pHttpRequest->release();
  63. }
  64. }
  65. /**
  66. * Override autorelease method to prevent developers from calling it.
  67. * If this method is called , it would trigger CCASSERT.
  68. * @return cocos2d::Ref* always return nullptr.
  69. */
  70. cocos2d::Ref* autorelease()
  71. {
  72. CCASSERT(false, "HttpResponse is used between network thread and ui thread \
  73. therefore, autorelease is forbidden here");
  74. return nullptr;
  75. }
  76. // getters, will be called by users
  77. /**
  78. * Get the corresponding HttpRequest object which leads to this response.
  79. * There's no paired setter for it, because it's already set in class constructor
  80. * @return HttpRequest* the corresponding HttpRequest object which leads to this response.
  81. */
  82. HttpRequest* getHttpRequest() const
  83. {
  84. return _pHttpRequest;
  85. }
  86. /**
  87. * To see if the http request is returned successfully.
  88. * Although users can judge if (http response code = 200), we want an easier way.
  89. * If this getter returns false, you can call getResponseCode and getErrorBuffer to find more details.
  90. * @return bool the flag that represent whether the http request return successfully or not.
  91. */
  92. bool isSucceed() const
  93. {
  94. return _succeed;
  95. }
  96. /**
  97. * Get the http response data.
  98. * @return std::vector<char>* the pointer that point to the _responseData.
  99. */
  100. std::vector<char>* getResponseData()
  101. {
  102. return &_responseData;
  103. }
  104. /**
  105. * Get the response headers.
  106. * @return std::vector<char>* the pointer that point to the _responseHeader.
  107. */
  108. std::vector<char>* getResponseHeader()
  109. {
  110. return &_responseHeader;
  111. }
  112. /**
  113. * Get the http response code to judge whether response is successful or not.
  114. * I know that you want to see the _responseCode is 200.
  115. * If _responseCode is not 200, you should check the meaning for _responseCode by the net.
  116. * @return long the value of _responseCode
  117. */
  118. long getResponseCode() const
  119. {
  120. return _responseCode;
  121. }
  122. /**
  123. * Get the error buffer which will tell you more about the reason why http request failed.
  124. * @return const char* the pointer that point to _errorBuffer.
  125. */
  126. const char* getErrorBuffer() const
  127. {
  128. return _errorBuffer.c_str();
  129. }
  130. // setters, will be called by HttpClient
  131. // users should avoid invoking these methods
  132. /**
  133. * Set whether the http request is returned successfully or not,
  134. * This setter is mainly used in HttpClient, users mustn't set it directly
  135. * @param value the flag represent whether the http request is successful or not.
  136. */
  137. void setSucceed(bool value)
  138. {
  139. _succeed = value;
  140. }
  141. /**
  142. * Set the http response data buffer, it is used by HttpClient.
  143. * @param data the pointer point to the response data buffer.
  144. */
  145. void setResponseData(std::vector<char>* data)
  146. {
  147. _responseData = *data;
  148. }
  149. /**
  150. * Set the http response headers buffer, it is used by HttpClient.
  151. * @param data the pointer point to the response headers buffer.
  152. */
  153. void setResponseHeader(std::vector<char>* data)
  154. {
  155. _responseHeader = *data;
  156. }
  157. /**
  158. * Set the http response code.
  159. * @param value the http response code that represent whether the request is successful or not.
  160. */
  161. void setResponseCode(long value)
  162. {
  163. _responseCode = value;
  164. }
  165. /**
  166. * Set the error buffer which will tell you more the reason why http request failed.
  167. * @param value a string pointer that point to the reason.
  168. */
  169. void setErrorBuffer(const char* value)
  170. {
  171. _errorBuffer.clear();
  172. _errorBuffer.assign(value);
  173. }
  174. /**
  175. * Set the response data by the string pointer and the defined size.
  176. * @param value a string pointer that point to response data buffer.
  177. * @param n the defined size that the response data buffer would be copied.
  178. */
  179. void setResponseDataString(const char* value, size_t n)
  180. {
  181. _responseDataString.clear();
  182. _responseDataString.assign(value, n);
  183. }
  184. /**
  185. * Get the string pointer that point to the response data.
  186. * @return const char* the string pointer that point to the response data.
  187. */
  188. const char* getResponseDataString() const
  189. {
  190. return _responseDataString.c_str();
  191. }
  192. protected:
  193. bool initWithRequest(HttpRequest* request);
  194. // properties
  195. HttpRequest* _pHttpRequest; /// the corresponding HttpRequest pointer who leads to this response
  196. bool _succeed; /// to indicate if the http request is successful simply
  197. std::vector<char> _responseData; /// the returned raw data. You can also dump it as a string
  198. std::vector<char> _responseHeader; /// the returned raw header data. You can also dump it as a string
  199. long _responseCode; /// the status code returned from libcurl, e.g. 200, 404
  200. std::string _errorBuffer; /// if _responseCode != 200, please read _errorBuffer to find the reason
  201. std::string _responseDataString; // the returned raw data. You can also dump it as a string
  202. };
  203. }
  204. NS_CC_END
  205. // end group
  206. /// @}
  207. #endif //__HTTP_RESPONSE_H__