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.
15 * At present this serves from ~/gitlive/**** - that probably needs more thought..
19 public errordomain FakeServerError {
23 public class FakeServerCache : Object
27 public string content_type;
30 public static Gee.HashMap<string,FakeServerCache> cache;
32 public static FakeServerCache factory(string fname, string scheme)
35 cache = new Gee.HashMap<string,FakeServerCache>();
38 // print ("CACHE look for ==%s==\n", fname);
39 if (scheme == "resources") {
40 return new FakeServerCache.from_resource(fname);
43 if (cache.has_key(fname)) {
44 print ("CACHE got %s\n", fname);
45 return cache.get(fname);
47 print ("CACHE create %s\n", fname);
49 var el = new FakeServerCache(fname);
54 // called onload to clear the temporary cached file..
55 public static void remove(string fname) {
59 if (!cache.has_key(fname)) {
66 cache.unset(fname, out v);
71 public static void clear()
79 public static FakeServerCache factory_with_data(string data) {
81 cache = new Gee.HashMap<string,FakeServerCache>();
83 var el = new FakeServerCache.with_data(data);
84 print("CACHE - store %s\n", el.fname);
85 cache.set(el.fname, el);
92 public FakeServerCache.with_data( string data )
94 this.fname = "/" + GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, data, data.length) + ".js";
95 this.data = data.data;
96 this.content_type = "text/javascript";
97 this.size= data.length;
101 public FakeServerCache.from_resource( string fname )
108 var file = File.new_for_path ( BuilderApplication.configDirectory() + "/" + fname);
109 if (!file.query_exists()) {
111 this.content_type = "";
116 var info = file.query_info(
118 FileQueryInfoFlags.NONE
121 this.content_type = info.get_content_type();
122 this.size = info.get_size();
126 GLib.FileUtils.get_data(file.get_path(), out data);
130 this.content_type = "";
137 public FakeServerCache( string fname ) {
141 var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
142 if (!file.query_exists()) {
144 this.content_type = "";
148 var info = file.query_info(
150 FileQueryInfoFlags.NONE
152 this.content_type = info.get_content_type();
153 this.size = info.get_size();
157 GLib.FileUtils.get_data(file.get_path(), out data);
161 this.content_type = "";
167 print("FakeServerCache :%s, %s (%s/%d)\n", fname ,
168 this.content_type, this.size.to_string(), this.data.length);
174 public void run(WebKit.URISchemeRequest request, Cancellable? cancellable)
176 var stream = new GLib.MemoryInputStream.from_data (this.data, GLib.free);
177 print("SEND %s\nwe", this.size.to_string());
179 request.finish(stream,
192 public class FakeServer : Object
196 public FakeServer(WebKit.WebView wkview)
201 var cx = WebKit.WebContext.get_default();
202 //var cx = this.view.get_context();
203 cx.register_uri_scheme("xhttp", serve);
204 cx.register_uri_scheme("resources", serve);
205 cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
207 // these do not help for cross domain requests..
209 //cx.get_security_manager().register_uri_scheme_as_cors_enabled("xhttp");
210 //cx.get_security_manager().register_uri_scheme_as_cors_enabled("http");
211 //cx.register_uri_scheme_as_cors_enabled("xhttp");
212 // = crash cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
216 public void serve(WebKit.URISchemeRequest request)
218 // request is URISchemeRequest
220 print("REQ: %s\n",request.get_path());
221 var cdata = FakeServerCache.factory(request.get_path() , request.get_scheme());
223 if (cdata.size < 1 ) {
224 print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
225 request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
229 print("Send :%s, %s (%s/%d)", request.get_path(),
230 cdata.content_type, cdata.size.to_string(), cdata.data.length);
231 cdata.run(request, null);