HttpRequest.h
9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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"
#include "base/ccMacros.h"
/**
* @addtogroup network
* @{
*/
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);
#define httpresponse_selector(_SELECTOR) (cocos2d::network::SEL_HttpResponse)(&_SELECTOR)
/**
* 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
*/
class CC_DLL HttpRequest : public Ref
{
public:
/**
* The HttpRequest type enum used in the HttpRequest::setRequestType.
*/
enum class Type
{
GET,
POST,
PUT,
DELETE,
UNKNOWN,
};
/**
* Constructor.
* Because HttpRequest object will be used between UI thread 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)
, _pTarget(nullptr)
, _pSelector(nullptr)
, _pCallback(nullptr)
, _pUserData(nullptr)
{
}
/** Destructor. */
virtual ~HttpRequest()
{
if (_pTarget)
{
_pTarget->release();
}
}
/**
* Override autorelease method to avoid developers to call it.
* If this function was called, it would trigger assert in debug mode
*
* @return Ref* always return nullptr.
*/
Ref* autorelease()
{
CCASSERT(false, "HttpResponse is used between network thread and ui thread \
therefore, autorelease is forbidden here");
return nullptr;
}
// setter/getters for properties
/**
* Set request type of HttpRequest object before being sent,now it support the enum value of HttpRequest::Type.
*
* @param type the request type.
*/
void setRequestType(Type type)
{
_requestType = type;
}
/**
* Get the request type of HttpRequest object.
*
* @return HttpRequest::Type.
*/
Type getRequestType() const
{
return _requestType;
}
/**
* Set the url address of HttpRequest object.
* The url value could be like these: "http://httpbin.org/ip" or "https://httpbin.org/get"
*
* @param url the string object.
*/
void setUrl(const std::string& url)
{
_url = url;
}
/**
* Get the url address of HttpRequest object.
*
* @return const char* the pointer of _url.
*/
const char* getUrl() const
{
return _url.c_str();
}
/**
* Set the request data of HttpRequest object.
*
* @param buffer the buffer of request data, it support binary data.
* @param len the size of request data.
*/
void setRequestData(const char* buffer, size_t len)
{
_requestData.assign(buffer, buffer + len);
}
/**
* Get the request data pointer of HttpRequest object.
*
* @return char* the request data pointer.
*/
char* getRequestData()
{
if(!_requestData.empty())
return _requestData.data();
return nullptr;
}
/**
* Get the size of request data
*
* @return ssize_t the size of request data
*/
ssize_t getRequestDataSize() const
{
return _requestData.size();
}
/**
* Set a string tag to identify your request.
* This tag can be found in HttpResponse->getHttpRequest->getTag().
*
* @param tag the string object.
*/
void setTag(const std::string& tag)
{
_tag = tag;
}
/**
* Get the string tag to identify the request.
* The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback.
*
* @return const char* the pointer of _tag
*/
const char* getTag() const
{
return _tag.c_str();
}
/**
* 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 manually.
*
* @param pUserData the string pointer
*/
void setUserData(void* pUserData)
{
_pUserData = pUserData;
}
/**
* 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.
*
* @return void* the pointer of user-customed data.
*/
void* getUserData() const
{
return _pUserData;
}
/**
* Set the target and related callback selector of HttpRequest object.
* When response come back, we would call (pTarget->*pSelector) to process response data.
*
* @param pTarget the target object pointer.
* @param pSelector the SEL_HttpResponse function.
*/
void setResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
{
doSetResponseCallback(pTarget, pSelector);
}
/**
* Set response callback function of HttpRequest object.
* When response come back, we would call _pCallback to process response data.
*
* @param callback the ccHttpRequestCallback function.
*/
void setResponseCallback(const ccHttpRequestCallback& callback)
{
_pCallback = callback;
}
/**
* Get the target of callback selector function, mainly used by HttpClient.
*
* @return Ref* the target of callback selector function
*/
Ref* getTarget() const
{
return _pTarget;
}
/**
* This sub class is just for migration SEL_CallFuncND to SEL_HttpResponse,someday this way will be removed.
*
* @lua NA
*/
class _prxy
{
public:
/** Constructor. */
_prxy( SEL_HttpResponse cb ) :_cb(cb) {}
/** Destructor. */
~_prxy(){};
operator SEL_HttpResponse() const { return _cb; }
protected:
SEL_HttpResponse _cb;
};
/**
* Get _prxy object by the _pSelector.
*
* @return _prxy the _prxy object
*/
_prxy getSelector() const
{
return _prxy(_pSelector);
}
/**
* Get ccHttpRequestCallback callback function.
*
* @return const ccHttpRequestCallback& ccHttpRequestCallback callback function.
*/
const ccHttpRequestCallback& getCallback() const
{
return _pCallback;
}
/**
* Set custom-defined headers.
*
* @param headers The string vector of custom-defined headers.
*/
void setHeaders(const std::vector<std::string>& headers)
{
_headers = headers;
}
/**
* Get custom headers.
*
* @return std::vector<std::string> the string vector of custom-defined headers.
*/
std::vector<std::string> getHeaders() const
{
return _headers;
}
private:
void doSetResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
{
if (_pTarget)
{
_pTarget->release();
}
_pTarget = pTarget;
_pSelector = pSelector;
if (_pTarget)
{
_pTarget->retain();
}
}
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
};
}
NS_CC_END
// end group
/// @}
#endif //__HTTP_REQUEST_H__