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             if (cache.has_key(fname)) {
36                 return cache.get(fname);
37             }
38             var el = new  FakeServerCache(fname);
39  
40             cache.set(fname, el);
41             return el;
42         }
43
44         public static FakeServerCache factory_with_data(string data) {
45              if (cache == null) {
46                 cache = new Gee.HashMap<string,FakeServerCache>();
47            }
48             var el = new  FakeServerCache.with_data(data);
49              cache.set(el.fname, el);
50         }
51     
52         public FakeServerCache.with_data( string data ) {
53             this.fname = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, data, data.length) + ".js";
54             this.data = data;
55             this.content_type = "text/javascript";
56             this.size= data.length;
57             this.delete_after = true;
58           
59         }
60
61         public FakeServerCache( string fname ) {
62                this.delete_after = false;
63                 this.fname = fname;
64
65
66             
67                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
68                 if (!file.query_exists()) {
69                     this.data = "";
70                     this.content_type = "";
71                     this.size = 0;
72                     return;
73                 }
74                  var info = file.query_info(
75                                  "standard::*",
76                                 FileQueryInfoFlags.NONE
77                 );
78             this.content_type = info.get_content_type();
79             this.size = info.get_size();
80             string data;
81             size_t length;
82             try { 
83                 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
84             } catch (Error e) {
85                 this.data = "";
86                 this.size = 0;
87                  this.content_type = "";
88                 return;
89             }
90
91             this.data = data;
92
93             print("FakeServerCache :%s, %s (%s/%d)\n", fname , 
94                   this.content_type, this.size.to_string(), this.data.length);
95             
96
97         }
98
99  
100         public void run(WebKit.URISchemeRequest request, Cancellable? cancellable) 
101         {
102             var stream =  new GLib.MemoryInputStream.from_data (this.data.data,  GLib.free);
103                 request.finish(stream,
104                          this.size,
105                          this.content_type);
106                  
107             if (this.delete_after) { 
108                 FakeServerCache v;
109                 cache.unset(this.fname, out v);
110             }
111
112                 
113             return;
114              
115         }
116 }
117
118 public class FakeServer : Object
119 {
120         WebKit.WebView view;
121         
122         public FakeServer(WebKit.WebView wkview)
123         {
124                 this.view = wkview;
125                 
126                 
127                 // 
128                 
129                   
130         // Hook up signals.
131   
132         //this.view.resource_load_started.connect(on_resource_request_starting);
133         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
134         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
135           
136          //
137         var cx = WebKit.WebContext.get_default();
138         //var cx = this.view.get_context();
139         cx.register_uri_scheme("xhttp",  serve);
140         cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
141        // = crash  cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
142     }
143     
144     
145     public void serve(WebKit.URISchemeRequest request)
146     { 
147                 // request is URISchemeRequest
148                          
149                 print("REQ: %s\n",request.get_path());
150                 var cdata = FakeServerCache.factory(request.get_path());
151         
152                 if (cdata.size < 1 ) {
153                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
154                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
155                         return;
156                 }
157         
158                 print("Send :%s, %s (%s/%d)", request.get_path(), 
159                       cdata.content_type, cdata.size.to_string(), cdata.data.length);
160                 cdata.run(request,    null);
161                  
162         }
163
164    
165 }