2010-11-13 23:59:23 +00:00
|
|
|
/*
|
|
|
|
* s3fs - FUSE-based file system backed by Amazon S3
|
|
|
|
*
|
|
|
|
* Copyright 2007-2008 Randy Rizun <rrizun@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-08-10 15:29:39 +00:00
|
|
|
#include <stdio.h>
|
2011-06-26 00:37:52 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2013-03-30 13:37:14 +00:00
|
|
|
#include <map>
|
2010-11-13 23:59:23 +00:00
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
#include "common.h"
|
2010-11-13 23:59:23 +00:00
|
|
|
#include "string_util.h"
|
|
|
|
|
2011-06-26 00:37:52 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
static const char hexAlphabet[] = "0123456789ABCDEF";
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
string lower(string s)
|
|
|
|
{
|
2010-11-13 23:59:23 +00:00
|
|
|
// change each character of the string to lower case
|
2013-03-30 13:37:14 +00:00
|
|
|
for(unsigned int i = 0; i < s.length(); i++){
|
2010-11-13 23:59:23 +00:00
|
|
|
s[i] = tolower(s[i]);
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
2010-11-13 23:59:23 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
string IntToStr(int n)
|
|
|
|
{
|
2010-12-21 15:24:46 +00:00
|
|
|
stringstream result;
|
|
|
|
result << n;
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
string trim_left(const string &s, const string &t /* = SPACES */)
|
|
|
|
{
|
2011-06-26 00:37:52 +00:00
|
|
|
string d(s);
|
|
|
|
return d.erase(0, s.find_first_not_of(t));
|
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
string trim_right(const string &s, const string &t /* = SPACES */)
|
|
|
|
{
|
2011-06-26 00:37:52 +00:00
|
|
|
string d(s);
|
|
|
|
string::size_type i(d.find_last_not_of(t));
|
2013-03-30 13:37:14 +00:00
|
|
|
if(i == string::npos){
|
2011-06-26 00:37:52 +00:00
|
|
|
return "";
|
2013-03-30 13:37:14 +00:00
|
|
|
}else{
|
2011-06-26 00:37:52 +00:00
|
|
|
return d.erase(d.find_last_not_of(t) + 1);
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
2011-06-26 00:37:52 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
string trim(const string &s, const string &t /* = SPACES */)
|
|
|
|
{
|
2011-06-26 00:37:52 +00:00
|
|
|
string d(s);
|
|
|
|
return trim_left(trim_right(d, t), t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* urlEncode a fuse path,
|
|
|
|
* taking into special consideration "/",
|
|
|
|
* otherwise regular urlEncode.
|
|
|
|
*/
|
2013-03-30 13:37:14 +00:00
|
|
|
string urlEncode(const string &s)
|
|
|
|
{
|
2011-06-26 00:37:52 +00:00
|
|
|
string result;
|
|
|
|
for (unsigned i = 0; i < s.length(); ++i) {
|
2013-03-30 13:37:14 +00:00
|
|
|
if (s[i] == '/') { // Note- special case for fuse paths...
|
2011-06-26 00:37:52 +00:00
|
|
|
result += s[i];
|
2013-03-30 13:37:14 +00:00
|
|
|
} else if (isalnum(s[i])) {
|
2011-06-26 00:37:52 +00:00
|
|
|
result += s[i];
|
2013-03-30 13:37:14 +00:00
|
|
|
} else if (s[i] == '.' || s[i] == '-' || s[i] == '*' || s[i] == '_') {
|
2011-06-26 00:37:52 +00:00
|
|
|
result += s[i];
|
2013-03-30 13:37:14 +00:00
|
|
|
} else if (s[i] == ' ') {
|
2011-06-26 00:37:52 +00:00
|
|
|
result += '+';
|
2013-03-30 13:37:14 +00:00
|
|
|
} else {
|
2011-06-26 00:37:52 +00:00
|
|
|
result += "%";
|
|
|
|
result += hexAlphabet[static_cast<unsigned char>(s[i]) / 16];
|
|
|
|
result += hexAlphabet[static_cast<unsigned char>(s[i]) % 16];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-07-10 06:24:06 +00:00
|
|
|
//
|
|
|
|
// ex. target="http://......?keyword=value&..."
|
|
|
|
//
|
|
|
|
bool get_keyword_value(string& target, const char* keyword, string& value)
|
|
|
|
{
|
|
|
|
if(!keyword){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t spos;
|
|
|
|
size_t epos;
|
|
|
|
if(string::npos == (spos = target.find(keyword))){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
spos += strlen(keyword);
|
|
|
|
if('=' != target.at(spos)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
spos++;
|
|
|
|
if(string::npos == (epos = target.find('&', spos))){
|
|
|
|
value = target.substr(spos);
|
|
|
|
}else{
|
|
|
|
value = target.substr(spos, (epos - spos));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
string prepare_url(const char* url)
|
|
|
|
{
|
2013-08-10 15:29:39 +00:00
|
|
|
DPRNNN("URL is %s", url);
|
2011-06-26 00:37:52 +00:00
|
|
|
|
|
|
|
string uri;
|
|
|
|
string host;
|
|
|
|
string path;
|
|
|
|
string url_str = str(url);
|
|
|
|
string token = str("/" + bucket);
|
|
|
|
int bucket_pos = url_str.find(token);
|
|
|
|
int bucket_length = token.size();
|
|
|
|
int uri_length = 7;
|
|
|
|
|
2013-03-30 13:37:14 +00:00
|
|
|
if(!strncasecmp(url_str.c_str(), "https://", 8)){
|
2011-06-26 00:37:52 +00:00
|
|
|
uri_length = 8;
|
2013-03-30 13:37:14 +00:00
|
|
|
}
|
2011-06-26 00:37:52 +00:00
|
|
|
uri = url_str.substr(0, uri_length);
|
|
|
|
host = bucket + "." + url_str.substr(uri_length, bucket_pos - uri_length).c_str();
|
|
|
|
path = url_str.substr((bucket_pos + bucket_length));
|
|
|
|
|
|
|
|
url_str = uri + host + path;
|
|
|
|
|
2013-08-10 15:29:39 +00:00
|
|
|
DPRNNN("URL changed is %s", url_str.c_str());
|
2011-06-26 00:37:52 +00:00
|
|
|
|
|
|
|
return str(url_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current date
|
|
|
|
* in a format suitable for a HTTP request header.
|
|
|
|
*/
|
2013-03-30 13:37:14 +00:00
|
|
|
string get_date()
|
|
|
|
{
|
2011-06-26 00:37:52 +00:00
|
|
|
char buf[100];
|
|
|
|
time_t t = time(NULL);
|
|
|
|
strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
|
|
|
|
return buf;
|
|
|
|
}
|
2013-03-30 13:37:14 +00:00
|
|
|
|