Skip to content

Commit aa7c8b5

Browse files
committed
ch: Add helper function for PUT requests
1 parent 54b9568 commit aa7c8b5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/ch/ch_monitor.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,43 @@ virCHMonitorPutNoContent(virCHMonitorPtr mon, const char *endpoint)
14261426
return ret;
14271427
}
14281428

1429+
static int
1430+
virCHMonitorPut(virCHMonitorPtr mon,
1431+
const char *endpoint,
1432+
char *payload)
1433+
{
1434+
g_autofree char *url = NULL;
1435+
int responseCode = 0;
1436+
int ret = -1;
1437+
struct curl_slist *headers = NULL;
1438+
1439+
url = g_strdup_printf("%s/%s", URL_ROOT, endpoint);
1440+
headers = curl_slist_append(headers, "Accept: application/json");
1441+
headers = curl_slist_append(headers, "Content-Type: application/json");
1442+
headers = curl_slist_append(headers, "Expect:");
1443+
1444+
virObjectLock(mon);
1445+
1446+
/* reset all options of a libcurl session handle at first */
1447+
curl_easy_reset(mon->handle);
1448+
1449+
curl_easy_setopt(mon->handle, CURLOPT_UNIX_SOCKET_PATH, mon->socketpath);
1450+
curl_easy_setopt(mon->handle, CURLOPT_URL, url);
1451+
curl_easy_setopt(mon->handle, CURLOPT_CUSTOMREQUEST, "PUT");
1452+
curl_easy_setopt(mon->handle, CURLOPT_HTTPHEADER, headers);
1453+
curl_easy_setopt(mon->handle, CURLOPT_POSTFIELDS, payload);
1454+
1455+
responseCode = virCHMonitorCurlPerform(mon->handle);
1456+
1457+
virObjectUnlock(mon);
1458+
1459+
if (responseCode == 200 || responseCode == 204)
1460+
ret = 0;
1461+
1462+
curl_slist_free_all(headers);
1463+
return ret;
1464+
}
1465+
14291466
struct curl_data {
14301467
char *content;
14311468
size_t size;

0 commit comments

Comments
 (0)