@@ -28,29 +28,107 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
28
#include <string.h>
29
29
#include <my_global.h>
30
30
#include <curl/curl.h>
31
- #include " mysql.h"
31
+ #include < mysql.h>
32
32
#include <stdarg.h>
33
33
34
34
my_bool send_post_init (UDF_INIT * initid , UDF_ARGS * args , char * message )
35
35
{
36
- if (args -> arg_count < 2 ) {
37
- strcpy (message , "Send() requires >=2 arguments" );
38
- return 1 ;
39
- }
40
- if (args -> arg_type [0 ] != STRING_RESULT ) {
41
- strcpy (message , "Send() requires [addr:string]" );
42
- return 1 ;
43
- }
44
- return 0 ;
36
+ return wrapped_init (initid , args , message );
45
37
}
46
38
47
39
my_bool send_post_deinit (UDF_INIT * initid , UDF_ARGS * args , char * message )
48
40
{
49
- if (initid -> ptr ){
41
+ return wrapped_deinit (initid , args , message );
42
+ }
43
+
44
+ long long send_post (UDF_INIT * initid , UDF_ARGS * args , char * is_null , char * error )
45
+ {
46
+ CURLcode code = 1 ;
47
+ char method [] = "POST" ;
48
+ if (wrapup_request (args , method , & code ))
49
+ {
50
+ * error = 1 ;
51
+ }
52
+ if (code )
53
+ {
54
+ * error = 1 ;
55
+ }
56
+ return code ;
57
+ }
58
+
59
+ my_bool send_put_init (UDF_INIT * initid , UDF_ARGS * args , char * message )
60
+ {
61
+ return wrapped_init (initid , args , message );
62
+ }
63
+
64
+ my_bool send_put_deinit (UDF_INIT * initid , UDF_ARGS * args , char * message )
65
+ {
66
+ return wrapped_deinit (initid , args , message );
67
+ }
68
+
69
+ long long send_put (UDF_INIT * initid , UDF_ARGS * args , char * is_null , char * error )
70
+ {
71
+ CURLcode code = 1 ;
72
+ char method [] = "PUT" ;
73
+ if (wrapup_request (args , method , & code ))
74
+ {
75
+ * error = 1 ;
76
+ }
77
+ if (code )
78
+ {
79
+ * error = 1 ;
80
+ }
81
+ return code ;
82
+ }
83
+
84
+ my_bool send_delete_init (UDF_INIT * initid , UDF_ARGS * args , char * message )
85
+ {
86
+ return wrapped_init (initid , args , message );
87
+ }
88
+
89
+ my_bool send_delete_deinit (UDF_INIT * initid , UDF_ARGS * args , char * message )
90
+ {
91
+ return wrapped_deinit (initid , args , message );
92
+ }
93
+
94
+ long long send_delete (UDF_INIT * initid , UDF_ARGS * args , char * is_null , char * error )
95
+ {
96
+ CURLcode code = 1 ;
97
+ char method [] = "DELETE" ;
98
+ if (wrapup_request (args , method , & code ))
99
+ {
100
+ * error = 1 ;
101
+ }
102
+ if (code )
103
+ {
104
+ * error = 1 ;
105
+ }
106
+ return code ;
107
+ }
108
+
109
+ my_bool wrapped_init (UDF_INIT * initid , UDF_ARGS * args , char * message )
110
+ {
111
+ if (args -> arg_count < 2 )
112
+ {
113
+ strcpy (message , "Function requires >=2 arguments" );
114
+ return 1 ;
115
+ }
116
+ if (args -> arg_type [0 ] != STRING_RESULT )
117
+ {
118
+ strcpy (message , "Function requires [addr:string]" );
119
+ return 1 ;
120
+ }
121
+ return 0 ;
122
+ }
123
+
124
+ my_bool wrapped_deinit (UDF_INIT * initid , UDF_ARGS * args , char * message )
125
+ {
126
+ if (initid -> ptr )
127
+ {
50
128
free (initid -> ptr );
51
129
initid -> ptr = NULL ;
52
130
}
53
- return 0 ;
131
+ return 0 ;
54
132
}
55
133
56
134
void json_string (char * res_str , char * attr_name , char * arg , enum Item_result arg_type , int trail_com )
@@ -125,7 +203,8 @@ void encapsulate_data(UDF_ARGS* udf_args, char** res_str)
125
203
* res_str = (char * )calloc (res_len + 3 , sizeof (char ));
126
204
* * res_str = '{' ;
127
205
unsigned long current_pos = 1 ;
128
- for (int i = 0 ; i < num_of_args ; i ++ ){
206
+ for (int i = 0 ; i < num_of_args ; i ++ )
207
+ {
129
208
strcpy ((* res_str + current_pos ), char_args [i ]);
130
209
current_pos += strlen (char_args [i ]);
131
210
free (char_args [i ]);
@@ -134,28 +213,28 @@ void encapsulate_data(UDF_ARGS* udf_args, char** res_str)
134
213
(* res_str )[res_len + 2 ] = '\0' ;
135
214
}
136
215
137
- long long send_post ( UDF_INIT * initid , UDF_ARGS * args , char * is_null , char * error )
216
+ int wrapup_request ( UDF_ARGS * args , const char * method , CURLcode * code )
138
217
{
139
218
curl_global_init (CURL_GLOBAL_ALL );
140
- char * addr = args -> args [0 ];
141
- char * json = NULL ;
219
+ CURL * handle = curl_easy_init ();
220
+ char * addr = args -> args [0 ];
221
+ char * json = NULL ;
142
222
encapsulate_data (args , & json );
143
- if (json == NULL ) {
144
- * error = 1 ;
145
- return 0 ;
223
+ if (json == NULL || handle == NULL )
224
+ {
225
+ return 1 ;
146
226
}
147
- CURL * handle = curl_easy_init ();
148
227
struct curl_slist * list = NULL ;
149
228
list = curl_slist_append (list , "Content-Type: application/json" );
150
- list = curl_slist_append ( list , "accept: application/json" );
229
+ curl_easy_setopt ( handle , CURLOPT_CUSTOMREQUEST , method );
151
230
curl_easy_setopt (handle , CURLOPT_HTTPHEADER , list );
152
- curl_easy_setopt (handle , CURLOPT_URL , addr );
153
- curl_easy_setopt (handle , CURLOPT_POSTFIELDS , json );
154
- CURLcode code = curl_easy_perform (handle );
155
- if (code != 0 )
231
+ curl_easy_setopt (handle , CURLOPT_URL , addr );
232
+ curl_easy_setopt (handle , CURLOPT_POSTFIELDS , json );
233
+ if (list == NULL )
156
234
{
157
- * error = 1 ;
158
- }
159
- curl_global_cleanup ();
160
- return code ;
235
+ return 1 ;
236
+ }
237
+ * code = curl_easy_perform (handle );
238
+ curl_global_cleanup ();
239
+ return 0 ;
161
240
}
0 commit comments