Add get_header() to answer_type.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
tastytea 2020-01-09 13:56:20 +01:00
parent bb6b00114e
commit 447fc05dbe
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 34 additions and 0 deletions

View File

@ -99,6 +99,15 @@ struct answer_type
*/
friend std::ostream &operator <<(std::ostream &out,
const answer_type &answer);
/*!
* @brief Returns the value of a header field.
*
* Case insensitive, only ASCII.
*
* @since 0.1.0
*/
string_view get_header(string_view field) const;
};
} // namespace mastodonpp

View File

@ -16,9 +16,15 @@
#include "answer.hpp"
#include <algorithm>
#include <cctype>
namespace mastodonpp
{
using std::search;
using std::tolower;
answer_type::operator bool() const
{
return (curl_error_code == 0 && http_status == 200);
@ -35,4 +41,23 @@ std::ostream &operator <<(std::ostream &out, const answer_type &answer)
return out;
}
string_view answer_type::get_header(const string_view field) const
{
const string_view searchstring{string(field) += ':'};
auto it{search(headers.begin(), headers.end(),
searchstring.begin(), searchstring.end(),
[](unsigned char a, unsigned char b)
{ return tolower(a) == tolower(b); })};
if (it != headers.end())
{
auto pos{static_cast<size_t>(it - headers.begin())};
pos = headers.find(':', pos) + 2;
const auto endpos{headers.find('\n', pos)};
return headers.substr(pos, endpos - pos);
}
return {};
}
} // namespace mastodonpp