Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3459

Show
Ignore:
Timestamp:
04/24/08 22:27:25 (7 months ago)
Author:
kris
Message:

updated HttpClient? to support keepalive, and added an example to HttpGet?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/net/http/HttpClient.d

    r3250 r3459  
    8181{        
    8282        // callback for sending PUT content 
    83         alias void delegate (IBuffer)   Pump; 
    84  
     83        alias void delegate (IBuffer) Pump; 
     84         
    8585        // this is struct rather than typedef to avoid compiler bugs 
    8686        private struct RequestMethod 
     
    109109        private bool                    doRedirect = true; 
    110110 
     111        // attempt keepalive?  
     112        private bool                    keepalive = false; 
     113 
    111114        // limit the number of redirects, or catch circular redirects 
    112115        private uint                    redirections,  
    113116                                        redirectionLimit = 5; 
    114117 
    115         // use HTTP v1.0? 
    116         private static const char[] DefaultHttpVersion = "HTTP/1.0"; 
     118        // the http version being sent with requests 
     119        private char[]                  httpVersion; 
     120 
     121        // http version id 
     122        public enum Version {OnePointZero, OnePointOne}; 
    117123 
    118124        // standard set of request methods ... 
     
    156162 
    157163                tmp          = new Buffer (1024 * 4); 
     164                output       = new Buffer (1024 * 16); 
     165 
    158166                paramsOut    = new HttpParams  (new Buffer (1024 * 4)); 
    159167                headersOut   = new HttpHeaders (new Buffer (1024 * 4)); 
     
    161169 
    162170                // decode the host name (may take a second or two) 
    163                 auto host = uri.getHost ()
     171                auto host = uri.getHost
    164172                if (host) 
    165173                    address = new InternetAddress (host, uri.getValidPort); 
    166174                else 
    167175                   responseLine.error ("invalid url provided to HttpClient ctor"); 
     176 
     177                // default the http version to 1.0 
     178                setVersion (Version.OnePointZero); 
    168179        } 
    169180 
     
    293304 
    294305        /*********************************************************************** 
     306         
     307                Set the request method 
     308 
     309        ***********************************************************************/ 
     310 
     311        void setRequest (RequestMethod method) 
     312        { 
     313                this.method = method; 
     314        } 
     315 
     316        /*********************************************************************** 
     317         
     318                Set the request version 
     319 
     320        ***********************************************************************/ 
     321 
     322        void setVersion (Version v) 
     323        { 
     324                static const char[][] versions = ["HTTP/1.0", "HTTP/1.1"]; 
     325 
     326                httpVersion = versions[v]; 
     327        } 
     328 
     329        /*********************************************************************** 
    295330 
    296331                enable/disable the internal redirection suppport 
     
    314349        } 
    315350 
    316         /** 
    317          * Deprecated: use setTimeout(TimeSpan) instead 
    318          */ 
    319         deprecated void setTimeout(double interval) 
    320         { 
    321                 setTimeout(TimeSpan.interval(interval)); 
    322         } 
    323  
    324         /*********************************************************************** 
    325  
    326                 Overridable method to create a Socket. You may find a need  
    327                 to override this for some purpose; perhaps to add input or  
    328                 output filters. 
    329                   
    330         ***********************************************************************/ 
    331  
    332         protected SocketConduit createSocket () 
    333         { 
    334                 return new SocketConduit; 
     351        /*********************************************************************** 
     352 
     353                Control keepalive option  
     354 
     355        ***********************************************************************/ 
     356 
     357        void keepAlive (bool yes) 
     358        { 
     359                keepalive = yes; 
    335360        } 
    336361 
     
    393418                        responseLine.error ("too many redirections, or a circular redirection"); 
    394419     
    395                     // create socket, and connect it  
    396                     socket = createSocket; 
    397                     socket.setTimeout (timeout); 
    398                     socket.connect (address); 
    399      
    400                     // create buffers for input and output 
     420                    // new socket for each request? 
     421                    if (keepalive is false) 
     422                        close; 
     423 
     424                    // create socket and connect it. Retain prior socket if 
     425                    // not closed between calls 
     426                    if (socket is null) 
     427                       { 
     428                       socket = new SocketConduit; 
     429                       socket.setTimeout (timeout); 
     430                       socket.connect (address); 
     431                       } 
     432     
     433                    // setup buffers for input and output 
     434                    output.setConduit(socket); 
    401435                    if (input) 
    402                         input.clear, input.setConduit (socket)
     436                        input.setConduit(socket).clear
    403437                    else 
    404                        input = new Buffer (socket); 
    405                     output = new Buffer (socket); 
     438                       input = new Buffer(socket); 
    406439     
    407440                    // save for read() method 
     
    413446     
    414447                    // http/1.0 needs connection:close 
    415                     headersOut.add (HttpHeader.Connection, "close"); 
     448                    if (keepalive is false) 
     449                        headersOut.add (HttpHeader.Connection, "close"); 
    416450     
    417451                    // attach/extend query parameters if user has added some 
     
    429463                    // should we emit query as part of request line? 
    430464                    if (query.length) 
    431                         if (method is Get) 
     465                        if (method != Post) 
    432466                            output ("?"), uri.encode (&output.consume, query, uri.IncQueryAll); 
    433467                        else  
    434                            if (method is Post && pump.funcptr is null) 
     468                           if (pump.funcptr is null) 
    435469                              { 
    436470                              // we're POSTing query text - add default info 
     
    443477     
    444478                    // complete the request line, and emit headers too 
    445                     output (" ") (DefaultHttpVersion) (HttpConst.Eol); 
     479                    output (" ") (httpVersion) (HttpConst.Eol); 
    446480        
    447481                    headersOut.produce (&output.consume, HttpConst.Eol); 
     
    501535                                    auto host = uri.getHost(); 
    502536                                    if (host) 
    503                                         address = new InternetAddress (uri.getHost, uri.getValidPort); 
     537                                       address = new InternetAddress (uri.getHost, uri.getValidPort); 
    504538                                    else 
    505539                                       responseLine.error ("redirect has invalid url: "~redirect); 
  • trunk/tango/net/http/HttpGet.d

    r3160 r3459  
    8383} 
    8484 
     85 
     86debug (HttpGet) 
     87{        
     88        import tango.io.Console; 
     89 
     90        void main() 
     91        { 
     92                // open a web-page for reading (see HttpPost for writing) 
     93                auto page = new HttpGet ("http://www.digitalmars.com/d/intro.html"); 
     94 
     95                // retrieve and flush display content 
     96                Cout (cast(char[]) page.read) (); 
     97        } 
     98}