Skip to content

Commit 6e1041b

Browse files
committed
Add PUT and DELETE actions
1 parent 826c903 commit 6e1041b

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ set(
4444
IRODS_CURL_PLUGINS
4545
msiCurlGetObj
4646
msiCurlGetStr
47+
msiCurlDelete
48+
msiCurlPut
4749
msiCurlPost
4850
)
4951

@@ -91,6 +93,8 @@ install(
9193
FILES
9294
${CMAKE_SOURCE_DIR}/rules/curlGetObj.r
9395
${CMAKE_SOURCE_DIR}/rules/curlGetStr.r
96+
${CMAKE_SOURCE_DIR}/rules/curlDelete.r
97+
${CMAKE_SOURCE_DIR}/rules/curlPut.r
9498
${CMAKE_SOURCE_DIR}/rules/curlPost.r
9599
DESTINATION ${IRODS_HOME_DIRECTORY}/clients/icommands/test/rules
96100
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ

microservices/core/include/irods_ms_plugin_curl.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class irodsCurl {
7676

7777
irods::error get_str( char*, char** );
7878

79+
irods::error del( char*, char** );
80+
81+
irods::error put( char*, keyValPair_t*, char** );
82+
7983
irods::error post( char*, keyValPair_t*, char** );
8084

8185

microservices/core/src/irods_ms_plugin_curl.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,91 @@ irods::error irodsCurl::get_str( char *url, char **buffer ) {
134134
return SUCCESS();
135135
}
136136

137+
irods::error irodsCurl::del( char *url, char **buffer ) {
138+
CURLcode res = CURLE_OK;
139+
string_t string;
140+
curlProgress_t prog; // for progress and cutoff
141+
142+
// Destination string_t init
143+
string.ptr = strdup("");
144+
string.len = 0;
145+
146+
// Progress struct init
147+
prog.downloaded = 0;
148+
prog.cutoff = 0;
149+
150+
// Set up easy handler
151+
curl_easy_setopt( curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
152+
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, &irodsCurl::write_str );
153+
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &string );
154+
curl_easy_setopt( curl, CURLOPT_URL, url );
155+
curl_easy_setopt( curl, CURLOPT_CUSTOMREQUEST, "DELETE");
156+
157+
// CURL call
158+
res = curl_easy_perform( curl );
159+
160+
// Output
161+
*buffer = string.ptr;
162+
163+
// Error logging
164+
if ( res != CURLE_OK ) {
165+
rodsLog( LOG_ERROR, "irodsCurl::delete: cURL error: %s", curl_easy_strerror( res ) );
166+
return CODE(PLUGIN_ERROR);
167+
}
168+
169+
return SUCCESS();
170+
}
171+
172+
irods::error irodsCurl::put( char *url, keyValPair_t *post_fields, char **response ) {
173+
CURLcode res = CURLE_OK;
174+
175+
char *headers, *data; // input
176+
char *encoded_data = NULL;
177+
178+
struct curl_slist *header_list = NULL;
179+
180+
string_t string; // server response
181+
int must_encode = 0; // for the time being...
182+
183+
// Parse POST fields
184+
data = getValByKey(post_fields, IRODS_CURL_DATA_KW);
185+
headers = getValByKey(post_fields, IRODS_CURL_HEADERS_KW);
186+
187+
// Init string
188+
string.ptr = strdup("");
189+
string.len = 0;
190+
191+
// url-encode data
192+
if (must_encode && data) {
193+
encoded_data = curl_easy_escape(curl, data, 0);
194+
}
195+
196+
// Set headers
197+
if (headers && strlen(headers)) {
198+
header_list = curl_slist_append(header_list, headers);
199+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
200+
}
201+
202+
// Set up easy handler
203+
curl_easy_setopt(curl, CURLOPT_URL, url);
204+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
205+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
206+
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
207+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &irodsCurl::write_str);
208+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &string);
209+
210+
// CURL call
211+
res = curl_easy_perform(curl);
212+
213+
// Cleanup
214+
if (header_list) curl_slist_free_all(header_list);
215+
if (encoded_data) curl_free(encoded_data);
216+
217+
// Output
218+
*response = string.ptr;
219+
220+
return CODE(res);
221+
}
137222

