Add API to replace headers in requests and responses (Closes #481).

This commit is contained in:
Benedikt-Alexander Mokroß 2021-09-10 08:12:14 +02:00
parent 777617321d
commit 1ce3e3bd16
11 changed files with 152 additions and 2 deletions

View File

@ -180,6 +180,46 @@ public:
}
/**
* Erases all occurrences of key and replaces them with a new entry
* @param key
* @param value
* @return - true if an entry was replaced, false if entry was only inserted.
*/
bool replaceOrPut(const Key& key, const StringKeyLabel& value) {
std::lock_guard<concurrency::SpinLock> lock(m_lock);
bool needsErase = m_map.find(key) != m_map.end();
if (needsErase) {
m_map.erase(key);
}
m_map.insert({key, value});
m_fullyInitialized = false;
return needsErase;
}
/**
* Erases all occurrences of key and replaces them with a new entry. Not thread-safe.
* @param key
* @param value
* @return - `true` if an entry was replaced, `false` if entry was only inserted.
*/
bool replaceOrPut_LockFree(const Key& key, const StringKeyLabel& value) {
bool needsErase = m_map.find(key) != m_map.end();
if (needsErase) {
m_map.erase(key);
}
m_map.insert({key, value});
m_fullyInitialized = false;
return needsErase;
}
/**
* Get value as &id:oatpp::String;.
* @param key

View File

@ -101,6 +101,15 @@ bool Request::putHeaderIfNotExists(const oatpp::String& key, const oatpp::String
return m_headers.putIfNotExists(key, value);
}
bool Request::replaceOrPutHeader(const String &key, const String &value) {
return m_headers.replaceOrPut(key, value);
}
bool Request::replaceOrPutHeader_Unsafe(const data::share::StringKeyLabelCI_FAST &key,
const data::share::StringKeyLabel &value) {
return m_headers.replaceOrPut(key, value);
}
void Request::putHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value) {
m_headers.put(key, value);
}

View File

@ -154,6 +154,22 @@ public:
*/
bool putHeaderIfNotExists(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value);
/**
* Add http header.
* @param key - &id:oatpp::data::share::StringKeyLabelCI_FAST;.

View File

@ -66,6 +66,14 @@ bool Response::putHeaderIfNotExists(const oatpp::String& key, const oatpp::Strin
return m_headers.putIfNotExists(key, value);
}
bool Response::replaceOrPutHeader(const String &key, const String &value) {
return m_headers.replaceOrPut(key, value);
}
bool Response::replaceOrPutHeader_Unsafe(const data::share::StringKeyLabelCI_FAST &key,
const data::share::StringKeyLabel &value) {
return m_headers.replaceOrPut(key, value);
}
void Response::putHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value) {
m_headers.put(key, value);
}

View File

@ -116,6 +116,22 @@ public:
*/
bool putHeaderIfNotExists(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value);
/**
* Add http header.
* @param key - &id:oatpp::data::share::StringKeyLabelCI_FAST;.

View File

@ -68,6 +68,14 @@ bool Request::putHeaderIfNotExists(const oatpp::String& key, const oatpp::String
return m_headers.putIfNotExists(key, value);
}
bool Request::replaceOrPutHeader(const String &key, const String &value) {
return m_headers.replaceOrPut(key, value);
}
bool Request::replaceOrPutHeader_Unsafe(const data::share::StringKeyLabelCI_FAST &key,
const data::share::StringKeyLabel &value) {
return m_headers.replaceOrPut(key, value);
}
void Request::putHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value) {
m_headers.put(key, value);
}

View File

@ -109,6 +109,22 @@ public:
*/
bool putHeaderIfNotExists(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value);
/**
* Add http header.
* @param key - &id:oatpp::data::share::StringKeyLabelCI_FAST;.

View File

@ -56,6 +56,15 @@ bool Response::putHeaderIfNotExists(const oatpp::String& key, const oatpp::Strin
return m_headers.putIfNotExists(key, value);
}
bool Response::replaceOrPutHeader(const String &key, const String &value) {
return m_headers.replaceOrPut(key, value);
}
bool Response::replaceOrPutHeader_Unsafe(const data::share::StringKeyLabelCI_FAST &key,
const data::share::StringKeyLabel &value) {
return m_headers.replaceOrPut(key, value);
}
void Response::putHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value) {
m_headers.put(key, value);
}

View File

@ -104,6 +104,22 @@ public:
*/
bool putHeaderIfNotExists(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader(const oatpp::String& key, const oatpp::String& value);
/**
* Replaces or adds header.
* @param key - &id:oatpp::String;.
* @param value - &id:oatpp::String;.
* @return - `true` if header was replaces, `false` if header was added.
*/
bool replaceOrPutHeader_Unsafe(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value);
/**
* Add http header.
* @param key - &id:oatpp::data::share::StringKeyLabelCI_FAST;.

View File

@ -483,6 +483,12 @@ void FullTest::onRun() {
OATPP_ASSERT(value == "Hello World!!!");
}
{ // test header replacement
auto response = client->getInterceptors(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
OATPP_ASSERT(response->getHeader("to-be-replaced") == "replaced_value");
}
if((i + 1) % iterationsStep == 0) {
auto ticks = oatpp::base::Environment::getMicroTickCount() - lastTick;
lastTick = oatpp::base::Environment::getMicroTickCount();

View File

@ -83,6 +83,11 @@ public:
response->putHeader("header-out-inter3", "inter3");
return response;
}
ENDPOINT_INTERCEPTOR(interceptor, replacer) {
auto response = (this->*intercepted)(request);
response->replaceOrPutHeader("to-be-replaced", "replaced_value");
return response;
}
ENDPOINT_INTERCEPTOR(interceptor, asserter) {
auto response = (this->*intercepted)(request);
@ -100,8 +105,9 @@ public:
OATPP_ASSERT(request->getHeader("header-in-inter2") == "inter2");
OATPP_ASSERT(request->getHeader("header-in-inter3") == "inter3");
return createResponse(Status::CODE_200, "Hello World!!!");
auto response = createResponse(Status::CODE_200, "Hello World!!!");
response->putHeader("to-be-replaced", "original_value");
return response;
}