1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
/*
* SPDX-FileCopyrightText: 2016 Mathieu Stefani
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
Mathieu Stefani, 15 février 2016
Example of custom headers registering
*/
#include <pistache/http_headers.h>
#include <pistache/net.h>
#include <sys/types.h>
// Quiet a warning about "minor" and "major" being doubly defined.
#ifdef major
#undef major
#endif
#ifdef minor
#undef minor
#endif
using namespace Pistache;
class XProtocolVersion : public Http::Header::Header
{
public:
NAME("X-Protocol-Version");
XProtocolVersion() = default;
XProtocolVersion(uint32_t major, uint32_t minor)
: maj(major)
, min(minor)
{ }
void parse(const std::string& str) override
{
auto p = str.find('.');
std::string major, minor;
if (p != std::string::npos)
{
major = str.substr(0, p);
minor = str.substr(p + 1);
}
else
{
major = str;
}
maj = std::stoi(major);
if (!minor.empty())
min = std::stoi(minor);
}
void write(std::ostream& os) const override
{
os << maj;
os << "." << min;
}
uint32_t majorVersion() const
{
return maj;
}
uint32_t minorVersion() const
{
return min;
}
private:
uint32_t maj = 0;
uint32_t min = 0;
};
int main()
{
Http::Header::Registry::instance().registerHeader<XProtocolVersion>();
}
|