diff --git a/module/Library/CURL.cpp b/module/Library/CURL.cpp index c9282ac0..91af6992 100644 --- a/module/Library/CURL.cpp +++ b/module/Library/CURL.cpp @@ -16,6 +16,7 @@ SQMOD_DECL_TYPENAME(SqCpResponse, _SC("SqCprResponse")) SQMOD_DECL_TYPENAME(SqCpParameters, _SC("SqCprParameters")) SQMOD_DECL_TYPENAME(SqCpPayload, _SC("SqCprPayload")) SQMOD_DECL_TYPENAME(SqCpProxies, _SC("SqCprProxies")) +SQMOD_DECL_TYPENAME(SqCpRedirect, _SC("SqCprRedirect")) SQMOD_DECL_TYPENAME(SqCpSession, _SC("SqCprSession")) /* ------------------------------------------------------------------------------------------------ @@ -404,10 +405,20 @@ static const EnumElement g_StatusCodes[] = { {_SC("MISC_CODE_OFFSET"), cpr::status::MISC_CODE_OFFSET} }; +// ------------------------------------------------------------------------------------------------ +static const EnumElement g_PostRedirectFlags[] = { + {_SC("Post301"), static_cast< SQInteger >(cpr::PostRedirectFlags::POST_301)}, + {_SC("Post302"), static_cast< SQInteger >(cpr::PostRedirectFlags::POST_302)}, + {_SC("Post303"), static_cast< SQInteger >(cpr::PostRedirectFlags::POST_303)}, + {_SC("Postall"), static_cast< SQInteger >(cpr::PostRedirectFlags::POST_ALL)}, + {_SC("None"), static_cast< SQInteger >(cpr::PostRedirectFlags::NONE)}, +}; + // ------------------------------------------------------------------------------------------------ static const EnumElements g_EnumList[] = { - {_SC("SqCprErrorCode"), g_ErrorCodes}, - {_SC("SqCprStatusCode"), g_StatusCodes} + {_SC("SqCprErrorCode"), g_ErrorCodes}, + {_SC("SqCprStatusCode"), g_StatusCodes}, + {_SC("SqCprPostRedirectFlags"), g_PostRedirectFlags} }; // ================================================================================================ @@ -605,6 +616,26 @@ void Register_CURL(HSQUIRRELVM vm) .CbFunc(_SC("While"), &CpProxies::While) ); + // -------------------------------------------------------------------------------------------- + cpns.Bind(_SC("Redirect"), + Class< CpRedirect >(vm, SqCpRedirect::Str) + // Constructors + .Ctor() + .Ctor< SQInteger >() + .Ctor< SQInteger, bool >() + .Ctor< SQInteger, bool, SQInteger >() + // Meta-methods + .SquirrelFunc(_SC("_typename"), &SqCpRedirect::Fn) + // Properties + .Prop(_SC("Maximum"), &CpRedirect::GetMaximum, &CpRedirect::SetMaximum) + .Prop(_SC("Follow"), &CpRedirect::GetFollow, &CpRedirect::SetFollow) + .Prop(_SC("Flags"), &CpRedirect::GetFlags, &CpRedirect::SetFlags) + // Member Methods + .Func(_SC("SetMaximum"), &CpRedirect::ApplyMaximum) + .Func(_SC("SetFollow"), &CpRedirect::ApplyFollow) + .Func(_SC("SetFlags"), &CpRedirect::ApplyFlags) + ); + // -------------------------------------------------------------------------------------------- cpns.Bind(_SC("Session"), Class< CpSession, NoCopy< CpSession > >(vm, SqCpSession::Str) @@ -631,7 +662,6 @@ void Register_CURL(HSQUIRRELVM vm) .Func(_SC("YieldProxies"), &CpSession::YieldProxies) .FmtFunc(_SC("SetNTLM"), &CpSession::SetNTLM_) .Func(_SC("SetRedirect"), &CpSession::SetRedirect_) - .Func(_SC("SetMaxRedirects"), &CpSession::SetMaxRedirects_) .Func(_SC("SetCookies"), &CpSession::SetCookies_) .FmtFunc(_SC("SetBody"), &CpSession::SetBody_) .Func(_SC("SetLowSpeed"), &CpSession::SetLowSpeed_) diff --git a/module/Library/CURL.hpp b/module/Library/CURL.hpp index 2c507a86..7451deb6 100644 --- a/module/Library/CURL.hpp +++ b/module/Library/CURL.hpp @@ -888,7 +888,7 @@ struct CpResponse : public cpr::Response */ void SetStatusCode(SQInteger value) { - cpr::Response::status_code = value; + cpr::Response::status_code = static_cast< long >(value); } /* -------------------------------------------------------------------------------------------- @@ -1604,6 +1604,152 @@ struct CpProxies : public cpr::Proxies } }; +/* ------------------------------------------------------------------------------------------------ + * Wrapper for cpr::Redirect that can be bound to the script engine. +*/ +struct CpRedirect : public cpr::Redirect +{ + using cpr::Redirect::Redirect; + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + CpRedirect() = default; + + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + explicit CpRedirect(SQInteger maximum) + : cpr::Redirect(static_cast< long >(maximum), true, cpr::PostRedirectFlags::POST_ALL) + { + } + + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + CpRedirect(SQInteger maximum, bool follow) + : cpr::Redirect(static_cast< long >(maximum), follow, cpr::PostRedirectFlags::POST_ALL) + { + } + + /* -------------------------------------------------------------------------------------------- + * Explicit constructor. + */ + CpRedirect(SQInteger maximum, bool follow, SQInteger post_flags) + : cpr::Redirect(static_cast< long >(maximum), follow, static_cast< cpr::PostRedirectFlags >(post_flags)) + { + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. + */ + explicit CpRedirect(const cpr::Redirect & e) : cpr::Redirect(e) { } + + /* -------------------------------------------------------------------------------------------- + * Move constructor. + */ + explicit CpRedirect(cpr::Redirect && e) : cpr::Redirect(e) { } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. + */ + CpRedirect(const CpRedirect &) = default; + + /* -------------------------------------------------------------------------------------------- + * Move constructor. + */ + CpRedirect(CpRedirect &&) noexcept = default; + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~CpRedirect() = default; + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. + */ + CpRedirect & operator = (const CpRedirect &) = default; + + /* -------------------------------------------------------------------------------------------- + * Move assignment operator. + */ + CpRedirect & operator = (CpRedirect &&) noexcept = default; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the maximum number of redirects to follow. 0: Refuse any redirects. -1: Infinite number of redirects. + */ + SQMOD_NODISCARD SQInteger GetMaximum() const noexcept + { + return static_cast< SQInteger >(cpr::Redirect::maximum); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the maximum number of redirects to follow. 0: Refuse any redirects. -1: Infinite number of redirects. + */ + void SetMaximum(SQInteger value) noexcept + { + cpr::Redirect::maximum = static_cast< long >(value); + } + + /* -------------------------------------------------------------------------------------------- + * Modify the maximum number of redirects to follow. 0: Refuse any redirects. -1: Infinite number of redirects. + */ + CpRedirect & ApplyMaximum(SQInteger value) noexcept + { + SetMaximum(value); + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve whether to follow 3xx redirects. + */ + SQMOD_NODISCARD bool GetFollow() const noexcept + { + return cpr::Redirect::follow; + } + + /* -------------------------------------------------------------------------------------------- + * Modify whether to follow 3xx redirects. + */ + void SetFollow(bool value) noexcept + { + cpr::Redirect::follow = value; + } + + /* -------------------------------------------------------------------------------------------- + * Modify whether to follow 3xx redirects. + */ + CpRedirect & ApplyFollow(bool value) noexcept + { + SetMaximum(value); + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the flags to control how to act after a redirect for a post request. + */ + SQMOD_NODISCARD bool GetFlags() const noexcept + { + return cpr::Redirect::follow; + } + + /* -------------------------------------------------------------------------------------------- + * Modify the flags to control how to act after a redirect for a post request. + */ + void SetFlags(bool value) noexcept + { + cpr::Redirect::follow = value; + } + + /* -------------------------------------------------------------------------------------------- + * Modify the flags to control how to act after a redirect for a post request. + */ + CpRedirect & ApplyFlags(bool value) noexcept + { + SetMaximum(value); + return *this; + } +}; + /* ------------------------------------------------------------------------------------------------ * Wrapper for cpr::Session that can be bound to the script engine. */ @@ -1620,7 +1766,7 @@ struct CpSession : public cpr::Session /* -------------------------------------------------------------------------------------------- * URL constructor. */ - CpSession(StackStrF & url) + explicit CpSession(StackStrF & url) : cpr::Session() { cpr::Session::SetUrl(cpr::Url(url.mPtr, url.GetSize())); @@ -1654,7 +1800,7 @@ struct CpSession : public cpr::Session /* -------------------------------------------------------------------------------------------- * Throw exception if the session is locked. */ - void LockCheck() + void LockCheck() const { if (mPending) { @@ -1827,23 +1973,13 @@ struct CpSession : public cpr::Session /* -------------------------------------------------------------------------------------------- * Modify redirect option. */ - CpSession & SetRedirect_(bool redirect) + CpSession & SetRedirect_(CpRedirect & redirect) { LockCheck(); cpr::Session::SetRedirect(redirect); return *this; // Allow chaining } - /* -------------------------------------------------------------------------------------------- - * Modify max-redirects option. - */ - CpSession & SetMaxRedirects_(SQInteger max_redirects) - { - LockCheck(); - cpr::Session::SetMaxRedirects(cpr::MaxRedirects(static_cast< int32_t >(max_redirects))); - return *this; // Allow chaining - } - /* -------------------------------------------------------------------------------------------- * Modify cookies option. */