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