Enhanced CTest HTTP Request API to support PUT file uploads.

This commit is contained in:
Zach Mullen 2009-12-30 11:10:42 -05:00
parent 646e71f290
commit 0a0788a72d
2 changed files with 33 additions and 11 deletions

View File

@ -172,22 +172,42 @@ HTTPResponseCallback(void *ptr, size_t size, size_t nmemb, void *data)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int cmCTest::HTTPRequest(std::string url, HTTPMethod method, int cmCTest::HTTPRequest(std::string url, HTTPMethod method,
std::string& response, std::string& response,
std::string fields, int timeout) std::string fields,
std::string putFile, int timeout)
{ {
CURL* curl; CURL* curl;
FILE* file;
::curl_global_init(CURL_GLOBAL_ALL); ::curl_global_init(CURL_GLOBAL_ALL);
curl = ::curl_easy_init(); curl = ::curl_easy_init();
//set request options //set request options based on method
if(method == cmCTest::HTTP_GET && fields.size()) switch(method)
{ {
url += "?" + fields; case cmCTest::HTTP_POST:
} ::curl_easy_setopt(curl, CURLOPT_POST, 1);
else ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
{ break;
::curl_easy_setopt(curl, CURLOPT_POST, 1); case cmCTest::HTTP_PUT:
::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str()); 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_URL, url.c_str());
::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

View File

@ -83,7 +83,8 @@ public:
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
enum HTTPMethod { enum HTTPMethod {
HTTP_GET, HTTP_GET,
HTTP_POST HTTP_POST,
HTTP_PUT
}; };
/** /**
@ -91,7 +92,8 @@ public:
*/ */
static int HTTPRequest(std::string url, HTTPMethod method, static int HTTPRequest(std::string url, HTTPMethod method,
std::string& response, std::string& response,
std::string fields = "", int timeout = 10); std::string fields = "",
std::string putFile = "", int timeout = 0);
#endif #endif
/** Get a testing part id from its string name. Returns PartCount /** Get a testing part id from its string name. Returns PartCount