Changeset 1104
- Timestamp:
- 06/26/08 02:05:39 (2 months ago)
- Files:
-
- trunk/mango/net/servlet/ServletFileCache.d (added)
- trunk/mango/net/servlet/ServletResponse.d (modified) (6 diffs)
- trunk/mango/net/servlet/model/IServletResponse.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/mango/net/servlet/ServletResponse.d
r1101 r1104 22 22 private import tango.io.model.IBuffer; 23 23 24 private import tango.net.http.HttpParams, 24 private import tango.net.http.HttpConst, 25 tango.net.http.HttpParams, 25 26 tango.net.http.HttpCookies, 26 tango.net.http.HttpHeaders, 27 tango.net.http.HttpConst; 27 tango.net.http.HttpHeaders; 28 28 29 29 private import mango.net.http.server.HttpResponse, … … 48 48 private alias HttpResponse.managed managed; 49 49 50 alias bool delegate(FileConduit file, uint size, char[] mime) Callback; 51 52 50 53 /********************************************************************** 51 54 … … 147 150 148 151 /*********************************************************************** 152 153 set expected output size, and content-type 154 155 ***********************************************************************/ 156 157 void setHeaders (uint size, char[] mime) 158 { 159 setContentLength (size); 160 161 if (mime is null) 162 mime = "text/plain"; 163 setContentType (mime); 164 } 165 166 /*********************************************************************** 149 167 150 168 ***********************************************************************/ … … 188 206 bool copyFile (IServletContext context, char[] path) 189 207 { 208 return cacheFile (context, path, null); 209 } 210 211 /*********************************************************************** 212 213 ***********************************************************************/ 214 215 bool cacheFile (IServletContext context, char[] path, IFileCache cache) 216 { 217 cache.Info info; 218 190 219 // does the file exist? 191 220 if (file is null) … … 193 222 194 223 try { 224 if (cache) 225 if (cache.get (path, info)) 226 { 227 setHeaders (info.size, info.mime); 228 buffer.append (info.data); 229 return true; 230 } 231 195 232 char[512] tmp = void; 196 233 file.open (context.getResourceAsPath (path, tmp)); 197 234 198 // set expected output size 199 setContentLength (cast(int) file.length); 200 201 // set content-type if not already set 202 if (getContentType is null) 203 { 204 auto p = Path.parse (path); 205 char[] mime = context.getMimeType (p.suffix); 206 if (mime is null) 207 mime = "text/plain"; 208 209 setContentType (mime); 210 } 211 212 // copy file to output 213 buffer.copy (file); 235 auto p = Path.parse (path); 236 info.size = file.length; 237 info.mime = context.getMimeType (p.suffix); 238 setHeaders (info.size, info.mime); 239 240 // should we cache this? 241 if (cache && cache.put (path, info, file)) 242 buffer.append (info.data); 243 else 244 buffer.copy (file); 214 245 return true; 215 246 … … 225 256 } 226 257 } 258 trunk/mango/net/servlet/model/IServletResponse.d
r1101 r1104 14 14 15 15 private import tango.io.model.IBuffer; 16 private import tango.io.model.IConduit; 16 17 17 private import tango.net.http.HttpParams, 18 private import tango.net.http.HttpConst, 19 tango.net.http.HttpParams, 18 20 tango.net.http.HttpCookies, 19 tango.net.http.HttpHeaders, 20 tango.net.http.HttpConst; 21 tango.net.http.HttpHeaders; 21 22 22 23 private import mango.net.servlet.model.IServletContext; 24 25 26 /****************************************************************************** 27 28 ******************************************************************************/ 29 30 interface IFileCache 31 { 32 struct Info 33 { 34 ulong size; 35 char[] mime; 36 void[] data; 37 } 38 39 bool get (char[] path, ref Info info); 40 41 bool put (char[] path, ref Info info, InputStream stream); 42 } 43 23 44 24 45 /****************************************************************************** … … 107 128 108 129 bool copyFile (IServletContext context, char[] path); 130 131 /*********************************************************************** 132 133 ***********************************************************************/ 134 135 bool cacheFile (IServletContext context, char[] path, IFileCache cache); 109 136 }
