| 1 | #ifndef CPR_REDIRECT_H |
| 2 | #define CPR_REDIRECT_H |
| 3 | |
| 4 | #include <cstdint> |
| 5 | |
| 6 | namespace cpr { |
| 7 | enum class PostRedirectFlags : uint8_t { |
| 8 | /** |
| 9 | * Respect RFC 7231 (section 6.4.2 to 6.4.4). |
| 10 | * Same as CURL_REDIR_POST_301 (https://curl.se/libcurl/c/CURLOPT_POSTREDIR.html). |
| 11 | **/ |
| 12 | POST_301 = 0x1 << 0, |
| 13 | /** |
| 14 | * Maintain the request method after a 302 redirect. |
| 15 | * Same as CURL_REDIR_POST_302 (https://curl.se/libcurl/c/CURLOPT_POSTREDIR.html). |
| 16 | **/ |
| 17 | POST_302 = 0x1 << 1, |
| 18 | /** |
| 19 | * Maintain the request method after a 303 redirect. |
| 20 | * Same as CURL_REDIR_POST_303 (https://curl.se/libcurl/c/CURLOPT_POSTREDIR.html). |
| 21 | **/ |
| 22 | POST_303 = 0x1 << 2, |
| 23 | /** |
| 24 | * Default value. |
| 25 | * Convenience option to enable all flags. |
| 26 | * Same as CURL_REDIR_POST_ALL (https://curl.se/libcurl/c/CURLOPT_POSTREDIR.html). |
| 27 | **/ |
| 28 | POST_ALL = POST_301 | POST_302 | POST_303, |
| 29 | /** |
| 30 | * * Convenience option to disable all flags. |
| 31 | **/ |
| 32 | NONE = 0x0 |
| 33 | }; |
| 34 | |
| 35 | PostRedirectFlags operator|(PostRedirectFlags lhs, PostRedirectFlags rhs); |
| 36 | PostRedirectFlags operator&(PostRedirectFlags lhs, PostRedirectFlags rhs); |
| 37 | PostRedirectFlags operator^(PostRedirectFlags lhs, PostRedirectFlags rhs); |
| 38 | PostRedirectFlags operator~(PostRedirectFlags flag); |
| 39 | PostRedirectFlags& operator|=(PostRedirectFlags& lhs, PostRedirectFlags rhs); |
| 40 | PostRedirectFlags& operator&=(PostRedirectFlags& lhs, PostRedirectFlags rhs); |
| 41 | PostRedirectFlags& operator^=(PostRedirectFlags& lhs, PostRedirectFlags rhs); |
| 42 | bool any(PostRedirectFlags flag); |
| 43 | |
| 44 | class Redirect { |
| 45 | public: |
| 46 | /** |
| 47 | * The maximum number of redirects to follow. |
| 48 | * 0: Refuse any redirects. |
| 49 | * -1: Infinite number of redirects. |
| 50 | * Default: 50 |
| 51 | * https://curl.se/libcurl/c/CURLOPT_MAXREDIRS.html |
| 52 | **/ |
| 53 | // NOLINTNEXTLINE (google-runtime-int) |
| 54 | long maximum{50L}; |
| 55 | /** |
| 56 | * Follow 3xx redirects. |
| 57 | * Default: true |
| 58 | * https://curl.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html |
| 59 | **/ |
| 60 | bool follow{true}; |
| 61 | /** |
| 62 | * Continue to send authentication (user+password) credentials when following locations, even when hostname changed. |
| 63 | * Default: false |
| 64 | * https://curl.se/libcurl/c/CURLOPT_UNRESTRICTED_AUTH.html |
| 65 | **/ |
| 66 | bool cont_send_cred{false}; |
| 67 | /** |
| 68 | * Flags to control how to act after a redirect for a post request. |
| 69 | * Default: POST_ALL |
| 70 | **/ |
| 71 | PostRedirectFlags post_flags{PostRedirectFlags::POST_ALL}; |
| 72 | |
| 73 | Redirect() = default; |
| 74 | // NOLINTNEXTLINE (google-runtime-int) |
| 75 | Redirect(long p_maximum, bool p_follow, bool p_cont_send_cred, PostRedirectFlags p_post_flags) : maximum(p_maximum), follow(p_follow), cont_send_cred(p_cont_send_cred), post_flags(p_post_flags){}; |
| 76 | // NOLINTNEXTLINE (google-runtime-int) |
| 77 | explicit Redirect(long p_maximum) : maximum(p_maximum){}; |
| 78 | explicit Redirect(bool p_follow) : follow(p_follow){}; |
| 79 | Redirect(bool p_follow, bool p_cont_send_cred) : follow(p_follow), cont_send_cred(p_cont_send_cred){}; |
| 80 | explicit Redirect(PostRedirectFlags p_post_flags) : post_flags(p_post_flags){}; |
| 81 | }; |
| 82 | } // namespace cpr |
| 83 | |
| 84 | #endif |
| 85 | |