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.wkview.resource_request_starting.connect(on_resource_request_starting);
23         this.wkview.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
24         this.wkview.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
25           
26          // 
27          webcontext.register_uri_scheme("xhttp",  ( request) => {
28                          // request is URISchemeRequest
29         
30         }
31         
32         private bool on_navigation_policy_decision_requested(
33                 WebKit.WebFrame frame,
34         WebKit.NetworkRequest request,
35         WebKit.WebNavigationAction navigation_action,
36         WebKit.WebPolicyDecision policy_decision
37         ) {
38         policy_decision.ignore();
39         
40         // not sure if we should allow navigations...
41         return true;
42     }
43     
44     private void on_resource_request_starting(
45                 WebKit.WebFrame web_frame,
46         WebKit.WebResource web_resource, 
47         WebKit.NetworkRequest request,
48         WebKit.NetworkResponse? response) {
49         if (response != null) {
50             // A request that was previously approved resulted in a redirect.
51             return;
52         }
53
54         string? uri = request.get_uri();
55         if (uri == null) {
56                         return;
57                 }
58                 if (Regex.match_simple ("\.php", uri)) {
59                         return;
60                 }
61          
62         request.set_uri("x"+ uri);
63            
64     }
65     public void serve(Webkit.URISchemeRequest request)
66     { 
67                          // request is URISchemeRequest    
68                 var  file = File.new_for_path ("my-test.bin");
69                 var stream = file.read();
70                 var info = file.query_info(
71                                  "standard::*",
72                                 FileQueryInfoFlags.NONE
73                 );
74                 
75                 request.finish (InputStream stream, info.get_size(), info.get_content_type());
76                 
77 }