16 #include <zypp-core/zyppng/core/String> 20 #undef ZYPP_BASE_LOGGER_LOGGROUP 21 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::plugin" 38 Impl(
const std::string & command_r )
52 Impl( std::istream & stream_r );
63 if ( command_r.find(
'\n' ) != std::string::npos )
75 {
_body = std::move(body_r); }
77 static std::string
escapeHeader( std::string_view val,
bool escapeColon=
true ) {
93 for (
auto c = val.begin (); c!= val.end(); c++ ) {
96 escaped.push_back(
'\\');
97 escaped.push_back(
'n');
101 escaped.push_back(
'\\');
102 escaped.push_back(
'r');
106 escaped.push_back(
'\\');
107 escaped.push_back(
'\\');
112 escaped.push_back(
'\\');
113 escaped.push_back(
'c');
119 escaped.push_back (*c);
127 std::string unescaped;
128 for (
auto c = val.begin (); c!= val.end(); ) {
130 unescaped.push_back (*c);
136 if ( c == val.end() )
141 unescaped.push_back(
'\n');
146 unescaped.push_back(
'\r');
151 unescaped.push_back(
'\\');
156 unescaped.push_back(
':');
170 using KeyRange = std::pair<HeaderList::iterator, HeaderList::iterator>;
178 const std::string &
getHeader(
const std::string & key_r )
const 181 if ( r.first == r.second )
183 const std::string & ret( r.first->second );
184 if ( ++r.first != r.second )
189 const std::string &
getHeader(
const std::string & key_r,
const std::string & default_r )
const 192 if ( r.first == r.second )
194 const std::string & ret( r.first->second );
195 if ( ++r.first != r.second )
200 const std::string &
getHeaderNT(
const std::string & key_r,
const std::string & default_r )
const 203 return iter !=
_header.end() ? iter->second : default_r;
206 HeaderList::value_type
mkHeaderPair(
const std::string & key_r,
const std::string & value_r )
208 return HeaderList::value_type( key_r, value_r );
211 void setHeader(
const std::string & key_r,
const std::string & value_r )
217 void addHeader(
const std::string & key_r,
const std::string & value_r )
224 for (
const auto & el : contents_r )
231 if ( sep == std::string::npos )
243 std::ostream &
writeTo( std::ostream & stream_r )
const;
254 static shared_ptr<Impl> _nullimpl(
new Impl );
258 friend Impl * rwcowClone<Impl>(
const Impl * rhs );
261 {
return new Impl( *
this ); }
268 return str <<
"PluginFrame[" << obj.
command() <<
"](" << obj.
headerList().size() <<
"){" << obj.
body().size() <<
"}";
289 if ( ! stream_r.good() )
295 if ( ! stream_r.good() )
307 std::optional<uint64_t> cLen;
310 if ( !contentLen.empty() ) {
311 cLen = zyppng::str::safe_strtonum<uint64_t>(contentLen);
325 _body.resize ( (*cLen)+1,
'\0' );
326 stream_r.read (
_body.data(), (*cLen)+1 );
328 if ( ! stream_r.good() )
330 if (
_body.back() !=
'\0' )
338 if ( ! stream_r.good() )
350 stream_r << _command <<
"\n";
360 for_( it, _header.begin(), _header.end() )
361 stream_r << escapeHeader(it->first) <<
':' << escapeHeader(it->second,
false) <<
"\n";
367 stream_r.write( _body.data(), _body.size() );
386 static std::string _val(
"ACK" );
392 static std::string _val(
"ERROR" );
398 static std::string _val(
"_ENOMETHOD" );
404 static std::string _val(
"content-length");
413 : _pimpl( new
Impl( command_r ) )
421 : _pimpl( new
Impl( command_r,
std::move(body_r) ) )
425 : _pimpl( new
Impl( command_r, contents_r ) )
429 : _pimpl( new
Impl( command_r,
std::move(body_r), contents_r ) )
433 : _pimpl( new
Impl( stream_r ) )
Impl(const std::string &command_r, ByteArray &&body_r)
std::ostream & writeTo(std::ostream &stream_r) const
Write frame to stream.
PluginFrame()
Default ctor (empty frame)
void addRawHeader(const ByteArray &header)
static shared_ptr< Impl > nullimpl()
Offer default Impl.
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
std::pair< HeaderListIterator, HeaderListIterator > constKeyRange
Command frame for communication with PluginScript.
void setCommand(const std::string &command_r)
Set the frame command.
const std::string & command() const
Return the frame command.
const ByteArray & body() const
Return the frame body.
std::multimap< std::string, std::string > HeaderList
The header list.
void clearHeader(const std::string &key_r)
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
const std::string & getHeaderNT(const std::string &key_r, const std::string &default_r=std::string()) const
Not throwing version returing one of the matching header values or default_r string.
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
bool empty() const
Whether this is an empty frame.
Impl(const std::string &command_r)
ByteArray & bodyRef()
Return a reference to the frame body.
Impl(const std::string &command_r, ByteArray &&body_r, HeaderInitializerList contents_r)
void addHeader(const std::string &key_r, const std::string &value_r=std::string())
Add header for key_r leaving already existing headers for key_r unchanged.
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
static const std::string & errorCommand()
"ERROR" command.
void setCommand(const std::string &command_r)
void addRawHeader(const std::string_view data)
const std::string & getHeader(const std::string &key_r, const std::string &default_r) const
std::ostream & writeTo(std::ostream &stream_r) const
std::pair< HeaderList::iterator, HeaderList::iterator > KeyRange
void setBody(ByteArray &&body_r)
static const std::string & enomethodCommand()
"_ENOMETHOD" command.
static std::string unescapeHeader(std::string_view val)
void addHeader(HeaderInitializerList contents_r)
const ByteArray & body() const
Base class for PluginFrame Exception.
Impl(const std::string &command_r, HeaderInitializerList contents_r)
static const std::string & ackCommand()
"ACK" command.
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
std::string numstring(char n, int w=0)
void setHeader(const std::string &key_r, const std::string &value_r)
HeaderList & headerList()
void setBody(const std::string &body_r)
Set the frame body.
Impl * clone() const
clone for RWCOW_pointer
HeaderList::value_type mkHeaderPair(const std::string &key_r, const std::string &value_r)
const std::string & getHeaderNT(const std::string &key_r, const std::string &default_r) const
void addHeader(const std::string &key_r, const std::string &value_r)
const std::initializer_list< std::pair< std::string, std::string > > & HeaderInitializerList
const std::string & getHeader(const std::string &key_r) const
std::string receiveUpTo(std::istream &str, const char delim_r, bool returnDelim_r)
Return stream content up to the next ocurrence of delim_r or EOF delim_r, if found, is always read from the stream.
HeaderList::const_iterator HeaderListIterator
Header list iterator.
void setHeader(const std::string &key_r, const std::string &value_r=std::string())
Set header for key_r removing all other occurrences of key_r.
HeaderList & headerList()
Modifyalble header list for internal use only.
const std::string & getHeader(const std::string &key_r) const
Return header value for key_r.
const HeaderList & headerList() const
std::ostream & operator<<(std::ostream &str, const PluginFrame::Impl &obj)
PluginFrame implementation.
Easy-to use interface to the ZYPP dependency resolver.
void clearHeader(const std::string &key_r)
Remove all headers for key_r.
static std::string escapeHeader(std::string_view val, bool escapeColon=true)
const std::string & command() const
static const std::string & contentLengthHeader()
"content-lenght" header name
zypp::ByteArray ByteArray