Don't append character to std::string.

pull/4/head
Tatsuhiro Tsujikawa 2011-11-12 21:27:24 +09:00
parent 5347efb967
commit db5cc4db27
11 changed files with 84 additions and 68 deletions

View File

@ -311,16 +311,12 @@ std::string createSockPoolKey
std::string key; std::string key;
if(!username.empty()) { if(!username.empty()) {
key += util::percentEncode(username); key += util::percentEncode(username);
key += '@'; key += "@";
} }
key += host; key += host;
key += A2STR::COLON_C; key += fmt(":%u", port);
key += util::uitos(port);
if(!proxyhost.empty()) { if(!proxyhost.empty()) {
key += A2STR::SLASH_C; key += fmt("/%s:%u", proxyhost.c_str(), proxyport);
key += proxyhost;
key += A2STR::COLON_C;
key += util::uitos(proxyport);
} }
return key; return key;
} }

View File

@ -208,7 +208,7 @@ void HttpHeader::fill
std::string::const_iterator> p = util::stripIter(first, j); std::string::const_iterator> p = util::stripIter(first, j);
if(!name.empty() && p.first != p.second) { if(!name.empty() && p.first != p.second) {
if(!value.empty()) { if(!value.empty()) {
value += ' '; value += " ";
} }
value.append(p.first, p.second); value.append(p.first, p.second);
} }

View File

@ -313,7 +313,7 @@ std::string HttpRequest::createProxyRequest() const
std::pair<std::string, std::string> HttpRequest::getProxyAuthString() const std::pair<std::string, std::string> HttpRequest::getProxyAuthString() const
{ {
std::string authText = proxyRequest_->getUsername(); std::string authText = proxyRequest_->getUsername();
authText += ':'; authText += ":";
authText += proxyRequest_->getPassword(); authText += proxyRequest_->getPassword();
std::string val = "Basic "; std::string val = "Basic ";
val += base64::encode(authText.begin(), authText.end()); val += base64::encode(authText.begin(), authText.end());

View File

@ -61,28 +61,32 @@ std::string encode(InputIterator first, InputIterator last)
} }
size_t r = len%3; size_t r = len%3;
InputIterator j = last-r; InputIterator j = last-r;
char temp[4];
while(first != j) { while(first != j) {
int n = static_cast<unsigned char>(*first++) << 16; int n = static_cast<unsigned char>(*first++) << 16;
n += static_cast<unsigned char>(*first++) << 8; n += static_cast<unsigned char>(*first++) << 8;
n += static_cast<unsigned char>(*first++); n += static_cast<unsigned char>(*first++);
res += CHAR_TABLE[n >> 18]; temp[0] = CHAR_TABLE[n >> 18];
res += CHAR_TABLE[(n >> 12) & 0x3fu]; temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
res += CHAR_TABLE[(n >> 6) & 0x3fu]; temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
res += CHAR_TABLE[n & 0x3fu]; temp[3] = CHAR_TABLE[n & 0x3fu];
res.append(temp, sizeof(temp));
} }
if(r == 2) { if(r == 2) {
int n = static_cast<unsigned char>(*first++) << 16; int n = static_cast<unsigned char>(*first++) << 16;
n += static_cast<unsigned char>(*first++) << 8; n += static_cast<unsigned char>(*first++) << 8;
res += CHAR_TABLE[n >> 18]; temp[0] = CHAR_TABLE[n >> 18];
res += CHAR_TABLE[(n >> 12) & 0x3fu]; temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
res += CHAR_TABLE[(n >> 6) & 0x3fu]; temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
res += '='; temp[3] = '=';
res.append(temp, sizeof(temp));
} else if(r == 1) { } else if(r == 1) {
int n = static_cast<unsigned char>(*first++) << 16; int n = static_cast<unsigned char>(*first++) << 16;
res += CHAR_TABLE[n >> 18]; temp[0] = CHAR_TABLE[n >> 18];
res += CHAR_TABLE[(n >> 12) & 0x3fu]; temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
res += '='; temp[2] = '=';
res += '='; temp[3] = '=';
res.append(temp, sizeof(temp));
} }
return res; return res;
} }

