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  uint8[]  data;
24         public string content_type;
25         public int64 size; 
26          
27         public static Gee.HashMap<string,FakeServerCache> cache;
28         
29         public static FakeServerCache factory(string fname)
30         {
31                 if (cache == null) {
32                         cache = new Gee.HashMap<string,FakeServerCache>();
33                 }
34            // print ("CACHE look for ==%s==\n", fname);
35             if (cache.has_key(fname)) {
36                         print ("CACHE got  %s\n", fname);
37                         return cache.get(fname);
38                 }
39             print ("CACHE create %s\n", fname);
40             
41             var el = new  FakeServerCache(fname);
42  
43             cache.set(fname, el);
44             return el;
45         }
46         // called onload to clear the temporary cached file..
47         public static void remove(string fname) {
48                 if (cache == null) {
49                         return;
50                 }
51                 if (!cache.has_key(fname)) {
52                         return;
53                 }
54                 
55  
56             FakeServerCache v;
57  
58             cache.unset(fname, out v);
59             
60
61             
62         }
63         public static  void clear()
64         {
65                 if (cache == null) {
66                         return;
67                 }
68                 cache.clear();
69         }
70     
71         public static FakeServerCache factory_with_data(string data) {
72                 if (cache == null) {
73                         cache = new Gee.HashMap<string,FakeServerCache>();
74                 }
75                 var el = new  FakeServerCache.with_data(data);
76                 print("CACHE - store %s\n", el.fname);
77                 cache.set(el.fname, el);
78                 return el;
79         }
80     
81         public FakeServerCache.with_data( string data )
82         {
83                 this.fname = "/" + GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, data, data.length) + ".js";
84                 this.data = data.data;
85                 this.content_type = "text/javascript";
86                 this.size= data.length;
87          
88           
89         }
90
91         public FakeServerCache( string fname ) {
92                
93                 this.fname = fname;
94  
95                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
96                 if (!file.query_exists()) {
97                         this.data = "".data;
98                         this.content_type = "";
99                         this.size = 0;
100                         return;
101                 }
102                 var info = file.query_info(
103                                  "standard::*",
104                                 FileQueryInfoFlags.NONE
105                 );
106                 this.content_type = info.get_content_type();
107                 this.size = info.get_size();
108                 uint8[] data;
109                 size_t length;
110                 try { 
111                         GLib.FileUtils.get_data(file.get_path(), out data);
112                 } catch (Error e) {
113                         this.data = "".data;
114                         this.size = 0;
115                         this.content_type = "";
116                         return;
117                 }
118
119                 this.data = data;
120
121                 print("FakeServerCache :%s, %s (%s/%d)\n", fname , 
122                         this.content_type, this.size.to_string(), this.data.length);
123             
124
125         }
126
127  
128         public void run(WebKit.URISchemeRequest request, Cancellable? cancellable) 
129         {
130                 var stream =  new GLib.MemoryInputStream.from_data (this.data,  GLib.free);
131                 print("SEND %s\nwe", this.size.to_string()); 
132
133                 request.finish(stream,
134                                          this.size,
135                                          this.content_type);
136                                  
137                 
138                 
139             return;
140              
141         }
142         
143     
144 }
145
146 public class FakeServer : Object
147 {
148         WebKit.WebView view;
149         
150         public FakeServer(WebKit.WebView wkview)
151         {
152                 this.view = wkview;
153                 
154                 
155                 // 
156                 
157                   
158                 // Hook up signals.
159
160                 //this.view.resource_load_started.connect(on_resource_request_starting);
161                 //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
162                 //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
163                   
164                  //
165                 var cx = WebKit.WebContext.get_default();
166                 //var cx = this.view.get_context();
167                 cx.register_uri_scheme("xhttp",  serve);
168                 cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
169
170                 // these do not help for cross domain requests..
171                         
172                 //cx.get_security_manager().register_uri_scheme_as_cors_enabled("xhttp");
173                 //cx.get_security_manager().register_uri_scheme_as_cors_enabled("http");
174                 //cx.register_uri_scheme_as_cors_enabled("xhttp");
175        // = crash  cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
176     }
177     
178     
179     public void serve(WebKit.URISchemeRequest request)
180     { 
181                 // request is URISchemeRequest
182                          
183                 print("REQ: %s\n",request.get_path());
184                 var cdata = FakeServerCache.factory(request.get_path());
185         
186                 if (cdata.size < 1 ) {
187                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
188                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
189                         return;
190                 }
191         
192                 print("Send :%s, %s (%s/%d)", request.get_path(), 
193                       cdata.content_type, cdata.size.to_string(), cdata.data.length);
194                 cdata.run(request,    null);
195                  
196         }
197
198    
199 }