diff --git a/include/answer.hpp b/include/answer.hpp index 2eb05aa..56a8324 100644 --- a/include/answer.hpp +++ b/include/answer.hpp @@ -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 diff --git a/src/answer.cpp b/src/answer.cpp index 8006f90..8cc9236 100644 --- a/src/answer.cpp +++ b/src/answer.cpp @@ -16,9 +16,15 @@ #include "answer.hpp" +#include +#include + 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(it - headers.begin())}; + pos = headers.find(':', pos) + 2; + const auto endpos{headers.find('\n', pos)}; + return headers.substr(pos, endpos - pos); + } + + return {}; +} + } // namespace mastodonpp