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
23         public string data;
24         public string content_type;
25         public int64 size; 
26
27         public static Gee.HashMap<string,GLib.MemoryInputStream> cache;
28         
29         public static factory(string fname)
30         {
31             if (cache == null) {
32                 cache = new Gee.HashMap<string,GLib.MemoryInputStream>();
33             }
34             if (cache.has_key(fn)) {
35                 return cache.get(fn);
36             }
37             var el = new  FakeServerCache(fn);
38  
39              
40             cache.set(fn, el);
41             return el;
42         }
43
44
45         public FakeServerCache( string fname) {
46                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
47                 if (!file.query_exists()) {
48                     this.data = "";
49                     this.content_type = "";
50                     this.size = 0;
51                     return;
52                 }
53                  var info = file.query_info(
54                                  "standard::*",
55                                 FileQueryInfoFlags.NONE
56                 );
57             this.content_type = info.get_content_type();
58             this.size = info.get_size();
59             string data;
60             size_t length;
61             GLib.FileUtils.get_contents(file.get_path(), out data, out length);
62             this.data = data;
63             
64
65         }
66 }
67
68 public class FakeServer : Object
69 {
70         WebKit.WebView view;
71         
72         public FakeServer(WebKit.WebView wkview)
73         {
74                 this.view = wkview;
75                 
76                 
77                 // 
78                 
79                   
80         // Hook up signals.
81   
82         //this.view.resource_load_started.connect(on_resource_request_starting);
83         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
84         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
85           
86          // 
87          this.view.get_context().register_uri_scheme("xhttp",  serve);
88         
89     }
90     
91     
92     public void serve(WebKit.URISchemeRequest request)
93     { 
94                 // request is URISchemeRequest
95                          
96                 print("REQ: %s\n",request.get_path());
97                 
98                 
99                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + request.get_path());
100                 if (!file.query_exists()) {
101                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
102                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
103                         return;
104                 }
105                 
106
107                 if (!cache.has_key(file.get_path())) {
108                     
109         
110                     string data;
111                     size_t length;
112                     GLib.FileUtils.get_contents(file.get_path(), out data, out length);
113  
114                     var stream = new GLib.MemoryInputStream.from_data (data.data,  GLib.free);
115                     cache.set(file.get_path(), stream);
116                 }
117                 
118                 // we could cache these memory streams... so no need to keep reading from disk...
119                 // then what happens if file get's updated - neet to check the data against the cache..
120                 
121                 
122                 print("Sending %s (%s:%s)\n", request.get_path(), info.get_size().to_string(), info.get_content_type());
123                 
124                 request.finish (  cache.get(request.get_path()), info.get_size()  , info.get_content_type());
125                 //stream.close();
126         }
127 }