1 module awebview.gui.resourceinterceptor; 2 3 import std.path, std.file, std.array, std.algorithm; 4 5 import awebview.gui.application; 6 import awebview.wrapper.resourceinterceptor; 7 8 9 class LocalResourceInterceptor : ResourceInterceptor 10 { 11 this(Application application, string dirEXE) 12 { 13 super(); 14 _app = application; 15 _dirEXE = dirEXE.pathSplitter.buildPath; 16 } 17 18 19 override 20 ResourceResponse onRequest(ResourceRequest req) 21 { 22 immutable isFile = req.url.scheme == "file"; 23 24 if(!isFile) 25 return super.onRequest(req); 26 27 immutable path = req.url.path.to!string; 28 auto pathSp = path.pathSplitter.map!"a.idup".array; 29 30 if(pathSp.length >= 1) 31 { 32 auto spsp = pathSp[$-1].split("-"); 33 if(spsp.length == 4 && spsp[0] == "Activity" && spsp[2] == "HTMLPage"){ 34 auto activity = _app.getActivity(spsp[1]); 35 if(spsp[3].extension == ".html"){ // Activity-%[act.id%]-HTMLPage-%[page.id%].html 36 auto html = activity[spsp[3].baseName(".html")].html; 37 return ResourceResponse(cast(ubyte[])html, "text/html"); 38 }else if(activity.nowPage != activity[spsp[3]]){ // Activity-%[act.id%]-HTMLPage-%[other page.id%] 39 _app.runAtNextFrame((){ activity.load(spsp[3]); }); 40 return ResourceResponse(null); 41 }else{ // Activity-%[act.id%]-HTMLPage-%[now page.id%] 42 _app.runAtNextFrame((){ activity.reload(); }); 43 return ResourceResponse(null); 44 } 45 }else if(spsp.length == 2 && spsp[0] == "HTMLElement"){ // HTMLElement-%[elem.id%].%[ext%] 46 //string mime = extToMIME.get(spsp[1].extension, "text/plain"); 47 auto elem = _app.getActivity(spsp[1]).nowPage[spsp[1]]; 48 auto data = elem.rawResource; 49 string mime = elem.mime; 50 return ResourceResponse(cast(ubyte[])data, mime); 51 } 52 } 53 54 return super.onRequest(req); 55 } 56 57 58 private: 59 string _dirEXE; 60 Application _app; 61 } 62 63 64 /** 65 http://webdesign.about.com/od/multimedia/a/mime-types-by-file-extension.htm 66 */ 67 shared immutable string[string] extToMIME; 68 69 shared static this() 70 { 71 extToMIME = 72 [ 73 ".323" : "text/h323", 74 ".acx" : "application/internet-property-stream", 75 ".ai" : "application/postscript", 76 ".aif" : "audio/x-aiff", 77 ".aifc" : "audio/x-aiff", 78 ".aiff" : "audio/x-aiff", 79 ".asf" : "video/x-ms-asf", 80 ".asr" : "video/x-ms-asf", 81 ".asx" : "video/x-ms-asf", 82 ".au" : "audio/basic", 83 ".avi" : "video/x-msvideo", 84 ".axs" : "application/olescript", 85 ".bas" : "text/plain", 86 ".bcpio" : "application/x-bcpio", 87 ".bin" : "application/octet-stream", 88 ".bmp" : "image/bmp", 89 ".c" : "text/plain", 90 ".cat" : "application/vnd.ms-pkiseccat", 91 ".cdf" : "application/x-cdf", 92 ".cdf" : "application/x-netcdf", 93 ".cer" : "application/x-x509-ca-cert", 94 ".class" : "application/octet-stream", 95 ".clp" : "application/x-msclip", 96 ".cmx" : "image/x-cmx", 97 ".cod" : "image/cis-cod", 98 ".cpio" : "application/x-cpio", 99 ".crd" : "application/x-mscardfile", 100 ".crl" : "application/pkix-crl", 101 ".crt" : "application/x-x509-ca-cert", 102 ".csh" : "application/x-csh", 103 ".css" : "text/css", 104 ".dcr" : "application/x-director", 105 ".der" : "application/x-x509-ca-cert", 106 ".dir" : "application/x-director", 107 ".dll" : "application/x-msdownload", 108 ".dms" : "application/octet-stream", 109 ".doc" : "application/msword", 110 ".dot" : "application/msword", 111 ".dvi" : "application/x-dvi", 112 ".dxr" : "application/x-director", 113 ".eps" : "application/postscript", 114 ".etx" : "text/x-setext", 115 ".evy" : "application/envoy", 116 ".exe" : "application/octet-stream", 117 ".fif" : "application/fractals", 118 ".flr" : "x-world/x-vrml", 119 ".gif" : "image/gif", 120 ".gtar" : "application/x-gtar", 121 ".gz" : "application/x-gzip", 122 ".h" : "text/plain", 123 ".hdf" : "application/x-hdf", 124 ".hlp" : "application/winhlp", 125 ".hqx" : "application/mac-binhex40", 126 ".hta" : "application/hta", 127 ".htc" : "text/x-component", 128 ".htm" : "text/html", 129 ".html" : "text/html", 130 ".htt" : "text/webviewhtml", 131 ".ico" : "image/x-icon", 132 ".ief" : "image/ief", 133 ".iii" : "application/x-iphone", 134 ".ins" : "application/x-internet-signup", 135 ".isp" : "application/x-internet-signup", 136 ".jfif" : "image/pipeg", 137 ".jpe" : "image/jpeg", 138 ".jpeg" : "image/jpeg", 139 ".jpg" : "image/jpeg", 140 ".js" : "text/javascript", 141 ".latex" : "application/x-latex", 142 ".lha" : "application/octet-stream", 143 ".lsf" : "video/x-la-asf", 144 ".lsx" : "video/x-la-asf", 145 ".lzh" : "application/octet-stream", 146 ".m13" : "application/x-msmediaview", 147 ".m14" : "application/x-msmediaview", 148 ".m3u" : "audio/x-mpegurl", 149 ".man" : "application/x-troff-man", 150 ".mdb" : "application/x-msaccess", 151 ".me" : "application/x-troff-me", 152 ".mht" : "message/rfc822", 153 ".mhtml" : "message/rfc822", 154 ".mid" : "audio/mid", 155 ".mny" : "application/x-msmoney", 156 ".mov" : "video/quicktime", 157 ".movie" : "video/x-sgi-movie", 158 ".mp2" : "video/mpeg", 159 ".mp3" : "audio/mpeg", 160 ".mpa" : "video/mpeg", 161 ".mpe" : "video/mpeg", 162 ".mpeg" : "video/mpeg", 163 ".mpg" : "video/mpeg", 164 ".mpp" : "application/vnd.ms-project", 165 ".mpv2" : "video/mpeg", 166 ".ms" : "application/x-troff-ms", 167 ".msg" : "application/vnd.ms-outlook", 168 ".mvb" : "application/x-msmediaview", 169 ".nc" : "application/x-netcdf", 170 ".nws" : "message/rfc822", 171 ".oda" : "application/oda", 172 ".p10" : "application/pkcs10", 173 ".p12" : "application/x-pkcs12", 174 ".p7b" : "application/x-pkcs7-certificates", 175 ".p7c" : "application/x-pkcs7-mime", 176 ".p7m" : "application/x-pkcs7-mime", 177 ".p7r" : "application/x-pkcs7-certreqresp", 178 ".p7s" : "application/x-pkcs7-signature", 179 ".pbm" : "image/x-portable-bitmap", 180 ".pdf" : "application/pdf", 181 ".pfx" : "application/x-pkcs12", 182 ".pgm" : "image/x-portable-graymap", 183 ".pko" : "application/ynd.ms-pkipko", 184 ".pma" : "application/x-perfmon", 185 ".pmc" : "application/x-perfmon", 186 ".pml" : "application/x-perfmon", 187 ".pmr" : "application/x-perfmon", 188 ".pmw" : "application/x-perfmon", 189 ".png" : "image/png", 190 ".pnm" : "image/x-portable-anymap", 191 ".pot" : "application/vnd.ms-powerpoint", 192 ".ppm" : "image/x-portable-pixmap", 193 ".pps" : "application/vnd.ms-powerpoint", 194 ".ppt" : "application/vnd.ms-powerpoint", 195 ".prf" : "application/pics-rules", 196 ".ps" : "application/postscript", 197 ".pub" : "application/x-mspublisher", 198 ".qt" : "video/quicktime", 199 ".ra" : "audio/x-pn-realaudio", 200 ".ram" : "audio/x-pn-realaudio", 201 ".ras" : "image/x-cmu-raster", 202 ".rgb" : "image/x-rgb", 203 ".rmi" : "audio/mid", 204 ".roff" : "application/x-troff", 205 ".rtf" : "application/rtf", 206 ".rtx" : "text/richtext", 207 ".scd" : "application/x-msschedule", 208 ".sct" : "text/scriptlet", 209 ".setpay" : "application/set-payment-initiation", 210 ".setreg" : "application/set-registration-initiation", 211 ".sh" : "application/x-sh", 212 ".shar" : "application/x-shar", 213 ".sit" : "application/x-stuffit", 214 ".snd" : "audio/basic", 215 ".spc" : "application/x-pkcs7-certificates", 216 ".spl" : "application/futuresplash", 217 ".src" : "application/x-wais-source", 218 ".sst" : "application/vnd.ms-pkicertstore", 219 ".stl" : "application/vnd.ms-pkistl", 220 ".stm" : "text/html", 221 ".sv4cpio" : "application/x-sv4cpio", 222 ".sv4crc" : "application/x-sv4crc", 223 ".svg" : "image/svg+xml", 224 ".swf" : "application/x-shockwave-flash", 225 ".t" : "application/x-troff", 226 ".tar" : "application/x-tar", 227 ".tcl" : "application/x-tcl", 228 ".tex" : "application/x-tex", 229 ".texi" : "application/x-texinfo", 230 ".texinfo" : "application/x-texinfo", 231 ".tgz" : "application/x-compressed", 232 ".tif" : "image/tiff", 233 ".tiff" : "image/tiff", 234 ".tr" : "application/x-troff", 235 ".trm" : "application/x-msterminal", 236 ".tsv" : "text/tab-separated-values", 237 ".txt" : "text/plain", 238 ".uls" : "text/iuls", 239 ".ustar" : "application/x-ustar", 240 ".vcf" : "text/x-vcard", 241 ".vrml" : "x-world/x-vrml", 242 ".wav" : "audio/x-wav", 243 ".wcm" : "application/vnd.ms-works", 244 ".wdb" : "application/vnd.ms-works", 245 ".wks" : "application/vnd.ms-works", 246 ".wmf" : "application/x-msmetafile", 247 ".wps" : "application/vnd.ms-works", 248 ".wri" : "application/x-mswrite", 249 ".wrl" : "x-world/x-vrml", 250 ".wrz" : "x-world/x-vrml", 251 ".xaf" : "x-world/x-vrml", 252 ".xbm" : "image/x-xbitmap", 253 ".xla" : "application/vnd.ms-excel", 254 ".xlc" : "application/vnd.ms-excel", 255 ".xlm" : "application/vnd.ms-excel", 256 ".xls" : "application/vnd.ms-excel", 257 ".xlt" : "application/vnd.ms-excel", 258 ".xlw" : "application/vnd.ms-excel", 259 ".xof" : "x-world/x-vrml", 260 ".xpm" : "image/x-xpixmap", 261 ".xwd" : "image/x-xwindowdump", 262 ".z" : "application/x-compress", 263 ".zip" : "application/zip", 264 ]; 265 }