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