src/Builder4/FakeServer.vala
[app.Builder.js] / src / Builder4 / FakeServer.vala
1 /**
2  * Idea is to serve the files from the file system, so no need to setup apache etc...
3  * This should work for the static content like css / javascript etc.. but 
4  * will cause issues with 'dynamic' xhr files (eg. the php stuff)
5  *
6  * the idea is nicked from geary.
7  * 
8  */
9
10 public class FakeServer : Object
11 {
12         WebKit.WebView view;
13         
14         public FakeServer(WebKit.WebView wkview)
15         {
16                 this.view = wkview;
17                 // 
18                 
19                   
20         // Hook up signals.
21   
22         this.view.resource_load_started.connect(on_resource_request_starting);
23         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
24         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
25           
26          // 
27          this.view.get_context().register_uri_scheme("xhttp",  serve);
28         
29         }
30         /*
31         private bool on_navigation_policy_decision_requested(
32                 WebKit.WebFrame frame,
33         WebKit.NetworkRequest request,
34         WebKit.WebNavigationAction navigation_action,
35         WebKit.WebPolicyDecision policy_decision
36         ) {
37         policy_decision.ignore();
38         
39         // not sure if we should allow navigations...
40         return true;
41     }
42     */
43     private void on_resource_request_starting(
44                 WebKit.WebResource resource, 
45                 WebKit.URIRequest request) {
46         if (resource == null) {
47             // A request that was previously approved resulted in a redirect.
48             return;
49         }
50
51         string? uri = request.get_uri();
52         
53         if (uri == null) {
54                         return;
55                 }
56                 print("%s\n",uri);
57                 if (Regex.match_simple ("\\.php", uri)) {
58                         return;
59                 }
60          
61         request.set_uri("x"+ uri);
62            
63     }
64     public void serve(WebKit.URISchemeRequest request)
65     { 
66                 // request is URISchemeRequest
67                          
68                 print(request.get_path());
69                          
70                 var  file = File.new_for_path ("/home/alan/gitlive/" + request.get_path());
71                 var stream = file.read();
72                 var info = file.query_info(
73                                  "standard::*",
74                                 FileQueryInfoFlags.NONE
75                 );
76                 
77                 request.finish (  stream, info.get_size(), info.get_content_type());
78         }
79 }