12#include <zypp-core/Url.h>
21 switch ( field.field_val_case () ) {
22 case zypp::proto::DataField::FieldValCase::kBoolVal:
25 case zypp::proto::DataField::FieldValCase::kDoubleVal:
26 v = field.double_val();
28 case zypp::proto::DataField::FieldValCase::kIntVal:
31 case zypp::proto::DataField::FieldValCase::kLongVal:
34 case zypp::proto::DataField::FieldValCase::kStrVal:
37 case zypp::proto::DataField::FieldValCase::FIELD_VAL_NOT_SET:
38 ZYPP_THROW( std::logic_error(
"Unexpected DataField type"));
47 field.set_str_val( val.
asString () );
48 else if ( val.
isInt() )
49 field.set_int_val( val.
asInt() );
51 field.set_long_val( val.
asInt64() );
53 field.set_double_val( val.
asDouble() );
55 field.set_bool_val( val.
asBool() );
57 ZYPP_THROW( std::logic_error(
"Unexpected FieldVal type"));
62 const auto c = msg.
code();
63 const auto validCode = ( c >= ProvideMessage::Code::FirstInformalCode && c <= ProvideMessage::Code::LastInformalCode )
64 || ( c >= ProvideMessage::Code::FirstSuccessCode && c <= ProvideMessage::Code::LastSuccessCode )
65 || ( c >= ProvideMessage::Code::FirstRedirCode && c <= ProvideMessage::Code::LastRedirCode )
66 || ( c >= ProvideMessage::Code::FirstClientErrCode && c <= ProvideMessage::Code::LastClientErrCode )
67 || ( c >= ProvideMessage::Code::FirstSrvErrCode && c <= ProvideMessage::Code::LastSrvErrCode )
68 || ( c >= ProvideMessage::Code::FirstControllerCode && c <= ProvideMessage::Code::LastControllerCode)
69 || ( c >= ProvideMessage::Code::FirstWorkerCode && c <= ProvideMessage::Code::LastWorkerCode );
71 return zyppng::expected<void>::error(
ZYPP_EXCPT_PTR ( InvalidMessageReceivedException(
"Invalid code in ProvideMessage")) );
74 #define DEF_REQ_FIELD( fname ) bool has_##fname = false
76 #define REQ_FIELD_CHECK( msgtype, fname, ftype ) \
77 if ( name == #fname ) { \
78 if ( !std::holds_alternative<ftype>(val.asVariant()) ) { \
79 error = ZYPP_EXCPT_PTR( InvalidMessageReceivedException( zypp::str::Str() << "Parse error " << #msgtype << ", Field " << #fname << " has invalid type" ) ); \
85 #define OR_REQ_FIELD_CHECK( msgtype, fname, ftype ) else REQ_FIELD_CHECK( msgtype, fname, ftype )
87 #define OPT_FIELD_CHECK( msgtype, fname, ftype ) \
88 if ( name == #fname ) { \
89 if ( !std::holds_alternative<ftype>(val.asVariant() ) ) { \
90 error = ZYPP_EXCPT_PTR( InvalidMessageReceivedException( zypp::str::Str() << "Parse error " << #msgtype << ", Field " << #fname << " has invalid type" ) ); \
95 #define OR_OPT_FIELD_CHECK( msgtype, fname, ftype ) else OPT_FIELD_CHECK( msgtype, fname, ftype )
97 #define FAIL_IF_NOT_SEEN_REQ_FIELD( msgtype, fname ) \
99 return expected<void>::error( ZYPP_EXCPT_PTR( InvalidMessageReceivedException( zypp::str::Str() << #msgtype <<" message does not contain required " << #fname << " field" ) ) )
101 #define FAIL_IF_ERROR( ) \
102 if ( error ) return expected<void>::error( error )
104 const auto &validateErrorMsg = [](
const auto &msg ){
105 std::exception_ptr error;
115 return expected<void>::success();
120 case ProvideMessage::Code::ProvideStarted: {
121 std::exception_ptr error;
133 case ProvideMessage::Code::ProvideFinished: {
134 std::exception_ptr error;
147 case ProvideMessage::Code::AttachFinished: {
151 case ProvideMessage::Code::DetachFinished: {
155 case ProvideMessage::Code::AuthInfo: {
156 std::exception_ptr error;
173 case ProvideMessage::Code::MediaChanged:
176 case ProvideMessage::Code::Redirect: {
177 std::exception_ptr error;
187 case ProvideMessage::Code::Metalink: {
188 std::exception_ptr error;
198 case ProvideMessage::Code::BadRequest:
199 case ProvideMessage::Code::Unauthorized:
200 case ProvideMessage::Code::Forbidden:
201 case ProvideMessage::Code::PeerCertificateInvalid:
202 case ProvideMessage::Code::NotFound:
203 case ProvideMessage::Code::ExpectedSizeExceeded:
204 case ProvideMessage::Code::ConnectionFailed:
205 case ProvideMessage::Code::Timeout:
206 case ProvideMessage::Code::Cancelled:
207 case ProvideMessage::Code::InvalidChecksum:
208 case ProvideMessage::Code::MountFailed:
209 case ProvideMessage::Code::Jammed:
210 case ProvideMessage::Code::NoAuthData:
211 case ProvideMessage::Code::MediaChangeAbort:
212 case ProvideMessage::Code::MediaChangeSkip:
213 case ProvideMessage::Code::InternalError: {
214 const auto &e = validateErrorMsg(msg);
219 case ProvideMessage::Code::Provide: {
220 std::exception_ptr error;
235 case ProvideMessage::Code::Cancel:
239 case ProvideMessage::Code::Attach: {
240 std::exception_ptr error;
264 if ( ! ( ( has_verify_data == has_verify_type ) && ( has_verify_type == has_media_nr ) ) )
265 return expected<void>::error(
ZYPP_EXCPT_PTR ( InvalidMessageReceivedException(
"Error in Attach message, one of the following fields is not set or invalid: ( verify_type, verify_data, media_nr ). Either none or all need to be set. ")) );
269 case ProvideMessage::Code::Detach: {
270 std::exception_ptr error;
280 case ProvideMessage::Code::AuthDataRequest: {
281 std::exception_ptr error;
294 case ProvideMessage::Code::MediaChangeRequest: {
295 std::exception_ptr error;
314 if ( c >= ProvideMessage::Code::FirstClientErrCode && c <= ProvideMessage::Code::LastSrvErrCode ) {
315 const auto &e = validateErrorMsg(msg);
322 return expected<void>::success();
332 const auto &res = RpcMessageStream::parseMessageInto<zypp::proto::ProvideMessage>( message, *msg.
_impl );
336 ERR <<
"Invalid message for ID: " << msg.
_impl->request_id() << std::endl;;
337 return zyppng::expected<zyppng::ProvideMessage>::error( valid.error() );
340 return zyppng::expected<zyppng::ProvideMessage>::success( std::move(msg) );
342 ERR <<
"Failed to parse message" << std::endl;;
343 return zyppng::expected<zyppng::ProvideMessage>::error( res.error() );
349 *msg.
_impl = std::move(message);
352 ERR <<
"Invalid message for ID: " << msg.
_impl->request_id() << std::endl;;
353 return zyppng::expected<zyppng::ProvideMessage>::error( valid.error() );
356 return zyppng::expected<zyppng::ProvideMessage>::success( std::move(msg) );
362 msg.
setCode ( ProvideMessage::Code::ProvideStarted );
367 if ( stagingFilename )
376 msg.
setCode ( ProvideMessage::Code::ProvideFinished );
387 msg.
setCode ( ProvideMessage::Code::AttachFinished );
396 msg.
setCode ( ProvideMessage::Code::DetachFinished );
405 msg.
setCode ( ProvideMessage::Code::AuthInfo );
410 for (
auto i : extraValues ) {
419 msg.
setCode ( ProvideMessage::Code::MediaChanged );
428 msg.
setCode ( ProvideMessage::Code::Redirect );
438 msg.
setCode ( ProvideMessage::Code::Metalink );
440 for(
const auto &val : newUrls )
449 if ( code < Code::FirstClientErrCode || code > Code::LastSrvErrCode )
450 ZYPP_THROW(std::out_of_range(
"code must be between 400 and 599"));
461 msg.
setCode ( ProvideMessage::Code::Provide );
479 msg.
setCode ( ProvideMessage::Code::Cancel );
485 ProvideMessage ProvideMessage::createAttach(
const uint32_t reqId,
const zypp::Url &url,
const std::string attachId,
const std::string &label,
const std::optional<std::string> &verifyType,
const std::optional<std::string> &verifyData,
const std::optional<int32_t> &mediaNr )
488 msg.
setCode ( ProvideMessage::Code::Attach );
494 if ( verifyType.has_value() && verifyData.has_value() && mediaNr.has_value() ) {
499 if ( !( ( verifyType.has_value() == verifyData.has_value() ) && ( verifyData.has_value() == mediaNr.has_value() ) ) )
500 WAR <<
"Attach message requires verifyType, verifyData and mediaNr either set together or not set at all." << std::endl;
509 msg.
setCode ( ProvideMessage::Code::Detach );
519 msg.
setCode ( ProvideMessage::Code::AuthDataRequest );
522 if ( lastTriedUser.size() )
524 if ( lastAuthTimestamp )
533 msg.
setCode ( ProvideMessage::Code::MediaChangeRequest );
537 for (
const auto &device : devices )
547 return _impl->request_id();
552 _impl->set_request_id(
id );
557 return _impl->message_code();
562 _impl->set_message_code ( newCode );
567 std::vector<ProvideMessage::FieldVal>
values;
568 const auto &fields =
_impl->fields();
569 for (
const auto &field : fields ) {
570 if ( field.key() !=
str )
584 const auto &fields =
_impl->fields();
585 auto i = std::find_if( fields.rbegin(), fields.rend(), [&
str](
const auto &val ){ return val.key() == str; } );
586 if ( i == fields.rend() )
595 auto &fields =
_impl->fields();
596 for (
const auto &val : fields ) {
604 return value( std::string_view(
str), defaultVal );
614 auto &fields = *
_impl->mutable_fields();
615 auto i = std::find_if( fields.rbegin(), fields.rend(), [&name](
const auto &val ){ return val.key() == name; } );
616 if ( i == fields.rend() ) {
617 auto &newVal = *
_impl->add_fields();
618 newVal.set_key( name.data() );
631 auto &newVal = *
_impl->add_fields();
632 newVal.set_key( name.data() );
638 auto &fields =
_impl->fields();
639 for (
const auto &val : fields ) {
std::string asCompleteString() const
Returns a complete string representation of the Url object.
static ProvideMessage createProvideStarted(const uint32_t reqId, const zypp::Url &url, const std::optional< std::string > &localFilename={}, const std::optional< std::string > &stagingFilename={})
static ProvideMessage createAuthInfo(const uint32_t reqId, const std::string &user, const std::string &pw, int64_t timestamp, const std::map< std::string, std::string > &extraValues={})
zypp::RWCOW_pointer< zypp::proto::ProvideMessage > _impl
static ProvideMessage createRedirect(const uint32_t reqId, const zypp::Url &newUrl)
static ProvideMessage createMetalinkRedir(const uint32_t reqId, const std::vector< zypp::Url > &newUrls)
static ProvideMessage createCancel(const uint32_t reqId)
void forEachVal(const std::function< bool(const std::string &name, const FieldVal &val)> &cb) const
HeaderValueMap headers() const
static ProvideMessage createMediaChanged(const uint32_t reqId)
static ProvideMessage createProvideFinished(const uint32_t reqId, const std::string &localFilename, bool cacheHit)
static expected< ProvideMessage > create(const zyppng::RpcMessage &message)
std::vector< FieldVal > values(const std::string_view &str) const
static ProvideMessage createMediaChangeRequest(const uint32_t reqId, const std::string &label, int32_t mediaNr, const std::vector< std::string > &devices, const std::optional< std::string > &desc)
static ProvideMessage createAttachFinished(const uint32_t reqId)
zypp::proto::ProvideMessage & impl()
FieldVal value(const std::string_view &str, const FieldVal &defaultVal=FieldVal()) const
void setCode(const uint newCode)
void setValue(const std::string &name, const FieldVal &value)
static ProvideMessage createAuthDataRequest(const uint32_t reqId, const zypp::Url &effectiveUrl, const std::string &lastTriedUser="", const std::optional< int64_t > &lastAuthTimestamp={}, const std::map< std::string, std::string > &extraValues={})
static ProvideMessage createProvide(const uint32_t reqId, const zypp::Url &url, const std::optional< std::string > &filename={}, const std::optional< std::string > &deltaFile={}, const std::optional< int64_t > &expFilesize={}, bool checkExistOnly=false)
void setRequestId(const uint id)
static ProvideMessage createDetachFinished(const uint32_t reqId)
void addValue(const std::string &name, const FieldVal &value)
static ProvideMessage createDetach(const uint32_t reqId, const zypp::Url &attachUrl)
static ProvideMessage createErrorResponse(const uint32_t reqId, const uint code, const std::string &reason, bool transient=false)
static ProvideMessage createAttach(const uint32_t reqId, const zypp::Url &url, const std::string attachId, const std::string &label, const std::optional< std::string > &verifyType={}, const std::optional< std::string > &verifyData={}, const std::optional< int32_t > &mediaNr={})
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
constexpr std::string_view AttachId("attach_id")
constexpr std::string_view VerifyData("verify_data")
constexpr std::string_view VerifyType("verify_type")
constexpr std::string_view Label("label")
constexpr std::string_view MediaNr("media_nr")
constexpr std::string_view Url("url")
constexpr std::string_view LastUser("username")
constexpr std::string_view EffectiveUrl("effective_url")
constexpr std::string_view LastAuthTimestamp("last_auth_timestamp")
constexpr std::string_view Password("password")
constexpr std::string_view Username("username")
constexpr std::string_view AuthTimestamp("auth_timestamp")
constexpr std::string_view Url("url")
constexpr std::string_view Reason("reason")
constexpr std::string_view Transient("transient")
constexpr std::string_view LocalFilename("local_filename")
constexpr std::string_view CacheHit("cacheHit")
constexpr std::string_view Url("url")
constexpr std::string_view ExpectedFilesize("expected_filesize")
constexpr std::string_view DeltaFile("delta_file")
constexpr std::string_view CheckExistOnly("check_existance_only")
constexpr std::string_view Filename("filename")
constexpr std::string_view StagingFilename("staging_filename")
constexpr std::string_view Url("url")
constexpr std::string_view LocalFilename("local_filename")
constexpr std::string_view NewUrl("new_url")
static void fieldValToProto(const ProvideMessage::FieldVal &val, zypp::proto::DataField &field)
static expected< void > validateMessage(const ProvideMessage &msg)
static ProvideMessage::FieldVal fieldValFromProto(const zypp::proto::DataField &field)
#define FAIL_IF_NOT_SEEN_REQ_FIELD(msgtype, fname)
#define DEF_REQ_FIELD(fname)
#define OR_OPT_FIELD_CHECK(msgtype, fname, ftype)
#define REQ_FIELD_CHECK(msgtype, fname, ftype)
#define OR_REQ_FIELD_CHECK(msgtype, fname, ftype)
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.