[webtools] Header fixed about User-Agent 55/21455/5
Cedric Delamarre [Wed, 15 Apr 2020 12:28:01 +0000 (14:28 +0200)]
   // must not return an error
   http_get("https://api.github.com/repos/scilab/scilab/stats/commit_activity", cert="none")

Change-Id: I86d2b2da102f6ab6cfc84fe7123ed4b69b43c475

scilab/modules/webtools/sci_gateway/cpp/sci_http_delete.cpp
scilab/modules/webtools/sci_gateway/cpp/sci_http_get.cpp
scilab/modules/webtools/sci_gateway/cpp/sci_http_put_post.cpp
scilab/modules/webtools/sci_gateway/cpp/sci_http_upload.cpp
scilab/modules/webtools/src/cpp/sciCurl.cpp
scilab/modules/webtools/src/cpp/sciCurl.hxx

index 83170f2..a6d8cbd 100644 (file)
@@ -60,6 +60,8 @@ types::Function::ReturnValue sci_http_delete(types::typed_list &in, types::optio
         return types::Function::Error;
     }
 
+    sciCurlObj->setCommonHeaders(curl);
+
     char* pcURL = wide_string_to_UTF8(in[0]->getAs<types::String>()->get(0));
     curl_easy_setopt(curl, CURLOPT_URL, pcURL);
     FREE(pcURL);
index 91556f5..b4134ee 100644 (file)
@@ -63,6 +63,8 @@ types::Function::ReturnValue sci_http_get(types::typed_list &in, types::optional
         return types::Function::Error;
     }
 
+    sciCurlObj->setCommonHeaders(curl);
+
     char* pcURL = wide_string_to_UTF8(in[0]->getAs<types::String>()->get(0));
     curl_easy_setopt(curl, CURLOPT_URL, pcURL);
     FREE(pcURL);
@@ -138,7 +140,7 @@ types::Function::ReturnValue sci_http_get(types::typed_list &in, types::optional
     if(_iRetCount == 2)
     {
         long http_code = 0;
-        curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
+        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
         out.push_back(new types::Double((double)http_code));
     }
 
index 3876984..7f754e5 100644 (file)
@@ -64,6 +64,8 @@ types::Function::ReturnValue sci_http_put_post(types::typed_list &in, types::opt
         return types::Function::Error;
     }
 
+    sciCurlObj->setCommonHeaders(curl);
+
     char* pcURL = wide_string_to_UTF8(in[0]->getAs<types::String>()->get(0));
     curl_easy_setopt(curl, CURLOPT_URL, pcURL);
     FREE(pcURL);
index a89b161..d37385f 100644 (file)
@@ -64,6 +64,8 @@ types::Function::ReturnValue sci_http_upload(types::typed_list &in, types::optio
         return types::Function::Error;
     }
 
+    sciCurlObj->setCommonHeaders(curl);
+
     char* pcURL = wide_string_to_UTF8(in[0]->getAs<types::String>()->get(0));
     curl_easy_setopt(curl, CURLOPT_URL, pcURL);
     FREE(pcURL);
index cb7601d..6b37004 100644 (file)
@@ -20,6 +20,8 @@ extern "C"
 {
     #include "getScilabPreference.h"
     #include "freeArrayOfString.h"
+    #include "getos.h"
+    #include "version.h"
 }
 
 SciCurl* SciCurl::me = nullptr;
@@ -176,3 +178,19 @@ int SciCurl::setProxy(CURL* curl)
     freeArrayOfString(values, N);
     return 0;
 }
+
+void SciCurl::setCommonHeaders(CURL* curl)
+{
+    char* OperatingSystem = getOSFullName();
+    char* Release = getOSRelease();
+
+    // Scilab version
+    std::string pcUserAgent = "Scilab/" + std::to_string(SCI_VERSION_MAJOR)+"."+ std::to_string(SCI_VERSION_MINOR)+"."+ std::to_string(SCI_VERSION_MAINTENANCE);
+    // OS name
+    pcUserAgent += " (" + std::string(OperatingSystem) + " " + std::string(Release) + ")";
+    // set user agent header
+    curl_easy_setopt(curl, CURLOPT_USERAGENT, pcUserAgent.data());
+
+    FREE(OperatingSystem);
+    FREE(Release);
+}
\ No newline at end of file
index 9d9be1e..7349e80 100644 (file)
@@ -34,6 +34,7 @@ public:
     void getResultAsObject(CURL* curl);
     void getResultAsFile(CURL* curl, FILE* fd);
     int setProxy(CURL* curl);
+    void setCommonHeaders(CURL* curl);
 
     types::InternalType* getResult(void);
     void clear(void);