diff --git a/src/curl.cpp b/src/curl.cpp index 3419f5f0c3..a4e651b9c6 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -4218,6 +4218,7 @@ bool S3fsCurl::UploadMultipartPostComplete() if (it == responseHeaders.end()) { return false; } + std::string etag = peeloff(it->second); // check etag(md5); // @@ -4227,11 +4228,11 @@ bool S3fsCurl::UploadMultipartPostComplete() // https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingKMSEncryption.html // if(S3fsCurl::is_content_md5 && sse_type_t::SSE_C != S3fsCurl::GetSseType() && sse_type_t::SSE_KMS != S3fsCurl::GetSseType()){ - if(!etag_equals(it->second, partdata.etag)){ + if(!etag_equals(etag, partdata.etag)){ return false; } } - partdata.petag->etag = it->second; + partdata.petag->etag = etag; partdata.uploaded = true; return true; @@ -4255,11 +4256,7 @@ bool S3fsCurl::CopyMultipartPostComplete() { std::string etag; partdata.uploaded = simple_parse_xml(bodydata.c_str(), bodydata.size(), "ETag", etag); - if(etag.size() >= 2 && *etag.begin() == '"' && *etag.rbegin() == '"'){ - etag.erase(etag.size() - 1); - etag.erase(0, 1); - } - partdata.petag->etag = etag; + partdata.petag->etag = peeloff(etag); bodydata.clear(); headdata.clear(); diff --git a/src/curl_util.cpp b/src/curl_util.cpp index 1816636ccf..3b484d50c5 100644 --- a/src/curl_util.cpp +++ b/src/curl_util.cpp @@ -319,17 +319,9 @@ const char* getCurlDebugHead(curl_infotype type) // // compare ETag ignoring quotes and case // -bool etag_equals(std::string s1, std::string s2) +bool etag_equals(const std::string& s1, const std::string& s2) { - if(s1.length() > 1 && s1[0] == '\"' && *s1.rbegin() == '\"'){ - s1.erase(s1.size() - 1); - s1.erase(0, 1); - } - if(s2.length() > 1 && s2[0] == '\"' && *s2.rbegin() == '\"'){ - s2.erase(s2.size() - 1); - s2.erase(0, 1); - } - return 0 == strcasecmp(s1.c_str(), s2.c_str()); + return 0 == strcasecmp(peeloff(s1).c_str(), peeloff(s2).c_str()); } /* diff --git a/src/curl_util.h b/src/curl_util.h index 8dc0d7d377..596c6d4447 100644 --- a/src/curl_util.h +++ b/src/curl_util.h @@ -42,7 +42,7 @@ std::string url_to_host(const std::string &url); std::string get_bucket_host(); const char* getCurlDebugHead(curl_infotype type); -bool etag_equals(std::string s1, std::string s2); +bool etag_equals(const std::string& s1, const std::string& s2); #endif // S3FS_CURL_UTIL_H_ diff --git a/src/string_util.cpp b/src/string_util.cpp index 0b75a45653..8277074bff 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -125,6 +125,14 @@ std::string trim(std::string s, const char *t /* = SPACES */) return trim_left(trim_right(std::move(s), t), t); } +std::string peeloff(std::string s) +{ + if(s.size() < 2 || *s.begin() != '"' || *s.rbegin() != '"'){ + return s; + } + return s.substr(1, s.size() - 2); +} + // // Three url encode functions // diff --git a/src/string_util.h b/src/string_util.h index 9132afef63..34cdbb14d7 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -79,6 +79,7 @@ std::string trim_left(std::string s, const char *t = SPACES); std::string trim_right(std::string s, const char *t = SPACES); std::string trim(std::string s, const char *t = SPACES); std::string lower(std::string s); +std::string peeloff(std::string s); // // Date string diff --git a/src/test_string_util.cpp b/src/test_string_util.cpp index 99184df5ee..bdae2e644d 100644 --- a/src/test_string_util.cpp +++ b/src/test_string_util.cpp @@ -49,6 +49,15 @@ void test_trim() ASSERT_EQUALS(std::string("1234"), trim_right("1234 ")); ASSERT_EQUALS(std::string(" 1234"), trim_right(" 1234")); ASSERT_EQUALS(std::string("1234"), trim_right("1234")); + + ASSERT_EQUALS(std::string("1234"), peeloff("\"1234\"")); // "1234" -> 1234 + ASSERT_EQUALS(std::string("\"1234\""), peeloff("\"\"1234\"\"")); // ""1234"" -> "1234" + ASSERT_EQUALS(std::string("\"1234"), peeloff("\"\"1234\"")); // ""1234" -> "1234 + ASSERT_EQUALS(std::string("1234\""), peeloff("\"1234\"\"")); // "1234"" -> 1234" + ASSERT_EQUALS(std::string("\"1234"), peeloff("\"1234")); // "1234 -> "1234 + ASSERT_EQUALS(std::string("1234\""), peeloff("1234\"")); // 1234" -> 1234" + ASSERT_EQUALS(std::string(" \"1234\""), peeloff(" \"1234\"")); // _"1234" -> _"1234" + ASSERT_EQUALS(std::string("\"1234\" "), peeloff("\"1234\" ")); // "1234"_ -> "1234"_ } void test_base64()