Changeset 1486

Show
Ignore:
Timestamp:
04/02/08 23:58:46 (8 months ago)
Author:
mikey
Message:

api improvements: allow more fluent http requests

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/peer/http/stubHTTPConnection.php

    r1485 r1486  
    2222     * request object to open connection 
    2323     * 
    24      * @var  stubHTTPRequest 
     24     * @var  stubHTTPURL 
    2525     */ 
    26     protected $request  = null; 
     26    protected $url      = null; 
     27    /** 
     28     * contains request headers 
     29     * 
     30     * @var  stubHeaderList 
     31     */ 
     32    protected $headers  = null; 
     33    /** 
     34     * timeout 
     35     * 
     36     * @var  int 
     37     */ 
     38    protected $timeout  = 30; 
    2739    /** 
    2840     * end-of-line marker 
    2941     */ 
    30     const END_OF_LINE = "\r\n"; 
     42    const END_OF_LINE   = "\r\n"; 
    3143 
    3244    /** 
     
    3749    public function __construct(stubHTTPURL $http) 
    3850    { 
    39         $this->request = new stubHTTPRequest($http); 
     51        $this->url     = $http; 
     52        $this->headers = new stubHeaderList(); 
    4053    } 
    4154 
     
    4356     * set timeout for connection 
    4457     * 
    45      * @param  int  $timeout  timeout for connection in seconds 
     58     * @param   int                 $timeout  timeout for connection in seconds 
     59     * @return  stubHTTPConnection 
    4660     */ 
    47     public function setTimeout($timeout) 
     61    public function timeout($timeout) 
    4862    { 
    49         $this->request->setTimeout($timeout); 
     63        $this->timeout = $timeout; 
     64        return $this; 
     65    } 
     66 
     67    /** 
     68     * do the request with the given user agent header 
     69     * 
     70     * @param   string              $userAgent 
     71     * @return  stubHTTPConnection 
     72     */ 
     73    public function asUserAgent($userAgent) 
     74    { 
     75        $this->headers->putUserAgent($userAgent); 
     76        return $this; 
     77    } 
     78 
     79    /** 
     80     * say the connection was refered from given url 
     81     * 
     82     * @param   string              $referer 
     83     * @return  stubHTTPConnection 
     84     */ 
     85    public function referedFrom($referer) 
     86    { 
     87        $this->headers->putReferer($referer); 
     88        return $this; 
     89    } 
     90 
     91    /** 
     92     * add some cookie data to the request 
     93     * 
     94     * @param   array<string,string>  $cookieValues  list of key-value pairs 
     95     * @return  stubHTTPConnection 
     96     */ 
     97    public function withCookie(array $cookieValues) 
     98    { 
     99        $this->headers->putCookie($cookieValues); 
     100        return $this; 
     101    } 
     102 
     103    /** 
     104     * authorize with given credentials 
     105     * 
     106     * @param   string              $user 
     107     * @param   string              $password 
     108     * @return  stubHTTPConnection 
     109     */ 
     110    public function authorizedAs($user, $password) 
     111    { 
     112        $this->headers->putAuthorization($user, $password); 
     113        return $this; 
     114    } 
     115 
     116    /** 
     117     * adds any arbitrary header 
     118     * 
     119     * @param   string             $key    name of header 
     120     * @param   string             $value  value of header 
     121     * @return  stubHTTPConnection 
     122     */ 
     123    public function usingHeader($key, $value) 
     124    { 
     125        $this->headers->put($key, $value); 
     126        return $this; 
    50127    } 
    51128 
     
    53130     * returns response object for given URL after GET request 
    54131     * 
    55      * @param   stubHeaderList    $headers  optional  list of headers to use 
    56132     * @return  stubHTTPResponse 
    57133     */ 
    58     public function get(stubHeaderList $headers = null
     134    public function get(
    59135    { 
    60         if (null !== $headers) { 
    61             $this->request->setHeaders($headers); 
    62         } 
    63          
    64         $response = $this->request->send(stubHTTPRequest::METHOD_GET); 
    65         return $response; 
     136        return stubHTTPRequest::create($this->url, $this->headers, $this->timeout) 
     137                              ->send(stubHTTPRequest::METHOD_GET); 
    66138    } 
    67139 
     
    69141     * returns response object for given URL after HEAD request 
    70142     * 
    71      * @param   stubHeaderList    $headers  optional  list of headers to use 
    72143     * @return  stubHTTPResponse 
    73144     */ 
    74     public function head(stubHeaderList $headers = null
     145    public function head(
    75146    { 
    76         if (null !== $headers) { 
    77             $this->request->setHeaders($headers); 
    78         } 
    79          
    80         $response = $this->request->send(stubHTTPRequest::METHOD_HEAD); 
    81         return $response; 
     147        return stubHTTPRequest::create($this->url, $this->headers, $this->timeout) 
     148                              ->send(stubHTTPRequest::METHOD_HEAD); 
    82149    } 
    83150 
     
    85152     * returns response object for given URL after POST request 
    86153     * 
    87      * @param   array             $postValues   post data to send with POST request 
    88      * @param   stubHeaderList    $headers      optional  list of headers to use 
     154     * @param   array<string,string>  $postValues  post data to send with POST request 
    89155     * @return  stubHTTPResponse 
    90156     */ 
    91     public function post($postValues, stubHeaderList $headers
     157    public function post($postValues
    92158    { 
    93         if (null !== $headers) { 
    94             $this->request->setHeaders($headers); 
    95         } 
    96          
    97         $this->request->preparePost($postValues); 
    98         $response = $this->request->send(stubHTTPRequest::METHOD_POST); 
    99         return $response; 
     159        return stubHTTPRequest::create($this->url, $this->headers, $this->timeout) 
     160                              ->preparePost($postValues) 
     161                              ->send(stubHTTPRequest::METHOD_POST); 
    100162    } 
    101163} 
  • trunk/src/main/php/net/stubbles/peer/http/stubHTTPRequest.php

    r1485 r1486  
    2424     * @var  stubHTTPURL 
    2525     */ 
    26     protected $http    = null; 
     26    protected $http    = null; 
    2727    /** 
    2828     * contains request headers 
     
    3030     * @var  stubHeaderList 
    3131     */ 
    32     protected $headers = null; 
     32    protected $headers = null; 
    3333    /** 
    3434     * contains body for request 
     
    3636     * @var  string 
    3737     */ 
    38     protected $body    = ''; 
     38    protected $body    = ''; 
    3939    /** 
    4040     * timeout 
     
    4242     * @var  int 
    4343     */ 
    44     protected $timeout = 30; 
     44    protected $timeout = 30; 
    4545    /** 
    4646     * request method type: GET 
    4747     */ 
    48     const METHOD_GET  = 'GET'; 
     48    const METHOD_GET  = 'GET'; 
    4949    /** 
    5050     * request method type: POST 
    5151     */ 
    52     const METHOD_POST = 'POST'; 
     52    const METHOD_POST = 'POST'; 
    5353    /** 
    5454     * request method type: HEAD 
    5555     */ 
    56     const METHOD_HEAD = 'HEAD'; 
     56    const METHOD_HEAD = 'HEAD'; 
    5757    /** 
    5858     * HTTP version: 1.0 
    5959     */ 
    60     const VERSION_1_0 = 'HTTP/1.0'; 
     60    const VERSION_1_0 = 'HTTP/1.0'; 
    6161    /** 
    6262     * HTTP version: 1.1 
    6363     */ 
    64     const VERSION_1_1 = 'HTTP/1.1'; 
     64    const VERSION_1_1 = 'HTTP/1.1'; 
    6565 
    6666    /** 
    67      * constructor for this class. 
     67     * constructor 
    6868     * 
    69      * @param  stubHTTPURL  $http  HTTP URL to perform a request to 
     69     * @param  stubHTTPURL     $http     HTTP URL to perform a request to 
     70     * @param  stubHeaderList  $header   list of request headers 
     71     * @param  int             $timeout  timeout for connection in seconds 
    7072     */ 
    71     public function __construct(stubHTTPURL $http
     73    public function __construct(stubHTTPURL $http, stubHeaderList $header, $timeout
    7274    { 
    7375        $this->http    = $http; 
    74         $this->headers = new stubHeaderList(); 
    75     } 
    76  
    77     /** 
    78      * set headers to given list of headers 
    79      * 
    80      * @param  stubHeaderList  $headers  list of headers to use 
    81      */ 
    82     public function setHeaders(stubHeaderList $headers) 
    83     { 
    84         $this->headers = $headers; 
    85     } 
    86  
    87     /** 
    88      * set timeout for request 
    89      * 
    90      * @param  int  $timeout  timeout for connection in seconds 
    91      */ 
    92     public function setTimeout($timeout) 
    93     { 
     76        $this->headers = $header; 
    9477        $this->timeout = $timeout; 
    9578    } 
    9679 
    9780    /** 
    98      * creates needed headers for post request and encodes post values 
     81     * factory method for more fluent use 
    9982     * 
    100      * @param  array  $post_value  post values to submit 
     83     * @param   stubHTTPURL      $http     HTTP URL to perform a request to 
     84     * @param   stubHeaderList   $header   list of request headers 
     85     * @param   int              $timeout  timeout for connection in seconds 
     86     * @return  stubHTTPRequest 
    10187     */ 
    102     public function preparePost(array $post_value
     88    public static function create(stubHTTPURL $http, stubHeaderList $header, $timeout
    10389    { 
    104         foreach ($post_value as $key => $value) { 
     90        return new self($http, $header, $timeout); 
     91    } 
     92 
     93    /** 
     94     * creates required headers for post request and encodes post values 
     95     * 
     96     * @param   array<string,string>  $postValue  post values to submit 
     97     * @return  stubHTTPRequest 
     98     */ 
     99    public function preparePost(array $postValue) 
     100    { 
     101        foreach ($postValue as $key => $value) { 
    105102            $this->body .= urlencode($key) . '=' . urlencode($value) . '&'; 
    106103        } 
     
    108105        $this->headers->put('Content-Type', 'application/x-www-form-urlencoded'); 
    109106        $this->headers->put('Content-Length', strlen($this->body)); 
     107        return $this; 
    110108    } 
    111109 
     
    135133        // prepare last headers and write all headers to socket 
    136134        $this->headers->putDate(); 
    137         if ($this->headers->containsKey('User-Agent') == false) { 
     135        if ($this->headers->containsKey('User-Agent') === false) { 
    138136            $this->headers->putUserAgent('stubbles HTTP Client'); 
    139137        } 
  • trunk/src/main/php/net/stubbles/peer/http/stubHTTPResponse.php

    r1485 r1486  
    2424     * @var  stubSocket 
    2525     */ 
    26     protected $socket                 = null; 
     26    protected $socket               = null; 
    2727    /** 
    2828     * list of response parts 
     
    3030     * @var  array 
    3131     */ 
    32     protected $response               = array(); 
     32    protected $response             = array(); 
    3333    /** 
    3434     * contains headers of response 
     
    3636     * @var  stubHeaderList 
    3737     */ 
    38     protected $headers                = null; 
     38    protected $headers              = null; 
    3939    /** 
    4040     * contains body of response 
     
    4242     * @var  string 
    4343     */ 
    44     protected $body                   = ''; 
     44    protected $body                 = ''; 
    4545    /** 
    4646     * response type data: status line 
     
    103103    public function __destruct() 
    104104    { 
    105         $this->socket->__destruct(); 
     105        $this->socket->disconnect(); 
    106106    } 
    107107 
  • trunk/src/main/php/net/stubbles/peer/http/stubHTTPURL.php

    r1485 r1486  
    161161     * creates a stubHTTPConnection for this URL 
    162162     * 
     163     * To submit a complete HTTP request use this: 
     164     * <code> 
     165     * $response = $url->connect()->asUserAgent('Not Mozilla') 
     166     *                            ->timeout(5) 
     167     *                            ->usingHeader('X-Money', 'Euro') 
     168     *                            ->get(); 
     169     * </code> 
     170     * 
    163171     * @return  stubHTTPConnection 
    164172     */ 
    165     public function getConnection() 
     173    public function connect() 
    166174    { 
    167175        stubClassLoader::load('net::stubbles::peer::http::stubHTTPConnection'); 
    168         $httpconnection = new stubHTTPConnection($this); 
    169         return $httpconnection; 
     176        return new stubHTTPConnection($this); 
    170177    } 
    171178 
  • trunk/src/main/php/net/stubbles/peer/stubHeaderList.php

    r1485 r1486  
    4646     * creates header with value for key 
    4747     * 
    48      * @param   string  $key    name of header 
    49      * @param   string  $value  value of header 
     48     * @param   string          $key    name of header 
     49     * @param   string          $value  value of header 
     50     * @return  stubHeaderList 
    5051     * @throws  stubIllegalArgumentException 
    5152     */ 
     
    6162         
    6263        $this->headers[$key] = (string) $value; 
     64        return $this; 
    6365    } 
    6466 
     
    6668     * removes header with given key 
    6769     * 
    68      * @param  string  $key    name of header 
     70     * @param   string  $key    name of header 
     71     * @return  stubHeaderList 
    6972     */ 
    7073    public function remove($key) 
     
    7376            unset($this->headers[$key]); 
    7477        } 
     78         
     79        return $this; 
    7580    } 
    7681 
     
    7883     * creates header for user agent 
    7984     * 
    80      * @param  string  $userAgent  name of user agent 
     85     * @param   string          $userAgent  name of user agent 
     86     * @return  stubHeaderList 
    8187     */ 
    8288    public function putUserAgent($userAgent) 
    8389    { 
    8490        $this->put('User-Agent', $userAgent); 
     91        return $this; 
    8592    } 
    8693 
     
    8895     * creates header for referer 
    8996     * 
    90      * @param  string  $referer  referer url 
     97     * @param   string          $referer  referer url 
     98     * @return  stubHeaderList 
    9199     */ 
    92100    public function putReferer($referer) 
    93101    { 
    94102        $this->put('Referer', $referer); 
     103        return $this; 
    95104    } 
    96105 
     
    98107     * creates header for cookie 
    99108     * 
    100      * @param  array  $cookieValues  cookie values 
     109     * @param   array           $cookieValues  cookie values 
     110     * @return  stubHeaderList 
    101111     */ 
    102112    public function putCookie(array $cookieValues) 
     
    108118         
    109119        $this->put('Cookie', $cookieValue); 
     120        return $this; 
    110121    } 
    111122 
     
    113124     * creates header for authorization 
    114125     * 
    115      * @param  string  $user      login name 
    116      * @param  string  $password  login password 
     126     * @param   string          $user      login name 
     127     * @param   string          $password  login password 
     128     * @return  stubHeaderList 
    117129     */ 
    118130    public function putAuthorization($user, $password) 
    119131    { 
    120132        $this->put('Authorization', 'BASIC ' . base64_encode($user . ':' . $password)); 
     133        return $this; 
    121134    } 
    122135 
     
    124137     * adds a date header 
    125138     * 
    126      * @param  int  $date  optional  timestamp to use as date 
     139     * @param   int             $date  optional  timestamp to use as date 
     140     * @return  stubHeaderList 
    127141     */ 
    128142    public function putDate($date = null) 
     
    135149         
    136150        $this->put('Date', $date . ' GMT'); 
     151        return $this; 
    137152    } 
    138153 
    139154    /** 
    140155     * creates X-Binford header 
     156     * 
     157     * @return  stubHeaderList 
    141158     */ 
    142159    public function enablePower() 
    143160    { 
    144161        $this->put('X-Binford', 'More power!'); 
     162        return $this; 
    145163    } 
    146164 
    147165    /** 
    148166     * removes all headers 
     167     * 
     168     * @return  stubHeaderList 
    149169     */ 
    150170    public function clear() 
    151171    { 
    152172        $this->headers = array(); 
     173        return $this; 
    153174    } 
    154175 
  • trunk/src/test/php/net/stubbles/peer/http/stubHTTPURLTestCase.php

    r1485 r1486  
    239239    { 
    240240        $http = stubHTTPURL::fromString('http://example.com/'); 
    241         $httpconnection = $http->getConnection(); 
     241        $httpconnection = $http->connect(); 
    242242        $this->assertType('stubHTTPConnection', $httpconnection); 
    243243    } 
  • trunk/src/test/php/net/stubbles/peer/stubHeaderListTestCase.php

    r1485 r1486  
    4848    public function put() 
    4949    {     
    50         $this->headerList->put('Binford', 6100); 
     50        $this->assertSame($this->headerList, $this->headerList->put('Binford', 6100)); 
    5151        $this->assertEquals(1, $this->headerList->size()); 
    5252        $this->assertTrue($this->headerList->containsKey('Binford')); 
     
    9494    public function remove() 
    9595    { 
    96         $this->headerList->put('Binford', '6100'); 
    97         $this->headerList->remove('Binford'); 
     96        $this->assertSame($this->headerList, $this->headerList->put('Binford', '6100')); 
     97        $this->assertSame($this->headerList, $this->headerList->remove('Binford')); 
    9898        $this->assertFalse($this->headerList->containsKey('Binford')); 
    9999    } 
     
    106106    public function putX() 
    107107    { 
    108         $this->headerList->putUserAgent('Binford 6100'); 
    109         $this->headerList->putReferer('Home Improvement'); 
    110         $this->headerList->putCookie(array('testcookie1' => 'testvalue1 %&')); 
    111         $this->headerList->putAuthorization('user', 'pass'); 
     108        $this->assertSame($this->headerList, $this->headerList->putUserAgent('Binford 6100')); 
     109        $this->assertSame($this->headerList, $this->headerList->putReferer('Home Improvement')); 
     110        $this->assertSame($this->headerList, $this->headerList->putCookie(array('testcookie1' => 'testvalue1 %&'))); 
     111        $this->assertSame($this->headerList, $this->headerList->putAuthorization('user', 'pass')); 
    112112        $time = time(); 
    113         $this->headerList->putDate($time); 
    114         $this->headerList->enablePower(); 
     113        $this->assertSame($this->headerList, $this->headerList->putDate($time)); 
     114        $this->assertSame($this->headerList, $this->headerList->enablePower()); 
    115115         
    116116        $this->assertTrue($this->headerList->containsKey('User-Agent')); 
     
    137137    public function clear() 
    138138    { 
    139         $this->headerList->putUserAgent('Binford 6100'); 
    140         $this->headerList->putReferer('Home Improvement'); 
    141         $this->headerList->putCookie(array('testcookie1' => 'testvalue1 %&')); 
    142         $this->headerList->putAuthorization('user', 'pass'); 
    143         $this->headerList->putDate(time()); 
    144         $this->headerList->enablePower(); 
     139        $this->assertSame($this->headerList, 
     140                          $this->headerList->putUserAgent('Binford 6100') 
     141                                            ->putReferer('Home Improvement') 
     142                                            ->putCookie(array('testcookie1' => 'testvalue1 %&')) 
     143                                            ->putAuthorization('user', 'pass') 
     144                                            ->putDate(time()) 
     145                                            ->enablePower() 
     146        ); 
    145147         
    146148        $this->assertEquals(6, $this->headerList->size());