Changeset 1104

Show
Ignore:
Timestamp:
06/26/08 02:05:39 (2 months ago)
Author:
kris
Message:

added support for a file cache (model), and a very simple implementation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/mango/net/servlet/ServletResponse.d

    r1101 r1104  
    2222private import  tango.io.model.IBuffer; 
    2323 
    24 private import  tango.net.http.HttpParams, 
     24private import  tango.net.http.HttpConst, 
     25                tango.net.http.HttpParams, 
    2526                tango.net.http.HttpCookies, 
    26                 tango.net.http.HttpHeaders, 
    27                 tango.net.http.HttpConst; 
     27                tango.net.http.HttpHeaders; 
    2828 
    2929private import  mango.net.http.server.HttpResponse, 
     
    4848        private alias HttpResponse.managed managed; 
    4949 
     50        alias bool delegate(FileConduit file, uint size, char[] mime) Callback; 
     51 
     52 
    5053        /********************************************************************** 
    5154 
     
    147150 
    148151        /*********************************************************************** 
     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        /*********************************************************************** 
    149167 
    150168        ***********************************************************************/ 
     
    188206        bool copyFile (IServletContext context, char[] path) 
    189207        { 
     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 
    190219                // does the file exist? 
    191220                if (file is null)    
     
    193222 
    194223                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 
    195232                    char[512] tmp = void; 
    196233                    file.open (context.getResourceAsPath (path, tmp)); 
    197234 
    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); 
    214245                    return true; 
    215246 
     
    225256        } 
    226257} 
     258 
  • trunk/mango/net/servlet/model/IServletResponse.d

    r1101 r1104  
    1414 
    1515private import  tango.io.model.IBuffer; 
     16private import  tango.io.model.IConduit; 
    1617 
    17 private import  tango.net.http.HttpParams, 
     18private import  tango.net.http.HttpConst, 
     19                tango.net.http.HttpParams, 
    1820                tango.net.http.HttpCookies, 
    19                 tango.net.http.HttpHeaders, 
    20                 tango.net.http.HttpConst; 
     21                tango.net.http.HttpHeaders; 
    2122 
    2223private import  mango.net.servlet.model.IServletContext; 
     24 
     25 
     26/****************************************************************************** 
     27 
     28******************************************************************************/ 
     29 
     30interface 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 
    2344 
    2445/****************************************************************************** 
     
    107128 
    108129        bool copyFile (IServletContext context, char[] path); 
     130 
     131        /*********************************************************************** 
     132         
     133        ***********************************************************************/ 
     134 
     135        bool cacheFile (IServletContext context, char[] path, IFileCache cache); 
    109136}