axmol/cocos/network/HttpRequest.h

366 lines
10 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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_REQUEST_H__
#define __HTTP_REQUEST_H__
#include <string>
#include <vector>
#include "base/CCRef.h"
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
2015-03-26 22:07:44 +08:00
/**
* @addtogroup core
* @{
*/
2014-01-02 16:25:35 +08:00
NS_CC_BEGIN
namespace network {
class HttpClient;
class HttpResponse;
typedef std::function<void(HttpClient* client, HttpResponse* response)> ccHttpRequestCallback;
typedef void (cocos2d::Ref::*SEL_HttpResponse)(HttpClient* client, HttpResponse* response);
2014-01-02 21:53:18 +08:00
#define httpresponse_selector(_SELECTOR) (cocos2d::network::SEL_HttpResponse)(&_SELECTOR)
/**
2015-03-19 16:59:21 +08:00
* Defines the object which users must packed for HttpClient::send(HttpRequest*) method.
* Please refer to tests/test-cpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample
* @since v2.0.2
*
* @lua NA
*/
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
#ifdef DELETE
#undef DELETE
#endif
#endif
2015-03-19 16:59:21 +08:00
2014-08-22 13:42:46 +08:00
class CC_DLL HttpRequest : public Ref
{
public:
2015-03-19 16:45:53 +08:00
/**
* The HttpRequest type enum used in the HttpRequest::setRequestType.
*/
enum class Type
{
GET,
POST,
PUT,
DELETE,
UNKNOWN,
};
2015-03-19 16:45:53 +08:00
/**
2015-03-19 16:59:21 +08:00
* Constructor.
* Because HttpRequest object will be used between UI thead and network thread,
requestObj->autorelease() is forbidden to avoid crashes in AutoreleasePool
new/retain/release still works, which means you need to release it manually
Please refer to HttpRequestTest.cpp to find its usage.
*/
HttpRequest()
{
_requestType = Type::UNKNOWN;
_url.clear();
_requestData.clear();
_tag.clear();
_pTarget = nullptr;
_pSelector = nullptr;
_pCallback = nullptr;
_pUserData = nullptr;
};
2015-03-19 16:45:53 +08:00
/** Destructor. */
virtual ~HttpRequest()
{
if (_pTarget)
{
_pTarget->release();
}
};
2015-03-19 16:45:53 +08:00
/**
* Override autorelease method to avoid developers to call it.
* If this function was called, it would trigger assert in debug mode
2015-03-19 16:59:21 +08:00
*
* @return Ref* always return nullptr.
2015-03-19 16:45:53 +08:00
*/
Ref* autorelease(void)
{
CCASSERT(false, "HttpResponse is used between network thread and ui thread \
therefore, autorelease is forbidden here");
return NULL;
}
// setter/getters for properties
2015-03-19 16:45:53 +08:00
/**
* Set request type of HttpRequest object before being sent,now it support the enum value of HttpRequest::Type.
2015-03-19 16:59:21 +08:00
*
* @param type the request type.
*/
inline void setRequestType(Type type)
{
_requestType = type;
};
2015-03-19 16:45:53 +08:00
/**
* Get the request type of HttpRequest object.
2015-03-19 16:59:21 +08:00
*
* @return HttpRequest::Type.
2015-03-19 16:45:53 +08:00
*/
inline Type getRequestType()
{
return _requestType;
};
2015-03-19 16:45:53 +08:00
/**
* Set the url address of HttpRequest object.
* The url value could be like these: "http://httpbin.org/ip" or "https://httpbin.org/get"
2015-03-19 16:59:21 +08:00
*
* @param url the string pointer.
*/
inline void setUrl(const char* url)
{
_url = url;
};
2015-03-19 16:45:53 +08:00
/**
* Get the url address of HttpRequest object.
2015-03-19 16:59:21 +08:00
*
* @return const char* the pointer of _url.
2015-03-19 16:45:53 +08:00
*/
inline const char* getUrl()
{
return _url.c_str();
};
2015-03-19 16:45:53 +08:00
/**
* Set the request data of HttpRequest object.
2015-03-19 16:59:21 +08:00
*
* @param buffer the buffer of request data, it support binary data.
* @param len the size of request data.
*/
2014-03-20 16:24:55 +08:00
inline void setRequestData(const char* buffer, size_t len)
{
_requestData.assign(buffer, buffer + len);
};
2015-03-19 16:45:53 +08:00
/**
2015-03-19 16:59:21 +08:00
* Get the request data pointer of HttpRequest object.
*
* @return char* the request data pointer.
2015-03-19 16:45:53 +08:00
*/
inline char* getRequestData()
{
if(_requestData.size() != 0)
return &(_requestData.front());
return nullptr;
}
2015-03-19 16:45:53 +08:00
/**
* Get the size of request data
2015-03-19 16:59:21 +08:00
*
2015-03-19 16:45:53 +08:00
* @return ssize_t the size of request data
*/
2013-12-12 12:07:20 +08:00
inline ssize_t getRequestDataSize()
{
return _requestData.size();
}
2015-03-19 16:45:53 +08:00
/**
* Set a string tag to identify your request.
2015-03-19 16:59:21 +08:00
* This tag can be found in HttpResponse->getHttpRequest->getTag().
*
2015-03-19 16:45:53 +08:00
* @param tag the string pointer
*/
inline void setTag(const char* tag)
{
_tag = tag;
};
2015-03-19 16:45:53 +08:00
/**
* Get the string tag to identify the request.
2015-03-19 16:59:21 +08:00
* The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback.
*
2015-03-19 16:45:53 +08:00
* @return const char* the pointer of _tag
*/
inline const char* getTag()
{
return _tag.c_str();
};
2015-03-19 16:45:53 +08:00
/**
* Set user-customed data of HttpRequest object.
* You can attach a customed data in each request, and get it back in response callback.
* But you need to new/delete the data pointer manully.
2015-03-19 16:59:21 +08:00
*
2015-03-19 16:45:53 +08:00
* @param pUserData the string pointer
*/
inline void setUserData(void* pUserData)
{
_pUserData = pUserData;
};
2015-03-19 16:45:53 +08:00
/**
* Get the user-customed data pointer which were pre-setted.
* Don't forget to delete it. HttpClient/HttpResponse/HttpRequest will do nothing with this pointer.
2015-03-19 16:59:21 +08:00
*
2015-03-19 16:45:53 +08:00
* @return void* the pointer of user-customed data.
*/
inline void* getUserData()
{
return _pUserData;
};
2015-03-19 16:45:53 +08:00
/**
* Set the target and related callback selector.
* When response come back, it would call (pTarget->*pSelector) to process something.
2015-03-19 16:59:21 +08:00
*
2015-03-19 16:45:53 +08:00
* @param pTarget the target object pointer.
* @param pSelector the callback function.
*/
CC_DEPRECATED_ATTRIBUTE inline void setResponseCallback(Ref* pTarget, SEL_CallFuncND pSelector)
{
setResponseCallback(pTarget, (SEL_HttpResponse) pSelector);
}
2015-03-19 16:45:53 +08:00
/**
* Set the target and related callback selector of HttpRequest object.
* When response come back, we would call (pTarget->*pSelector) to process response data.
2015-03-19 16:59:21 +08:00
*
2015-03-19 16:45:53 +08:00
* @param pTarget the target object pointer.
* @param pSelector the SEL_HttpResponse function.
*/
CC_DEPRECATED_ATTRIBUTE inline void setResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
{
_pTarget = pTarget;
_pSelector = pSelector;
if (_pTarget)
{
_pTarget->retain();
}
}
2015-03-19 16:45:53 +08:00
/**
* Set response callback function of HttpRequest object.
* When response come back, we would call _pCallback to process response data.
2015-03-19 16:59:21 +08:00
*
* @param callback the ccHttpRequestCallback function.
2015-03-19 16:45:53 +08:00
*/
inline void setResponseCallback(const ccHttpRequestCallback& callback)
{
_pCallback = callback;
}
2015-03-19 16:45:53 +08:00
/**
2015-03-19 16:59:21 +08:00
* Get the target of callback selector funtion, mainly used by HttpClient.
*
2015-03-19 16:45:53 +08:00
* @return Ref* the target of callback selector funtion
*/
inline Ref* getTarget()
{
return _pTarget;
}
2015-03-19 16:45:53 +08:00
/**
* This sub class is just for migration SEL_CallFuncND to SEL_HttpResponse,someday this way will be removed.
2015-03-19 16:59:21 +08:00
*
* @lua NA
2015-03-19 16:45:53 +08:00
*/
class _prxy
{
public:
2015-03-19 16:45:53 +08:00
/** Constructor. */
_prxy( SEL_HttpResponse cb ) :_cb(cb) {}
2015-03-19 16:45:53 +08:00
/** Destructor. */
~_prxy(){};
2015-03-19 16:45:53 +08:00
/** Destructor. */
operator SEL_HttpResponse() const { return _cb; }
CC_DEPRECATED_ATTRIBUTE operator SEL_CallFuncND() const { return (SEL_CallFuncND) _cb; }
protected:
SEL_HttpResponse _cb;
};
2015-03-19 16:45:53 +08:00
/**
2015-03-19 16:59:21 +08:00
* Get _prxy object by the _pSelector.
*
2015-03-19 16:45:53 +08:00
* @return _prxy the _prxy object
*/
inline _prxy getSelector()
{
return _prxy(_pSelector);
}
2015-03-19 16:45:53 +08:00
/**
2015-03-19 16:59:21 +08:00
* Get ccHttpRequestCallback callback function.
*
* @return const ccHttpRequestCallback& ccHttpRequestCallback callback function.
2015-03-19 16:45:53 +08:00
*/
inline const ccHttpRequestCallback& getCallback()
{
return _pCallback;
}
2015-03-19 16:45:53 +08:00
/**
2015-03-19 16:59:21 +08:00
* Set custom-defined headers.
*
* @param pHeaders the string vector of custom-defined headers.
2015-03-19 16:45:53 +08:00
*/
inline void setHeaders(std::vector<std::string> pHeaders)
{
_headers=pHeaders;
}
2015-03-19 16:45:53 +08:00
/**
2015-03-19 16:59:21 +08:00
* Get custom headers.
*
* @return std::vector<std::string> the string vector of custom-defined headers.
2015-03-19 16:45:53 +08:00
*/
inline std::vector<std::string> getHeaders()
{
return _headers;
}
protected:
// properties
Type _requestType; /// kHttpRequestGet, kHttpRequestPost or other enums
std::string _url; /// target url that this request is sent to
std::vector<char> _requestData; /// used for POST
std::string _tag; /// user defined tag, to identify different requests in response callback
Ref* _pTarget; /// callback target of pSelector function
SEL_HttpResponse _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(HttpClient *sender, HttpResponse * response)
ccHttpRequestCallback _pCallback; /// C++11 style callbacks
void* _pUserData; /// You can add your customed data here
std::vector<std::string> _headers; /// custom http headers
};
}
2014-01-02 16:25:35 +08:00
NS_CC_END
2015-03-26 22:07:44 +08:00
// end group
/// @}
#endif //__HTTP_REQUEST_H__