Skip to content

Commit

Permalink
Merge pull request #4 from pakrentos/dev
Browse files Browse the repository at this point in the history
New HTTP methods
  • Loading branch information
pakrentos authored May 18, 2020
2 parents 36bb772 + 1e3e5f6 commit a9d7ab6
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ sudo ./install.sh

### Usage

``` sql
SELECT send_post([<URL or IP with port>] as alias0, data1 as alias1, data2 as alias2, ...) FROM ...;
``` text
SELECT send_<post/put/delete>([<URL or IP with port>] as alias0, data1 as alias1, data2 as alias2, ...) FROM ...;
```

Returns 0 per request if request was successfully delivered (but HTTP response code could be 4xx or 5xx)
Expand Down
137 changes: 108 additions & 29 deletions src/libmsqlcurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,107 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <string.h>
#include <my_global.h>
#include <curl/curl.h>
#include "mysql.h"
#include <mysql.h>
#include <stdarg.h>

my_bool send_post_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if (args->arg_count < 2) {
strcpy(message, "Send() requires >=2 arguments");
return 1;
}
if (args->arg_type[0] != STRING_RESULT) {
strcpy(message, "Send() requires [addr:string]");
return 1;
}
return 0;
return wrapped_init(initid, args, message);
}

my_bool send_post_deinit(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if(initid->ptr){
return wrapped_deinit(initid, args, message);
}

long long send_post(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
CURLcode code = 1;
char method[] = "POST";
if (wrapup_request(args, method, &code))
{
*error = 1;
}
if (code)
{
*error = 1;
}
return code;
}

my_bool send_put_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return wrapped_init(initid, args, message);
}

my_bool send_put_deinit(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return wrapped_deinit(initid, args, message);
}

long long send_put(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
CURLcode code = 1;
char method[] = "PUT";
if (wrapup_request(args, method, &code))
{
*error = 1;
}
if (code)
{
*error = 1;
}
return code;
}

my_bool send_delete_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return wrapped_init(initid, args, message);
}

my_bool send_delete_deinit(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return wrapped_deinit(initid, args, message);
}

long long send_delete(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
CURLcode code = 1;
char method[] = "DELETE";
if (wrapup_request(args, method, &code))
{
*error = 1;
}
if (code)
{
*error = 1;
}
return code;
}

my_bool wrapped_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if (args->arg_count < 2)
{
strcpy(message, "Function requires >=2 arguments");
return 1;
}
if (args->arg_type[0] != STRING_RESULT)
{
strcpy(message, "Function requires [addr:string]");
return 1;
}
return 0;
}

my_bool wrapped_deinit(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if (initid->ptr)
{
free(initid->ptr);
initid->ptr = NULL;
}
return 0;
return 0;
}

void json_string(char *res_str, char *attr_name, char *arg, enum Item_result arg_type, int trail_com)
Expand Down Expand Up @@ -125,7 +203,8 @@ void encapsulate_data(UDF_ARGS* udf_args, char** res_str)
*res_str = (char *)calloc(res_len + 3, sizeof(char));
**res_str = '{';
unsigned long current_pos = 1;
for(int i = 0; i < num_of_args; i++){
for(int i = 0; i < num_of_args; i++)
{
strcpy((*res_str + current_pos), char_args[i]);
current_pos += strlen(char_args[i]);
free(char_args[i]);
Expand All @@ -134,28 +213,28 @@ void encapsulate_data(UDF_ARGS* udf_args, char** res_str)
(*res_str)[res_len + 2] = '\0';
}

long long send_post(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
int wrapup_request(UDF_ARGS *args, const char *method, CURLcode *code)
{
curl_global_init(CURL_GLOBAL_ALL);
char* addr = args->args[0];
char* json = NULL;
CURL *handle = curl_easy_init();
char *addr = args->args[0];
char *json = NULL;
encapsulate_data(args, &json);
if(json == NULL) {
*error = 1;
return 0;
if (json == NULL || handle == NULL)
{
return 1;
}
CURL* handle = curl_easy_init();
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Content-Type: application/json");
list = curl_slist_append(list, "accept: application/json");
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(handle, CURLOPT_URL, addr);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, json);
CURLcode code = curl_easy_perform(handle);
if(code != 0)
curl_easy_setopt(handle, CURLOPT_URL, addr);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, json);
if(list == NULL)
{
*error = 1;
}
curl_global_cleanup();
return code;
return 1;
}
*code = curl_easy_perform(handle);
curl_global_cleanup();
return 0;
}
22 changes: 20 additions & 2 deletions src/libmsqlcurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef UNTITLED2_LIBRARY_H
#define UNTITLED2_LIBRARY_H
#ifndef LIBMYSQLCURL_LIBRARY_H
#define LIBMYSQLCURL_LIBRARY_H
#include <curl/curl.h>
#include <mysql.h>

Expand All @@ -32,6 +32,24 @@ my_bool send_post_deinit(UDF_INIT *, UDF_ARGS *, char *);

long long send_post(UDF_INIT *, UDF_ARGS *, char *, char *);

my_bool send_put_init(UDF_INIT *, UDF_ARGS *, char *);

my_bool send_put_deinit(UDF_INIT *, UDF_ARGS *, char *);

long long send_put(UDF_INIT *, UDF_ARGS *, char *, char *);

my_bool send_delete_init(UDF_INIT *, UDF_ARGS *, char *);

my_bool send_delete_deinit(UDF_INIT *, UDF_ARGS *, char *);

long long send_delete(UDF_INIT *, UDF_ARGS *, char *, char *);

my_bool wrapped_init(UDF_INIT *, UDF_ARGS *, char *);

my_bool wrapped_deinit(UDF_INIT *, UDF_ARGS *, char *);

int wrapup_request(UDF_ARGS *, const char *, CURLcode *);

void encapsulate_data(UDF_ARGS *, char **);

void json_string(char *, char *, char *, enum Item_result, int);
Expand Down
6 changes: 5 additions & 1 deletion src/libmsqlcurl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

DROP FUNCTION IF EXISTS send_post;
DROP FUNCTION IF EXISTS send_put;
DROP FUNCTION IF EXISTS send_delete;

CREATE FUNCTION send_post RETURNS integer SONAME 'libmsqlcurl.so';
CREATE FUNCTION send_post RETURNS integer SONAME 'libmsqlcurl.so';
CREATE FUNCTION send_put RETURNS integer SONAME 'libmsqlcurl.so';
CREATE FUNCTION send_delete RETURNS integer SONAME 'libmsqlcurl.so';

0 comments on commit a9d7ab6

Please sign in to comment.