From e8d234a32a7e865d296a688025cf459fcbefe220 Mon Sep 17 00:00:00 2001 From: dantezhu Date: Mon, 10 Nov 2014 17:11:30 +0800 Subject: [PATCH 1/2] fix lua http download binary data's bug --- .../lua-bindings/manual/network/lua_xml_http_request.cpp | 8 ++------ .../lua-bindings/manual/network/lua_xml_http_request.h | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp index 149c2446af..c78dc28221 100644 --- a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp +++ b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp @@ -244,16 +244,13 @@ void LuaMinXmlHttpRequest::_sendRequest() /** get the response data **/ std::vector *buffer = response->getResponseData(); - char* concatenated = (char*) malloc(buffer->size() + 1); - std::string s2(buffer->begin(), buffer->end()); - strcpy(concatenated, s2.c_str()); if (statusCode == 200) { //Succeeded _status = 200; _readyState = DONE; - _data << concatenated; + _data.assign(buffer->begin(), buffer->end()); _dataSize = buffer->size(); } else @@ -262,7 +259,6 @@ void LuaMinXmlHttpRequest::_sendRequest() } // Free Memory. free((void*) concatHeader); - free((void*) concatenated); // TODO: call back lua function int handler = cocos2d::ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE ); @@ -282,7 +278,7 @@ void LuaMinXmlHttpRequest::_sendRequest() void LuaMinXmlHttpRequest::getByteData(unsigned char* byteData) { - _data.read((char*)byteData, _dataSize); + memcpy((char*)byteData, _data.c_str(), _dataSize); } /* function to regType */ diff --git a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h index 754a4e855a..6018af022a 100644 --- a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h +++ b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h @@ -94,7 +94,7 @@ public: void getByteData(unsigned char* byteData); - inline std::string getDataStr() { return _data.str(); } + inline std::string getDataStr() { return _data; } inline size_t getDataSize() { return _dataSize; } @@ -111,7 +111,7 @@ private: std::string _url; std::string _meth; std::string _type; - std::stringstream _data; + std::string _data; size_t _dataSize; int _readyState; int _status; From 78ef234e8f373f8799a92ccd09bfea0048a7df2f Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Tue, 18 Nov 2014 16:02:38 +0800 Subject: [PATCH 2/2] Resolve the bugs that XmlHttpRequest would truncate binary data --- .../manual/network/lua_xml_http_request.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp index c78dc28221..20f82600d0 100644 --- a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp +++ b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp @@ -232,11 +232,8 @@ void LuaMinXmlHttpRequest::_sendRequest() // set header std::vector *headers = response->getResponseHeader(); - char* concatHeader = (char*) malloc(headers->size() + 1); std::string header(headers->begin(), headers->end()); - strcpy(concatHeader, header.c_str()); - - std::istringstream stream(concatHeader); + std::istringstream stream(header); std::string line; while(std::getline(stream, line)) { _gotHeader(line); @@ -257,8 +254,6 @@ void LuaMinXmlHttpRequest::_sendRequest() { _status = 0; } - // Free Memory. - free((void*) concatHeader); // TODO: call back lua function int handler = cocos2d::ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE ); @@ -638,7 +633,7 @@ static int lua_get_XMLHttpRequest_responseText(lua_State* L) return 0; } #endif - lua_pushstring(L, self->getDataStr().c_str()); + lua_pushlstring(L, self->getDataStr().c_str(), self->getDataSize()); return 1; #if COCOS2D_DEBUG >= 1 @@ -671,7 +666,7 @@ static int lua_get_XMLHttpRequest_response(lua_State* L) if (self->getReadyState() != LuaMinXmlHttpRequest::DONE || self->getErrorFlag()) return 0; - lua_pushstring(L, self->getDataStr().c_str()); + lua_pushlstring(L, self->getDataStr().c_str(), self->getDataSize()); return 1; } else if(self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::ARRAY_BUFFER) @@ -712,7 +707,7 @@ static int lua_get_XMLHttpRequest_response(lua_State* L) } else { - lua_pushstring(L, self->getDataStr().c_str()); + lua_pushlstring(L, self->getDataStr().c_str(), self->getDataSize()); return 1; }