[ci skip][lua] Bug fix in xhr:getResponseHeader("xxx") and better coding particle. (#18252)

* [lua] Bug fix in xhr:getResponseHeader("xxx") and better coding particle.

* More const auto &.
This commit is contained in:
James Chen 2017-09-12 21:54:35 -05:00 committed by minggo
parent e161b613a4
commit 2fbb6fe32a
2 changed files with 126 additions and 134 deletions

View File

@ -22,16 +22,110 @@
THE SOFTWARE. THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#include "scripting/lua-bindings/manual/network/lua_xml_http_request.h" #include "scripting/lua-bindings/manual/network/lua_xml_http_request.h"
#include <string>
#include "scripting/lua-bindings/manual/tolua_fix.h" #include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/CCLuaStack.h" #include "scripting/lua-bindings/manual/CCLuaStack.h"
#include "scripting/lua-bindings/manual/CCLuaValue.h" #include "scripting/lua-bindings/manual/CCLuaValue.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h" #include "scripting/lua-bindings/manual/CCLuaEngine.h"
#include "scripting/lua-bindings/manual/cocos2d/LuaScriptHandlerMgr.h" #include "scripting/lua-bindings/manual/cocos2d/LuaScriptHandlerMgr.h"
#include "network/HttpClient.h"
#include <unordered_map>
#include <string>
#include <sstream>
using namespace cocos2d; using namespace cocos2d;
using namespace std;
class LuaMinXmlHttpRequest : public cocos2d::Ref
{
public:
enum class ResponseType
{
STRING,
ARRAY_BUFFER,
BLOB,
DOCUMENT,
JSON
};
// Ready States (http://www.w3.org/TR/XMLHttpRequest/#interface-xmlhttprequest)
static const unsigned short UNSENT = 0;
static const unsigned short OPENED = 1;
static const unsigned short HEADERS_RECEIVED = 2;
static const unsigned short LOADING = 3;
static const unsigned short DONE = 4;
LuaMinXmlHttpRequest();
~LuaMinXmlHttpRequest();
inline void setResponseType(ResponseType type) { _responseType = type; }
inline ResponseType getResponseType() const { return _responseType; }
inline void setWithCredentialsValue(bool value) { _withCredentialsValue = value; }
inline bool getWithCredentialsValue() const { return _withCredentialsValue; }
inline void setTimeout(unsigned timeOut) {_timeout = timeOut; }
inline unsigned getTimeout() const { return _timeout;}
inline void setReadyState(int readyState) { _readyState = readyState; }
inline int getReadyState() const { return _readyState ;}
inline cocos2d::network::HttpRequest* getHttpRequest() const { return _httpRequest; }
inline const std::string& getStatusText() const { return _statusText ;}
inline void setStatus(int status) { _status = status; }
inline int getStatus() { return _status; }
inline const std::string& getUrl() { return _url; }
inline void setUrl(const std::string& url) { _url = url ;}
inline const std::string& getMethod() const { return _meth;}
inline void setMethod(const std::string& meth) { _meth = meth ; }
inline void setAsync(bool isAsync) { _isAsync = isAsync; }
inline void setIsNetWork(bool isNetWork) {_isNetwork = isNetWork; }
void _setHttpRequestHeader();
void _sendRequest();
void setRequestHeader(const char* field, const char* value);
const std::unordered_map<std::string, std::string>& getHttpHeader() const { return _httpHeader ;}
void clearHttpHeader() { _httpHeader.clear(); }
void getByteData(unsigned char* byteData) const;
inline const std::string& getDataStr() const { return _data; }
inline size_t getDataSize() const { return _dataSize; }
inline void setErrorFlag(bool errorFlag) { _errorFlag = errorFlag; }
inline bool getErrorFlag() const { return _errorFlag; }
inline void setAborted(bool isAborted) { _isAborted = isAborted; }
inline bool isAborted() const { return _isAborted; }
private:
void _gotHeader(const std::string& header);
std::string _url;
std::string _meth;
std::string _type;
std::string _data;
size_t _dataSize;
int _readyState;
int _status;
std::string _statusText;
ResponseType _responseType;
unsigned _timeout;
bool _isAsync;
cocos2d::network::HttpRequest* _httpRequest;
bool _isNetwork;
bool _withCredentialsValue;
std::unordered_map<std::string, std::string> _httpHeader;
std::unordered_map<std::string, std::string> _requestHeader;
bool _errorFlag;
bool _isAborted;
};
LuaMinXmlHttpRequest::LuaMinXmlHttpRequest() LuaMinXmlHttpRequest::LuaMinXmlHttpRequest()
: :
@ -66,7 +160,7 @@ LuaMinXmlHttpRequest::~LuaMinXmlHttpRequest()
* @brief Implementation for header retrieving. * @brief Implementation for header retrieving.
* @param header * @param header
*/ */
void LuaMinXmlHttpRequest::_gotHeader(string header) void LuaMinXmlHttpRequest::_gotHeader(const std::string& header)
{ {
// Get Header and Set StatusText // Get Header and Set StatusText
// Split String into Tokens // Split String into Tokens
@ -78,8 +172,8 @@ void LuaMinXmlHttpRequest::_gotHeader(string header)
if (found_header_field != std::string::npos) if (found_header_field != std::string::npos)
{ {
// Found a header field. // Found a header field.
string http_field; std::string http_field;
string http_value; std::string http_value;
http_field = header.substr(0,found_header_field); http_field = header.substr(0,found_header_field);
http_value = header.substr(found_header_field+1, header.length()); http_value = header.substr(found_header_field+1, header.length());
@ -99,11 +193,10 @@ void LuaMinXmlHttpRequest::_gotHeader(string header)
strcpy(cstr, header.c_str()); strcpy(cstr, header.c_str());
pch = strtok(cstr," "); pch = strtok(cstr," ");
while (pch != NULL) while (pch != nullptr)
{ {
std::stringstream ss;
stringstream ss; std::string val;
string val;
ss << pch; ss << pch;
val = ss.str(); val = ss.str();
@ -112,7 +205,7 @@ void LuaMinXmlHttpRequest::_gotHeader(string header)
// Check for HTTP Header to set statusText // Check for HTTP Header to set statusText
if (found_http != std::string::npos) { if (found_http != std::string::npos) {
stringstream mystream; std::stringstream mystream;
// Get Response Status // Get Response Status
pch = strtok (NULL, " "); pch = strtok (NULL, " ");
@ -139,11 +232,11 @@ void LuaMinXmlHttpRequest::_gotHeader(string header)
*/ */
void LuaMinXmlHttpRequest::setRequestHeader(const char* field, const char* value) void LuaMinXmlHttpRequest::setRequestHeader(const char* field, const char* value)
{ {
stringstream header_s; std::stringstream header_s;
stringstream value_s; std::stringstream value_s;
string header; std::string header;
map<string, string>::iterator iter = _requestHeader.find(field); auto iter = _requestHeader.find(field);
// Concatenate values when header exists. // Concatenate values when header exists.
if (iter != _requestHeader.end()) if (iter != _requestHeader.end())
@ -164,7 +257,7 @@ void LuaMinXmlHttpRequest::setRequestHeader(const char* field, const char* value
*/ */
void LuaMinXmlHttpRequest::_setHttpRequestHeader() void LuaMinXmlHttpRequest::_setHttpRequestHeader()
{ {
std::vector<string> header; std::vector<std::string> header;
for (auto it = _requestHeader.begin(); it != _requestHeader.end(); ++it) for (auto it = _requestHeader.begin(); it != _requestHeader.end(); ++it)
{ {
@ -270,7 +363,7 @@ void LuaMinXmlHttpRequest::_sendRequest()
retain(); retain();
} }
void LuaMinXmlHttpRequest::getByteData(unsigned char* byteData) void LuaMinXmlHttpRequest::getByteData(unsigned char* byteData) const
{ {
memcpy((char*)byteData, _data.c_str(), _dataSize); memcpy((char*)byteData, _data.c_str(), _dataSize);
} }
@ -832,7 +925,7 @@ static int lua_cocos2dx_XMLHttpRequest_send(lua_State* L)
return 0; return 0;
} }
#endif #endif
self->getHttpHeader().clear(); self->clearHttpHeader();
self->setErrorFlag(false); self->setErrorFlag(false);
argc = lua_gettop(L) - 1; argc = lua_gettop(L) - 1;
@ -955,8 +1048,8 @@ static int lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders(lua_State* L)
int argc = 0; int argc = 0;
LuaMinXmlHttpRequest* self = nullptr; LuaMinXmlHttpRequest* self = nullptr;
stringstream responseheaders; std::stringstream responseheaders;
string responseheader = ""; std::string responseheader = "";
#if COCOS2D_DEBUG >= 1 #if COCOS2D_DEBUG >= 1
tolua_Error tolua_err; tolua_Error tolua_err;
@ -976,7 +1069,7 @@ static int lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders(lua_State* L)
if ( 0 == argc ) if ( 0 == argc )
{ {
map<string, string> httpHeader = self->getHttpHeader(); const auto& httpHeader = self->getHttpHeader();
for (auto it = httpHeader.begin(); it != httpHeader.end(); ++it) for (auto it = httpHeader.begin(); it != httpHeader.end(); ++it)
{ {
@ -1002,7 +1095,7 @@ static int lua_cocos2dx_XMLHttpRequest_getResponseHeader(lua_State* L)
int argc = 0; int argc = 0;
LuaMinXmlHttpRequest* self = nullptr; LuaMinXmlHttpRequest* self = nullptr;
string responseheader = ""; std::string responseheader = "";
#if COCOS2D_DEBUG >= 1 #if COCOS2D_DEBUG >= 1
tolua_Error tolua_err; tolua_Error tolua_err;
@ -1028,18 +1121,17 @@ static int lua_cocos2dx_XMLHttpRequest_getResponseHeader(lua_State* L)
#endif #endif
responseheader = tolua_tostring(L, 2, ""); responseheader = tolua_tostring(L, 2, "");
stringstream streamData; const auto& headers = self->getHttpHeader();
streamData << responseheader; auto iter = headers.find(responseheader);
if (iter != headers.end())
string value = streamData.str();
auto iter = self->getHttpHeader().find(value);
if (iter != self->getHttpHeader().end())
{ {
tolua_pushstring(L, (iter->second).c_str()); tolua_pushstring(L, (iter->second).c_str());
return 1;
} }
else
{
lua_pushnil(L);
}
return 1;
} }
luaL_error(L, "'getResponseHeader' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 1); luaL_error(L, "'getResponseHeader' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 1);
@ -1056,7 +1148,7 @@ static int lua_cocos2dx_XMLHttpRequest_registerScriptHandler(lua_State* L)
int argc = 0; int argc = 0;
LuaMinXmlHttpRequest* self = nullptr; LuaMinXmlHttpRequest* self = nullptr;
string responseheader = ""; std::string responseheader = "";
#if COCOS2D_DEBUG >= 1 #if COCOS2D_DEBUG >= 1
tolua_Error tolua_err; tolua_Error tolua_err;
@ -1100,7 +1192,7 @@ static int lua_cocos2dx_XMLHttpRequest_unregisterScriptHandler(lua_State* L)
int argc = 0; int argc = 0;
LuaMinXmlHttpRequest* self = nullptr; LuaMinXmlHttpRequest* self = nullptr;
string responseheader = ""; std::string responseheader = "";
#if COCOS2D_DEBUG >= 1 #if COCOS2D_DEBUG >= 1
tolua_Error tolua_err; tolua_Error tolua_err;

View File

@ -21,8 +21,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#ifndef __COCOS_SCRIPTING_LUA_BINDINGS_LUA_XML_HTTP_REQUEST_H__ #pragma once
#define __COCOS_SCRIPTING_LUA_BINDINGS_LUA_XML_HTTP_REQUEST_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -32,103 +31,4 @@ extern "C" {
} }
#endif #endif
#include <map>
#include <sstream>
#include "network/HttpClient.h"
///@cond
class LuaMinXmlHttpRequest : public cocos2d::Ref
{
public:
enum class ResponseType
{
STRING,
ARRAY_BUFFER,
BLOB,
DOCUMENT,
JSON
};
// Ready States (http://www.w3.org/TR/XMLHttpRequest/#interface-xmlhttprequest)
static const unsigned short UNSENT = 0;
static const unsigned short OPENED = 1;
static const unsigned short HEADERS_RECEIVED = 2;
static const unsigned short LOADING = 3;
static const unsigned short DONE = 4;
LuaMinXmlHttpRequest();
~LuaMinXmlHttpRequest();
inline void setResponseType(ResponseType type) { _responseType = type; }
inline ResponseType getResponseType() {return _responseType; }
inline void setWithCredentialsValue(bool value) { _withCredentialsValue = value; }
inline bool getWithCredentialsValue() {return _withCredentialsValue; }
inline void setTimeout(unsigned timeOut) {_timeout = timeOut; }
inline unsigned getTimeout() { return _timeout;}
inline void setReadyState(int readyState) { _readyState = readyState; }
inline int getReadyState() { return _readyState ;}
inline cocos2d::network::HttpRequest* getHttpRequest() { return _httpRequest; }
inline std::string getStatusText() { return _statusText ;}
inline void setStatus(int status) { _status = status; }
inline int getStatus() { return _status; }
inline std::string getUrl(){return _url;}
inline void setUrl(std::string url) { _url = url ;}
inline std::string getMethod(){return _meth;}
inline void setMethod(std::string meth) { _meth = meth ; }
inline void setAsync(bool isAsync){ _isAsync = isAsync; }
inline void setIsNetWork(bool isNetWork) {_isNetwork = isNetWork; }
void _setHttpRequestHeader();
void _sendRequest();
void setRequestHeader(const char* field, const char* value);
std::map<std::string, std::string> getHttpHeader() { return _httpHeader ;}
void getByteData(unsigned char* byteData);
inline std::string getDataStr() { return _data; }
inline size_t getDataSize() { return _dataSize; }
inline void setErrorFlag(bool errorFlag) { _errorFlag = errorFlag; }
inline bool getErrorFlag() { return _errorFlag; }
inline void setAborted(bool isAborted) { _isAborted = isAborted; }
inline bool getAborted() { return _isAborted; }
private:
void _gotHeader(std::string header);
std::string _url;
std::string _meth;
std::string _type;
std::string _data;
size_t _dataSize;
int _readyState;
int _status;
std::string _statusText;
ResponseType _responseType;
unsigned _timeout;
bool _isAsync;
cocos2d::network::HttpRequest* _httpRequest;
bool _isNetwork;
bool _withCredentialsValue;
std::map<std::string, std::string> _httpHeader;
std::map<std::string, std::string> _requestHeader;
bool _errorFlag;
bool _isAborted;
};
TOLUA_API int register_xml_http_request(lua_State* L); TOLUA_API int register_xml_http_request(lua_State* L);
///@endcond
#endif //#ifndef __COCOS_SCRIPTING_LUA_BINDINGS_LUA_XML_HTTP_REQUEST_H__