mirror of
https://github.com/oatpp/oatpp.git
synced 2025-01-06 16:24:27 +08:00
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:
commit
87dbdea99c
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}}}}
|
||||
|
@ -36,6 +36,7 @@
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace oatpp { namespace web { namespace protocol { namespace http {
|
||||
|
||||
@ -651,6 +652,14 @@ public:
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
}}}}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -43,6 +43,8 @@ class Client : public oatpp::web::client::ApiClient {
|
||||
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)
|
||||
};
|
||||
|
||||
|
@ -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)
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user