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 FakeServerCache : Object
21 {
22
23         public string data;
24         public string content_type;
25         public int64 size; 
26         public bool delete_after; 
27
28         public static Gee.HashMap<string,FakeServerCache> cache;
29         
30         public static FakeServerCache factory(string fname)
31         {
32             if (cache == null) {
33                 cache = new Gee.HashMap<string,FakeServerCache>();
34            }
35             if (cache.has_key(fname)) {
36                 return cache.get(fname);
37             }
38             var el = new  FakeServerCache(fname);
39  
40             cache.set(fname, el);
41             return el;
42         }
43
44
45         public FakeServerCache( string fname) {
46                this.delete_after = false;
47                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
48                 if (!file.query_exists()) {
49                     this.data = "";
50                     this.content_type = "";
51                     this.size = 0;
52                     return;
53                 }
54                  var info = file.query_info(
55                                  "standard::*",
56                                 FileQueryInfoFlags.NONE
57                 );
58             this.content_type = info.get_content_type();
59             this.size = info.get_size();
60             string data;
61             size_t length;
62             try { 
63                 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
64             } catch (Error e) {
65                 this.data = "";
66                 this.size = 0;
67                  this.content_type = "";
68                 return;
69             }
70
71             this.data = data;
72
73             print("FakeServerCache :%s, %s (%s/%d)\n", fname , 
74                   this.content_type, this.size.to_string(), this.data.length);
75             
76
77         }
78
79  
80         public void run(WebKit.URISchemeRequest request, Cancellable? cancellable) 
81         {
82             var stream =  new GLib.MemoryInputStream.from_data (this.data.data,  GLib.free);
83                 request.finish(stream,
84                          this.size,
85                          this.content_type);
86                  
87              
88             return;
89              
90         }
91 }
92
93 public class FakeServer : Object
94 {
95         WebKit.WebView view;
96         
97         public FakeServer(WebKit.WebView wkview)
98         {
99                 this.view = wkview;
100                 
101                 
102                 // 
103                 
104                   
105         // Hook up signals.
106   
107         //this.view.resource_load_started.connect(on_resource_request_starting);
108         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
109         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
110           
111          //
112         var cx = WebKit.WebContext.get_default();
113         //var cx = this.view.get_context();
114         cx.register_uri_scheme("xhttp",  serve);
115         cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
116        // = crash  cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
117     }
118     
119     
120     public void serve(WebKit.URISchemeRequest request)
121     { 
122                 // request is URISchemeRequest
123                          
124                 print("REQ: %s\n",request.get_path());
125                 var cdata = FakeServerCache.factory(request.get_path());
126         
127                 if (cdata.size < 1 ) {
128                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
129                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
130                         return;
131                 }
132         
133                 print("Send :%s, %s (%s/%d)", request.get_path(), 
134                       cdata.content_type, cdata.size.to_string(), cdata.data.length);
135                 cdata.run(request,    null);
136                  
137         }
138
139    
140 }