| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2021 |
| 4 |
* Author: Shiting Long <[email protected]> (Forschungszentrum Juelich) |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <utils/davix_swift_utils.hpp> |
| 23 |
#include <iomanip> |
| 24 |
#include <ctime> |
| 25 |
#include <cstring> |
| 26 |
#include <davix_internal.hpp> |
| 27 |
#include <utils/stringutils.hpp> |
| 28 |
#include "libs/datetime/datetime_utils.hpp" |
| 29 |
#include <utils/davix_logger_internal.hpp> |
| 30 |
#include <utils/davix_utils_internal.hpp> |
| 31 |
#include "libs/alibxx/crypto/base64.hpp" |
| 32 |
#include "libs/alibxx/crypto/hmacsha.hpp" |
| 33 |
#include <openssl/md5.h> |
| 34 |
#include <sys/mman.h> |
| 35 |
|
| 36 |
namespace Davix { |
| 37 |
|
| 38 |
namespace Swift { |
| 39 |
|
| 40 |
std::string extract_swift_path(const Uri & uri) { |
| 41 |
const std::string path = uri.getPath(); |
| 42 |
|
| 43 |
std::size_t pos = path.find("/", 1); |
| 44 |
if(pos == std::string::npos) { |
| 45 |
return std::string("/"); |
| 46 |
} |
| 47 |
|
| 48 |
return path.substr(pos, path.size()); |
| 49 |
} |
| 50 |
|
| 51 |
std::string extract_swift_container(const Uri & uri) { |
| 52 |
const std::string path = uri.getPath(); |
| 53 |
std::size_t pos = path.find("/", 1); |
| 54 |
if(pos == std::string::npos) { |
| 55 |
return path.substr(1, path.size()-1); |
| 56 |
} |
| 57 |
return path.substr(1, pos-1); |
| 58 |
} |
| 59 |
|
| 60 |
Uri signURI(const RequestParams & params, const Uri & url) { |
| 61 |
Uri signed_url(url); |
| 62 |
|
| 63 |
if(!params.getSwiftAccount().empty()) { |
| 64 |
signed_url.setPath("/v1/" + params.getSwiftAccount() + url.getPath()); |
| 65 |
} |
| 66 |
else if (!params.getOSProjectID().empty()) { |
| 67 |
signed_url.setPath("/v1/AUTH_" + params.getOSProjectID() + url.getPath()); |
| 68 |
} |
| 69 |
return signed_url; |
| 70 |
} |
| 71 |
|
| 72 |
Uri swiftUriTransformer(const Uri & original_url, const RequestParams & params, const bool addDelimiter) { |
| 73 |
std::string delimiter = "&delimiter=%2F"; |
| 74 |
std::string prefix = "?prefix="; |
| 75 |
|
| 76 |
std::string protocol; |
| 77 |
|
| 78 |
const std::string url_string = original_url.getString(); |
| 79 |
std::size_t pos = url_string.find(':'); |
| 80 |
|
| 81 |
if(url_string.compare(pos-1,1,"s") == 0){ |
| 82 |
protocol = "swifts://"; |
| 83 |
} |
| 84 |
else{ |
| 85 |
protocol = "swift://"; |
| 86 |
} |
| 87 |
|
| 88 |
std::ostringstream ss; |
| 89 |
|
| 90 |
ss << protocol << original_url.getHost(); |
| 91 |
if(original_url.getPort() > 0) { |
| 92 |
ss << ":" << original_url.getPort(); |
| 93 |
} |
| 94 |
ss << "/"; |
| 95 |
|
| 96 |
std::string container = extract_swift_container(original_url); |
| 97 |
if(container.size() != 0){ |
| 98 |
ss << extract_swift_container(original_url) << "/"; |
| 99 |
} |
| 100 |
|
| 101 |
if(!original_url.getPath().empty()){ // there is something after '/', grab it |
| 102 |
std::string tmp = extract_swift_path(original_url); |
| 103 |
|
| 104 |
// if prefix doesn't end with '/', add one to handle query on folder |
| 105 |
if(tmp.compare(tmp.size()-1,1,"/") != 0) |
| 106 |
tmp += "/"; |
| 107 |
|
| 108 |
tmp.erase(0,1); |
| 109 |
prefix += Uri::queryParamEscape(tmp); |
| 110 |
} |
| 111 |
|
| 112 |
ss << prefix; |
| 113 |
|
| 114 |
// skip delimiter if where we want to list everything after a certain prefix, |
| 115 |
// useful in cases like GET Collection |
| 116 |
if(addDelimiter) |
| 117 |
ss << delimiter; |
| 118 |
|
| 119 |
return Uri(ss.str()); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
} |