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 if (cache.has_key(fname)) {
35 return cache.get(fname);
37 var el = new FakeServerCache(fname);
45 public FakeServerCache( string fname) {
46 var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
47 if (!file.query_exists()) {
49 this.content_type = "";
53 var info = file.query_info(
55 FileQueryInfoFlags.NONE
57 this.content_type = info.get_content_type();
58 this.size = info.get_size();
62 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
70 print("FakeServerCache :%s, %s (%s/%d)\n", fname ,
71 this.content_type, this.size.to_string(), this.data.length);
77 public InputStream? run_async( )
79 //var f = ensure_resource();
81 var stream = new GLib.MemoryInputStream.from_data (this.data.data, GLib.free);
85 private async InputStream? run_impl(Cancellable? cancellable) throws GLib.Error
87 SourceFunc callback = run_impl.callback;
88 InputStream? ret = null;
90 new Thread<void*>("builder-fake-webserver", () => {
94 ret = this.run_async();
101 // Schedule the callback in idle
102 Idle.add((owned)callback);
106 // Wait for it to finish, yield to caller
114 // Return the input stream
117 public void run(WebKit.URISchemeRequest request, Cancellable? cancellable)
119 run_impl.begin(cancellable, (obj, res) => {
120 InputStream? stream = null;
123 stream = this.run_impl.end(res);
125 request.finish_error(e);
127 if (stream == null) {
128 stream = new MemoryInputStream();
130 print("Send : %s (%s/%d)\n",
131 this.content_type, this.size.to_string(), this.data.length);
133 request.finish(stream,
142 public class FakeServer : Object
146 public FakeServer(WebKit.WebView wkview)
156 //this.view.resource_load_started.connect(on_resource_request_starting);
157 //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
158 //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
161 var cx = this.view.get_context();
162 cx.register_uri_scheme("xhttp", serve);
163 cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
168 public void serve(WebKit.URISchemeRequest request)
170 // request is URISchemeRequest
172 print("REQ: %s\n",request.get_path());
173 var cdata = FakeServerCache.factory(request.get_path());
175 if (cdata.size < 1 ) {
176 print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
177 request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
181 print("Send :%s, %s (%s/%d)", request.get_path(),
182 cdata.content_type, cdata.size.to_string(), cdata.data.length);
183 cdata.run(request, null);
184 //var stream = new GLib.MemoryInputStream.from_data (cdata.data.data, GLib.free);
186 // we could cache these memory streams... so no need to keep reading from disk...
187 // then what happens if file get's updated - neet to check the data against the cache..
191 //request.finish ( stream, cdata.size , cdata.content_type);