src/Builder4/FakeServer.vala
[app.Builder.js] / src / Builder4 / FakeServer.vala
1 /**
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)
4  * 
5  * so for now we have modified our server to serve use a base url of xhttp:
6  * 
7  * so all relative urls are based on that 
8  * 
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)
12  *
13  * the idea is nicked from geary.
14  * 
15  */
16 public errordomain FakeServerError {
17         FILE_DOES_NOT_EXIST
18 }
19
20 public class FakeServerCache : Object
21 {
22         public string fname;
23         public string data;
24         public string content_type;
25         public int64 size; 
26         public bool delete_after; 
27
28         public static Gee.HashMap<string,FakeServerCache> cache;
29         
30         public static FakeServerCache factory(string fname)
31         {
32             if (cache == null) {
33                 cache = new Gee.HashMap<string,FakeServerCache>();
34            }
35             print ("CACHE look for %s\n", fname);
36             if (cache.has_key(fname)) {
37                 print ("CACHE got  %s\n", fname);
38                 return cache.get(fname);
39             }
40             print ("CACHE create  %s\n", fname);
41             
42             var el = new  FakeServerCache(fname);
43  
44             cache.set(fname, el);
45             return el;
46         }
47
48         public static FakeServerCache factory_with_data(string data) {
49              if (cache == null) {
50                 cache = new Gee.HashMap<string,FakeServerCache>();
51            }
52             var el = new  FakeServerCache.with_data(data);
53             print("CACHE - store %s\n", el.fname);
54              cache.set(el.fname, el);
55             return el;
56         }
57     
58         public FakeServerCache.with_data( string data ) {
59             this.fname = "/" + GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, data, data.length) + ".js";
60             this.data = data;
61             this.content_type = "text/javascript";
62             this.size= data.length;
63             this.delete_after = true;
64           
65         }
66
67         public FakeServerCache( string fname ) {
68                this.delete_after = false;
69                 this.fname = fname;
70
71
72             
73                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
74                 if (!file.query_exists()) {
75                     this.data = "";
76                     this.content_type = "";
77                     this.size = 0;
78                     return;
79                 }
80                  var info = file.query_info(
81                                  "standard::*",
82                                 FileQueryInfoFlags.NONE
83                 );
84             this.content_type = info.get_content_type();
85             this.size = info.get_size();
86             string data;
87             size_t length;
88             try { 
89                 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
90             } catch (Error e) {
91                 this.data = "";
92                 this.size = 0;
93                  this.content_type = "";
94                 return;
95             }
96
97             this.data = data;
98
99             print("FakeServerCache :%s, %s (%s/%d)\n", fname , 
100                   this.content_type, this.size.to_string(), this.data.length);
101             
102
103         }
104
105  
106         public void run(WebKit.URISchemeRequest request, Cancellable? cancellable) 
107         {
108             var stream =  new GLib.MemoryInputStream.from_data (this.data.data,  GLib.free);
109                 request.finish(stream,
110                          this.size,
111                          this.content_type);
112                  
113             if (this.delete_after) { 
114                 FakeServerCache v;
115                 cache.unset(this.fname, out v);
116             }
117
118                 
119             return;
120              
121         }
122 }
123
124 public class FakeServer : Object
125 {
126         WebKit.WebView view;
127         
128         public FakeServer(WebKit.WebView wkview)
129         {
130                 this.view = wkview;
131                 
132                 
133                 // 
134                 
135                   
136         // Hook up signals.
137   
138         //this.view.resource_load_started.connect(on_resource_request_starting);
139         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
140         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
141           
142          //
143         var cx = WebKit.WebContext.get_default();
144         //var cx = this.view.get_context();
145         cx.register_uri_scheme("xhttp",  serve);
146         cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
147        // = crash  cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
148     }
149     
150     
151     public void serve(WebKit.URISchemeRequest request)
152     { 
153                 // request is URISchemeRequest
154                          
155                 print("REQ: %s\n",request.get_path());
156                 var cdata = FakeServerCache.factory(request.get_path());
157         
158                 if (cdata.size < 1 ) {
159                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
160                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
161                         return;
162                 }
163         
164                 print("Send :%s, %s (%s/%d)", request.get_path(), 
165                       cdata.content_type, cdata.size.to_string(), cdata.data.length);
166                 cdata.run(request,    null);
167                  
168         }
169
170    
171 }