Merge pull request #55 from oatpp/issue_54_add_support_for_parsing_header_value_set

Add support for parsing header value set
#54
This commit is contained in:
Leonid Stryzhevskyi 2019-03-25 20:13:48 +02:00 committed by GitHub
commit 87dbdea99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 1 deletions

View File

@ -351,4 +351,21 @@ void Parser::parseHeaders(Headers& headers,
}
std::unordered_set<oatpp::data::share::StringKeyLabelCI> Parser::parseHeaderValueSet(const oatpp::data::share::StringKeyLabel& headerValue, char separator) {
std::unordered_set<oatpp::data::share::StringKeyLabelCI> result;
oatpp::parser::Caret caret(headerValue.getData(), headerValue.getSize());
while (caret.canContinue()) {
caret.skipChar(' '); // skip leading space
auto label = caret.putLabel();
caret.findChar(separator); // find separator char, or end of the header value
result.insert(oatpp::data::share::StringKeyLabelCI(headerValue.getMemoryHandle(), label.getData(), label.getSize()));
caret.inc();
}
return result;
}
}}}}

View File

@ -36,6 +36,7 @@
#include "oatpp/core/Types.hpp"
#include <unordered_map>
#include <unordered_set>
namespace oatpp { namespace web { namespace protocol { namespace http {
@ -650,6 +651,14 @@ public:
const std::shared_ptr<oatpp::base::StrBuffer>& headersText,
oatpp::parser::Caret& caret,
Status& error);
/**
* Parse header value separated by `char separator`.
* @param headerValue - value of the header.
* @param separator - separator char.
* @return - `std::unordered_set` of &id:oatpp::data::share::StringKeyLabelCI;.
*/
static std::unordered_set<oatpp::data::share::StringKeyLabelCI> parseHeaderValueSet(const oatpp::data::share::StringKeyLabel& headerValue, char separator);
};

View File

@ -50,7 +50,7 @@ namespace oatpp { namespace test { namespace web {
namespace {
//#define OATPP_TEST_USE_PORT 8123
//#define OATPP_TEST_USE_PORT 8000
class TestComponent {
public:
@ -180,6 +180,11 @@ void FullTest::onRun() {
OATPP_ASSERT(returnedData == data);
}
{
auto response = client->headerValueSet(" VALUE_1, VALUE_2, VALUE_3", connection);
OATPP_ASSERT(response->getStatusCode() == 200);
}
if((i + 1) % iterationsStep == 0) {
OATPP_LOGD("i", "%d", i + 1);
}

View File

@ -42,6 +42,8 @@ class Client : public oatpp::web::client::ApiClient {
API_CALL("GET", "headers", getWithHeaders, HEADER(String, param, "X-TEST-HEADER"))
API_CALL("POST", "body", postBody, BODY_STRING(String, body))
API_CALL("POST", "echo", echoBody, BODY_STRING(String, body))
API_CALL("GET", "header-value-set", headerValueSet, HEADER(String, valueSet, "X-VALUE-SET"))
#include OATPP_CODEGEN_END(ApiClient)
};

View File

@ -103,6 +103,15 @@ public:
return createResponse(Status::CODE_200, body);
}
ENDPOINT("GET", "header-value-set", headerValueSet,
HEADER(String, valueSet, "X-VALUE-SET")) {
auto set = oatpp::web::protocol::http::Parser::parseHeaderValueSet(valueSet, ',');
OATPP_ASSERT_HTTP(set.find("VALUE_1") != set.end(), Status::CODE_400, "No header 'VALUE_1' in value set");
OATPP_ASSERT_HTTP(set.find("VALUE_2") != set.end(), Status::CODE_400, "No header 'VALUE_2' in value set");
OATPP_ASSERT_HTTP(set.find("VALUE_3") != set.end(), Status::CODE_400, "No header 'VALUE_3' in value set");
return createResponse(Status::CODE_200, "");
}
#include OATPP_CODEGEN_END(ApiController)
};