2016-02-07 05:41:56 +00:00
|
|
|
/*
|
|
|
|
* s3fs - FUSE-based file system backed by Amazon S3
|
|
|
|
*
|
2017-05-07 11:24:17 +00:00
|
|
|
* Copyright(C) 2007 Randy Rizun <rrizun@gmail.com>
|
2016-02-07 05:41:56 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2019-07-12 10:33:53 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2016-02-07 05:41:56 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "s3fs.h"
|
2020-08-22 12:40:53 +00:00
|
|
|
#include "addhead.h"
|
|
|
|
#include "curl_util.h"
|
2016-02-07 05:41:56 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Symbols
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
#define ADD_HEAD_REGEX "reg:"
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Class AdditionalHeader
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
AdditionalHeader AdditionalHeader::singleton;
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Class AdditionalHeader method
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
AdditionalHeader::AdditionalHeader()
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(this == AdditionalHeader::get()){
|
|
|
|
is_enable = false;
|
|
|
|
}else{
|
|
|
|
abort();
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AdditionalHeader::~AdditionalHeader()
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(this == AdditionalHeader::get()){
|
|
|
|
Unload();
|
|
|
|
}else{
|
|
|
|
abort();
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AdditionalHeader::Load(const char* file)
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!file){
|
|
|
|
S3FS_PRN_WARN("file is NULL.");
|
|
|
|
return false;
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
Unload();
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-09-11 09:37:24 +00:00
|
|
|
std::ifstream AH(file);
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!AH.good()){
|
|
|
|
S3FS_PRN_WARN("Could not open file(%s).", file);
|
|
|
|
return false;
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// read file
|
2020-09-11 09:37:24 +00:00
|
|
|
std::string line;
|
2020-08-22 12:40:53 +00:00
|
|
|
ADDHEAD *paddhead;
|
|
|
|
while(getline(AH, line)){
|
2021-04-12 22:36:09 +00:00
|
|
|
if(line.empty()){
|
2020-08-22 12:40:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-12 22:36:09 +00:00
|
|
|
if('#' == line[0]){
|
2020-08-22 12:40:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// load a line
|
2020-09-11 09:37:24 +00:00
|
|
|
std::istringstream ss(line);
|
|
|
|
std::string key; // suffix(key)
|
|
|
|
std::string head; // additional HTTP header
|
|
|
|
std::string value; // header value
|
2020-08-22 12:40:53 +00:00
|
|
|
if(0 == isblank(line[0])){
|
|
|
|
ss >> key;
|
|
|
|
}
|
|
|
|
if(ss){
|
|
|
|
ss >> head;
|
|
|
|
if(ss && static_cast<size_t>(ss.tellg()) < line.size()){
|
|
|
|
value = line.substr(static_cast<int>(ss.tellg()) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check it
|
|
|
|
if(head.empty()){
|
|
|
|
if(key.empty()){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
S3FS_PRN_ERR("file format error: %s key(suffix) is no HTTP header value.", key.c_str());
|
|
|
|
Unload();
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
paddhead = new ADDHEAD;
|
|
|
|
if(0 == strncasecmp(key.c_str(), ADD_HEAD_REGEX, strlen(ADD_HEAD_REGEX))){
|
|
|
|
// regex
|
|
|
|
if(key.size() <= strlen(ADD_HEAD_REGEX)){
|
2020-09-11 09:37:24 +00:00
|
|
|
S3FS_PRN_ERR("file format error: %s key(suffix) does not have key std::string.", key.c_str());
|
2020-08-22 12:40:53 +00:00
|
|
|
delete paddhead;
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-25 09:02:32 +00:00
|
|
|
key.erase(0, strlen(ADD_HEAD_REGEX));
|
2020-08-22 12:40:53 +00:00
|
|
|
|
|
|
|
// compile
|
|
|
|
regex_t* preg = new regex_t;
|
|
|
|
int result;
|
|
|
|
if(0 != (result = regcomp(preg, key.c_str(), REG_EXTENDED | REG_NOSUB))){ // we do not need matching info
|
|
|
|
char errbuf[256];
|
|
|
|
regerror(result, preg, errbuf, sizeof(errbuf));
|
|
|
|
S3FS_PRN_ERR("failed to compile regex from %s key by %s.", key.c_str(), errbuf);
|
|
|
|
delete preg;
|
|
|
|
delete paddhead;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set
|
|
|
|
paddhead->pregex = preg;
|
|
|
|
paddhead->basestring = key;
|
|
|
|
paddhead->headkey = head;
|
|
|
|
paddhead->headvalue = value;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
// not regex, directly comparing
|
|
|
|
paddhead->pregex = NULL;
|
|
|
|
paddhead->basestring = key;
|
|
|
|
paddhead->headkey = head;
|
|
|
|
paddhead->headvalue = value;
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// add list
|
|
|
|
addheadlist.push_back(paddhead);
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// set flag
|
|
|
|
if(!is_enable){
|
|
|
|
is_enable = true;
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 23:44:50 +00:00
|
|
|
void AdditionalHeader::Unload()
|
2016-02-07 05:41:56 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
is_enable = false;
|
|
|
|
|
|
|
|
for(addheadlist_t::iterator iter = addheadlist.begin(); iter != addheadlist.end(); ++iter){
|
|
|
|
ADDHEAD *paddhead = *iter;
|
|
|
|
if(paddhead){
|
|
|
|
if(paddhead->pregex){
|
|
|
|
regfree(paddhead->pregex);
|
|
|
|
delete paddhead->pregex;
|
|
|
|
}
|
|
|
|
delete paddhead;
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
addheadlist.clear();
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AdditionalHeader::AddHeader(headers_t& meta, const char* path) const
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!is_enable){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(!path){
|
|
|
|
S3FS_PRN_WARN("path is NULL.");
|
|
|
|
return false;
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
size_t pathlength = strlen(path);
|
|
|
|
|
|
|
|
// loop
|
|
|
|
//
|
|
|
|
// [NOTE]
|
|
|
|
// Because to allow duplicate key, and then scanning the entire table.
|
|
|
|
//
|
|
|
|
for(addheadlist_t::const_iterator iter = addheadlist.begin(); iter != addheadlist.end(); ++iter){
|
|
|
|
const ADDHEAD *paddhead = *iter;
|
|
|
|
if(!paddhead){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(paddhead->pregex){
|
|
|
|
// regex
|
|
|
|
regmatch_t match; // not use
|
|
|
|
if(0 == regexec(paddhead->pregex, path, 1, &match, 0)){
|
|
|
|
// match -> adding header
|
|
|
|
meta[paddhead->headkey] = paddhead->headvalue;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// directly comparing
|
|
|
|
if(paddhead->basestring.length() < pathlength){
|
2021-06-13 11:03:10 +00:00
|
|
|
if(paddhead->basestring.empty() || 0 == strcmp(&path[pathlength - paddhead->basestring.length()], paddhead->basestring.c_str())){
|
2020-08-22 12:40:53 +00:00
|
|
|
// match -> adding header
|
|
|
|
meta[paddhead->headkey] = paddhead->headvalue;
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct curl_slist* AdditionalHeader::AddHeader(struct curl_slist* list, const char* path) const
|
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
headers_t meta;
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
if(!AddHeader(meta, path)){
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
for(headers_t::iterator iter = meta.begin(); iter != meta.end(); ++iter){
|
|
|
|
// Adding header
|
|
|
|
list = curl_slist_sort_insert(list, iter->first.c_str(), iter->second.c_str());
|
|
|
|
}
|
|
|
|
meta.clear();
|
|
|
|
S3FS_MALLOCTRIM(0);
|
2016-02-07 05:41:56 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-01-23 23:44:50 +00:00
|
|
|
bool AdditionalHeader::Dump() const
|
2016-02-07 05:41:56 +00:00
|
|
|
{
|
2020-10-13 14:00:11 +00:00
|
|
|
if(!S3fsLog::IsS3fsLogDbg()){
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-09-11 09:37:24 +00:00
|
|
|
std::ostringstream ssdbg;
|
|
|
|
int cnt = 1;
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-09-11 09:37:24 +00:00
|
|
|
ssdbg << "Additional Header list[" << addheadlist.size() << "] = {" << std::endl;
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
for(addheadlist_t::const_iterator iter = addheadlist.begin(); iter != addheadlist.end(); ++iter, ++cnt){
|
|
|
|
const ADDHEAD *paddhead = *iter;
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-09-11 09:37:24 +00:00
|
|
|
ssdbg << " [" << cnt << "] = {" << std::endl;
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
if(paddhead){
|
|
|
|
if(paddhead->pregex){
|
2020-09-11 09:37:24 +00:00
|
|
|
ssdbg << " type\t\t--->\tregex" << std::endl;
|
2020-08-22 12:40:53 +00:00
|
|
|
}else{
|
2020-09-11 09:37:24 +00:00
|
|
|
ssdbg << " type\t\t--->\tsuffix matching" << std::endl;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2020-09-11 09:37:24 +00:00
|
|
|
ssdbg << " base std::string\t--->\t" << paddhead->basestring << std::endl;
|
|
|
|
ssdbg << " add header\t--->\t" << paddhead->headkey << ": " << paddhead->headvalue << std::endl;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2020-09-11 09:37:24 +00:00
|
|
|
ssdbg << " }" << std::endl;
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-11 09:37:24 +00:00
|
|
|
ssdbg << "}" << std::endl;
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
// print all
|
|
|
|
S3FS_PRN_DBG("%s", ssdbg.str().c_str());
|
2016-02-07 05:41:56 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
2016-02-07 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
2020-08-22 12:40:53 +00:00
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2016-02-07 05:41:56 +00:00
|
|
|
* End:
|
2020-08-22 12:40:53 +00:00
|
|
|
* vim600: expandtab sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: expandtab sw=4 ts=4
|
2016-02-07 05:41:56 +00:00
|
|
|
*/
|