Skip to content

Commit

Permalink
Merge pull request #485 from htlxyz/master
Browse files Browse the repository at this point in the history
modify post binary data bug fixed #235
  • Loading branch information
hugohuang1111 committed Dec 10, 2014
2 parents 9af4c9b + 15ecf6c commit 20a8206
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ static int tolua_cocos2dx_httprequest_luabinding_CCHTTPRequest_setPOSTData00(lua
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'setPOSTData'", NULL);
#endif
{
self->setPOSTData(data);
size_t len;
lua_tolstring(tolua_S, 2, &len);
self->setPOSTData(data, len);
}
}
return 0;
Expand Down
41 changes: 35 additions & 6 deletions lib/cocos2d-x/external/extra/network/CCHTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,35 @@ void CCHTTPRequest::addPOSTValue(const char *key, const char *value)
m_postFields[string(key)] = string(value ? value : "");
}

void CCHTTPRequest::setPOSTData(const char *data)
void CCHTTPRequest::setPOSTData(const char *data, size_t len)
{
CCAssert(m_state == kCCHTTPRequestStateIdle, "CCHTTPRequest::setPOSTData() - request not idle");
CCAssert(data, "CCHTTPRequest::setPOSTData() - invalid post data");
m_postFields.clear();
if (0 == len) {
len = strlen(data);
}
if (0 == len) {
return;
}
if (m_postData)
{
free(m_postData);
m_postDataLen = 0;
m_postData = NULL;
}
m_postData = malloc(len + 1);
memset(m_postData, 0, len + 1);
if (NULL == m_postData)
{
return;
}
memcpy(m_postData, data, len);
m_postDataLen = len;
curl_easy_setopt(m_curl, CURLOPT_POST, 1L);
curl_easy_setopt(m_curl, CURLOPT_COPYPOSTFIELDS, data);
//curl_easy_setopt(m_curl, CURLOPT_COPYPOSTFIELDS, data);
curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, m_postData);
curl_easy_setopt(m_curl, CURLOPT_POSTFIELDSIZE, m_postDataLen);
}

void CCHTTPRequest::addFormFile(const char *name, const char *filePath, const char *contentType)
Expand Down Expand Up @@ -168,9 +190,10 @@ void CCHTTPRequest::setAcceptEncoding(int acceptEncoding)

void CCHTTPRequest::setTimeout(int timeout)
{
long to = timeout;
CCAssert(m_state == kCCHTTPRequestStateIdle, "CCHTTPRequest::setTimeout() - request not idle");
curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, timeout);
curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, to);
curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, to);
}

bool CCHTTPRequest::start(void)
Expand All @@ -181,7 +204,7 @@ bool CCHTTPRequest::start(void)
m_curlState = kCCHTTPRequestCURLStateBusy;
retain();

curl_easy_setopt(m_curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
curl_easy_setopt(m_curl, CURLOPT_HTTP_CONTENT_DECODING, 1L);
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writeDataCURL);
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this);
curl_easy_setopt(m_curl, CURLOPT_HEADERFUNCTION, writeHeaderCURL);
Expand Down Expand Up @@ -225,7 +248,7 @@ int CCHTTPRequest::getState(void)
int CCHTTPRequest::getResponseStatusCode(void)
{
CCAssert(m_state == kCCHTTPRequestStateCompleted, "Request not completed");
return m_responseCode;
return static_cast<int>(m_responseCode);
}

const CCHTTPRequestHeaders &CCHTTPRequest::getResponseHeaders(void)
Expand Down Expand Up @@ -497,6 +520,12 @@ void CCHTTPRequest::cleanup(void)
m_state = kCCHTTPRequestStateCleared;
m_responseBufferLength = 0;
m_responseDataLength = 0;
m_postDataLen = 0;
if (m_postData)
{
free(m_postData);
m_postData = NULL;
}
if (m_responseBuffer)
{
free(m_responseBuffer);
Expand Down
9 changes: 7 additions & 2 deletions lib/cocos2d-x/external/extra/network/CCHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CCHTTPRequest : public CCObject
void addPOSTValue(const char *key, const char *value);

/** @brief Set POST data to the request body, POST only. */
void setPOSTData(const char *data);
void setPOSTData(const char *data, size_t len = 0);


void addFormFile(const char *name, const char *filePath, const char *fileType="application/octet-stream");
Expand Down Expand Up @@ -153,6 +153,8 @@ class CCHTTPRequest : public CCObject
, m_responseBufferLength(0)
, m_responseDataLength(0)
, m_curlState(kCCHTTPRequestCURLStateIdle)
, m_postData(NULL)
, m_postDataLen(0)
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
, m_httpConnect(NULL)
, m_cookies(NULL)
Expand Down Expand Up @@ -198,6 +200,9 @@ class CCHTTPRequest : public CCObject
typedef map<string, string> Fields;
Fields m_postFields;
CCHTTPRequestHeaders m_headers;

void* m_postData;
size_t m_postDataLen;

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
jobject m_httpConnect;
Expand All @@ -209,7 +214,7 @@ class CCHTTPRequest : public CCObject
#endif

// response
int m_responseCode;
long m_responseCode;
CCHTTPRequestHeaders m_responseHeaders;
void *m_responseBuffer;
size_t m_responseBufferLength;
Expand Down

0 comments on commit 20a8206

Please sign in to comment.