View File

@ -275,14 +275,14 @@ void extractFileEntries
error_code::BITTORRENT_PARSE_ERROR); error_code::BITTORRENT_PARSE_ERROR);
} }
} }
std::string utf8Path = strjoin(pathelem.begin(), pathelem.end(), '/', std::string utf8Path = strjoin(pathelem.begin(), pathelem.end(), "/",
std::ptr_fun(util::encodeNonUtf8)); std::ptr_fun(util::encodeNonUtf8));
if(util::detectDirTraversal(utf8Path)) { if(util::detectDirTraversal(utf8Path)) {
throw DL_ABORT_EX2(fmt(MSG_DIR_TRAVERSAL_DETECTED, utf8Path.c_str()), throw DL_ABORT_EX2(fmt(MSG_DIR_TRAVERSAL_DETECTED, utf8Path.c_str()),
error_code::BITTORRENT_PARSE_ERROR); error_code::BITTORRENT_PARSE_ERROR);
} }
std::string pePath = std::string pePath =
strjoin(pathelem.begin(), pathelem.end(), '/', strjoin(pathelem.begin(), pathelem.end(), "/",
std::ptr_fun(static_cast<std::string (*)(const std::string&)> std::ptr_fun(static_cast<std::string (*)(const std::string&)>
(util::percentEncode))); (util::percentEncode)));
std::vector<std::string> uris; std::vector<std::string> uris;

View File

