From 0a0788a72d72ffe40624c6e47fdf5c30a7ab8ac6 Mon Sep 17 00:00:00 2001 From: Zach Mullen Date: Wed, 30 Dec 2009 11:10:42 -0500 Subject: [PATCH] Enhanced CTest HTTP Request API to support PUT file uploads. --- Source/cmCTest.cxx | 38 +++++++++++++++++++++++++++++--------- Source/cmCTest.h | 6 ++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index bcbe18e33..c6dffd736 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -172,22 +172,42 @@ HTTPResponseCallback(void *ptr, size_t size, size_t nmemb, void *data) //---------------------------------------------------------------------------- int cmCTest::HTTPRequest(std::string url, HTTPMethod method, std::string& response, - std::string fields, int timeout) + std::string fields, + std::string putFile, int timeout) { CURL* curl; + FILE* file; ::curl_global_init(CURL_GLOBAL_ALL); curl = ::curl_easy_init(); - //set request options - if(method == cmCTest::HTTP_GET && fields.size()) + //set request options based on method + switch(method) { - url += "?" + fields; - } - else - { - ::curl_easy_setopt(curl, CURLOPT_POST, 1); - ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str()); + case cmCTest::HTTP_POST: + ::curl_easy_setopt(curl, CURLOPT_POST, 1); + ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str()); + break; + case cmCTest::HTTP_PUT: + if(!cmSystemTools::FileExists(putFile.c_str())) + { + response = "Error: File "; + response += putFile + " does not exist.\n"; + return -1; + } + ::curl_easy_setopt(curl, CURLOPT_PUT, 1); + file = ::fopen(putFile.c_str(), "rb"); + ::curl_easy_setopt(curl, CURLOPT_INFILE, file); + //fall through to append GET fields + case cmCTest::HTTP_GET: + if(fields.size()) + { + url += "?" + fields; + } + break; + default: + break; } + ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); diff --git a/Source/cmCTest.h b/Source/cmCTest.h index 2b7912c51..8621b10dc 100644 --- a/Source/cmCTest.h +++ b/Source/cmCTest.h @@ -83,7 +83,8 @@ public: #ifdef CMAKE_BUILD_WITH_CMAKE enum HTTPMethod { HTTP_GET, - HTTP_POST + HTTP_POST, + HTTP_PUT }; /** @@ -91,7 +92,8 @@ public: */ static int HTTPRequest(std::string url, HTTPMethod method, std::string& response, - std::string fields = "", int timeout = 10); + std::string fields = "", + std::string putFile = "", int timeout = 0); #endif /** Get a testing part id from its string name. Returns PartCount