4#include "Stroika/Foundation/StroikaPreComp.h"
8#include "Stroika/Foundation/Containers/Set.h"
10#include "Stroika/Foundation/IO/Network/HTTP/Headers.h"
23 String ExtractReasonFromResponse_NonPlainText_ (
const String& responseBody,
bool atEOF)
31 auto words = responseBody.
FindEachMatch (kBunchaWordsOrTags_);
33 if (words.size () > 2) {
34 words.Remove (words.size () - 1);
39 for (
const String& w : wordText) {
41 if (not w.StartsWith (
"<"sv)) {
53 using namespace Streams;
56 if (
auto ctHdr = response.GetHeaders ().Lookup (
"Content-Type"sv)) {
59 if (ct == InternetMediaTypes::kText_PLAIN) {
64 InputStream::Ptr<Character> textStream = BinaryToText::Reader::New (response.
GetData ());
65 String roughText = textStream.ReadAll (span{buf});
66 return ExtractReasonFromResponse_NonPlainText_ (roughText, textStream.IsAtEOF ());
74 constexpr bool kIncludeTest_ =
false;
78 if constexpr (kIncludeTest_) {
79 Assert (ExtractReasonFromResponse_NonPlainText_ (
"<html> <head> <title>502 Bad Gateway</title></head><body><center> <h1> "
80 "502 Bad Gateway </h1></center></body></html>",
81 true) ==
"502 Bad Gateway 502 Bad Gateway");
82 Assert (ExtractReasonFromResponse_NonPlainText_ (
"<html> <head> <title>502 Bad Gateway</title></head><body><center> <h1> "
83 "502 Bad Gateway </h1></center></body></html>",
84 false) ==
"502 Bad Gateway 502 Bad Gateway...");
85 Assert (ExtractReasonFromResponse_NonPlainText_ (
86 "{ \"error\": \"unsupported_grant_type\", \"error_description\": \"Invalid grant_type\", ",
false) ==
87 "error unsupported_grant_type error_description Invalid grant_type...");
98Exception::Exception (
const Response& response)
99 : HTTP::Exception{response.GetStatus (), ExtractReasonFromResponse_ (response)}
100 , fResponse_{response}
104Response Exception::GetResponse ()
const
RegularExpression is a compiled regular expression which can be used to match on a String class.
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
nonvirtual String str() const
String is like std::u32string, except it is much easier to use, often much more space efficient,...
nonvirtual Containers::Sequence< RegularExpressionMatch > FindEachMatch(const RegularExpression ®Ex) const
nonvirtual String RTrim(bool(*shouldBeTrimmed)(Character)=Character::IsWhitespace) const
nonvirtual BLOB GetData() const
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
nonvirtual RESULT_CONTAINER Map(ELEMENT_MAPPER &&elementMapper) const
functional API which iterates over all members of an Iterable, applies a map function to each element...
Ptr New(const InputStream::Ptr< byte > &src, optional< AutomaticCodeCvtFlags > codeCvtFlags={}, optional< SeekableFlag > seekable={}, ReadAhead readAhead=eReadAheadAllowed)
Create an InputStream::Ptr<Character> from the arguments (usually binary source) - which can be used ...