Merge pull request #9149 from samuele3hu/v3_compile_binary

Resolve the bugs that XmlHttpRequest would truncate binary data
This commit is contained in:
minggo 2014-11-26 20:05:21 +08:00
commit 51b4736740
2 changed files with 8 additions and 17 deletions

View File

@ -232,11 +232,8 @@ void LuaMinXmlHttpRequest::_sendRequest()
// set header // set header
std::vector<char> *headers = response->getResponseHeader(); std::vector<char> *headers = response->getResponseHeader();
char* concatHeader = (char*) malloc(headers->size() + 1);
std::string header(headers->begin(), headers->end()); std::string header(headers->begin(), headers->end());
strcpy(concatHeader, header.c_str()); std::istringstream stream(header);
std::istringstream stream(concatHeader);
std::string line; std::string line;
while(std::getline(stream, line)) { while(std::getline(stream, line)) {
_gotHeader(line); _gotHeader(line);
@ -244,25 +241,19 @@ void LuaMinXmlHttpRequest::_sendRequest()
/** get the response data **/ /** get the response data **/
std::vector<char> *buffer = response->getResponseData(); std::vector<char> *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) if (statusCode == 200)
{ {
//Succeeded //Succeeded
_status = 200; _status = 200;
_readyState = DONE; _readyState = DONE;
_data << concatenated; _data.assign(buffer->begin(), buffer->end());
_dataSize = buffer->size(); _dataSize = buffer->size();
} }
else else
{ {
_status = 0; _status = 0;
} }
// Free Memory.
free((void*) concatHeader);
free((void*) concatenated);
// TODO: call back lua function // TODO: call back lua function
int handler = cocos2d::ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE ); int handler = cocos2d::ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE );
@ -282,7 +273,7 @@ void LuaMinXmlHttpRequest::_sendRequest()
void LuaMinXmlHttpRequest::getByteData(unsigned char* byteData) void LuaMinXmlHttpRequest::getByteData(unsigned char* byteData)
{ {
_data.read((char*)byteData, _dataSize); memcpy((char*)byteData, _data.c_str(), _dataSize);
} }
/* function to regType */ /* function to regType */
@ -642,7 +633,7 @@ static int lua_get_XMLHttpRequest_responseText(lua_State* L)
return 0; return 0;
} }
#endif #endif
lua_pushstring(L, self->getDataStr().c_str()); lua_pushlstring(L, self->getDataStr().c_str(), self->getDataSize());
return 1; return 1;
#if COCOS2D_DEBUG >= 1 #if COCOS2D_DEBUG >= 1
@ -675,7 +666,7 @@ static int lua_get_XMLHttpRequest_response(lua_State* L)
if (self->getReadyState() != LuaMinXmlHttpRequest::DONE || self->getErrorFlag()) if (self->getReadyState() != LuaMinXmlHttpRequest::DONE || self->getErrorFlag())
return 0; return 0;
lua_pushstring(L, self->getDataStr().c_str()); lua_pushlstring(L, self->getDataStr().c_str(), self->getDataSize());
return 1; return 1;
} }
else if(self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::ARRAY_BUFFER) else if(self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::ARRAY_BUFFER)
@ -716,7 +707,7 @@ static int lua_get_XMLHttpRequest_response(lua_State* L)
} }
else else
{ {
lua_pushstring(L, self->getDataStr().c_str()); lua_pushlstring(L, self->getDataStr().c_str(), self->getDataSize());
return 1; return 1;
} }

View File

@ -94,7 +94,7 @@ public:
void getByteData(unsigned char* byteData); void getByteData(unsigned char* byteData);
inline std::string getDataStr() { return _data.str(); } inline std::string getDataStr() { return _data; }
inline size_t getDataSize() { return _dataSize; } inline size_t getDataSize() { return _dataSize; }
@ -111,7 +111,7 @@ private:
std::string _url; std::string _url;
std::string _meth; std::string _meth;
std::string _type; std::string _type;
std::stringstream _data; std::string _data;
size_t _dataSize; size_t _dataSize;
int _readyState; int _readyState;
int _status; int _status;