Changeset 1095

Show
Ignore:
Timestamp:
06/19/08 15:06:43 (3 months ago)
Author:
kris
Message:

moved around some support for keepalive and other goodies. ServletResponse?.copyFile() now avoids heap usage

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/mango/net/http/server/HttpBridge.d

    r1092 r1095  
    9393 
    9494                // close and destroy this conduit (socket) 
    95                 //scope (exit) 
    96                        //conduit.detach; 
     95                scope (exit) 
     96                       if (! response.managedConnection) 
     97                             conduit.detach; 
    9798 
    98                 // reset the (probably overridden) input and output 
    99                 request.reset(); 
    100                 response.reset(); 
     99                do { 
     100                   // reset the (probably overridden) input and output 
     101                   request.reset; 
     102                   response.reset; 
    101103 
    102                 // first, extract HTTP headers from input 
    103                 request.readHeaders ()
     104                   // first, extract HTTP headers from input 
     105                   request.readHeaders
    104106 
    105                 // pass request off to the provider. It is the provider's  
    106                 // responsibility to flush the output! 
    107                 provider.service (request, response); 
     107                   // pass request off to the provider. It is the provider's  
     108                   // responsibility to flush the output! 
     109                   provider.service (request, response); 
     110                   } while (response.keepAlive); 
    108111        } 
    109112} 
  • trunk/mango/net/http/server/HttpMessage.d

    r1083 r1095  
    105105                encoding = null; 
    106106                mimeType = null; 
    107                 headers.reset(); 
     107                headers.reset; 
     108 
     109                // make some space in the buffer, perhaps for keepalive 
     110                buffer.compress; 
    108111        } 
    109112 
  • trunk/mango/net/http/server/HttpResponse.d

    r1059 r1095  
    5252        private IBuffer         output; 
    5353        private bool            commited; 
     54        private bool            keepalive; 
     55        private bool            isManaged; 
    5456 
    5557        static private InvalidStateException InvalidState; 
     
    110112                // reset output parameters 
    111113                params.reset; 
     114 
     115                // reset these too 
     116                keepalive = false; 
     117                isManaged = false; 
    112118        } 
    113119 
     
    266272        /********************************************************************** 
    267273 
     274                set the keepalive flag for this response, which will add 
     275                a header also. An exception is thrown if we're already in 
     276                a committed state 
     277 
     278        **********************************************************************/ 
     279 
     280        final void keepAlive (bool yes) 
     281        { 
     282                if (commited) 
     283                    throw InvalidState; 
     284                keepalive = yes; 
     285        } 
     286 
     287        /********************************************************************** 
     288 
     289                Return the keepalive flag for this response 
     290 
     291        **********************************************************************/ 
     292 
     293        final bool keepAlive () 
     294        { 
     295                return keepalive; 
     296        } 
     297 
     298        /********************************************************************** 
     299 
     300                Set internal flag to leave the connected socket open. Use 
     301                with extreme care 
     302 
     303        **********************************************************************/ 
     304 
     305        final void managedConnection (bool yes) 
     306        { 
     307                isManaged = yes; 
     308        } 
     309 
     310        /********************************************************************** 
     311 
     312                Return whether to leave the connected socket open. See  
     313                above 
     314 
     315        **********************************************************************/ 
     316 
     317        final bool managedConnection () 
     318        { 
     319                return isManaged; 
     320        } 
     321 
     322        /********************************************************************** 
     323 
    268324                Private method to send the response status, and the 
    269325                output headers, back to the user-agent 
     
    295351                        (HttpConst.Eol); 
    296352 
    297                    // tell client we don't support keep-alive 
     353                   // set connection header 
    298354                   if (! headers.get (HttpHeader.Connection)) 
    299                          headers.add (HttpHeader.Connection, "close"); 
     355                         headers.add (HttpHeader.Connection, keepalive ? "keep-alive" : "close"); 
    300356                   
    301357                   // write the header tokens, followed by a blank line 
    302358                   super.produce (&emit.consume, HttpConst.Eol); 
    303  
    304                    emit(HttpConst.Eol); 
     359                   emit (HttpConst.Eol); 
    305360                         
    306361                   version (ShowHeaders) 
  • trunk/mango/net/http/server/HttpServer.d

    r1092 r1095  
    4040{ 
    4141        private ServiceProvider provider; 
    42         private bool            close = true; 
    4342 
    4443        /********************************************************************** 
     
    8483        { 
    8584                return "http"; 
    86         } 
    87  
    88         /********************************************************************** 
    89  
    90                 Configure whether each connection is detached after being 
    91                 serviced. The default behaviour is to close the connection 
    92  
    93         **********************************************************************/ 
    94  
    95         void detach (bool yes) 
    96         { 
    97                 this.close = yes; 
    9885        } 
    9986 
     
    151138                ServiceBridge   bridge; 
    152139 
    153                 scope (exit) 
    154                        if (close) 
    155                            conduit.detach; 
    156  
    157140                // we know what this is because we created it (above) 
    158141                thread = cast(HttpThread) st; 
  • trunk/mango/net/servlet/ServletProvider.d

    r1094 r1095  
    333333 
    334334                // retrieve the requested uri 
    335                 path = request.uri.getPath(); 
    336                 log.info ("request path '{}'", path); 
     335                path = request.uri.getPath; 
     336                 
     337                // log the request 
     338                if (log.enabled (log.Trace)) 
     339                    log.trace ("request for path '{}' from uri '{}'", path, request.uri); 
    337340 
    338341                // lookup servlet for this path 
    339                 pm = cast (PathMapping) cache.get (path); 
     342                pm = cast(PathMapping) cache.get (path); 
    340343 
    341344                // construct a new cache entry if not found 
     
    354357                   } 
    355358 
    356                 // ready to go ... 
    357359                try { 
    358                     // initialize the servlet environment 
    359                     request.set (pm.mapping.proxy.getName, pm.mapping.proxy.getContext); 
     360                    // ready to go ... 
     361                    auto proxy = pm.mapping.proxy; 
     362 
     363                    // initialize servlet environment 
     364                    request.set (proxy.getName, proxy.getContext); 
    360365 
    361366                    // execute servlet 
    362                     pm.mapping.proxy.getServlet.service (request, response); 
     367                    proxy.getServlet.service (request, response); 
    363368         
    364369                    // flush output on behalf of servlet ... 
  • trunk/mango/net/servlet/ServletResponse.d

    r1087 r1095  
    1818                tango.io.FileConduit; 
    1919 
     20private import  Path = tango.io.Path; 
     21 
    2022private import  tango.io.model.IBuffer; 
    2123 
     
    3436/****************************************************************************** 
    3537 
     38        Note that an instance of this is maintained by the server on a  
     39        per-thread basis. 
     40 
    3641******************************************************************************/ 
    3742 
    3843class ServletResponse : HttpResponse, IServletResponse 
    3944{ 
     45        // we retain one of these to avoid creation costs 
     46        private FileConduit file; 
     47 
    4048        /********************************************************************** 
    4149 
     
    184192        bool copyFile (IServletContext context, char[] path) 
    185193        { 
    186                 FileConduit source; 
     194                // does the file exist? 
     195                if (file is null)    
     196                    file = new FileConduit; 
    187197 
    188198                try { 
    189                     // does the file exist? 
    190                     source = new FileConduit (context.getResourceAsPath (path)); 
     199                    file.open (context.getResourceAsPath (path)); 
    191200 
    192201                    // set expected output size 
    193                     setContentLength (cast(int) source.length); 
     202                    setContentLength (cast(int) file.length); 
    194203 
    195204                    // set content-type if not already set 
    196205                    if (getContentType is null) 
    197206                       { 
    198                        char[] mime = context.getMimeType (source.path.suffix); 
     207                       auto p = Path.parse (path); 
     208                       char[] mime = context.getMimeType (p.suffix); 
    199209                       if (mime is null) 
    200210                           mime = "text/plain";         
     
    204214 
    205215                    // copy file to output 
    206                     buffer.copy (source); 
     216                    buffer.copy (file); 
    207217                    return true; 
    208218 
     
    213223                      finally  
    214224                            { 
    215                             if (source) 
    216                                 source.detach; 
     225                            file.detach; 
    217226                            } 
    218227                return false; 
    219228        } 
    220229} 
    221  
    222