Add get_header() to answer_type.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
bb6b00114e
commit
447fc05dbe
|
@ -99,6 +99,15 @@ struct answer_type
|
||||||
*/
|
*/
|
||||||
friend std::ostream &operator <<(std::ostream &out,
|
friend std::ostream &operator <<(std::ostream &out,
|
||||||
const answer_type &answer);
|
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
|
} // namespace mastodonpp
|
||||||
|
|
|
@ -16,9 +16,15 @@
|
||||||
|
|
||||||
#include "answer.hpp"
|
#include "answer.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
namespace mastodonpp
|
namespace mastodonpp
|
||||||
{
|
{
|
||||||
|
|
||||||
|
using std::search;
|
||||||
|
using std::tolower;
|
||||||
|
|
||||||
answer_type::operator bool() const
|
answer_type::operator bool() const
|
||||||
{
|
{
|
||||||
return (curl_error_code == 0 && http_status == 200);
|
return (curl_error_code == 0 && http_status == 200);
|
||||||
|
@ -35,4 +41,23 @@ std::ostream &operator <<(std::ostream &out, const answer_type &answer)
|
||||||
return out;
|
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
|
} // namespace mastodonpp
|
||||||
|
|
Loading…
Reference in New Issue