138223
irods::error irodsCurl::post( char *url, keyValPair_t *post_fields, char **response ) {
139224
CURLcode res = CURLE_OK;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* libmsiCurlDelete.cpp
3+
*
4+
* Created on: 12 June 2017
5+
* Author: Hurng-Chun Lee <[email protected]>
6+
*/
7+
#include "irods_ms_plugin_curl.hpp"
8+
9+
int msiCurlDelete(msParam_t* msp_in_str_url, msParam_t* msp_out_str_response, ruleExecInfo_t* rei) {
10+
11+
if (rei == nullptr) {
12+
rodsLog(LOG_ERROR, "msiCurlDelete: input rei is NULL");
13+
return SYS_INTERNAL_NULL_INPUT_ERR;
14+
}
15+
16+
if (msp_in_str_url == nullptr) {
17+
rodsLog(LOG_ERROR, "msiCurlDelete: msp_in_str_url is NULL");
18+
return SYS_INTERNAL_NULL_INPUT_ERR;
19+
}
20+
if (msp_in_str_url->type == nullptr) {
21+
rodsLog(LOG_ERROR, "msiCurlDelete: msp_in_str_url->type is NULL");
22+
return SYS_INTERNAL_NULL_INPUT_ERR;
23+
}
24+
if (strcmp(msp_in_str_url->type, STR_MS_T)) {
25+
rodsLog(LOG_ERROR, "msiCurlDelete: first argument should be STR_MS_T, was [%s]", msp_in_str_url->type);
26+
return USER_PARAM_TYPE_ERR;
27+
}
28+
29+
if (msp_out_str_response == nullptr) {
30+
rodsLog(LOG_ERROR, "msiCurlDelete: msp_out_str_response is NULL");
31+
return SYS_INTERNAL_NULL_INPUT_ERR;
32+
}
33+
34+
irodsCurl myCurl(rei->rsComm);
35+
msp_out_str_response->type = strdup(STR_MS_T);
36+
irods::error res = myCurl.del(static_cast<char*>(msp_in_str_url->inOutStruct), reinterpret_cast<char**>(&msp_out_str_response->inOutStruct));
37+
return res.code();
38+
}
39+
40+
extern "C"
41+
irods::ms_table_entry* plugin_factory() {
42+
irods::ms_table_entry* msvc = new irods::ms_table_entry(2);
43+
msvc->add_operation<
44+
msParam_t*,
45+
msParam_t*,
46+
ruleExecInfo_t*>("msiCurlDelete",
47+
std::function<int(
48+
msParam_t*,
49+
msParam_t*,
50+
ruleExecInfo_t*)>(msiCurlDelete));
51+
return msvc;
52+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* libmsiCurlPut.cpp
3+
*
4+
* Created on: 12 June 2017
5+
* Author: Hurng-Chun Lee <[email protected]>
6+
*/
7+
#include "irods_ms_plugin_curl.hpp"
8+
9+
int msiCurlPut(msParam_t* msp_in_str_url, msParam_t* msp_in_kvp_options, msParam_t* msp_out_str_response, ruleExecInfo_t* rei) {
10+
11+
if (rei == nullptr) {
12+
rodsLog(LOG_ERROR, "msiCurlPut: input rei is NULL");
13+
return SYS_INTERNAL_NULL_INPUT_ERR;
14+
}
15+
16+
if (msp_in_str_url == nullptr) {
17+
rodsLog(LOG_ERROR, "msiCurlPut: msp_in_str_url is NULL");
18+
return SYS_INTERNAL_NULL_INPUT_ERR;
19+
}
20+
if (msp_in_str_url->type == nullptr) {
21+
rodsLog(LOG_ERROR, "msiCurlPut: msp_in_str_url->type is NULL");
22+
return SYS_INTERNAL_NULL_INPUT_ERR;
23+
}
24+
if (strcmp(msp_in_str_url->type, STR_MS_T)) {
25+
rodsLog(LOG_ERROR, "msiCurlPut: first argument should be STR_MS_T, was [%s]", msp_in_str_url->type);
26+
return USER_PARAM_TYPE_ERR;
27+
}
28+
29+
if (msp_in_kvp_options == nullptr) {
30+
rodsLog(LOG_ERROR, "msiCurlPut: msp_in_kvp_options is NULL");
31+
return SYS_INTERNAL_NULL_INPUT_ERR;
32+
}
33+
if (msp_in_kvp_options->type == nullptr) {
34+
rodsLog(LOG_ERROR, "msiCurlPut: msp_in_kvp_options->type is NULL");
35+
return SYS_INTERNAL_NULL_INPUT_ERR;
36+
}
37+
if (strcmp(msp_in_kvp_options->type, KeyValPair_MS_T)) {
38+
rodsLog(LOG_ERROR, "msiCurlPut: second argument should be KeyValPair_MS_T, was [%s]", msp_in_kvp_options->type);
39+
return USER_PARAM_TYPE_ERR;
40+
}
41+
42+
if (msp_out_str_response == nullptr) {
43+
rodsLog(LOG_ERROR, "msiCurlPut: msp_out_str_response is NULL");
44+
return SYS_INTERNAL_NULL_INPUT_ERR;
45+
}
46+
47+
irodsCurl myCurl(rei->rsComm);
48+
msp_out_str_response->type = strdup(STR_MS_T);
49+
irods::error res = myCurl.put(static_cast<char*>(msp_in_str_url->inOutStruct), static_cast<keyValPair_t*>(msp_in_kvp_options->inOutStruct), reinterpret_cast<char**>(&msp_out_str_response->inOutStruct));
50+
return res.code();
51+
}
52+
53+
extern "C"
54+
irods::ms_table_entry* plugin_factory() {
55+
irods::ms_table_entry* msvc = new irods::ms_table_entry(3);
56+
msvc->add_operation<
57+
msParam_t*,
58+
msParam_t*,
59+
msParam_t*,
60+
ruleExecInfo_t*>("msiCurlPut",
61+
std::function<int(
62+
msParam_t*,
63+
msParam_t*,
64+
msParam_t*,
65+
ruleExecInfo_t*)>(msiCurlPut));
66+
return msvc;
67+
}

rules/curlDelete.r

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
curlDelete {
2+
msiCurlDelete(*url, *outStr);
3+
writeLine("stdout", *outStr);
4+
}
5+
INPUT *url="http://www.textfiles.com/art/dragon.txt"
6+
OUTPUT ruleExecOut

rules/curlPut.r

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
curlPut {
2+
*postFields."data" = *data
3+
msiCurlPut(*url, *postFields, *response);
4+
writeLine("stdout", "server response: "++*response);
5+
}
6+
INPUT *url="http://httpbin.org/post",*data="Sent from iRODS"
7+
OUTPUT ruleExecOut
8+
# See also http://requestb.in/ for quick testing of POST requests

0 commit comments

Comments
 (0)