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,GLib.MemoryInputStream> cache;
29 public static factory(string fname)
32 cache = new Gee.HashMap<string,GLib.MemoryInputStream>();
34 if (cache.has_key(fn)) {
42 public class FakeServer : Object
46 public FakeServer(WebKit.WebView wkview)
56 //this.view.resource_load_started.connect(on_resource_request_starting);
57 //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
58 //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
61 this.view.get_context().register_uri_scheme("xhttp", serve);
66 public void serve(WebKit.URISchemeRequest request)
68 // request is URISchemeRequest
70 print("REQ: %s\n",request.get_path());
73 var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + request.get_path());
74 if (!file.query_exists()) {
75 print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
76 request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
79 var info = file.query_info(
81 FileQueryInfoFlags.NONE
84 if (!cache.has_key(file.get_path())) {
89 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
91 var stream = new GLib.MemoryInputStream.from_data (data.data, GLib.free);
92 cache.set(file.get_path(), stream);
95 // we could cache these memory streams... so no need to keep reading from disk...
96 // then what happens if file get's updated - neet to check the data against the cache..
99 print("Sending %s (%s:%s)\n", request.get_path(), info.get_size().to_string(), info.get_content_type());
101 request.finish ( cache.get(request.get_path()), info.get_size() , info.get_content_type());