2012-08-07 18:28:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __HTTP_RESPONSE__
|
|
|
|
#define __HTTP_RESPONSE__
|
|
|
|
|
|
|
|
#include "cocos2d.h"
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "network/HttpRequest.h"
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
namespace network {
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
/**
|
|
|
|
@brief defines the object which users will receive at onHttpCompleted(sender, HttpResponse) callback
|
2012-08-09 10:45:11 +08:00
|
|
|
Please refer to samples/TestCpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample
|
2012-08-08 12:05:02 +08:00
|
|
|
@since v2.0.2
|
|
|
|
*/
|
2013-10-15 18:00:03 +08:00
|
|
|
class HttpResponse : public cocos2d::Object
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:15:53 +08:00
|
|
|
/** Constructor, it's used by HttpClient internal, users don't need to create HttpResponse manually
|
2012-08-08 12:05:02 +08:00
|
|
|
@param request the corresponding HttpRequest which leads to this response
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpResponse(HttpRequest* request)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
_pHttpRequest = request;
|
|
|
|
if (_pHttpRequest)
|
|
|
|
{
|
|
|
|
_pHttpRequest->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
_succeed = false;
|
|
|
|
_responseData.clear();
|
|
|
|
_errorBuffer.clear();
|
|
|
|
}
|
2012-08-08 12:05:02 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
/** Destructor, it will be called in HttpClient internal,
|
2012-08-08 12:05:02 +08:00
|
|
|
users don't need to desturct HttpResponse object manully
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual ~HttpResponse()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
if (_pHttpRequest)
|
|
|
|
{
|
|
|
|
_pHttpRequest->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
/** Override autorelease method to prevent developers from calling it */
|
2013-10-15 18:00:03 +08:00
|
|
|
cocos2d::Object* autorelease(void)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "HttpResponse is used between network thread and ui thread \
|
2012-08-07 18:28:07 +08:00
|
|
|
therefore, autorelease is forbidden here");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
// getters, will be called by users
|
|
|
|
|
|
|
|
/** Get the corresponding HttpRequest object which leads to this response
|
|
|
|
There's no paired setter for it, coz it's already setted in class constructor
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
inline HttpRequest* getHttpRequest()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
return _pHttpRequest;
|
|
|
|
}
|
2012-08-08 12:05:02 +08:00
|
|
|
|
|
|
|
/** To see if the http reqeust is returned successfully,
|
|
|
|
Althrough users can judge if (http return code = 200), we want an easier way
|
|
|
|
If this getter returns false, you can call getResponseCode and getErrorBuffer to find more details
|
|
|
|
*/
|
|
|
|
inline bool isSucceed()
|
|
|
|
{
|
|
|
|
return _succeed;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Get the http response raw data */
|
|
|
|
inline std::vector<char>* getResponseData()
|
|
|
|
{
|
|
|
|
return &_responseData;
|
|
|
|
}
|
2013-05-13 22:57:07 +08:00
|
|
|
|
|
|
|
/** get the Rawheader **/
|
|
|
|
inline std::vector<char>* getResponseHeader()
|
|
|
|
{
|
|
|
|
return &_responseHeader;
|
|
|
|
}
|
2012-08-08 12:05:02 +08:00
|
|
|
|
|
|
|
/** Get the http response errorCode
|
|
|
|
* I know that you want to see http 200 :)
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
inline long getResponseCode()
|
2012-08-08 12:05:02 +08:00
|
|
|
{
|
|
|
|
return _responseCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the rror buffer which will tell you more about the reason why http request failed
|
|
|
|
*/
|
|
|
|
inline const char* getErrorBuffer()
|
|
|
|
{
|
|
|
|
return _errorBuffer.c_str();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
// setters, will be called by HttpClient
|
2012-08-08 12:05:02 +08:00
|
|
|
// users should avoid invoking these methods
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
/** Set if the http request is returned successfully,
|
|
|
|
Althrough users can judge if (http code == 200), we want a easier way
|
2013-06-20 14:15:53 +08:00
|
|
|
This setter is mainly used in HttpClient, users mustn't set it directly
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2012-08-07 18:28:07 +08:00
|
|
|
inline void setSucceed(bool value)
|
|
|
|
{
|
|
|
|
_succeed = value;
|
|
|
|
};
|
2012-08-08 12:05:02 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
/** Set the http response raw buffer, is used by HttpClient
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2012-08-07 18:28:07 +08:00
|
|
|
inline void setResponseData(std::vector<char>* data)
|
|
|
|
{
|
|
|
|
_responseData = *data;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
/** Set the http response Header raw buffer, is used by HttpClient
|
2013-05-13 22:57:07 +08:00
|
|
|
*/
|
|
|
|
inline void setResponseHeader(std::vector<char>* data)
|
|
|
|
{
|
|
|
|
_responseHeader = *data;
|
|
|
|
}
|
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
|
|
|
|
/** Set the http response errorCode
|
|
|
|
*/
|
2013-12-12 12:07:20 +08:00
|
|
|
inline void setResponseCode(long value)
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
_responseCode = value;
|
|
|
|
}
|
|
|
|
|
2012-08-08 12:05:02 +08:00
|
|
|
|
|
|
|
/** Set the error buffer which will tell you more the reason why http request failed
|
|
|
|
*/
|
2012-08-07 18:28:07 +08:00
|
|
|
inline void setErrorBuffer(const char* value)
|
|
|
|
{
|
|
|
|
_errorBuffer.clear();
|
|
|
|
_errorBuffer.assign(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
2013-06-20 14:15:53 +08:00
|
|
|
bool initWithRequest(HttpRequest* request);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
// properties
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpRequest* _pHttpRequest; /// the corresponding HttpRequest pointer who leads to this response
|
2012-08-08 12:05:02 +08:00
|
|
|
bool _succeed; /// to indecate if the http reqeust is successful simply
|
|
|
|
std::vector<char> _responseData; /// the returned raw data. You can also dump it as a string
|
2013-05-13 22:57:07 +08:00
|
|
|
std::vector<char> _responseHeader; /// the returned raw header data. You can also dump it as a string
|
2013-12-12 12:07:20 +08:00
|
|
|
long _responseCode; /// the status code returned from libcurl, e.g. 200, 404
|
2012-08-07 18:28:07 +08:00
|
|
|
std::string _errorBuffer; /// if _responseCode != 200, please read _errorBuffer to find the reason
|
2012-08-08 12:05:02 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
};
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
#endif //__HTTP_RESPONSE_H__
|