[moriarty] r322 committed - added support for gzip compression

0 views
Skip to first unread message

mori...@googlecode.com

unread,
Sep 9, 2011, 7:10:41 PM9/9/11
to moriarty...@googlegroups.com
Revision: 322
Author: k.j.w.a...@gmail.com
Date: Fri Sep 9 16:09:37 2011
Log: added support for gzip compression
http://code.google.com/p/moriarty/source/detail?r=322

Modified:
/trunk/graph.class.php
/trunk/httprequest.class.php
/trunk/httprequestfactory.class.php
/trunk/httpresponse.class.php
/trunk/sparqlservicebase.class.php

=======================================
--- /trunk/graph.class.php Fri Sep 9 05:55:33 2011
+++ /trunk/graph.class.php Fri Sep 9 16:09:37 2011
@@ -39,7 +39,7 @@
* @return HttpResponse
*/
function apply_changeset($cs) {
- return $this->apply_changeset_rdfxml( $cs->to_rdfxml());
+ return $this->apply_changeset_turtle( $cs->to_turtle());
}

/**
@@ -50,6 +50,29 @@
function apply_versioned_changeset($cs) {
return $this->apply_versioned_changeset_rdfxml( $cs->to_rdfxml());
}
+
+ /**
+ * Apply a changeset to the graph
+ * @param string rdfxml the changeset to apply, serialised as RDF/XML
+ * @return HttpResponse
+ */
+ function apply_changeset_turtle($turtle) {
+ if (empty( $this->request_factory) ) {
+ $this->request_factory = new HttpRequestFactory();
+ }
+
+ $uri = $this->uri;
+
+ $request = $this->request_factory->make( 'POST', $uri,
$this->credentials);
+ $request->set_accept("*/*");
+ $request->set_content_type("application/vnd.talis.changeset+turtle");
+ $request->set_body( $turtle );
+
+ return $request->execute();
+ }
+
+
+

/**
* Apply a changeset to the graph
@@ -115,7 +138,7 @@
* @param string turtle the RDF to be submitted, serialised as Turtle
* @return HttpResponse
*/
- function submit_turtle($turtle) {
+ function submit_turtle($turtle, $gzip_encode=false) {
if (empty( $this->request_factory) ) {
$this->request_factory = new HttpRequestFactory();
}
@@ -123,11 +146,11 @@
$request = $this->request_factory->make( 'POST', $uri,
$this->credentials);
$request->set_content_type("text/turtle");
$request->set_accept("*/*");
- $request->set_body( $turtle );
+ $request->set_body( $turtle, $gzip_encode);
return $request->execute();
}

- function
submit_ntriples_in_batches_from_file($filename,$no_of_lines=500,
$stop_on_failure=true) {
+ function
submit_ntriples_in_batches_from_file($filename,$no_of_lines=500,
$callback=false) {
$responses = array();
$pointer = fopen($filename, 'r');
$batch = '';
@@ -138,16 +161,23 @@
if($lineCount==$no_of_lines){
$response = $this->submit_turtle($batch);
$responses[] = $response;
- if($response->is_success()===false AND $stop_on_failure){
+ if(is_callable($callback)){
+ call_user_func($callback, $response);
+ } else if($response->is_success()===false){
return $responses;
- } else {
+ }
+
$lineCount=0;
$batch='';
- }
+
}
}
if(!empty($batch)){
- $responses[]=$this->submit_turtle($batch);
+ $response=$this->submit_turtle($batch);
+ if(is_callable($callback)){
+ call_user_func($callback, $response);
+ }
+ $responses[]=$response;
}
return $responses;
}
=======================================
--- /trunk/httprequest.class.php Tue Aug 2 01:13:40 2011
+++ /trunk/httprequest.class.php Fri Sep 9 16:09:37 2011
@@ -133,7 +133,7 @@
$this->_async_key = $this->client->send_request($this);
}

- function get_async_response()
+ function get_async_response()
{
$response = null;
if ($this->_response_from_cache === null ||
$this->_always_validate_cache) {
@@ -202,8 +202,17 @@
* Set content to be sent with the request
* @param string val the content to be sent
*/
- function set_body($val) {
- $this->body = $val;
+ function set_body($val, $gzip_body=false) {
+ if($gzip_body) {
+ $val = gzencode($val);
+ $this->body = $val;
+ $this->headers['Content-Encoding'] = 'gzip';
+ $this->headers['Content-Length'] = strlen($val);
+ }
+ else
+ {
+ $this->body = $val;
+ }
}

/**
@@ -214,6 +223,14 @@
return $this->body;
}

+ /**
+ * Set the HTTP accept-encoding header for the request
+ * @param string val the media types to be used as the accept header
value
+ */
+ function set_accept_encoding($val) {
+ $this->headers['Accept-Encoding'] = $val;
+ }
+
/**
* Set the HTTP accept header for the request
* @param string val the media types to be used as the accept header
value
=======================================
--- /trunk/httprequestfactory.class.php Tue Aug 2 01:13:40 2011
+++ /trunk/httprequestfactory.class.php Fri Sep 9 16:09:37 2011
@@ -15,7 +15,7 @@
function __construct() {
if (defined('MORIARTY_HTTP_CACHE_READ_ONLY') ||
defined('MORIARTY_ALWAYS_CACHE_EVERYTHING') ) {
$this->always_validate_cache(FALSE);
- }
+ }
else {
$this->always_validate_cache(TRUE);
}
@@ -39,6 +39,7 @@

function make( $method, $uri, $credentials = null) {
$request = new HttpRequest( $method, $uri, $credentials );
+ $request->set_accept_encoding('gzip');
$request->always_validate_cache($this->_always_validate_cache);

$request->use_stale_response_on_failure($this->_use_stale_response_on_failure);
$request->set_proxy($this->_proxy);
=======================================
--- /trunk/httpresponse.class.php Tue Aug 2 01:13:40 2011
+++ /trunk/httpresponse.class.php Fri Sep 9 16:09:37 2011
@@ -26,7 +26,7 @@
* The entity body returned with the response
* @var string
*/
- var $body;
+ private $body;
/**
* The request that was responsible for generating this response
* @var HttpRequest
@@ -51,6 +51,27 @@
function __construct($status_code = null) {
$this->status_code = $status_code;
}
+
+ public function __get($k){
+ return $this->$k;
+ }
+ public function __set($k, $v){
+ if($k=='body' AND $this->content_is_gzip_encoded()){
+ //strip the gzip header
+ if(ord($v[0]) == 0x1f && ord($v[1]) == 0x8b)
+ {
+ $v = substr($v,10);
+ $v = gzinflate($v);
+ }
+ }
+ $this->$k=$v;
+ }
+
+ private function content_is_gzip_encoded(){
+ return (isset($this->headers['content-encoding'])
+ AND
+ strpos($this->headers['content-encoding'], 'gzip')!==false);
+ }

/**
* Tests whether the response indicates the request was successful
=======================================
--- /trunk/sparqlservicebase.class.php Tue Aug 2 01:13:40 2011
+++ /trunk/sparqlservicebase.class.php Fri Sep 9 16:09:37 2011
@@ -145,8 +145,7 @@
$request->set_content_type(MIME_FORMENCODED);
}

- return $request->execute();
-
+ return $request->execute();
}

function get_max_uri_length() {

Reply all
Reply to author
Forward
0 new messages