2 * Originally this was supposed to intercept http calls and redirect them
3 * but that is not supported in webkit2 (without using the extension api)
5 * so for now we have modified our server to serve use a base url of xhttp:
7 * so all relative urls are based on that
9 * Idea is to serve the files from the file system, so no need to setup apache etc...
10 * This should work for the static content like css / javascript etc.. but
11 * will cause issues with 'dynamic' xhr files (eg. the php stuff)
13 * the idea is nicked from geary.
16 public errordomain FakeServerError {
20 public class FakeServerCache : Object
24 public string content_type;
27 public static Gee.HashMap<string,FakeServerCache> cache;
29 public static FakeServerCache factory(string fname)
32 cache = new Gee.HashMap<string,FakeServerCache>();
34 print ("CACHE look for ==%s==\n", fname);
35 if (cache.has_key(fname)) {
36 print ("CACHE got %s\n", fname);
37 return cache.get(fname);
39 print ("CACHE create %s\n", fname);
41 var el = new FakeServerCache(fname);
46 // called onload to clear the temporary cached file..
47 public static void remove(string fname) {
51 if (!cache.has_key(fname)) {
57 print("CACHE delete %s\n", this.fname);
58 cache.unset(fname, out v);
64 public static FakeServerCache factory_with_data(string data) {
66 cache = new Gee.HashMap<string,FakeServerCache>();
68 var el = new FakeServerCache.with_data(data);
69 print("CACHE - store %s\n", el.fname);
70 cache.set(el.fname, el);
74 public FakeServerCache.with_data( string data ) {
75 this.fname = "/" + GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, data, data.length) + ".js";
77 this.content_type = "text/javascript";
78 this.size= data.length;
83 public FakeServerCache( string fname ) {
89 var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
90 if (!file.query_exists()) {
92 this.content_type = "";
96 var info = file.query_info(
98 FileQueryInfoFlags.NONE
100 this.content_type = info.get_content_type();
101 this.size = info.get_size();
105 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
109 this.content_type = "";
115 print("FakeServerCache :%s, %s (%s/%d)\n", fname ,
116 this.content_type, this.size.to_string(), this.data.length);
122 public void run(WebKit.URISchemeRequest request, Cancellable? cancellable)
124 var stream = new GLib.MemoryInputStream.from_data (this.data.data, GLib.free);
125 request.finish(stream,
136 public class FakeServer : Object
140 public FakeServer(WebKit.WebView wkview)
150 //this.view.resource_load_started.connect(on_resource_request_starting);
151 //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
152 //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
155 var cx = WebKit.WebContext.get_default();
156 //var cx = this.view.get_context();
157 cx.register_uri_scheme("xhttp", serve);
158 cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
159 // = crash cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
163 public void serve(WebKit.URISchemeRequest request)
165 // request is URISchemeRequest
167 print("REQ: %s\n",request.get_path());
168 var cdata = FakeServerCache.factory(request.get_path());
170 if (cdata.size < 1 ) {
171 print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
172 request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
176 print("Send :%s, %s (%s/%d)", request.get_path(),
177 cdata.content_type, cdata.size.to_string(), cdata.data.length);
178 cdata.run(request, null);