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  * At present this serves from ~/gitlive/****  - that probably needs more thought..
16  * 
17  * 
18  */
19 public errordomain FakeServerError {
20         FILE_DOES_NOT_EXIST
21 }
22
23 public class FakeServerCache : Object
24 {
25         public string fname;
26         public  uint8[]  data;
27         public string content_type;
28         public int64 size; 
29          
30         public static Gee.HashMap<string,FakeServerCache> cache;
31         
32         public static FakeServerCache factory(string fname, string scheme)
33         {
34                 if (cache == null) {
35                         cache = new Gee.HashMap<string,FakeServerCache>();
36                 }
37                 
38            // print ("CACHE look for ==%s==\n", fname);
39             if (scheme == "resources") {
40                         return new FakeServerCache.from_resource(fname);
41                 }
42             
43             if (cache.has_key(fname)) {
44                         print ("CACHE got  %s\n", fname);
45                         return cache.get(fname);
46                 }
47             print ("CACHE create %s\n", fname);
48             
49             var el = new  FakeServerCache(fname);
50  
51             cache.set(fname, el);
52             return el;
53         }
54         // called onload to clear the temporary cached file..
55         public static void remove(string fname) {
56                 if (cache == null) {
57                         return;
58                 }
59                 if (!cache.has_key(fname)) {
60                         return;
61                 }
62                 
63  
64             FakeServerCache v;
65  
66             cache.unset(fname, out v);
67             
68
69             
70         }
71         public static  void clear()
72         {
73                 if (cache == null) {
74                         return;
75                 }
76                 cache.clear();
77         }
78     
79         public static FakeServerCache factory_with_data(string data) {
80                 if (cache == null) {
81                         cache = new Gee.HashMap<string,FakeServerCache>();
82                 }
83                 var el = new  FakeServerCache.with_data(data);
84                 print("CACHE - store %s\n", el.fname);
85                 cache.set(el.fname, el);
86                 return el;
87         }
88     
89     
90     
91     
92         public FakeServerCache.with_data( string data )
93         {
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;
98          
99           
100         }
101         public FakeServerCache.from_resource( string fname )
102         {
103                 
104                 
105                 
106                 this.fname = fname;
107                 
108                 var  file = File.new_for_path ( BuilderApplication.configDirectory() + "/resources/" + fname);
109                 if (!file.query_exists()) {
110                         this.data = "".data;
111                         this.content_type = "";
112                         this.size = 0;
113                         return;
114                 }
115                 try {
116                     var info = file.query_info(
117                                      "standard::*",
118                                     FileQueryInfoFlags.NONE
119                     );
120                 
121                     this.content_type = info.get_content_type();
122                     this.size = info.get_size();
123                     uint8[] data;
124                      
125                      
126                     GLib.FileUtils.get_data(file.get_path(), out data);
127                     this.data = data
128                 } catch (Error e) {
129                         this.data = "".data;
130                         this.size = 0;
131                         this.content_type = "";
132                         return;
133                 }
134                 ;
135
136           
137         }
138         public FakeServerCache( string fname ) {
139                
140                 this.fname = fname;
141                 
142                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
143                 if (!file.query_exists()) {
144                         this.data = "".data;
145                         this.content_type = "";
146                         this.size = 0;
147                         return;
148                 }
149                 var info = file.query_info(
150                                  "standard::*",
151                                 FileQueryInfoFlags.NONE
152                 );
153                 this.content_type = info.get_content_type();
154                 this.size = info.get_size();
155                 uint8[] data;
156                 size_t length;
157                 try { 
158                         GLib.FileUtils.get_data(file.get_path(), out data);
159                 } catch (Error e) {
160                         this.data = "".data;
161                         this.size = 0;
162                         this.content_type = "";
163                         return;
164                 }
165
166                 this.data = data;
167
168                 print("FakeServerCache :%s, %s (%s/%d)\n", fname , 
169                         this.content_type, this.size.to_string(), this.data.length);
170             
171
172         }
173
174  
175         public void run(WebKit.URISchemeRequest request, Cancellable? cancellable) 
176         {
177                 var stream =  new GLib.MemoryInputStream.from_data (this.data,  GLib.free);
178                 print("SEND %s\nwe", this.size.to_string()); 
179
180                 request.finish(stream,
181                                          this.size,
182                                          this.content_type);
183                                  
184                 
185                 
186             return;
187              
188         }
189         
190     
191 }
192
193 public class FakeServer : Object
194 {
195         WebKit.WebView view;
196         
197         public FakeServer(WebKit.WebView wkview)
198         {
199                 this.view = wkview;
200                 
201                  
202                 var cx = WebKit.WebContext.get_default();
203                 //var cx = this.view.get_context();
204                 cx.register_uri_scheme("xhttp",  serve);
205                 cx.register_uri_scheme("resources",  serve);
206                 cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
207
208                 // these do not help for cross domain requests..
209                         
210                 //cx.get_security_manager().register_uri_scheme_as_cors_enabled("xhttp");
211                 //cx.get_security_manager().register_uri_scheme_as_cors_enabled("http");
212                 //cx.register_uri_scheme_as_cors_enabled("xhttp");
213        // = crash  cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
214     }
215     
216     
217     public void serve(WebKit.URISchemeRequest request)
218     { 
219                 // request is URISchemeRequest
220                          
221                 print("REQ: %s\n",request.get_path());
222                 var cdata = FakeServerCache.factory(request.get_path() , request.get_scheme());
223         
224                 if (cdata.size < 1 ) {
225                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
226                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
227                         return;
228                 }
229         
230                 print("Send :%s, %s (%s/%d)", request.get_path(), 
231                       cdata.content_type, cdata.size.to_string(), cdata.data.length);
232                 cdata.run(request,    null);
233                  
234         }
235
236    
237 }