Browse Source

Added functionality to allow CTest to easily access web APIs. This will be used for better communication with CDash.

Zach Mullen 16 years ago
parent
commit
3ff0b94055
2 changed files with 64 additions and 0 deletions
  1. 52 0
      Source/cmSystemTools.cxx
  2. 12 0
      Source/cmSystemTools.h

+ 52 - 0
Source/cmSystemTools.cxx

@@ -38,6 +38,8 @@
 # include <sys/wait.h>
 # include <sys/wait.h>
 #endif
 #endif
 
 
+#include "cm_curl.h"
+
 #include <sys/stat.h>
 #include <sys/stat.h>
 
 
 #if defined(_WIN32) && \
 #if defined(_WIN32) && \
@@ -2925,3 +2927,53 @@ bool cmSystemTools::CheckRPath(std::string const& file,
   return false;
   return false;
 #endif
 #endif
 }
 }
+
+//----------------------------------------------------------------------------
+static size_t
+HTTPResponseCallback(void *ptr, size_t size, size_t nmemb, void *data)
+{
+  register int realsize = (int)(size * nmemb);
+
+  std::string *response
+    = static_cast<std::string*>(data);
+  const char* chPtr = static_cast<char*>(ptr);
+  *response += chPtr;
+
+  return realsize;
+}
+
+//----------------------------------------------------------------------------
+int cmSystemTools::HTTPRequest(std::string url, HTTPMethod method,
+                                       std::string& response,
+                                       std::string fields, int timeout)
+{
+  CURL* curl;
+  ::curl_global_init(CURL_GLOBAL_ALL);
+  curl = ::curl_easy_init();
+
+  //set request options
+  if(method == cmSystemTools::HTTP_GET && fields.size())
+    {
+    url += "?" + fields;
+    }
+  else
+    {
+    ::curl_easy_setopt(curl, CURLOPT_POST, 1);
+    ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
+    }
+  ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
+  ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
+  ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+
+  //set response options
+  ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTTPResponseCallback);
+  ::curl_easy_setopt(curl, CURLOPT_FILE, (void *)&response);
+  ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
+
+  CURLcode res = ::curl_easy_perform(curl);
+
+  ::curl_easy_cleanup(curl);
+  ::curl_global_cleanup();
+  
+  return static_cast<int>(res);
+}

+ 12 - 0
Source/cmSystemTools.h

@@ -46,6 +46,18 @@ public:
   static void ExpandRegistryValues(std::string& source,
   static void ExpandRegistryValues(std::string& source,
                                    KeyWOW64 view = KeyWOW64_Default);
                                    KeyWOW64 view = KeyWOW64_Default);
 
 
+  enum HTTPMethod {
+    HTTP_GET,
+    HTTP_POST
+  };
+
+  /**
+   * Perform an HTTP request.
+   */
+  static int HTTPRequest(std::string url, HTTPMethod method,
+                          std::string& response,
+                          std::string fields = "", int timeout = 10);
+
   /**
   /**
    * Platform independent escape spaces, unix uses backslash,
    * Platform independent escape spaces, unix uses backslash,
    * windows double quotes the string.
    * windows double quotes the string.