Changeset: 7f919b96f320 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=7f919b96f320
Modified Files:
        monetdb5/mal/mal_http_daemon.c
        monetdb5/modules/atoms/mcurl.c
        monetdb5/modules/atoms/mcurl.mal
        sql/backends/monet5/rest/rest_jsonstore.c
Branch: default
Log Message:

small fixes and start implementing other curl wrappers


diffs (truncated from 354 to 300 lines):

diff --git a/monetdb5/mal/mal_http_daemon.c b/monetdb5/mal/mal_http_daemon.c
--- a/monetdb5/mal/mal_http_daemon.c
+++ b/monetdb5/mal/mal_http_daemon.c
@@ -67,7 +67,7 @@ send_page (struct MHD_Connection *connec
        rest = (*http_handler)(url, method, &page, postdata);
        (void)rest;
        response =
-               MHD_create_response_from_buffer (strlen (page), 
+               MHD_create_response_from_buffer (strlen (page),
                                                 (void *) page,
                                                 MHD_RESPMEM_MUST_COPY);
        if (!response)
@@ -105,8 +105,7 @@ iterate_post (void *coninfo_cls, enum MH
 
                        snprintf (answerstring, MAXANSWERSIZE, "%s", data);
                        con_info->answerstring = answerstring;
-        }
-               else
+               } else
                        con_info->answerstring = NULL;
 
                return MHD_NO;
@@ -148,8 +147,11 @@ answer_to_connection (void *cls, struct 
        char * page = NULL;
        struct connection_info_struct *con_info;
        int *done = cls;
+       char *answerstring = NULL;
        char *errorpage =
-               "<html><body>This doesn't seem to be right.</body></html>";
+               "<html><body>"
+               "Failed to handle error in http request."
+               "</body></html>";
 
        (void)version;
 
@@ -161,8 +163,10 @@ answer_to_connection (void *cls, struct 
 
                if (strcmp (method, "POST") == 0) {
                        con_info->postprocessor =
-                               MHD_create_post_processor (connection, 
POSTBUFFERSIZE,
-                                                          iterate_post, (void 
*) con_info);
+                               MHD_create_post_processor (connection,
+                                                          POSTBUFFERSIZE,
+                                                          iterate_post,
+                                                          (void *) con_info);
 
                        if (con_info->postprocessor == NULL) {
                                free (con_info);
@@ -191,17 +195,15 @@ answer_to_connection (void *cls, struct 
 
        if (strcmp (method, "PUT") == 0) {
                if (*upload_data_size != 0) {
-                       char *answerstring;
                        // TODO: check free answerstring
                        answerstring = malloc (MAXANSWERSIZE);
                        if (!answerstring)
                                return MHD_NO;
 
                        snprintf (answerstring, MAXANSWERSIZE, "%s", 
upload_data);
-                       con_info->answerstring = answerstring;
                        *upload_data_size = 0;
                        return send_page(connection, url, method, page,
-                                        con_info->answerstring);
+                                        answerstring);
                }
                *done = 1;
        }
diff --git a/monetdb5/modules/atoms/mcurl.c b/monetdb5/modules/atoms/mcurl.c
--- a/monetdb5/modules/atoms/mcurl.c
+++ b/monetdb5/modules/atoms/mcurl.c
@@ -125,6 +125,203 @@ handle_get_request(str *retval, str *url
        *retval = d;
        return msg;
 }
+
+static str
+handle_put_request(str *retval, str *url)
+{
+       str d = NULL;
+       str msg = MAL_SUCCEED;
+
+       CURL *curl_handle;
+       CURLcode res = CURLE_OK;
+
+       struct MemoryStruct chunk;
+
+       chunk.memory = malloc(1);  /* will be grown as needed by the realloc 
above */
+       chunk.size = 0;    /* no data at this point */
+
+       curl_global_init(CURL_GLOBAL_ALL);
+       /* init the curl session */
+       curl_handle = curl_easy_init();
+       curl_easy_setopt(curl_handle, CURLOPT_PUT, 1);
+       /* set URL to get */
+
+       curl_easy_setopt(curl_handle, CURLOPT_URL, *url);
+
+       /* no progress meter please */
+       curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
+
+       /* send all data to this function  */
+       curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, 
WriteMemoryCallback);
+
+       /* we want the body be written to this file handle instead of stdout */
+       curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
+
+       /* get it! */
+       res = curl_easy_perform(curl_handle);
+
+       /* check for errors */
+       if(res != CURLE_OK) {
+               msg = createException(MAL, "mcurl.deleterequest",
+                                                         "curl_easy_perform() 
failed: %s\n", curl_easy_strerror(res));
+       } else {
+               /*
+                * Now, our chunk.memory points to a memory block that is
+                * chunk.size bytes big and contains the remote file.
+                *
+                * Do something nice with it!
+                *
+                * You should be aware of the fact that at this point we might
+                * have an allocated data block, and nothing has yet
+                * deallocated that data. So when you're done with it, you
+                * should free() it as a nice application.
+                */
+
+               //printf(SZFMT " bytes retrieved\n", chunk.size);
+       }
+       if (chunk.size) {
+               d = GDKstrdup(chunk.memory);
+               if(chunk.memory)
+                       free(chunk.memory);
+       }
+       /* cleanup curl stuff */
+       curl_easy_cleanup(curl_handle);
+
+       *retval = d;
+       return msg;
+}
+
+static str
+handle_post_request(str *retval, str *url)
+{
+       str d = NULL;
+       str msg = MAL_SUCCEED;
+
+       CURL *curl_handle;
+       CURLcode res = CURLE_OK;
+
+       struct MemoryStruct chunk;
+
+       chunk.memory = malloc(1);  /* will be grown as needed by the realloc 
above */
+       chunk.size = 0;    /* no data at this point */
+
+       curl_global_init(CURL_GLOBAL_ALL);
+       /* init the curl session */
+       curl_handle = curl_easy_init();
+       curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
+       /* set URL to get */
+
+       curl_easy_setopt(curl_handle, CURLOPT_URL, *url);
+
+       /* no progress meter please */
+       curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
+
+       /* send all data to this function  */
+       curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, 
WriteMemoryCallback);
+
+       /* we want the body be written to this file handle instead of stdout */
+       curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
+
+       /* get it! */
+       res = curl_easy_perform(curl_handle);
+
+       /* check for errors */
+       if(res != CURLE_OK) {
+               msg = createException(MAL, "mcurl.deleterequest",
+                                                         "curl_easy_perform() 
failed: %s\n", curl_easy_strerror(res));
+       } else {
+               /*
+                * Now, our chunk.memory points to a memory block that is
+                * chunk.size bytes big and contains the remote file.
+                *
+                * Do something nice with it!
+                *
+                * You should be aware of the fact that at this point we might
+                * have an allocated data block, and nothing has yet
+                * deallocated that data. So when you're done with it, you
+                * should free() it as a nice application.
+                */
+
+               //printf(SZFMT " bytes retrieved\n", chunk.size);
+       }
+       if (chunk.size) {
+               d = GDKstrdup(chunk.memory);
+               if(chunk.memory)
+                       free(chunk.memory);
+       }
+       /* cleanup curl stuff */
+       curl_easy_cleanup(curl_handle);
+
+       *retval = d;
+       return msg;
+}
+
+static str
+handle_delete_request(str *retval, str *url)
+{
+       str d = NULL;
+       str msg = MAL_SUCCEED;
+
+       CURL *curl_handle;
+       CURLcode res = CURLE_OK;
+       char * delete_request = "DELETE";
+
+       struct MemoryStruct chunk;
+
+       chunk.memory = malloc(1);  /* will be grown as needed by the realloc 
above */
+       chunk.size = 0;    /* no data at this point */
+
+       curl_global_init(CURL_GLOBAL_ALL);
+       /* init the curl session */
+       curl_handle = curl_easy_init();
+       curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, delete_request);
+       /* set URL to get */
+
+       curl_easy_setopt(curl_handle, CURLOPT_URL, *url);
+
+       /* no progress meter please */
+       curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
+
+       /* send all data to this function  */
+       curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, 
WriteMemoryCallback);
+
+       /* we want the body be written to this file handle instead of stdout */
+       curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
+
+       /* get it! */
+       res = curl_easy_perform(curl_handle);
+
+       /* check for errors */
+       if(res != CURLE_OK) {
+               msg = createException(MAL, "mcurl.deleterequest",
+                                                         "curl_easy_perform() 
failed: %s\n", curl_easy_strerror(res));
+       } else {
+               /*
+                * Now, our chunk.memory points to a memory block that is
+                * chunk.size bytes big and contains the remote file.
+                *
+                * Do something nice with it!
+                *
+                * You should be aware of the fact that at this point we might
+                * have an allocated data block, and nothing has yet
+                * deallocated that data. So when you're done with it, you
+                * should free() it as a nice application.
+                */
+
+               //printf(SZFMT " bytes retrieved\n", chunk.size);
+       }
+       if (chunk.size) {
+               d = GDKstrdup(chunk.memory);
+               if(chunk.memory)
+                       free(chunk.memory);
+       }
+       /* cleanup curl stuff */
+       curl_easy_cleanup(curl_handle);
+
+       *retval = d;
+       return msg;
+}
+
 #endif
 
 #ifdef WIN32
@@ -134,6 +331,9 @@ handle_get_request(str *retval, str *url
 #endif
 
 mcurl_export str CURLgetRequest(str *retval, str *url);
+mcurl_export str CURLputRequest(str *retval, str *url);
+mcurl_export str CURLpostRequest(str *retval, str *url);
+mcurl_export str CURLdeleteRequest(str *retval, str *url);
 
 str
 CURLgetRequest(str *retval, str *url)
@@ -146,3 +346,39 @@ CURLgetRequest(str *retval, str *url)
        return MAL_SUCCEED;
 #endif
 }
+
+str
+CURLputRequest(str *retval, str *url)
+{
+#ifdef HAVE_CURL
+       return handle_put_request(retval, url);
+#else
+       (void)retval;
+       (void)url;
+       return MAL_SUCCEED;
+#endif
+}
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to