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
27         public static Gee.HashMap<string,FakeServerCache> cache;
28         
29         public static FakeServerCache factory(string fname)
30         {
31             if (cache == null) {
32                 cache = new Gee.HashMap<string,FakeServerCache>();
33            }
34             if (cache.has_key(fname)) {
35                 return cache.get(fname);
36             }
37             var el = new  FakeServerCache(fname);
38  
39             cache.set(fname, el);
40             return el;
41         }
42
43
44         public FakeServerCache( string fname) {
45                 var  file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
46                 if (!file.query_exists()) {
47                     this.data = "";
48                     this.content_type = "";
49                     this.size = 0;
50                     return;
51                 }
52                  var info = file.query_info(
53                                  "standard::*",
54                                 FileQueryInfoFlags.NONE
55                 );
56             this.content_type = info.get_content_type();
57             this.size = info.get_size();
58             string data;
59             size_t length;
60             try { 
61                 GLib.FileUtils.get_contents(file.get_path(), out data, out length);
62             } catch (Error e) {
63                 this.data = "";
64                 this.size = 0;
65                  this.content_type = "";
66                 return;
67             }
68
69             this.data = data;
70
71             print("FakeServerCache :%s, %s (%s/%d)\n", fname , 
72                   this.content_type, this.size.to_string(), this.data.length);
73             
74
75         }
76
77
78         public   InputStream? run_async( ) 
79         {
80                 //var f = ensure_resource();
81
82                 var stream =  new GLib.MemoryInputStream.from_data (this.data.data,  GLib.free);
83
84                 return stream;
85         }
86         private async InputStream? run_impl(Cancellable? cancellable) throws GLib.Error
87         {
88             SourceFunc callback = run_impl.callback;
89             InputStream? ret = null;
90             Error? err = null;
91             new Thread<void*>("builder-fake-webserver", () => {
92                     // Actually do it
93                     try
94                     {
95                             ret = this.run_async();
96                     }
97                     catch (Error e)
98                     {
99                             err = e;
100                     }
101
102                     // Schedule the callback in idle
103                     Idle.add((owned)callback);
104                     return null;
105             });
106
107             // Wait for it to finish, yield to caller
108             yield;
109
110             if (err != null)
111             {
112                     throw err;
113             }
114
115             // Return the input stream
116             return ret;
117         }
118     
119         public void run(WebKit.URISchemeRequest request, Cancellable? cancellable) 
120         {
121             var stream =  new GLib.MemoryInputStream.from_data (this.data.data,  GLib.free);
122                 request.finish(stream,
123                          this.size,
124                          this.content_type);
125                  
126             
127         
128             return;
129             
130             //run_impl.begin(cancellable, (obj, res) => {
131                 /*InputStream? stream = null;
132
133                 try {
134                         stream = this.run_impl.end(res);
135                 } catch (Error e)  {
136                     request.finish_error(e);
137                 }
138                 if (stream == null) {
139                     stream = new MemoryInputStream();
140                 }
141                 print("Send : %s (%s/%d)\n",  
142                       this.content_type, this.size.to_string(), this.data.length);
143                 
144                 request.finish(stream,
145                          this.size,
146                          this.content_type);
147                  
148             
149                 });
150             */
151         }
152 }
153
154 public class FakeServer : Object
155 {
156         WebKit.WebView view;
157         
158         public FakeServer(WebKit.WebView wkview)
159         {
160                 this.view = wkview;
161                 
162                 
163                 // 
164                 
165                   
166         // Hook up signals.
167   
168         //this.view.resource_load_started.connect(on_resource_request_starting);
169         //this.view.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
170         //this.view.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
171           
172          //
173         var cx = WebKit.WebContext.get_default();
174         //var cx = this.view.get_context();
175         cx.register_uri_scheme("xhttp",  serve);
176         cx.set_cache_model (WebKit.CacheModel.DOCUMENT_VIEWER);
177        // = crash  cx.set_process_model (WebKit.ProcessModel.MULTIPLE_SECONDARY_PROCESSES );
178     }
179     
180     
181     public void serve(WebKit.URISchemeRequest request)
182     { 
183                 // request is URISchemeRequest
184                          
185                 print("REQ: %s\n",request.get_path());
186                 var cdata = FakeServerCache.factory(request.get_path());
187         
188                 if (cdata.size < 1 ) {
189                         print("Skip file missing = %s/gitlive%s\n", GLib.Environment.get_home_dir() , request.get_path());
190                         request.finish_error(new FakeServerError.FILE_DOES_NOT_EXIST ("My error msg"));
191                         return;
192                 }
193         
194                 print("Send :%s, %s (%s/%d)", request.get_path(), 
195                       cdata.content_type, cdata.size.to_string(), cdata.data.length);
196                 cdata.run(request,    null);
197                 //var stream = new GLib.MemoryInputStream.from_data (cdata.data.data,  GLib.free);
198                     
199                 // we could cache these memory streams... so no need to keep reading from disk...
200                 // then what happens if file get's updated - neet to check the data against the cache..
201                 
202                 
203                 
204                 //request.finish (  stream, cdata.size  , cdata.content_type);
205                 //stream.close();
206         }
207
208    
209 }