libzypp: zypp-core/rpc/PluginFrame.h Source File

libzypp 17.38.14

PluginFrame.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_CORE_PLUGINFRAME_H
13#define ZYPP_CORE_PLUGINFRAME_H
14
15#include <iosfwd>
16#include <string>
17#include <map>
18
19#include <zypp-core/base/PtrTypes.h>
20
21#include <zypp-core/ByteArray.h>
22#include <zypp-core/rpc/PluginFrameException.h>
23
25namespace zypp
26{
27
41 class PluginFrame
42 {
43 friend std::ostream & operator<<( std::ostream & str, const PluginFrame & obj );
44 friend bool operator==( const PluginFrame & lhs, const PluginFrame & rhs );
45
46 using HeaderInitializerList = const std::initializer_list<std::pair<std::string, std::string>> &;
47
48 public:
50 static const std::string & ackCommand();
52 static const std::string & errorCommand();
54 static const std::string & enomethodCommand();
56 static const std::string &contentLengthHeader();
57
58 public:
60 using Exception = PluginFrameException;
61
63 PluginFrame();
64
68 PluginFrame( const std::string & command_r );
69
73 PluginFrame(const std::string & command_r, std::string body_r );
74
78 PluginFrame(const std::string & command_r, ByteArray body_r );
79
83 PluginFrame( const std::string & command_r, HeaderInitializerList contents_r );
84
88 PluginFrame(const std::string & command_r, ByteArray body_r, HeaderInitializerList contents_r );
89
94 PluginFrame( std::istream & stream_r );
95
96 public:
98 bool empty() const;
99
101 explicit operator bool() const
102 { return !empty(); }
103
104 public:
106 const std::string & command() const;
107
111 void setCommand( const std::string & command_r );
112
114 bool isAckCommand() const
115 { return command() == ackCommand(); }
116
118 bool isErrorCommand() const
119 {return command() == errorCommand(); }
120
122 bool isEnomethodCommand() const
123 {return command() == enomethodCommand(); }
124
126 const ByteArray & body() const;
127
136 ByteArray & bodyRef();
137
139 void setBody( const std::string & body_r );
140
142 void setBody( const ByteArray & body_r );
143
145 void setBody( ByteArray && body_r );
146
147 public:
149 using HeaderList = std::multimap<std::string, std::string>;
150
152 using HeaderListIterator = HeaderList::const_iterator;
153
154 private:
156 HeaderList & headerList();
157
158 public:
160 const HeaderList & headerList() const;
161
163 bool headerEmpty() const
164 { return headerList().empty(); }
165
167 unsigned headerSize() const
168 { return headerList().size(); }
169
171 HeaderListIterator headerBegin() const
172 { return headerList().begin(); }
173
175 HeaderListIterator headerEnd() const
176 { return headerList().end(); }
177
179 void headerClear()
180 { headerList().clear(); }
181
182
184 bool hasKey( const std::string & key_r ) const
185 { return ! keyEmpty( key_r ); }
186
188 bool keyEmpty( const std::string & key_r ) const
189 { return headerList().find( key_r ) == headerEnd(); }
190
192 bool keySize( const std::string & key_r ) const
193 { return headerList().count( key_r ); }
194
196 HeaderListIterator keyBegin( const std::string & key_r ) const
197 { return headerList().lower_bound( key_r ); }
198
200 HeaderListIterator keyEnd( const std::string & key_r ) const
201 { return headerList().upper_bound( key_r ); }
202
203
208 const std::string & getHeader( const std::string & key_r ) const;
209
213 std::string getHeader( const std::string & key_r, const std::string & default_r ) const;
214
216 std::string getHeaderNT( const std::string & key_r, const std::string & default_r = std::string() ) const;
217
222 void setHeader( const std::string & key_r, const std::string & value_r = std::string() );
223
228 void setHeader( HeaderInitializerList contents_r )
229 { headerList().clear(); addHeader( contents_r ); }
230
235 void addHeader( const std::string & key_r, const std::string & value_r = std::string() );
237 void addHeader( HeaderInitializerList contents_r );
238
243 void addRawHeader ( const ByteArray &header );
244
246 void clearHeader( const std::string & key_r );
247
248 public:
252 std::ostream & writeTo( std::ostream & stream_r ) const;
253
255 static std::ostream & writeTo( std::ostream & stream_r, const PluginFrame & frame_r )
256 { return frame_r.writeTo( stream_r ); }
257
261 std::istream & readFrom( std::istream & stream_r )
262 { *this = PluginFrame( stream_r ); return stream_r; }
263
265 static std::istream & readFrom( std::istream & stream_r, PluginFrame & frame_r )
266 { frame_r = PluginFrame( stream_r ); return stream_r; }
267
268 public:
270 struct Impl;
271 private:
273 RWCOW_pointer<Impl> _pimpl;
274 };
275
277 std::ostream & operator<<( std::ostream & str, const PluginFrame & obj );
278
280 inline std::ostream & dumpOn( std::ostream & str, const PluginFrame & obj )
281 { if ( str ) try { PluginFrame::writeTo( str, obj ); } catch(...){}; return str; }
282
284 inline std::istream & operator>>( std::istream & str, PluginFrame & obj )
285 { return PluginFrame::readFrom( str, obj ); }
286
288 bool operator==( const PluginFrame & lhs, const PluginFrame & rhs );
289
291 inline bool operator!=( const PluginFrame & lhs, const PluginFrame & rhs )
292 { return( ! operator==( lhs, rhs ) ); }
293
295} // namespace zypp
297#endif // ZYPP_CORE_PLUGINFRAME_H
ByteArray.h
PluginFrameException.h
PtrTypes.h
Impl
bool
zypp::ByteArray
Definition ByteArray.h:20
zypp::PluginFrameException
Base class for PluginFrame Exception.
Definition PluginFrameException.h:28
zypp::PluginFrame
Command frame for communication with PluginScript.
Definition PluginFrame.h:42
zypp::PluginFrame::ackCommand
static const std::string & ackCommand()
"ACK" command.
Definition PluginFrame.cc:384
zypp::PluginFrame::bodyRef
ByteArray & bodyRef()
Return a reference to the frame body.
Definition PluginFrame.cc:448
zypp::PluginFrame::setCommand
void setCommand(const std::string &command_r)
Set the frame command.
Definition PluginFrame.cc:442
zypp::PluginFrame::HeaderListIterator
HeaderList::const_iterator HeaderListIterator
Header list iterator.
Definition PluginFrame.h:152
zypp::PluginFrame::operator==
friend bool operator==(const PluginFrame &lhs, const PluginFrame &rhs)
relates: PluginFrame Comparison based on content.
Definition PluginFrame.cc:500
zypp::PluginFrame::setBody
void setBody(const std::string &body_r)
Set the frame body.
Definition PluginFrame.cc:451
zypp::PluginFrame::HeaderInitializerList
const std::initializer_list< std::pair< std::string, std::string > > & HeaderInitializerList
Definition PluginFrame.h:46
zypp::PluginFrame::keyBegin
HeaderListIterator keyBegin(const std::string &key_r) const
Return iterator pointing to the 1st header for key_r (or keyEnd(key_r)).
Definition PluginFrame.h:196
zypp::PluginFrame::PluginFrame
PluginFrame()
Default ctor (empty frame).
Definition PluginFrame.cc:408
zypp::PluginFrame::Exception
PluginFrameException Exception
Default exception type.
Definition PluginFrame.h:60
zypp::PluginFrame::headerClear
void headerClear()
Clear the list of headers.
Definition PluginFrame.h:179
zypp::PluginFrame::setHeader
void setHeader(HeaderInitializerList contents_r)
Set a new header list.
Definition PluginFrame.h:228
zypp::PluginFrame::headerEmpty
bool headerEmpty() const
Whether header list is empty.
Definition PluginFrame.h:163
zypp::PluginFrame::clearHeader
void clearHeader(const std::string &key_r)
Remove all headers for key_r.
Definition PluginFrame.cc:492
zypp::PluginFrame::keyEmpty
bool keyEmpty(const std::string &key_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition PluginFrame.h:188
zypp::PluginFrame::headerSize
unsigned headerSize() const
Return size of the header list.
Definition PluginFrame.h:167
zypp::PluginFrame::contentLengthHeader
static const std::string & contentLengthHeader()
"content-lenght" header name
Definition PluginFrame.cc:402
zypp::PluginFrame::isErrorCommand
bool isErrorCommand() const
Convenience to identify an ERROR command.
Definition PluginFrame.h:118
zypp::PluginFrame::readFrom
std::istream & readFrom(std::istream &stream_r)
Read frame from stream.
Definition PluginFrame.h:261
zypp::PluginFrame::empty
bool empty() const
Whether this is an empty frame.
Definition PluginFrame.cc:436
zypp::PluginFrame::body
const ByteArray & body() const
Return the frame body.
Definition PluginFrame.cc:445
zypp::PluginFrame::enomethodCommand
static const std::string & enomethodCommand()
"_ENOMETHOD" command.
Definition PluginFrame.cc:396
zypp::PluginFrame::keySize
bool keySize(const std::string &key_r) const
Return number of header entries for key_r.
Definition PluginFrame.h:192
zypp::PluginFrame::operator<<
friend std::ostream & operator<<(std::ostream &str, const PluginFrame &obj)
relates: PluginFrame Stream output for logging
Definition PluginFrame.cc:497
zypp::PluginFrame::command
const std::string & command() const
Return the frame command.
Definition PluginFrame.cc:439
zypp::PluginFrame::addHeader
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.
Definition PluginFrame.cc:481
zypp::PluginFrame::_pimpl
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition PluginFrame.h:273
zypp::PluginFrame::isEnomethodCommand
bool isEnomethodCommand() const
Convenience to identify an _ENOMETHOD command.
Definition PluginFrame.h:122
zypp::PluginFrame::keyEnd
HeaderListIterator keyEnd(const std::string &key_r) const
Return iterator pointing behind the last header for key_r.
Definition PluginFrame.h:200
zypp::PluginFrame::errorCommand
static const std::string & errorCommand()
"ERROR" command.
Definition PluginFrame.cc:390
zypp::PluginFrame::readFrom
static std::istream & readFrom(std::istream &stream_r, PluginFrame &frame_r)
Definition PluginFrame.h:265
zypp::PluginFrame::writeTo
static std::ostream & writeTo(std::ostream &stream_r, const PluginFrame &frame_r)
Definition PluginFrame.h:255
zypp::PluginFrame::headerEnd
HeaderListIterator headerEnd() const
Return iterator pointing behind the last header.
Definition PluginFrame.h:175
zypp::PluginFrame::getHeader
const std::string & getHeader(const std::string &key_r) const
Return header value for key_r.
Definition PluginFrame.cc:469
zypp::PluginFrame::HeaderList
std::multimap< std::string, std::string > HeaderList
The header list.
Definition PluginFrame.h:149
zypp::PluginFrame::headerList
HeaderList & headerList()
Modifyalble header list for internal use only.
Definition PluginFrame.cc:463
zypp::PluginFrame::addRawHeader
void addRawHeader(const ByteArray &header)
Definition PluginFrame.cc:487
zypp::PluginFrame::setHeader
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.
Definition PluginFrame.cc:478
zypp::PluginFrame::getHeaderNT
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.
Definition PluginFrame.cc:475
zypp::PluginFrame::isAckCommand
bool isAckCommand() const
Convenience to identify an ACK command.
Definition PluginFrame.h:114
zypp::PluginFrame::headerBegin
HeaderListIterator headerBegin() const
Return iterator pointing to the 1st header (or headerEnd).
Definition PluginFrame.h:171
zypp::PluginFrame::writeTo
std::ostream & writeTo(std::ostream &stream_r) const
Write frame to stream.
Definition PluginFrame.cc:460
zypp::PluginFrame::hasKey
bool hasKey(const std::string &key_r) const
Whether the header list contains at least one entry for key_r.
Definition PluginFrame.h:184
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::operator>>
std::istream & operator>>(std::istream &str, PluginFrame &obj)
relates: PluginFrame Construct from stream.
Definition PluginFrame.h:284
zypp::operator!=
bool operator!=(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition Capability.h:313
zypp::operator==
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition Capability.h:309
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
relates: Capability Detailed stream output
Definition Capability.cc:589
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::RWCOW_pointer
RW_pointer supporting 'copy on write' functionality.
Definition PtrTypes.h:469