Own values in add_header (#2285)

Also fix up indentation.
This commit is contained in:
Andrew Gaul 2023-08-20 18:59:18 +09:00 committed by GitHub
parent 8d04ee3e01
commit 6c55bcfdd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 40 deletions

View File

@ -110,7 +110,6 @@ bool AdditionalHeader::Load(const char* file)
return false; return false;
} }
std::unique_ptr<ADDHEAD> paddhead(new ADDHEAD);
if(0 == strncasecmp(key.c_str(), ADD_HEAD_REGEX, strlen(ADD_HEAD_REGEX))){ if(0 == strncasecmp(key.c_str(), ADD_HEAD_REGEX, strlen(ADD_HEAD_REGEX))){
// regex // regex
if(key.size() <= strlen(ADD_HEAD_REGEX)){ if(key.size() <= strlen(ADD_HEAD_REGEX)){
@ -119,33 +118,22 @@ bool AdditionalHeader::Load(const char* file)
} }
key.erase(0, strlen(ADD_HEAD_REGEX)); key.erase(0, strlen(ADD_HEAD_REGEX));
// compile // compile
std::unique_ptr<regex_t> preg(new regex_t); std::unique_ptr<regex_t> preg(new regex_t);
int result; int result;
if(0 != (result = regcomp(preg.get(), key.c_str(), REG_EXTENDED | REG_NOSUB))){ // we do not need matching info if(0 != (result = regcomp(preg.get(), key.c_str(), REG_EXTENDED | REG_NOSUB))){ // we do not need matching info
char errbuf[256]; char errbuf[256];
regerror(result, preg.get(), errbuf, sizeof(errbuf)); regerror(result, preg.get(), errbuf, sizeof(errbuf));
S3FS_PRN_ERR("failed to compile regex from %s key by %s.", key.c_str(), errbuf); S3FS_PRN_ERR("failed to compile regex from %s key by %s.", key.c_str(), errbuf);
continue; continue;
} }
// set
paddhead->pregex = std::move(preg);
paddhead->basestring = key;
paddhead->headkey = head;
paddhead->headvalue = value;
addheadlist.emplace_back(std::move(preg), key, head, value);
}else{ }else{
// not regex, directly comparing // not regex, directly comparing
paddhead->pregex.reset(nullptr); addheadlist.emplace_back(nullptr, key, head, value);
paddhead->basestring = key;
paddhead->headkey = head;
paddhead->headvalue = value;
} }
// add list
addheadlist.push_back(std::move(paddhead));
// set flag // set flag
is_enable = true; is_enable = true;
} }
@ -177,10 +165,7 @@ bool AdditionalHeader::AddHeader(headers_t& meta, const char* path) const
// Because to allow duplicate key, and then scanning the entire table. // Because to allow duplicate key, and then scanning the entire table.
// //
for(addheadlist_t::const_iterator iter = addheadlist.begin(); iter != addheadlist.end(); ++iter){ for(addheadlist_t::const_iterator iter = addheadlist.begin(); iter != addheadlist.end(); ++iter){
const ADDHEAD *paddhead = iter->get(); const add_header *paddhead = &*iter;
if(!paddhead){
continue;
}
if(paddhead->pregex){ if(paddhead->pregex){
// regex // regex
@ -230,19 +215,17 @@ bool AdditionalHeader::Dump() const
ssdbg << "Additional Header list[" << addheadlist.size() << "] = {" << std::endl; ssdbg << "Additional Header list[" << addheadlist.size() << "] = {" << std::endl;
for(addheadlist_t::const_iterator iter = addheadlist.begin(); iter != addheadlist.end(); ++iter, ++cnt){ for(addheadlist_t::const_iterator iter = addheadlist.begin(); iter != addheadlist.end(); ++iter, ++cnt){
const ADDHEAD *paddhead = iter->get(); const add_header *paddhead = &*iter;
ssdbg << " [" << cnt << "] = {" << std::endl; ssdbg << " [" << cnt << "] = {" << std::endl;
if(paddhead){ if(paddhead->pregex){
if(paddhead->pregex){ ssdbg << " type\t\t--->\tregex" << std::endl;
ssdbg << " type\t\t--->\tregex" << std::endl; }else{
}else{ ssdbg << " type\t\t--->\tsuffix matching" << std::endl;
ssdbg << " type\t\t--->\tsuffix matching" << std::endl;
}
ssdbg << " base std::string\t--->\t" << paddhead->basestring << std::endl;
ssdbg << " add header\t--->\t" << paddhead->headkey << ": " << paddhead->headvalue << std::endl;
} }
ssdbg << " base std::string\t--->\t" << paddhead->basestring << std::endl;
ssdbg << " add header\t--->\t" << paddhead->headkey << ": " << paddhead->headvalue << std::endl;
ssdbg << " }" << std::endl; ssdbg << " }" << std::endl;
} }

View File

@ -30,20 +30,36 @@
//---------------------------------------------- //----------------------------------------------
// Structure / Typedef // Structure / Typedef
//---------------------------------------------- //----------------------------------------------
typedef struct add_header{ struct add_header{
add_header(std::unique_ptr<regex_t> pregex, std::string basestring, std::string headkey, std::string headvalue)
: pregex(std::move(pregex))
, basestring(std::move(basestring))
, headkey(std::move(headkey))
, headvalue(std::move(headvalue))
{}
~add_header() { ~add_header() {
if(pregex){ if(pregex){
regfree(pregex.get()); regfree(pregex.get());
} }
} }
add_header(const add_header&) = delete;
add_header(add_header&& val)
: pregex(std::move(val.pregex))
, basestring(std::move(val.basestring))
, headkey(std::move(val.headkey))
, headvalue(std::move(val.headvalue))
{}
add_header& operator=(const add_header&) = delete;
add_header& operator=(add_header&&) = delete;
std::unique_ptr<regex_t> pregex; // not nullptr means using regex, nullptr means comparing suffix directly. std::unique_ptr<regex_t> pregex; // not nullptr means using regex, nullptr means comparing suffix directly.
std::string basestring; std::string basestring;
std::string headkey; std::string headkey;
std::string headvalue; std::string headvalue;
}ADDHEAD; };
typedef std::vector<std::unique_ptr<ADDHEAD>> addheadlist_t; typedef std::vector<add_header> addheadlist_t;
//---------------------------------------------- //----------------------------------------------
// Class AdditionalHeader // Class AdditionalHeader