Add support for byterange requests in the storage layer.
[bluesky.git] / bluesky / store.c
index c2bb95e..5d9dc43 100644 (file)
@@ -105,6 +105,8 @@ BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store)
     async->op = STORE_OP_NONE;
     async->key = NULL;
     async->data = NULL;
+    async->start = async->len = 0;
+    async->range_done = FALSE;
     async->result = -1;
     async->notifiers = NULL;
     async->notifier_count = 0;
@@ -218,6 +220,29 @@ void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async)
         g_mutex_unlock(async->store->lock);
     }
 
+    /* If the request was a range request but the backend read the entire
+     * object, select out the appropriate bytes. */
+    if (async->op == STORE_OP_GET
+            && !async->range_done
+            && async->result == 0
+            && async->data != NULL) {
+        if (async->start != 0 || async->len != 0) {
+            /* If the caller requesteda read outside the object, return an
+             * error. */
+            if (async->start + async->len > async->data->len) {
+                g_warning("Range request outside object boundaries!\n");
+                async->result = -1;
+            } else {
+                if (async->len == 0)
+                    async->len = async->data->len - async->start;
+                BlueSkyRCStr *newstr = bluesky_string_new(g_memdup(&async->data->data[async->start], async->len), async->len);
+                bluesky_string_unref(async->data);
+                async->data = newstr;
+                async->range_done = TRUE;
+            }
+        }
+    }
+
     async->status = ASYNC_COMPLETE;
     g_cond_broadcast(async->completion_cond);