Browse code

slack: reuse http_client api

Arsen Semenov authored on 16/09/2021 10:42:24
Showing 3 changed files
... ...
@@ -8,24 +8,5 @@ include ../../Makefile.defs
8 8
 auto_gen=
9 9
 NAME=slack.so
10 10
 
11
-ifeq ($(CROSS_COMPILE),)
12
-CURL_BUILDER=$(shell \
13
-        if pkg-config --exists libcurl; then \
14
-                echo 'pkg-config libcurl'; \
15
-        else \
16
-                which curl-config; \
17
-        fi)
18
-endif
19
-
20
-ifneq ($(CURL_BUILDER),)
21
-        CURLDEFS += $(shell $(CURL_BUILDER) --cflags)
22
-        CURLLIBS += $(shell $(CURL_BUILDER) --libs)
23
-else
24
-        CURLDEFS+=-I$(LOCALBASE)/include -I$(SYSBASE)/include
25
-        CURLLIBS+=-L$(LOCALBASE)/lib -L$(SYSBASE)/lib -lcurl
26
-endif
27
-
28
-DEFS+=$(CURLDEFS)
29
-LIBS=$(CURLLIBS)
30 11
 
31 12
 include ../../Makefile.modules
... ...
@@ -78,6 +78,11 @@ struct module_exports exports = {
78 78
 static int mod_init(void) {
79 79
 	LM_INFO("slack module init\n");
80 80
 
81
+	if(httpc_load_api(&httpapi) != 0) {
82
+		LM_ERR("can not bind to http_client API \n");
83
+		return -1;
84
+	}
85
+
81 86
 	_slmsg_buf = (char*)pkg_malloc((buf_size+1)*sizeof(char));
82 87
 	if(_slmsg_buf==NULL)
83 88
 	{
... ...
@@ -99,17 +104,34 @@ static void mod_destroy() {
99 104
 	return;
100 105
 }
101 106
 
107
+/* free and reset str */
108
+static void slack_free_str(str *string)
109
+{
110
+	str ptr = STR_NULL;
111
+	if (string->s == NULL)
112
+		return;
113
+
114
+	ptr = *string;
115
+
116
+	if(ptr.s != NULL && ptr.len > 0)
117
+		pkg_free(ptr.s);
118
+
119
+	string->s = NULL;
120
+	string->len = 0;
121
+
122
+	return;
123
+}
124
+
102 125
 /**
103
- * send message with curl
126
+ * send slack message using http_client api
104 127
  * @return 0 on success, -1 on error
105 128
  */
106
-static int _curl_send(const char* uri, str *post_data)
129
+static int slack_curl_send(struct sip_msg* msg, char* uri, str *post_data)
107 130
 {
108 131
 	int datasz;
109 132
 	char* send_data;
110
-	CURL *curl_handle;
111
-	CURLcode res;
112
-	// LM_DBG("sending to[%s]\n", uri);
133
+	str ret = STR_NULL;
134
+	int curl = 0;
113 135
 
114 136
 	datasz = snprintf(NULL, 0, BODY_FMT, slack_channel, slack_username, post_data->s, slack_icon);
115 137
 	if (datasz < 0) {
... ...
@@ -123,31 +145,19 @@ static int _curl_send(const char* uri, str *post_data)
123 145
     }
124 146
     snprintf(send_data, datasz+1, BODY_FMT, slack_channel, slack_username, post_data->s, slack_icon);
125 147
 
126
-	curl_global_init(CURL_GLOBAL_ALL);
127
-
128
-	if((curl_handle=curl_easy_init())==NULL) {
129
-    	LM_ERR("Unable to init cURL library\n");
130
-		curl_global_cleanup();
131
-		pkg_free(send_data);
132
-        return -1;
133
-    }
134
-
135
-	curl_easy_setopt(curl_handle, CURLOPT_URL, uri);
136
-	curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, send_data);
137
-	res = curl_easy_perform(curl_handle);
148
+	/* send request */
149
+	curl = httpapi.http_client_query(msg, uri, &ret, send_data, NULL);
150
+	pkg_free(send_data);
138 151
 
139
-	if (res != CURLE_OK) {
140
-		LM_ERR("slack request send error: %s\n", curl_easy_strerror(res));
141
-		curl_easy_cleanup(curl_handle);
142
-		curl_global_cleanup();
143
-		pkg_free(send_data);
152
+	if(curl >= 300 || curl < 100) {
153
+		LM_ERR("request failed with error: %d\n", curl);
154
+		slack_free_str(&ret);
144 155
 		return -1;
145 156
 	}
146 157
 
147
-	LM_INFO("slack request sent [%d]\n", datasz);
148
-	curl_easy_cleanup(curl_handle);
149
-	curl_global_cleanup();
150
-	pkg_free(send_data);
158
+	LM_DBG("slack send response: [%.*s]\n", ret.len, ret.s);
159
+	slack_free_str(&ret);
160
+
151 161
 	return 0;
152 162
 }
153 163
 
... ...
@@ -243,7 +253,7 @@ static inline int slack_helper(struct sip_msg* msg, sl_msg_t *sm)
243 253
 
244 254
 	txt.s = _slmsg_buf;
245 255
 
246
-	return _curl_send(slack_url, &txt);
256
+	return slack_curl_send(msg, slack_url, &txt);
247 257
 }
248 258
 
249 259
 static int slack_send1(struct sip_msg* msg, char* frm, char* str2)
... ...
@@ -273,7 +283,7 @@ static int ki_slack_send(sip_msg_t *msg, str *slmsg)
273 283
 		return -1;
274 284
 	}
275 285
 
276
-	res = _curl_send(slack_url, &txt);
286
+	res = slack_curl_send(msg, slack_url, &txt);
277 287
 	pv_elem_free_all(xmodel);
278 288
 	return res;
279 289
 }
... ...
@@ -22,6 +22,8 @@
22 22
 #ifndef slack_h
23 23
 #define slack_h
24 24
 
25
+#include "../../modules/http_client/curl_api.h"
26
+
25 27
 #include "../../core/sr_module.h"
26 28
 #include "../../core/dprint.h"
27 29
 #include "../../core/parser/parse_content.h"
... ...
@@ -29,7 +31,6 @@
29 31
 #include "../../core/kemi.h"
30 32
 
31 33
 #include <string.h>
32
-#include <curl/curl.h>
33 34
 
34 35
 #define BODY_FMT "{\"channel\": \"%s\", \"username\": \"%s\", \"text\": \"%s\", \"icon_emoji\": \"%s\" }"
35 36
 #define SLACK_URL_MAX_SIZE 128
... ...
@@ -42,7 +43,11 @@ static int _slack_print_log(struct sip_msg* msg, pv_elem_p list, char *buf, int
42 43
 	return pv_printf(msg, list, buf, len);
43 44
 }
44 45
 
45
-static int _curl_send(const char* uri, str *post_data );
46
+/* http_client api */
47
+httpc_api_t httpapi;
48
+
49
+static void slack_free_str(str *str);
50
+static int slack_curl_send(struct sip_msg* msg, char* uri, str *post_data );
46 51
 static int _slack_parse_url_param(char *val);
47 52
 static int _slack_url_param(modparam_t type, void *val);
48 53