command/commit

Basics to load from a url

authorJan Wielemaker
Thu Mar 10 15:21:24 2011 +0100
committerJan Wielemaker
Thu Mar 10 15:21:24 2011 +0100
commit36eada48a8eb8617400f1daed4e63a827c9cb15b
treebeb8892ac51b4cc4bbd850b312d757017d7769ca
parent173e54bcf9427bf25e670789fc8426e672d2d480
Diff style: patch stat
diff --git a/api/command.pl b/api/command.pl
index 93ae579..98a74f0 100644
--- a/api/command.pl
+++ b/api/command.pl
@@ -29,6 +29,7 @@
 */
 
 :- module(api_command, []).
+:- use_module(library(semweb/rdf_db)).
 :- use_module(library(http/http_dispatch)).
 :- use_module(library(http/http_path)).
 :- use_module(library(http/http_parameters)).
@@ -42,6 +43,7 @@
 http:location(cmd, root(cmd), []).
 
 :- http_handler(cmd(ping), ping, []).
+:- http_handler(cmd(load), load, []).
 
 %%	ping(+Request)
 %
@@ -55,3 +57,44 @@ http:location(cmd, root(cmd), []).
 ping(_Request) :-
 	format('Content-type: text/plain\n\n'),
 	format('alive\n').
+
+
+%%	load(+Request)
+%
+%	HTTP handler to load data into ClioPatria
+
+load(Request) :-
+	memberchk(method(get), Request), !,
+	http_parameters(Request,
+			[ url(Data,
+			      [ description('URL of data to load')])
+			]),
+	text_output(rdf_load(Data), []).
+
+
+		 /*******************************
+		 *	       MESSAGE		*
+		 *******************************/
+
+:- meta_predicate
+	text_output(0, +).
+
+text_output(Goal, _Options) :-
+	format('Content-Type: text/plain~n'),
+	format('Transfer-Encoding: chunked~n~n'),
+	thread_self(Me),
+	setup_call_cleanup(asserta((user:message_hook(_Term, Level, Lines) :-
+				   	send_message(Me, Level, Lines)), Ref),
+			   catch(Goal, E, print_message(error, E)),
+			   erase(Ref)), !.
+
+send_message(Me, Level, Lines) :-
+	thread_self(Me),
+	prefix(Level, Prefix),
+	print_message_lines(current_output, Prefix, Lines),
+	flush_output,
+	fail.
+
+prefix(informational, '% ').
+prefix(warning, 'WARNING: ').
+prefix(error, 'ERROR: ').
diff --git a/script/cp-client.c b/script/cp-client.c
index 629a43f..9d9acb6 100644
--- a/script/cp-client.c
+++ b/script/cp-client.c
@@ -33,6 +33,7 @@ char *prefix  = "";
 char *host    = "localhost";
 int   port    = 3020;
 CURL *curl    = NULL;
+int   debug   = 0;
 
 #define FALSE 0
 #define TRUE 1
@@ -95,7 +96,7 @@ cmd_url(const char *cmd, ...)
 
   va_start(args, cmd);
   o += snprintf(o, end-o,
-		"http://%s:%d%s/cmd/%s\n", host, port, prefix, cmd);
+		"http://%s:%d%s/cmd/%s", host, port, prefix, cmd);
   for(;; an++)
   { argtype t = va_arg(args, argtype);
 
@@ -111,6 +112,8 @@ cmd_url(const char *cmd, ...)
 	o += snprintf(o, end-o, "%s%s=", an == 0 ? "?" : "&", param);
 	o += snprintf(o, end-o, "%s", e);
 	curl_free(e);
+
+	continue;
       }
       case A_LONG:
       { const char *param = va_arg(args, const char*);
@@ -118,6 +121,8 @@ cmd_url(const char *cmd, ...)
 
 	o += snprintf(o, end-o, "%s%s=", an == 0 ? "?" : "&", param);
 	o += snprintf(o, end-o, "%ld", value);
+
+	continue;
       }
     }
   }
@@ -153,9 +158,11 @@ ping()
 static int
 load_one(const char *data)
 { CURLcode rc;
+  const char *url = cmd_url("load", A_STRING, "url", data, A_END);
 
-  curl_easy_setopt(curl, CURLOPT_URL,
-		   cmd_url("load", A_STRING, "data", data, A_END));
+  if ( debug > 0 )
+    fprintf(stderr, "URL=%s\n", url);
+  curl_easy_setopt(curl, CURLOPT_URL, url);
   rc = curl_easy_perform(curl);
 
   if ( rc )