Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[httprequest] fix unsafe parser #1629

Merged
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 84 additions & 34 deletions cocos/scripting/js-bindings/manual/jsb_xmlhttprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,74 @@
using namespace cocos2d;
using namespace cocos2d::network;

namespace {
std::unordered_map<int, std::string> _httpStatusCodeMap =
{
{100, "Continue"},
{101, "Switching Protocols"},
{102, "Processing"},
{200, "OK"},
{201, "Created"},
{202, "Accepted"},
{203, "Non-authoritative Information"},
{204, "No Content"},
{205, "Reset Content"},
{206, "Partial Content"},
{207, "Multi-Status"},
{208, "Already Reported"},
{226, "IM Used"},
{300, "Multiple Choices"},
{301, "Moved Permanently"},
{302, "Found"},
{303, "See Other"},
{304, "Not Modified"},
{305, "Use Proxy"},
{307, "Temporary Redirect"},
{308, "Permanent Redirect"},
{400, "Bad Request"},
{401, "Unauthorized"},
{402, "Payment Required"},
{403, "Forbidden"},
{404, "Not Found"},
{405, "Method Not Allowed"},
{406, "Not Acceptable"},
{407, "Proxy Authentication Required"},
{408, "Request Timeout"},
{409, "Conflict"},
{410, "Gone"},
{411, "Length Required"},
{412, "Precondition Failed"},
{413, "Payload Too Large"},
{414, "Request-URI Too Long"},
{415, "Unsupported Media Type"},
{416, "Requested Range Not Satisfiable"},
{417, "Expectation Failed"},
{418, "I'm a teapot"},
{421, "Misdirected Request"},
{422, "Unprocessable Entity"},
{423, "Locked"},
{424, "Failed Dependency"},
{426, "Upgrade Required"},
{428, "Precondition Required"},
{429, "Too Many Requests"},
{431, "Request Header Fields Too Large"},
{444, "Connection Closed Without Response"},
{451, "Unavailable For Legal Reasons"},
{499, "Client Closed Request"},
{500, "Internal Server Error"},
{501, "Not Implemented"},
{502, "Bad Gateway"},
{503, "Service Unavailable"},
{504, "Gateway Timeout"},
{505, "HTTP Version Not Supported"},
{506, "Variant Also Negotiates"},
{507, "Insufficient Storage"},
{508, "Loop Detected"},
{510, "Not Extended"},
{511, "Network Authentication Required"},
{599, "Network Connect Timeout Error"}
};
PatriceJiang marked this conversation as resolved.
Show resolved Hide resolved
}
class XMLHttpRequest : public Ref
{
public:
Expand Down Expand Up @@ -306,43 +374,25 @@ void XMLHttpRequest::getHeader(const std::string& header)
{
// Get Header and Set StatusText
// Split String into Tokens
char* cstr = new (std::nothrow) char [header.length()+1];

// Seems like we have the response Code! Parse it and check for it.
char* pch;
strncpy(cstr, header.c_str(), header.length());
cstr[header.length()] = '\0';

pch = strtok(cstr, " ");
while (pch != nullptr)
if (header.find("HTTP") == 0)
{
std::stringstream ss;
std::string val;

ss << pch;
val = ss.str();
size_t found_http = val.find("HTTP");

// Check for HTTP Header to set statusText
if (found_http != std::string::npos) {

std::stringstream mystream;

// Get Response Status
pch = strtok (nullptr, " ");
//mystream << pch; //ignore HTTP statusCode

pch = strtok (nullptr, " ");
mystream << pch;

_statusText = mystream.str();

int _v1, _v2, code = 0;
char statusText[64] = {0};
sscanf(header.c_str(), "HTTP/%d.%d %d %64[^\n]", &_v1, &_v2, &code, statusText);
_statusText = statusText;
if(_statusText.empty())
Copy link
Contributor Author

@PatriceJiang PatriceJiang Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

服务器可能返回不完整的协议, 缺少statusText.
可以根据协议标准补齐。

{
auto itCode = _httpStatusCodeMap.find(code);
if(itCode != _httpStatusCodeMap.end())
{
_statusText = itCode->second;
}
else
{
CCLOG("XMLHTTPRequest invalid response code %d", code);
}
}

pch = strtok (nullptr, " ");
}

CC_SAFE_DELETE_ARRAY(cstr);
}
}

Expand Down