diff options
author | Zack Galbreath <zack.galbreath@kitware.com> | 2017-05-03 14:50:04 (GMT) |
---|---|---|
committer | Zack Galbreath <zack.galbreath@kitware.com> | 2017-05-04 16:21:21 (GMT) |
commit | 1a7d00bd12f261428edb8fef09461bee8067ceee (patch) | |
tree | 7d998892f75ff11470ff6c884d4e168bdddd6c63 /Source | |
parent | 294cf948dc37799e1980685afbf1fa585ad5b0fb (diff) | |
download | CMake-1a7d00bd12f261428edb8fef09461bee8067ceee.zip CMake-1a7d00bd12f261428edb8fef09461bee8067ceee.tar.gz CMake-1a7d00bd12f261428edb8fef09461bee8067ceee.tar.bz2 |
ctest_submit: Add HTTPHEADER option
Allow CTest script writers to specify additional HTTP headers to be sent
to CDash during submission.
The motivating case for this feature is a corresponding change in CDash.
This will allow projects to refuse submissions from any site not bearing
a valid authentication token.
Diffstat (limited to 'Source')
-rw-r--r-- | Source/CTest/cmCTestCurl.cxx | 25 | ||||
-rw-r--r-- | Source/CTest/cmCTestCurl.h | 5 | ||||
-rw-r--r-- | Source/CTest/cmCTestSubmitCommand.cxx | 16 | ||||
-rw-r--r-- | Source/CTest/cmCTestSubmitCommand.h | 2 | ||||
-rw-r--r-- | Source/CTest/cmCTestSubmitHandler.cxx | 12 | ||||
-rw-r--r-- | Source/CTest/cmCTestSubmitHandler.h | 6 |
6 files changed, 64 insertions, 2 deletions
diff --git a/Source/CTest/cmCTestCurl.cxx b/Source/CTest/cmCTestCurl.cxx index 06b5e81..b80ea5a 100644 --- a/Source/CTest/cmCTestCurl.cxx +++ b/Source/CTest/cmCTestCurl.cxx @@ -143,9 +143,17 @@ bool cmCTestCurl::UploadFile(std::string const& local_file, ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION, curlWriteMemoryCallback); ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback); - // Be sure to set Content-Type to satisfy fussy modsecurity rules + // Set Content-Type to satisfy fussy modsecurity rules. struct curl_slist* headers = ::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml"); + // Add any additional headers that the user specified. + for (std::vector<std::string>::const_iterator h = this->HttpHeaders.begin(); + h != this->HttpHeaders.end(); ++h) { + cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, + " Add HTTP Header: \"" << *h << "\"" << std::endl, + this->Quiet); + headers = ::curl_slist_append(headers, h->c_str()); + } ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers); std::vector<char> responseData; std::vector<char> debugData; @@ -203,7 +211,22 @@ bool cmCTestCurl::HttpRequest(std::string const& url, ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData); ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1); + // Add headers if any were specified. + struct curl_slist* headers = CM_NULLPTR; + if (!this->HttpHeaders.empty()) { + for (std::vector<std::string>::const_iterator h = + this->HttpHeaders.begin(); + h != this->HttpHeaders.end(); ++h) { + cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, + " Add HTTP Header: \"" << *h << "\"" << std::endl, + this->Quiet); + headers = ::curl_slist_append(headers, h->c_str()); + } + } + + ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers); CURLcode res = ::curl_easy_perform(this->Curl); + ::curl_slist_free_all(headers); if (!responseData.empty()) { response = std::string(responseData.begin(), responseData.end()); diff --git a/Source/CTest/cmCTestCurl.h b/Source/CTest/cmCTestCurl.h index 67608cb..427a392 100644 --- a/Source/CTest/cmCTestCurl.h +++ b/Source/CTest/cmCTestCurl.h @@ -23,6 +23,10 @@ public: // currently only supports CURLOPT_SSL_VERIFYPEER_OFF // and CURLOPT_SSL_VERIFYHOST_OFF void SetCurlOptions(std::vector<std::string> const& args); + void SetHttpHeaders(std::vector<std::string> const& v) + { + this->HttpHeaders = v; + } void SetUseHttp10On() { this->UseHttp10 = true; } void SetTimeOutSeconds(int s) { this->TimeOutSeconds = s; } void SetQuiet(bool b) { this->Quiet = b; } @@ -35,6 +39,7 @@ protected: private: cmCTest* CTest; CURL* Curl; + std::vector<std::string> HttpHeaders; std::string HTTPProxyAuth; std::string HTTPProxy; curl_proxytype HTTPProxyType; diff --git a/Source/CTest/cmCTestSubmitCommand.cxx b/Source/CTest/cmCTestSubmitCommand.cxx index 5cf4ddc..409eb51 100644 --- a/Source/CTest/cmCTestSubmitCommand.cxx +++ b/Source/CTest/cmCTestSubmitCommand.cxx @@ -129,6 +129,12 @@ cmCTestGenericHandler* cmCTestSubmitCommand::InitializeHandler() static_cast<cmCTestSubmitHandler*>(handler)->SelectParts(this->Parts); } + // Pass along any HTTPHEADER to the handler if this option was given. + if (!this->HttpHeaders.empty()) { + static_cast<cmCTestSubmitHandler*>(handler)->SetHttpHeaders( + this->HttpHeaders); + } + static_cast<cmCTestSubmitHandler*>(handler)->SetOption( "RetryDelay", this->RetryDelay.c_str()); static_cast<cmCTestSubmitHandler*>(handler)->SetOption( @@ -182,6 +188,11 @@ bool cmCTestSubmitCommand::CheckArgumentKeyword(std::string const& arg) } } // Arguments used by both modes. + if (arg == "HTTPHEADER") { + this->ArgumentDoing = ArgumentDoingHttpHeader; + return true; + } + if (arg == "RETRY_COUNT") { this->ArgumentDoing = ArgumentDoingRetryCount; return true; @@ -230,6 +241,11 @@ bool cmCTestSubmitCommand::CheckArgumentValue(std::string const& arg) return true; } + if (this->ArgumentDoing == ArgumentDoingHttpHeader) { + this->HttpHeaders.push_back(arg); + return true; + } + if (this->ArgumentDoing == ArgumentDoingRetryCount) { this->RetryCount = arg; return true; diff --git a/Source/CTest/cmCTestSubmitCommand.h b/Source/CTest/cmCTestSubmitCommand.h index e566abb..cf65cdc 100644 --- a/Source/CTest/cmCTestSubmitCommand.h +++ b/Source/CTest/cmCTestSubmitCommand.h @@ -70,6 +70,7 @@ protected: ArgumentDoingRetryCount, ArgumentDoingCDashUpload, ArgumentDoingCDashUploadType, + ArgumentDoingHttpHeader, ArgumentDoingLast2 }; @@ -83,6 +84,7 @@ protected: bool CDashUpload; std::string CDashUploadFile; std::string CDashUploadType; + std::vector<std::string> HttpHeaders; }; #endif diff --git a/Source/CTest/cmCTestSubmitHandler.cxx b/Source/CTest/cmCTestSubmitHandler.cxx index 96d1491..4aceddb 100644 --- a/Source/CTest/cmCTestSubmitHandler.cxx +++ b/Source/CTest/cmCTestSubmitHandler.cxx @@ -301,9 +301,19 @@ bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix, CURLcode res; FILE* ftpfile; char error_buffer[1024]; + // Set Content-Type to satisfy fussy modsecurity rules. struct curl_slist* headers = ::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml"); + // Add any additional headers that the user specified. + for (std::vector<std::string>::const_iterator h = this->HttpHeaders.begin(); + h != this->HttpHeaders.end(); ++h) { + cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, + " Add HTTP Header: \"" << *h << "\"" << std::endl, + this->Quiet); + headers = ::curl_slist_append(headers, h->c_str()); + } + /* In windows, this will init the winsock stuff */ ::curl_global_init(CURL_GLOBAL_ALL); std::string dropMethod(this->CTest->GetCTestConfiguration("DropMethod")); @@ -376,7 +386,6 @@ bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix, ::curl_easy_setopt(curl, CURLOPT_PUT, 1); ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); - // Be sure to set Content-Type to satisfy fussy modsecurity rules ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); std::string local_file = *file; @@ -1014,6 +1023,7 @@ int cmCTestSubmitHandler::HandleCDashUploadFile(std::string const& file, cmSystemTools::ExpandListArgument(curlopt, args); curl.SetCurlOptions(args); curl.SetTimeOutSeconds(SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT); + curl.SetHttpHeaders(this->HttpHeaders); std::string dropMethod; std::string url; this->ConstructCDashURL(dropMethod, url); diff --git a/Source/CTest/cmCTestSubmitHandler.h b/Source/CTest/cmCTestSubmitHandler.h index cf36546..2923f4f 100644 --- a/Source/CTest/cmCTestSubmitHandler.h +++ b/Source/CTest/cmCTestSubmitHandler.h @@ -43,6 +43,11 @@ public: // handle the cdash file upload protocol int HandleCDashUploadFile(std::string const& file, std::string const& type); + void SetHttpHeaders(std::vector<std::string> const& v) + { + this->HttpHeaders = v; + } + void ConstructCDashURL(std::string& dropMethod, std::string& url); private: @@ -95,6 +100,7 @@ private: bool HasWarnings; bool HasErrors; cmCTest::SetOfStrings Files; + std::vector<std::string> HttpHeaders; }; #endif |