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