2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2017 - ESI-Group - Cedric DELAMARRE
6 * This file is hereby licensed under the terms of the GNU GPL v2.0,
7 * pursuant to article 5.3.4 of the CeCILL v.2.1.
8 * This file was originally licensed under the terms of the CeCILL v2.1,
9 * and continues to be available under such terms.
10 * For more information, see the COPYING file which you should have received
11 * along with this program.
14 /*--------------------------------------------------------------------------*/
16 #include <curl/curl.h>
17 #include "webtools_gw.hxx"
18 #include "function.hxx"
22 #include "sciCurl.hxx"
26 #include "localization.h"
29 #include "sci_malloc.h"
32 /*--------------------------------------------------------------------------*/
33 types::Function::ReturnValue sci_http_put_post(types::typed_list &in, types::optional_list &opt, int _iRetCount, types::typed_list &out, const char* fname)
35 SciCurl* sciCurlObj = SciCurl::getInstance();
36 CURLcode res = CURLE_OK;
37 struct curl_slist *headers = NULL;
41 if (in.size() < 1 || in.size() > 2)
43 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), fname, 1, 2);
44 return types::Function::Error;
49 Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), fname, 1, 2);
50 return types::Function::Error;
54 if(in[0]->isString() == false && in[0]->getAs<types::String>()->isScalar() == false)
56 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar string expected.\n"), fname, 1);
57 return types::Function::Error;
60 CURL* curl = curl_easy_init();
63 Scierror(999, _("%s: CURL initialization failed.\n"), fname);
64 return types::Function::Error;
67 sciCurlObj->setCommonHeaders(curl);
69 char* pcURL = wide_string_to_UTF8(in[0]->getAs<types::String>()->get(0));
70 curl_easy_setopt(curl, CURLOPT_URL, pcURL);
73 sciCurlObj->getResultAsObject(curl);
74 if(strcmp(fname, "http_put") == 0)
76 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
78 else if(strcmp(fname, "http_patch") == 0)
80 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
84 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
87 // common optional argument
88 if(checkCommonOpt((void*)curl, opt, fname))
90 return types::Function::Error;
93 // set proxy information
94 if(sciCurlObj->setProxy(curl))
96 Scierror(999, _("%s: Wrong proxy information, please check in the 'internet' Scilab preference.\n"), fname);
97 return types::Function::Error;
100 // specific optional argument
101 for (const auto& o : opt)
103 if(o.first == L"format")
105 if(o.second->isString() == false && o.second->getAs<types::String>()->isScalar() == false)
107 Scierror(999, _("%s: Wrong type for input argument #%s: A scalar string expected.\n"), fname, o.first.data());
108 return types::Function::Error;
111 if( (wcscmp(o.second->getAs<types::String>()->get(0), L"JSON") == 0) ||
112 (wcscmp(o.second->getAs<types::String>()->get(0), L"json") == 0))
122 if(in[1]->isString() && in[1]->getAs<types::String>()->isScalar())
124 pcData = wide_string_to_UTF8(in[1]->getAs<types::String>()->get(0));
128 pcData = os_strdup(toJSON(in[1]).c_str());
134 headers = curl_slist_append(headers, "Accept: application/json");
135 headers = curl_slist_append(headers, "Content-Type: application/json");
136 headers = curl_slist_append(headers, "charsets: utf-8");
137 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
140 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, pcData);
143 res = curl_easy_perform(curl);
151 curl_slist_free_all(headers);
156 Scierror(999, _("%s: CURL execution failed.\n%s\n"), fname, curl_easy_strerror(res));
158 return types::Function::Error;
161 out.push_back(sciCurlObj->getResult());
166 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
167 out.push_back(new types::Double((double)http_code));
170 curl_easy_cleanup(curl);
171 return types::Function::OK;