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;
26 public bool delete_after;
28 public static Gee.HashMap<string,FakeServerCache> cache;
30 public static FakeServerCache factory(string fname)
33 cache = new Gee.HashMap<string,FakeServerCache>();
35 if (cache.has_key(fname)) {
36 return cache.get(fname);
38 var el = new FakeServerCache(fname);
45 public FakeServerCache( string fname) {
46 this.delete_after = false;
47 var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
48 if (!file.query_exists()) {
50 this.content_type = "";
54 var info = file.query_info(
56 FileQueryInfoFlags.NONE
58 this.content_type = info.get_content_type();
59 this.size = info.get_size();
63 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
67 this.content_type = "";
73 print("FakeServerCache :%s, %s (%s/%d)\n", fname ,
74 this.content_type, this.size.to_string(), this.data.length);
80 public void run(WebKit.URISchemeRequest request, Cancellable? cancellable)
82 var stream = new GLib.MemoryInputStream.from_data (this.data.data, GLib.free);
83 request.finish(stream,
94 public class FakeServer : Object
98 public FakeServer(WebKit.WebView wkview)
108 //this.view.resource_load_started.connect(on_resource_request_starting);
109 //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
110 //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
113 var cx = WebKit.WebContext.get_default();
114 //var cx = this.view.get_context();
115 cx.register_uri_scheme("xhttp", serve);
116 cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
117 // = crash cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
121 public void serve(WebKit.URISchemeRequest request)
123 // request is URISchemeRequest
125 print("REQ: %s\n",request.get_path());
126 var cdata = FakeServerCache.factory(request.get_path());
128 if (cdata.size < 1 ) {
129 print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
130 request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
134 print("Send :%s, %s (%s/%d)", request.get_path(),
135 cdata.content_type, cdata.size.to_string(), cdata.data.length);
136 cdata.run(request, null);
137 //var stream = new GLib.MemoryInputStream.from_data (cdata.data.data, GLib.free);
139 // we could cache these memory streams... so no need to keep reading from disk...
140 // then what happens if file get's updated - neet to check the data against the cache..
144 //request.finish ( stream, cdata.size , cdata.content_type);