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         if (uri == null) {
53                         return;
54                 }
55                 if (Regex.match_simple ("\\.php", uri)) {
56                         return;
57                 }
58          
59         request.set_uri("x"+ uri);
60            
61     }
62     public void serve(WebKit.URISchemeRequest request)
63     { 
64                 // request is URISchemeRequest
65                          
66                 print(request.path);
67                          
68                 var  file = File.new_for_path ("/home/alan/gitlive/" + request.path);
69                 var stream = file.read();
70                 var info = file.query_info(
71                                  "standard::*",
72                                 FileQueryInfoFlags.NONE
73                 );
74                 
75                 request.finish (  stream, info.get_size(), info.get_content_type());
76         }
77 }