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
17 public class FakeServer : Object
18 {
19         WebKit.WebView view;
20         
21         public FakeServer(WebKit.WebView wkview)
22         {
23                 this.view = wkview;
24                 // 
25                 
26                   
27         // Hook up signals.
28   
29         //this.view.resource_load_started.connect(on_resource_request_starting);
30         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
31         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
32           
33          // 
34          this.view.get_context().register_uri_scheme("xhttp",  serve);
35         
36         }
37          
38     public void serve(WebKit.URISchemeRequest request)
39     { 
40                 // request is URISchemeRequest
41                          
42                 print("REQ: %s\n",request.get_path());
43                 
44                 
45                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + request.get_path());
46                 if (!file.query_exists()) {
47                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
48                         return;
49                 }
50                 var info = file.query_info(
51                                  "standard::*",
52                                 FileQueryInfoFlags.NONE
53                 );
54                 
55                 string data;
56                 size_t length;
57                 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
58                 
59                 
60                 print("Sending %s (%s:%s)\n", request.get_path(), info.get_size().to_string(), info.get_content_type());
61                 
62                 request.finish (  stream, info.get_size()  , info.get_content_type());
63                 //stream.close();
64         }
65 }