@ -168,12 +168,12 @@ decodeString
checkEof(first, last); checkEof(first, last);
uint16_t codepoint = util::parseUInt(std::string(uchars, first), 16); uint16_t codepoint = util::parseUInt(std::string(uchars, first), 16);
if(codepoint <= 0x007fu) { if(codepoint <= 0x007fu) {
s += static_cast<char>(codepoint); unsigned char temp[1] = { static_cast<char>(codepoint) };
s.append(&temp[0], &temp[sizeof(temp)]);
} else if(codepoint <= 0x07ffu) { } else if(codepoint <= 0x07ffu) {
unsigned char c2 = 0x80u | (codepoint & 0x003fu); unsigned char temp[2] = { 0xC0u | (codepoint >> 6),
unsigned char c1 = 0xC0u | (codepoint >> 6); 0x80u | (codepoint & 0x003fu) };
s += c1; s.append(&temp[0], &temp[sizeof(temp)]);
s += c2;
} else if(in(codepoint, 0xD800u, 0xDBFFu)) { } else if(in(codepoint, 0xD800u, 0xDBFFu)) {
// surrogate pair // surrogate pair
if(*first != '\\' || first+1 == last || if(*first != '\\' || first+1 == last ||
@ -195,36 +195,31 @@ decodeString
uint32_t fullcodepoint = 0x010000u; uint32_t fullcodepoint = 0x010000u;
fullcodepoint += (codepoint & 0x03FFu) << 10; fullcodepoint += (codepoint & 0x03FFu) << 10;
fullcodepoint += (codepoint2 & 0x03FFu); fullcodepoint += (codepoint2 & 0x03FFu);
unsigned char c4 = 0x80u | (fullcodepoint & 0x003Fu); unsigned char temp[4] = { 0xf0u | (fullcodepoint >> 18),
unsigned char c3 = 0x80u | ((fullcodepoint >> 6) & 0x003Fu); 0x80u | ((fullcodepoint >> 12) & 0x003Fu),
unsigned char c2 = 0x80u | ((fullcodepoint >> 12) & 0x003Fu); 0x80u | ((fullcodepoint >> 6) & 0x003Fu),
unsigned char c1 = 0xf0u | (fullcodepoint >> 18); 0x80u | (fullcodepoint & 0x003Fu) };
s += c1; s.append(&temp[0], &temp[sizeof(temp)]);
s += c2;
s += c3;
s += c4;
} else { } else {
unsigned char c3 = 0x80u | (codepoint & 0x003Fu); unsigned char temp[3] = { 0xE0u | (codepoint >> 12),
unsigned char c2 = 0x80u | ((codepoint >> 6) & 0x003Fu); 0x80u | ((codepoint >> 6) & 0x003Fu),
unsigned char c1 = 0xE0u | (codepoint >> 12); 0x80u | (codepoint & 0x003Fu) };
s += c1; s.append(&temp[0], &temp[sizeof(temp)]);
s += c2;
s += c3;
} }
offset = first; offset = first;
} else { } else {
if(*first == 'b') { if(*first == 'b') {
s += '\b'; s += "\b";
} else if(*first == 'f') { } else if(*first == 'f') {
s += '\f'; s += "\f";
} else if(*first == 'n') { } else if(*first == 'n') {
s += '\n'; s += "\n";
} else if(*first == 'r') { } else if(*first == 'r') {
s += '\r'; s += "\r";
} else if(*first == 't') { } else if(*first == 't') {
s += '\t'; s += "\t";
} else { } else {
s += *first; s.append(first, first+1);
} }
++first; ++first;
offset = first; offset = first;
@ -278,7 +273,7 @@ decodeNumber
{ {
std::string s; std::string s;
if(*first == '-') { if(*first == '-') {
s += *first; s.append(first, first+1);
++first; ++first;
} }
std::string::const_iterator offset = first; std::string::const_iterator offset = first;
@ -292,7 +287,7 @@ decodeNumber
bool fp = false; bool fp = false;
if(*first == '.') { if(*first == '.') {
fp = true; fp = true;
s += *first; s.append(first, first+1);
++first; ++first;
offset = first; offset = first;
while(first != last && in(*first, '0', '9')) { while(first != last && in(*first, '0', '9')) {
@ -304,11 +299,11 @@ decodeNumber
} }
if(*first == 'e') { if(*first == 'e') {
fp = true; fp = true;
s += *first; s.append(first, first+1);
++first; ++first;
checkEof(first, last); checkEof(first, last);
if(*first == '+' || *first == '-') { if(*first == '+' || *first == '-') {
s += *first; s.append(first, first+1);
++first; ++first;
} }
offset = first; offset = first;
@ -488,7 +483,7 @@ std::string jsonEscape(const std::string& s)
for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi;
++i) { ++i) {
if(*i == '"' || *i == '\\' || *i == '/') { if(*i == '"' || *i == '\\' || *i == '/') {
t += '\\'; t += "\\";
t += *i; t += *i;
} else if(*i == '\b') { } else if(*i == '\b') {
t += "\\b"; t += "\\b";
@ -515,7 +510,7 @@ std::string jsonEscape(const std::string& s)
} }
t += temp; t += temp;
} else { } else {
t += *i; t.append(i, i+1);
} }
} }
return t; return t;
@ -574,22 +569,22 @@ decodeGetParams(const std::string& query)
// Assume batch call. // Assume batch call.
jsonRequest = jsonParam; jsonRequest = jsonParam;
} else { } else {
jsonRequest = '{'; jsonRequest = "{";
if(method.first != method.second) { if(method.first != method.second) {
jsonRequest += "\"method\":\""; jsonRequest += "\"method\":\"";
jsonRequest.append(method.first, method.second); jsonRequest.append(method.first, method.second);
jsonRequest += '"'; jsonRequest += "\"";
} }
if(id.first != id.second) { if(id.first != id.second) {
jsonRequest += ",\"id\":\""; jsonRequest += ",\"id\":\"";
jsonRequest.append(id.first, id.second); jsonRequest.append(id.first, id.second);
jsonRequest += '"'; jsonRequest += "\"";
} }
if(params.first != params.second) { if(params.first != params.second) {
jsonRequest += ",\"params\":"; jsonRequest += ",\"params\":";
jsonRequest += jsonParam; jsonRequest += jsonParam;
} }
jsonRequest += '}'; jsonRequest += "}";
} }
} }
return JsonGetParam(jsonRequest, callback); return JsonGetParam(jsonRequest, callback);

