X-Git-Url: http://git.roojs.org/?p=app.Builder.js;a=blobdiff_plain;f=src%2FBuilder4%2FFakeServer.vala;h=b788213f138c0385af902d29520bd2aecd7a1262;hp=fa18c93a9f5de6a8cb763c3369f6e1ddee9bb2cb;hb=HEAD;hpb=e5e6029edd9bed1537aba70ae17a20ac455480ac diff --git a/src/Builder4/FakeServer.vala b/src/Builder4/FakeServer.vala index fa18c93a9..b788213f1 100644 --- a/src/Builder4/FakeServer.vala +++ b/src/Builder4/FakeServer.vala @@ -12,6 +12,9 @@ * * the idea is nicked from geary. * + * At present this serves from ~/gitlive/**** - that probably needs more thought.. + * + * */ public errordomain FakeServerError { FILE_DOES_NOT_EXIST @@ -19,130 +22,174 @@ public errordomain FakeServerError { public class FakeServerCache : Object { - - public string data; + public string fname; + public uint8[] data; public string content_type; public int64 size; - + public static Gee.HashMap cache; - public static FakeServerCache factory(string fname) + public static FakeServerCache factory(string fname, string scheme) { - if (cache == null) { - cache = new Gee.HashMap(); - } + if (cache == null) { + cache = new Gee.HashMap(); + } + + // print ("CACHE look for ==%s==\n", fname); + if (scheme == "resources") { + return new FakeServerCache.from_resource(fname); + } + if (cache.has_key(fname)) { - return cache.get(fname); - } + print ("CACHE got %s\n", fname); + return cache.get(fname); + } + print ("CACHE create %s\n", fname); + var el = new FakeServerCache(fname); - - cache.set(fname, el); + cache.set(fname, el); return el; } - - - public FakeServerCache( string fname) { - var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname); - if (!file.query_exists()) { - this.data = ""; - this.content_type = ""; - this.size = 0; - return; + // called onload to clear the temporary cached file.. + public static void remove(string fname) { + if (cache == null) { + return; } - var info = file.query_info( - "standard::*", - FileQueryInfoFlags.NONE - ); - this.content_type = info.get_content_type(); - this.size = info.get_size(); - string data; - size_t length; - try { - GLib.FileUtils.get_contents(file.get_path(), out data, out length); - } catch (Error e) { - this.data = ""; - this.size = 0; - this.content_type = ""; - return; - } - - this.data = data; - - print("FakeServerCache :%s, %s (%s/%d)\n", fname , - this.content_type, this.size.to_string(), this.data.length); + if (!cache.has_key(fname)) { + return; + } + + + FakeServerCache v; + + cache.unset(fname, out v); + } - - - public InputStream? run_async( ) + public static void clear() { - //var f = ensure_resource(); - - var stream = new GLib.MemoryInputStream.from_data (this.data.data, GLib.free); - - return stream; + if (cache == null) { + return; + } + cache.clear(); } - private async InputStream? run_impl(Cancellable? cancellable) throws GLib.Error + + public static FakeServerCache factory_with_data(string data) { + if (cache == null) { + cache = new Gee.HashMap(); + } + var el = new FakeServerCache.with_data(data); + print("CACHE - store %s\n", el.fname); + cache.set(el.fname, el); + return el; + } + + + + + public FakeServerCache.with_data( string data ) { - SourceFunc callback = run_impl.callback; - InputStream? ret = null; - Error? err = null; - new Thread("builder-fake-webserver", () => { - // Actually do it - try - { - ret = this.run_async(); - } - catch (Error e) - { - err = e; - } + this.fname = "/" + GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, data, data.length) + ".js"; + this.data = data.data; + this.content_type = "text/javascript"; + this.size= data.length; + + + } + public FakeServerCache.from_resource( string fname ) + { + + + + this.fname = fname; + + var file = File.new_for_path ( BuilderApplication.configDirectory() + "/resources/" + fname); + if (!file.query_exists()) { + this.data = "".data; + this.content_type = ""; + this.size = 0; + return; + } + try { + var info = file.query_info( + "standard::*", + FileQueryInfoFlags.NONE + ); + + this.content_type = info.get_content_type(); + this.size = info.get_size(); + uint8[] data; + + + GLib.FileUtils.get_data(file.get_path(), out data); + this.data = data; + } catch (Error e) { + this.data = "".data; + this.size = 0; + this.content_type = ""; + return; + } + - // Schedule the callback in idle - Idle.add((owned)callback); - return null; - }); + + } + public FakeServerCache( string fname ) { + + this.fname = fname; + + var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname); + if (!file.query_exists()) { + this.data = "".data; + this.content_type = ""; + this.size = 0; + return; + } + try { + var info = file.query_info( + "standard::*", + FileQueryInfoFlags.NONE + ); + this.content_type = info.get_content_type(); + this.size = info.get_size(); + uint8[] data; + + + GLib.FileUtils.get_data(file.get_path(), out data); + this.data = data; + } catch (Error e) { + this.data = "".data; + this.size = 0; + this.content_type = ""; + return; + } - // Wait for it to finish, yield to caller - yield; + - if (err != null) - { - throw err; - } + print("FakeServerCache :%s, %s (%s/%d)\n", fname , + this.content_type, this.size.to_string(), this.data.length); + - // Return the input stream - return ret; } - / + + public void run(WebKit.URISchemeRequest request, Cancellable? cancellable) { + var stream = new GLib.MemoryInputStream.from_data (this.data, GLib.free); + print("SEND %s\nwe", this.size.to_string()); - - - run_impl.begin(cancellable, (obj, res) => { - InputStream? stream = null; - - try { - stream = this.run_impl.end(res); - } catch (Error e) { - request.finish_error(e); - } - if (stream == null) { - stream = new MemoryInputStream(); - } - print("Send : %s (%s/%d)\n", - this.content_type, this.size.to_string(), this.data.length); - request.finish(stream, - this.size, - this.content_type); - - - }); + this.size, + this.content_type); + + + + return; + } + + } public class FakeServer : Object @@ -153,22 +200,19 @@ public class FakeServer : Object { this.view = wkview; - - // - - - // Hook up signals. - - //this.view.resource_load_started.connect(on_resource_request_starting); - //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested); - //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested); - - // - var cx = WebKit.WebContext.get_default(); - //var cx = this.view.get_context(); - cx.register_uri_scheme("xhttp", serve); - cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER); - + + var cx = WebKit.WebContext.get_default(); + //var cx = this.view.get_context(); + cx.register_uri_scheme("xhttp", serve); + cx.register_uri_scheme("resources", serve); + cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER); + + // these do not help for cross domain requests.. + + //cx.get_security_manager().register_uri_scheme_as_cors_enabled("xhttp"); + //cx.get_security_manager().register_uri_scheme_as_cors_enabled("http"); + //cx.register_uri_scheme_as_cors_enabled("xhttp"); + // = crash cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES ); } @@ -177,7 +221,7 @@ public class FakeServer : Object // request is URISchemeRequest print("REQ: %s\n",request.get_path()); - var cdata = FakeServerCache.factory(request.get_path()); + var cdata = FakeServerCache.factory(request.get_path() , request.get_scheme()); if (cdata.size < 1 ) { print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path()); @@ -188,15 +232,7 @@ public class FakeServer : Object print("Send :%s, %s (%s/%d)", request.get_path(), cdata.content_type, cdata.size.to_string(), cdata.data.length); cdata.run(request, null); - //var stream = new GLib.MemoryInputStream.from_data (cdata.data.data, GLib.free); - - // we could cache these memory streams... so no need to keep reading from disk... - // then what happens if file get's updated - neet to check the data against the cache.. - - - - //request.finish ( stream, cdata.size , cdata.content_type); - //stream.close(); + }