| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2019 |
| 4 |
* Author: Georgios Bitzes <[email protected]> |
| 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 "CompatibilityHacks.hpp" |
| 23 |
#include <core/ContentProvider.hpp> |
| 24 |
#include <fileops/AzureIO.hpp> |
| 25 |
#include <fileops/S3IO.hpp> |
| 26 |
#include <utils/davix_logger_internal.hpp> |
| 27 |
|
| 28 |
namespace Davix { |
| 29 |
|
| 30 |
//------------------------------------------------------------------------------ |
| 31 |
// Azure has a rather restrictive limit of 256 MB uploaded per each HTTP |
| 32 |
// request. |
| 33 |
// |
| 34 |
// In case more than 256MB needs to be uploaded, we follow Azure's chunked |
| 35 |
// upload mechanism and commit at the end. |
| 36 |
// |
| 37 |
// All is good if davix _knows_ we're writing to Azure. What happens if we're |
| 38 |
// just redirected to a pre-signed Azure endpoint? |
| 39 |
// |
| 40 |
// We need to guess if we're talking to Azure after a redirect, and engage |
| 41 |
// the chunking mechanim as before. |
| 42 |
// |
| 43 |
// Function returns true if Azure mechanism was engaged, false otherwise. |
| 44 |
// (False means "This is not an Azure URL") |
| 45 |
//------------------------------------------------------------------------------ |
| 46 |
bool CompatibilityHacks::azureChunkedUpload(const std::string &requestType, const Uri& uri, Context &context, const RequestParams ¶ms, ContentProvider &provider, DavixError **err) { |
| 47 |
if(!shouldEngageAzureChunkedUpload(requestType, uri)) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
IOChainContext iocontext(context, uri, ¶ms); |
| 52 |
|
| 53 |
using std::placeholders::_1; |
| 54 |
using std::placeholders::_2; |
| 55 |
|
| 56 |
try { |
| 57 |
AzureIO azureio; |
| 58 |
azureio.writeFromProvider(iocontext, provider); |
| 59 |
} |
| 60 |
catch(DavixException &e) { |
| 61 |
e.toDavixError(err); |
| 62 |
} |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
//------------------------------------------------------------------------------ |
| 68 |
// Heuristic to check if URL + requestType should engage the Azure |
| 69 |
// chunk-upload mechanism. |
| 70 |
//------------------------------------------------------------------------------ |
| 71 |
bool CompatibilityHacks::shouldEngageAzureChunkedUpload(const std::string &requestType, const Uri& uri) { |
| 72 |
if(requestType != "PUT" && requestType != "put") { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
if(!uri.queryParamExists("sig") || !uri.queryParamExists("sr") || !uri.queryParamExists("sp")) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
if(uri.fragmentParamExists("azuremechanism")) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
//------------------------------------------------------------------------------ |
| 88 |
// Dynafed-assisted multi-chunk S3 upload. |
| 89 |
// Returns if dynafed mechanism was engaged. |
| 90 |
//------------------------------------------------------------------------------ |
| 91 |
bool CompatibilityHacks::dynafedAssistedS3Upload(const BackendRequest& originatingRequest, const Uri& uri, Context& context, const RequestParams ¶ms, ContentProvider &provider, DavixError** err) { |
| 92 |
std::string ugrs3post; |
| 93 |
std::string ugrpluginid; |
| 94 |
|
| 95 |
const uint64_t s3SizeThreshold = (1024ull * 1024ull * 1024ull * 5ull); |
| 96 |
|
| 97 |
if(!originatingRequest.getAnswerHeader("x-ugrs3posturl", ugrs3post)) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
if(!originatingRequest.getAnswerHeader("x-ugrpluginid", ugrpluginid)) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
if(ugrs3post.empty()) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
// Don't engage if size to upload is less than the specified threshold. |
| 110 |
// Override with 'forceMultiPart' fragment param. |
| 111 |
if((uint64_t)provider.getSize() < s3SizeThreshold && |
| 112 |
!uri.fragmentParamExists("forceMultiPart")) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "Engaging dynafed-assisted multi-part upload to S3, posturl: {}, pluginid: {}", ugrs3post, ugrpluginid); |
| 117 |
IOChainContext iocontext(context, uri, ¶ms); |
| 118 |
|
| 119 |
using std::placeholders::_1; |
| 120 |
using std::placeholders::_2; |
| 121 |
|
| 122 |
S3IO s3io; |
| 123 |
s3io.performUgrS3MultiPart(iocontext, ugrs3post, ugrpluginid, provider, err); |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
} |