View File

@ -127,9 +127,7 @@ InputIterator expandLoop
if(start <= end) { if(start <= end) {
std::string format; std::string format;
if(minus-first == colon-minus-1) { if(minus-first == colon-minus-1) {
format = "%0"; format = fmt("%%0%lud", minus-first);
format += util::uitos(minus-first);
format += 'd';
} else { } else {
format = "%d"; format = "%d";
} }

View File

@ -1415,20 +1415,18 @@ bool inPrivateAddress(const std::string& ipv4addr)
{ {
static const char A2_IP10[] = "10."; static const char A2_IP10[] = "10.";
static const char A2_IP192[] = "192.168."; static const char A2_IP192[] = "192.168.";
static const char A2_IP172[] = "172.";
if(util::startsWith(ipv4addr.begin(), ipv4addr.end(), if(util::startsWith(ipv4addr.begin(), ipv4addr.end(),
A2_IP10, vend(A2_IP10)-1) || A2_IP10, vend(A2_IP10)-1) ||
util::startsWith(ipv4addr.begin(), ipv4addr.end(), util::startsWith(ipv4addr.begin(), ipv4addr.end(),
A2_IP192, vend(A2_IP192)-1)) { A2_IP192, vend(A2_IP192)-1)) {
return true; return true;
} }
std::string p172("172.");
if(util::startsWith(ipv4addr.begin(), ipv4addr.end(), if(util::startsWith(ipv4addr.begin(), ipv4addr.end(),
p172.begin(), p172.end())) { A2_IP172, vend(A2_IP172)-1)) {
for(int i = 16; i <= 31; ++i) { for(int i = 16; i <= 31; ++i) {
std::string t(p172); std::string t(fmt("%d.", i));
t += util::itos(i); if(util::startsWith(ipv4addr.begin()+4, ipv4addr.end(),
t += '.';
if(util::startsWith(ipv4addr.begin(), ipv4addr.end(),
t.begin(), t.end())) { t.begin(), t.end())) {
return true; return true;
} }

View File

@ -141,7 +141,7 @@ std::string uitos(T value, bool comma = false)
++count; ++count;
char digit = value%10+'0'; char digit = value%10+'0';
if(comma && count > 3 && count%3 == 1) { if(comma && count > 3 && count%3 == 1) {
str += ','; str += ",";
} }
str += digit; str += digit;
value /= 10; value /= 10;

View File

@ -179,6 +179,16 @@ void JsonTest::testDecode()
const String* s = downcast<String>(list->get(0)); const String* s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("\"\\/\b\f\n\r\t"), s->s()); CPPUNIT_ASSERT_EQUAL(std::string("\"\\/\b\f\n\r\t"), s->s());
} }
{
// string: literal + escaped chars.
SharedHandle<ValueBase> r =
json::decode("[\"foo\\u0024b\\u00A2\\u20ACbaz\"]");
const List* list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("foo$b¢€baz"), s->s());
}
} }
void JsonTest::testDecode_error() void JsonTest::testDecode_error()

View File

@ -85,6 +85,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testIsUtf8String); CPPUNIT_TEST(testIsUtf8String);
CPPUNIT_TEST(testNextParam); CPPUNIT_TEST(testNextParam);
CPPUNIT_TEST(testNoProxyDomainMatch); CPPUNIT_TEST(testNoProxyDomainMatch);
CPPUNIT_TEST(testInPrivateAddress);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
private: private:
@ -155,6 +156,7 @@ public:
void testIsUtf8String(); void testIsUtf8String();
void testNextParam(); void testNextParam();
void testNoProxyDomainMatch(); void testNoProxyDomainMatch();
void testInPrivateAddress();
}; };
@ -1829,4 +1831,17 @@ void UtilTest::testNoProxyDomainMatch()
CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org")); CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
} }
void UtilTest::testInPrivateAddress()
{
CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
// Only checks prefix..
CPPUNIT_ASSERT(util::inPrivateAddress("10."));
CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
}
} // namespace aria2 } // namespace aria2