Merge pull request #95 from oatpp/add_random_utils

Add random utils. Add multipart generate random boundary method
This commit is contained in:
Leonid Stryzhevskyi 2019-07-28 01:56:16 +04:00 committed by GitHub
commit 98c33d3b92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 143 additions and 2 deletions

View File

@ -95,6 +95,8 @@ add_library(oatpp
oatpp/core/parser/ParsingError.hpp
oatpp/core/utils/ConversionUtils.cpp
oatpp/core/utils/ConversionUtils.hpp
oatpp/core/utils/Random.cpp
oatpp/core/utils/Random.hpp
oatpp/encoding/Base64.cpp
oatpp/encoding/Base64.hpp
oatpp/encoding/Hex.cpp

View File

@ -0,0 +1,50 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#include "Random.hpp"
namespace oatpp { namespace utils { namespace random {
#ifndef OATPP_COMPAT_BUILD_NO_THREAD_LOCAL
thread_local std::mt19937 Random::RANDOM_GENERATOR(std::random_device{}());
#else
std::mt19937 Random::RANDOM_GENERATOR (std::random_device{}());
oatpp::concurrency::SpinLock Random::RANDOM_LOCK;
#endif
void Random::randomBytes(p_char8 buffer, v_int32 bufferSize) {
#if defined(OATPP_COMPAT_BUILD_NO_THREAD_LOCAL)
std::lock_guard<oatpp::concurrency::SpinLock> randomLock(RANDOM_LOCK);
#endif
std::uniform_int_distribution<size_t> distribution(0, 255);
for(v_int32 i = 0; i < bufferSize; i ++) {
buffer[i] = distribution(RANDOM_GENERATOR);
}
}
}}}

View File

@ -0,0 +1,58 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#ifndef oatpp_utils_Random_hpp
#define oatpp_utils_Random_hpp
#include "oatpp/core/concurrency/SpinLock.hpp"
#include "oatpp/core/Types.hpp"
#include <random>
namespace oatpp { namespace utils { namespace random {
/**
* Utility class for random values.
*/
class Random {
private:
#ifndef OATPP_COMPAT_BUILD_NO_THREAD_LOCAL
static thread_local std::mt19937 RANDOM_GENERATOR;
#else
static std::mt19937 RANDOM_GENERATOR;
static oatpp::concurrency::SpinLock RANDOM_LOCK;
#endif
public:
/**
* Fill in buffer with random bytes [0..255].
* @param buffer - pointer to buffer.
* @param bufferSize - size of the buffer.
*/
static void randomBytes(p_char8 buffer, v_int32 bufferSize);
};
}}}
#endif // oatpp_utils_Random_hpp

View File

@ -25,6 +25,8 @@
#include "Multipart.hpp"
#include "oatpp/web/protocol/http/Http.hpp"
#include "oatpp/encoding/Base64.hpp"
#include "oatpp/core/utils/Random.hpp"
namespace oatpp { namespace web { namespace mime { namespace multipart {
@ -54,6 +56,11 @@ Multipart::Multipart(const Headers& requestHeaders){
}
std::shared_ptr<Multipart> Multipart::createSharedWithRandomBoundary(v_int32 boundarySize) {
auto boundary = generateRandomBoundary(boundarySize);
return std::make_shared<Multipart>(boundary);
}
oatpp::String Multipart::getBoundary() {
return m_boundary;
}
@ -91,4 +98,13 @@ v_int32 Multipart::count() {
return m_parts.size();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Other functions
oatpp::String generateRandomBoundary(v_int32 boundarySize) {
v_char8 buffer[boundarySize];
utils::random::Random::randomBytes(buffer, boundarySize);
return encoding::Base64::encode(buffer, boundarySize, encoding::Base64::ALPHABET_BASE64_URL_SAFE);
}
}}}}

View File

@ -64,6 +64,14 @@ public:
*/
virtual ~Multipart() = default;
/**
* Create Multipart object with random boundary. <br>
* It will generate random vector of size `boundarySize` in bytes encoded in base64.
* @param boundarySize - size of the random vecrot in bytes.
* @return - `std::shared_ptr` to Multipart.
*/
static std::shared_ptr<Multipart> createSharedWithRandomBoundary(v_int32 boundarySize = 15);
/**
* Get multipart boundary value.
* @return - multipart boundary value.
@ -98,6 +106,13 @@ public:
};
/**
* Generate random boundary for Multipart object. Base64 encoded.
* @param boundarySize - size in bytes of random vector.
* @return - &id:oatpp::String;.
*/
oatpp::String generateRandomBoundary(v_int32 boundarySize = 15);
}}}}

View File

@ -118,7 +118,7 @@ public:
std::shared_ptr<Multipart> createMultipart(const std::unordered_map<oatpp::String, oatpp::String>& map) {
auto multipart = std::make_shared<oatpp::web::mime::multipart::Multipart>("0--qwerty1234--0");
auto multipart = oatpp::web::mime::multipart::Multipart::createSharedWithRandomBoundary();
for(auto& pair : map) {

View File

@ -113,7 +113,7 @@ public:
std::shared_ptr<Multipart> createMultipart(const std::unordered_map<oatpp::String, oatpp::String>& map) {
auto multipart = std::make_shared<oatpp::web::mime::multipart::Multipart>("0--qwerty1234--0");
auto multipart = oatpp::web::mime::multipart::Multipart::createSharedWithRandomBoundary();
for(auto& pair : map) {