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 FakeServer : Object
21 {
22         static Gee.HashMap<string,string> cache;
23         WebKit.WebView view;
24         
25         public FakeServer(WebKit.WebView wkview)
26         {
27                 this.view = wkview;
28                 if (cache == null) {
29                     cache = new Gee.HashMap<string,string>();
30                 }
31                 
32                 // 
33                 
34                   
35         // Hook up signals.
36   
37         //this.view.resource_load_started.connect(on_resource_request_starting);
38         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
39         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
40           
41          // 
42          this.view.get_context().register_uri_scheme("xhttp",  serve);
43         
44     }
45     
46     
47     public void serve(WebKit.URISchemeRequest request)
48     { 
49                 // request is URISchemeRequest
50                          
51                 print("REQ: %s\n",request.get_path());
52                 
53                 
54                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + request.get_path());
55                 if (!file.query_exists()) {
56                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
57                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
58                         return;
59                 }
60                 var info = file.query_info(
61                                  "standard::*",
62                                 FileQueryInfoFlags.NONE
63                 );
64                 
65                 string data;
66                 size_t length;
67                 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
68                 
69                 var stream = new MemoryInputStream.from_data (data.data,  GLib.free);
70                 
71                 // we could cache these memory streams... so no need to keep reading from disk...
72                 // then what happens if file get's updated - neet to check the data against the cache..
73                 
74                 
75                 print("Sending %s (%s:%s)\n", request.get_path(), info.get_size().to_string(), info.get_content_type());
76                 
77                 request.finish (  stream, info.get_size()  , info.get_content_type());
78                 //stream.close();
79         }
80 }