HttpClient-apple.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /****************************************************************************
  2. Copyright (c) 2012 greathqy
  3. Copyright (c) 2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "platform/CCPlatformConfig.h"
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  25. #include "network/HttpClient.h"
  26. #include <queue>
  27. #include <errno.h>
  28. #import "network/HttpAsynConnection-apple.h"
  29. #include "network/HttpCookie.h"
  30. #include "base/CCDirector.h"
  31. #include "platform/CCFileUtils.h"
  32. NS_CC_BEGIN
  33. namespace network {
  34. static HttpClient *_httpClient = nullptr; // pointer to singleton
  35. static int processTask(HttpClient* client, HttpRequest *request, NSString *requestType, void *stream, long *errorCode, void *headerStream, char *errorBuffer);
  36. // Worker thread
  37. void HttpClient::networkThread()
  38. {
  39. increaseThreadCount();
  40. while (true) @autoreleasepool {
  41. HttpRequest *request;
  42. // step 1: send http request if the requestQueue isn't empty
  43. {
  44. std::lock_guard<std::mutex> lock(_requestQueueMutex);
  45. while (_requestQueue.empty()) {
  46. _sleepCondition.wait(_requestQueueMutex);
  47. }
  48. request = _requestQueue.at(0);
  49. _requestQueue.erase(0);
  50. }
  51. if (request == _requestSentinel) {
  52. break;
  53. }
  54. // Create a HttpResponse object, the default setting is http access failed
  55. HttpResponse *response = new (std::nothrow) HttpResponse(request);
  56. processResponse(response, _responseMessage);
  57. // add response packet into queue
  58. _responseQueueMutex.lock();
  59. _responseQueue.pushBack(response);
  60. _responseQueueMutex.unlock();
  61. _schedulerMutex.lock();
  62. if (nullptr != _scheduler)
  63. {
  64. _scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
  65. }
  66. _schedulerMutex.unlock();
  67. }
  68. // cleanup: if worker thread received quit signal, clean up un-completed request queue
  69. _requestQueueMutex.lock();
  70. _requestQueue.clear();
  71. _requestQueueMutex.unlock();
  72. _responseQueueMutex.lock();
  73. _responseQueue.clear();
  74. _responseQueueMutex.unlock();
  75. decreaseThreadCountAndMayDeleteThis();
  76. }
  77. // Worker thread
  78. void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response)
  79. {
  80. increaseThreadCount();
  81. char responseMessage[RESPONSE_BUFFER_SIZE] = { 0 };
  82. processResponse(response, responseMessage);
  83. _schedulerMutex.lock();
  84. if (nullptr != _scheduler)
  85. {
  86. _scheduler->performFunctionInCocosThread([this, response, request]{
  87. const ccHttpRequestCallback& callback = request->getCallback();
  88. Ref* pTarget = request->getTarget();
  89. SEL_HttpResponse pSelector = request->getSelector();
  90. if (callback != nullptr)
  91. {
  92. callback(this, response);
  93. }
  94. else if (pTarget && pSelector)
  95. {
  96. (pTarget->*pSelector)(this, response);
  97. }
  98. response->release();
  99. // do not release in other thread
  100. request->release();
  101. });
  102. }
  103. _schedulerMutex.unlock();
  104. decreaseThreadCountAndMayDeleteThis();
  105. }
  106. //Process Request
  107. static int processTask(HttpClient* client, HttpRequest* request, NSString* requestType, void* stream, long* responseCode, void* headerStream, char* errorBuffer)
  108. {
  109. if (nullptr == client)
  110. {
  111. strcpy(errorBuffer, "client object is invalid");
  112. return 0;
  113. }
  114. //create request with url
  115. NSString* urlstring = [NSString stringWithUTF8String:request->getUrl()];
  116. NSURL *url = [NSURL URLWithString:urlstring];
  117. NSMutableURLRequest *nsrequest = [NSMutableURLRequest requestWithURL:url
  118. cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
  119. timeoutInterval:HttpClient::getInstance()->getTimeoutForConnect()];
  120. //set request type
  121. [nsrequest setHTTPMethod:requestType];
  122. NSString* sysUA = nsrequest.allHTTPHeaderFields[@"User-Agent"];
  123. /* get custom header data (if set) */
  124. std::vector<std::string> headers=request->getHeaders();
  125. if(!headers.empty())
  126. {
  127. /* append custom headers one by one */
  128. for (auto& header : headers)
  129. {
  130. unsigned long i = header.find(':', 0);
  131. unsigned long length = header.size();
  132. std::string field = header.substr(0, i);
  133. std::string value = header.substr(i+1, length-i);
  134. NSString *headerField = [NSString stringWithUTF8String:field.c_str()];
  135. NSString *headerValue = [NSString stringWithUTF8String:value.c_str()];
  136. [nsrequest setValue:headerValue forHTTPHeaderField:headerField];
  137. }
  138. }
  139. else
  140. {
  141. //设置User-Agent
  142. NSString *userAgent = nil;
  143. #if TARGET_OS_IOS
  144. // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
  145. userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
  146. #elif TARGET_OS_WATCH
  147. // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
  148. userAgent = [NSString stringWithFormat:@"%@/%@ (%@; watchOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[WKInterfaceDevice currentDevice] model], [[WKInterfaceDevice currentDevice] systemVersion], [[WKInterfaceDevice currentDevice] screenScale]];
  149. #elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
  150. userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]];
  151. #endif
  152. if(userAgent)
  153. {
  154. [nsrequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
  155. }
  156. }
  157. //if request type is post or put,set header and data
  158. if([requestType isEqual: @"POST"] || [requestType isEqual: @"PUT"])
  159. {
  160. if ([requestType isEqual: @"PUT"])
  161. {
  162. [nsrequest setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
  163. }
  164. char* requestDataBuffer = request->getRequestData();
  165. if (nullptr != requestDataBuffer && 0 != request->getRequestDataSize())
  166. {
  167. NSData *postData = [NSData dataWithBytes:requestDataBuffer length:request->getRequestDataSize()];
  168. [nsrequest setHTTPBody:postData];
  169. }
  170. }
  171. //read cookie properties from file and set cookie
  172. std::string cookieFilename = client->getCookieFilename();
  173. if(!cookieFilename.empty() && nullptr != client->getCookie())
  174. {
  175. const CookiesInfo* cookieInfo = client->getCookie()->getMatchCookie(request->getUrl());
  176. if(cookieInfo != nullptr)
  177. {
  178. NSString *domain = [NSString stringWithCString:cookieInfo->domain.c_str() encoding:[NSString defaultCStringEncoding]];
  179. NSString *path = [NSString stringWithCString:cookieInfo->path.c_str() encoding:[NSString defaultCStringEncoding]];
  180. NSString *value = [NSString stringWithCString:cookieInfo->value.c_str() encoding:[NSString defaultCStringEncoding]];
  181. NSString *name = [NSString stringWithCString:cookieInfo->name.c_str() encoding:[NSString defaultCStringEncoding]];
  182. // create the properties for a cookie
  183. NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: name,NSHTTPCookieName,
  184. value, NSHTTPCookieValue, path, NSHTTPCookiePath,
  185. domain, NSHTTPCookieDomain,
  186. nil];
  187. // create the cookie from the properties
  188. NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
  189. // add the cookie to the cookie storage
  190. [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
  191. }
  192. }
  193. HttpAsynConnection *httpAsynConn = [[HttpAsynConnection new] autorelease];
  194. httpAsynConn.srcURL = urlstring;
  195. httpAsynConn.sslFile = nil;
  196. std::string sslCaFileName = client->getSSLVerification();
  197. if(!sslCaFileName.empty())
  198. {
  199. long len = sslCaFileName.length();
  200. long pos = sslCaFileName.rfind('.', len-1);
  201. httpAsynConn.sslFile = [NSString stringWithUTF8String:sslCaFileName.substr(0, pos).c_str()];
  202. }
  203. [httpAsynConn startRequest:nsrequest];
  204. while( httpAsynConn.finish != true)
  205. {
  206. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  207. }
  208. //if http connection return error
  209. if (httpAsynConn.connError != nil)
  210. {
  211. NSString* errorString = [httpAsynConn.connError localizedDescription];
  212. strcpy(errorBuffer, [errorString UTF8String]);
  213. return 0;
  214. }
  215. //if http response got error, just log the error
  216. if (httpAsynConn.responseError != nil)
  217. {
  218. NSString* errorString = [httpAsynConn.responseError localizedDescription];
  219. strcpy(errorBuffer, [errorString UTF8String]);
  220. }
  221. *responseCode = httpAsynConn.responseCode;
  222. //add cookie to cookies vector
  223. if(!cookieFilename.empty())
  224. {
  225. NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpAsynConn.responseHeader forURL:url];
  226. for (NSHTTPCookie *cookie in cookies)
  227. {
  228. //NSLog(@"Cookie: %@", cookie);
  229. NSString *domain = cookie.domain;
  230. //BOOL session = cookie.sessionOnly;
  231. NSString *path = cookie.path;
  232. BOOL secure = cookie.isSecure;
  233. NSDate *date = cookie.expiresDate;
  234. NSString *name = cookie.name;
  235. NSString *value = cookie.value;
  236. CookiesInfo cookieInfo;
  237. cookieInfo.domain = [domain cStringUsingEncoding: NSUTF8StringEncoding];
  238. cookieInfo.path = [path cStringUsingEncoding: NSUTF8StringEncoding];
  239. cookieInfo.secure = (secure == YES) ? true : false;
  240. cookieInfo.expires = [[NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]] cStringUsingEncoding: NSUTF8StringEncoding];
  241. cookieInfo.name = [name cStringUsingEncoding: NSUTF8StringEncoding];
  242. cookieInfo.value = [value cStringUsingEncoding: NSUTF8StringEncoding];
  243. cookieInfo.tailmatch = true;
  244. client->getCookie()->updateOrAddCookie(&cookieInfo);
  245. }
  246. }
  247. //handle response header
  248. NSMutableString *header = [NSMutableString string];
  249. [header appendFormat:@"HTTP/1.1 %ld %@\n", (long)httpAsynConn.responseCode, httpAsynConn.statusString];
  250. for (id key in httpAsynConn.responseHeader)
  251. {
  252. [header appendFormat:@"%@: %@\n", key, [httpAsynConn.responseHeader objectForKey:key]];
  253. }
  254. if (header.length > 0)
  255. {
  256. NSRange range = NSMakeRange(header.length-1, 1);
  257. [header deleteCharactersInRange:range];
  258. }
  259. NSData *headerData = [header dataUsingEncoding:NSUTF8StringEncoding];
  260. std::vector<char> *headerBuffer = (std::vector<char>*)headerStream;
  261. const void* headerptr = [headerData bytes];
  262. long headerlen = [headerData length];
  263. headerBuffer->insert(headerBuffer->end(), (char*)headerptr, (char*)headerptr+headerlen);
  264. //handle response data
  265. std::vector<char> *recvBuffer = (std::vector<char>*)stream;
  266. const void* ptr = [httpAsynConn.responseData bytes];
  267. long len = [httpAsynConn.responseData length];
  268. recvBuffer->insert(recvBuffer->end(), (char*)ptr, (char*)ptr+len);
  269. return 1;
  270. }
  271. // HttpClient implementation
  272. HttpClient* HttpClient::getInstance()
  273. {
  274. if (_httpClient == nullptr)
  275. {
  276. _httpClient = new (std::nothrow) HttpClient();
  277. }
  278. return _httpClient;
  279. }
  280. void HttpClient::destroyInstance()
  281. {
  282. if (nullptr == _httpClient)
  283. {
  284. CCLOG("HttpClient singleton is nullptr");
  285. return;
  286. }
  287. CCLOG("HttpClient::destroyInstance begin");
  288. auto thiz = _httpClient;
  289. _httpClient = nullptr;
  290. thiz->_scheduler->unscheduleAllForTarget(thiz);
  291. thiz->_schedulerMutex.lock();
  292. thiz->_scheduler = nullptr;
  293. thiz->_schedulerMutex.unlock();
  294. thiz->_requestQueueMutex.lock();
  295. thiz->_requestQueue.pushBack(thiz->_requestSentinel);
  296. thiz->_requestQueueMutex.unlock();
  297. thiz->_sleepCondition.notify_one();
  298. thiz->decreaseThreadCountAndMayDeleteThis();
  299. CCLOG("HttpClient::destroyInstance() finished!");
  300. }
  301. void HttpClient::enableCookies(const char* cookieFile)
  302. {
  303. _cookieFileMutex.lock();
  304. if (cookieFile)
  305. {
  306. _cookieFilename = std::string(cookieFile);
  307. _cookieFilename = FileUtils::getInstance()->fullPathForFilename(_cookieFilename);
  308. }
  309. else
  310. {
  311. _cookieFilename = (FileUtils::getInstance()->getWritablePath() + "cookieFile.txt");
  312. }
  313. _cookieFileMutex.unlock();
  314. if (nullptr == _cookie)
  315. {
  316. _cookie = new(std::nothrow)HttpCookie;
  317. }
  318. _cookie->setCookieFileName(_cookieFilename);
  319. _cookie->readFile();
  320. }
  321. void HttpClient::setSSLVerification(const std::string& caFile)
  322. {
  323. std::lock_guard<std::mutex> lock(_sslCaFileMutex);
  324. _sslCaFilename = caFile;
  325. }
  326. HttpClient::HttpClient()
  327. : _isInited(false)
  328. , _timeoutForConnect(30)
  329. , _timeoutForRead(60)
  330. , _threadCount(0)
  331. , _cookie(nullptr)
  332. , _requestSentinel(new HttpRequest())
  333. {
  334. CCLOG("In the constructor of HttpClient!");
  335. memset(_responseMessage, 0, sizeof(char) * RESPONSE_BUFFER_SIZE);
  336. _scheduler = Director::getInstance()->getScheduler();
  337. increaseThreadCount();
  338. }
  339. HttpClient::~HttpClient()
  340. {
  341. CC_SAFE_RELEASE(_requestSentinel);
  342. if (!_cookieFilename.empty() && nullptr != _cookie)
  343. {
  344. _cookie->writeFile();
  345. CC_SAFE_DELETE(_cookie);
  346. }
  347. CCLOG("HttpClient destructor");
  348. }
  349. //Lazy create semaphore & mutex & thread
  350. bool HttpClient::lazyInitThreadSemaphore()
  351. {
  352. if (_isInited)
  353. {
  354. return true;
  355. }
  356. else
  357. {
  358. auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this));
  359. t.detach();
  360. _isInited = true;
  361. }
  362. return true;
  363. }
  364. //Add a get task to queue
  365. void HttpClient::send(HttpRequest* request)
  366. {
  367. if (false == lazyInitThreadSemaphore())
  368. {
  369. return;
  370. }
  371. if (!request)
  372. {
  373. return;
  374. }
  375. request->retain();
  376. _requestQueueMutex.lock();
  377. _requestQueue.pushBack(request);
  378. _requestQueueMutex.unlock();
  379. // Notify thread start to work
  380. _sleepCondition.notify_one();
  381. }
  382. void HttpClient::sendImmediate(HttpRequest* request)
  383. {
  384. if(!request)
  385. {
  386. return;
  387. }
  388. request->retain();
  389. // Create a HttpResponse object, the default setting is http access failed
  390. HttpResponse *response = new (std::nothrow) HttpResponse(request);
  391. auto t = std::thread(&HttpClient::networkThreadAlone, this, request, response);
  392. t.detach();
  393. }
  394. // Poll and notify main thread if responses exists in queue
  395. void HttpClient::dispatchResponseCallbacks()
  396. {
  397. // log("CCHttpClient::dispatchResponseCallbacks is running");
  398. //occurs when cocos thread fires but the network thread has already quited
  399. HttpResponse* response = nullptr;
  400. _responseQueueMutex.lock();
  401. if (!_responseQueue.empty())
  402. {
  403. response = _responseQueue.at(0);
  404. _responseQueue.erase(0);
  405. }
  406. _responseQueueMutex.unlock();
  407. if (response)
  408. {
  409. HttpRequest *request = response->getHttpRequest();
  410. const ccHttpRequestCallback& callback = request->getCallback();
  411. Ref* pTarget = request->getTarget();
  412. SEL_HttpResponse pSelector = request->getSelector();
  413. if (callback != nullptr)
  414. {
  415. callback(this, response);
  416. }
  417. else if (pTarget && pSelector)
  418. {
  419. (pTarget->*pSelector)(this, response);
  420. }
  421. response->release();
  422. // do not release in other thread
  423. request->release();
  424. }
  425. }
  426. // Process Response
  427. void HttpClient::processResponse(HttpResponse* response, char* responseMessage)
  428. {
  429. auto request = response->getHttpRequest();
  430. long responseCode = -1;
  431. int retValue = 0;
  432. NSString* requestType = nil;
  433. // Process the request -> get response packet
  434. switch (request->getRequestType())
  435. {
  436. case HttpRequest::Type::GET: // HTTP GET
  437. requestType = @"GET";
  438. break;
  439. case HttpRequest::Type::POST: // HTTP POST
  440. requestType = @"POST";
  441. break;
  442. case HttpRequest::Type::PUT:
  443. requestType = @"PUT";
  444. break;
  445. case HttpRequest::Type::DELETE:
  446. requestType = @"DELETE";
  447. break;
  448. default:
  449. CCASSERT(false, "CCHttpClient: unknown request type, only GET,POST,PUT or DELETE is supported");
  450. break;
  451. }
  452. retValue = processTask(this,
  453. request,
  454. requestType,
  455. response->getResponseData(),
  456. &responseCode,
  457. response->getResponseHeader(),
  458. responseMessage);
  459. // write data to HttpResponse
  460. response->setResponseCode(responseCode);
  461. if (retValue != 0)
  462. {
  463. response->setSucceed(true);
  464. }
  465. else
  466. {
  467. response->setSucceed(false);
  468. response->setErrorBuffer(responseMessage);
  469. }
  470. }
  471. void HttpClient::increaseThreadCount()
  472. {
  473. _threadCountMutex.lock();
  474. ++_threadCount;
  475. _threadCountMutex.unlock();
  476. }
  477. void HttpClient::decreaseThreadCountAndMayDeleteThis()
  478. {
  479. bool needDeleteThis = false;
  480. _threadCountMutex.lock();
  481. --_threadCount;
  482. if (0 == _threadCount)
  483. {
  484. needDeleteThis = true;
  485. }
  486. _threadCountMutex.unlock();
  487. if (needDeleteThis)
  488. {
  489. delete this;
  490. }
  491. }
  492. void HttpClient::setTimeoutForConnect(int value)
  493. {
  494. std::lock_guard<std::mutex> lock(_timeoutForConnectMutex);
  495. _timeoutForConnect = value;
  496. }
  497. int HttpClient::getTimeoutForConnect()
  498. {
  499. std::lock_guard<std::mutex> lock(_timeoutForConnectMutex);
  500. return _timeoutForConnect;
  501. }
  502. void HttpClient::setTimeoutForRead(int value)
  503. {
  504. std::lock_guard<std::mutex> lock(_timeoutForReadMutex);
  505. _timeoutForRead = value;
  506. }
  507. int HttpClient::getTimeoutForRead()
  508. {
  509. std::lock_guard<std::mutex> lock(_timeoutForReadMutex);
  510. return _timeoutForRead;
  511. }
  512. const std::string& HttpClient::getCookieFilename()
  513. {
  514. std::lock_guard<std::mutex> lock(_cookieFileMutex);
  515. return _cookieFilename;
  516. }
  517. const std::string& HttpClient::getSSLVerification()
  518. {
  519. std::lock_guard<std::mutex> lock(_sslCaFileMutex);
  520. return _sslCaFilename;
  521. }
  522. }
  523. NS_CC_END
  524. #endif // #if CC_TARGET_PLATFORM == CC_PLATFORM_MAC