From e88615226b133dc32e31c95accf28206035a0563 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 13 Aug 2022 20:00:15 +0200 Subject: [PATCH] add Http::allow_redirects(bool allow_redirects) method --- winixd/utils/http.cpp | 9 ++++++++- winixd/utils/http.h | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/winixd/utils/http.cpp b/winixd/utils/http.cpp index 3faf641..1be5b39 100644 --- a/winixd/utils/http.cpp +++ b/winixd/utils/http.cpp @@ -71,6 +71,7 @@ Http & Http::begin() output_content_type = nullptr; debug_mode = 0; debug_info = nullptr; + follow_location = true; return *this; } @@ -407,6 +408,12 @@ void Http::add_bearer_token() } +void Http::allow_redirects(bool allow_redirects) +{ + this->follow_location = allow_redirects; +} + + void Http::initialize_curl_if_needed() @@ -488,7 +495,7 @@ bool Http::fetch_internal(const char * url, const std::string * in, pt::TextStre curl_easy_setopt(curl, CURLOPT_TIMEOUT, conn_timeout); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, conn_timeout); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, (follow_location) ? 1 : 0); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); diff --git a/winixd/utils/http.h b/winixd/utils/http.h index 0740939..a9c3dbd 100644 --- a/winixd/utils/http.h +++ b/winixd/utils/http.h @@ -51,6 +51,8 @@ class Http : public WinixBase public: static const long HTTP_STATUS_200_OK = 200; + static const long HTTP_STATUS_301_Moved_Permanently = 301; + static const long HTTP_STATUS_302_Found = 302; Http(); @@ -143,6 +145,12 @@ public: Http & add_bearer_token(const wchar_t * token); Http & add_bearer_token(const std::wstring & token); + /* + * if allow_redirects is true then we follow any Location http headers + * default: true + */ + void allow_redirects(bool allow_redirects); + bool get(const wchar_t * url, std::wstring & out, bool clear_str = true); bool get(const std::wstring & url, std::wstring & out, bool clear_str = true); bool get(const pt::WTextStream & url, std::wstring & out, bool clear_str = true); @@ -180,6 +188,7 @@ private: std::wstring * output_content_type; long debug_mode; pt::Space * debug_info; + bool follow_location; std::wstring temp_header; std::string temp_header_ascii;