Changeset 1604

Show
Ignore:
Timestamp:
05/27/08 10:05:56 (4 months ago)
Author:
mikey
Message:

add separate methods for reading response header and body: allows to use the infrastructure until header is read and to use the socket for reading the body externally

Files:

Legend:

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

    r1495 r1604  
    77 * @subpackage  peer_http 
    88 */ 
    9 stubClassLoader::load('net::stubbles::peer::stubHeaderList', 
     9stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalAccessException', 
     10                      'net::stubbles::peer::stubHeaderList', 
    1011                      'net::stubbles::peer::stubConnectionException', 
    1112                      'net::stubbles::peer::stubSocket' 
     
    118119    /** 
    119120     * read the response from socket and parse it 
     121     * 
     122     * @return  stubHTTPResponse 
    120123     */ 
    121124    public function read() 
     125    { 
     126        return $this->readHeader()->readBody(); 
     127    } 
     128 
     129    /** 
     130     * reads response headers 
     131     * 
     132     * @return  stubHTTPResponse 
     133     */ 
     134    public function readHeader() 
    122135    { 
    123136        $this->parseHead($this->socket->readLine()); 
     
    130143         
    131144        $this->headers = stubHeaderList::fromString($header); 
     145        return $this; 
     146    } 
     147 
     148    /** 
     149     * reads the response body 
     150     * 
     151     * @return  stubHTTPResponse 
     152     * @throws  stubIllegalAccessException 
     153     */ 
     154    public function readBody() 
     155    { 
     156        if (null === $this->headers) { 
     157            throw new stubIllegalAccessException('Need to read response headers first.'); 
     158        } 
     159         
    132160        if ($this->headers->get('Transfer-Encoding') === 'chunked') { 
    133161            $this->body = $this->readChunked(); 
     
    135163            $this->body = $this->readDefault($this->headers->get('Content-Length', 4096)); 
    136164        } 
     165         
     166        return $this; 
    137167    } 
    138168 
  • trunk/src/test/php/net/stubbles/peer/http/stubHTTPResponseTestCase.php

    r1495 r1604  
    124124        $this->assertNull($httpResponse->getHeader()); 
    125125        $this->assertEquals('', $httpResponse->getBody()); 
    126         $httpResponse->read(); 
     126        $this->assertSame($httpResponse, $httpResponse->read()); 
    127127        $this->assertType('stubHeaderList', $httpResponse->getHeader()); 
    128128        $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); 
     
    167167        $this->assertNull($httpResponse->getHeader()); 
    168168        $this->assertEquals('', $httpResponse->getBody()); 
    169         $httpResponse->read(); 
     169        $this->assertSame($httpResponse, $httpResponse->read()); 
    170170        $this->assertType('stubHeaderList', $httpResponse->getHeader()); 
    171171        $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); 
     
    211211        $this->assertNull($httpResponse->getHeader()); 
    212212        $this->assertEquals('', $httpResponse->getBody()); 
    213         $httpResponse->read(); 
     213        $this->assertSame($httpResponse, $httpResponse->read()); 
    214214        $this->assertType('stubHeaderList', $httpResponse->getHeader()); 
    215215        $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); 
    216216        $this->assertEquals('body', $httpResponse->getBody()); 
     217    } 
     218 
     219    /** 
     220     * reading the header only 
     221     * 
     222     * @test 
     223     */ 
     224    public function readHead() 
     225    { 
     226        $httpResponse = $this->getMock('stubHTTPResponse', 
     227                                       array('parseHead', 
     228                                             'readChunked', 
     229                                             'readDefault' 
     230                                       ), 
     231                                       array($this->mockSocket) 
     232                        ); 
     233        $this->mockSocket->expects($this->once()) 
     234                         ->method('readLine') 
     235                         ->will($this->returnValue('HTTP/1.1 200 OK' . stubHTTPConnection::END_OF_LINE)); 
     236        $this->mockSocket->expects($this->exactly(4)) 
     237                         ->method('eof') 
     238                         ->will($this->returnValue(false)); 
     239        $this->mockSocket->expects($this->exactly(3)) 
     240                         ->method('read') 
     241                         ->will($this->onConsecutiveCalls('Host: example.com' . stubHTTPConnection::END_OF_LINE, 
     242                                                          'Content-Length: 2048' . stubHTTPConnection::END_OF_LINE, 
     243                                                          stubHTTPConnection::END_OF_LINE 
     244                                       ) 
     245                           ); 
     246        $httpResponse->expects($this->once()) 
     247                     ->method('parseHead') 
     248                     ->with($this->equalTo('HTTP/1.1 200 OK' . stubHTTPConnection::END_OF_LINE)); 
     249        $this->assertNull($httpResponse->getHeader()); 
     250        $this->assertEquals('', $httpResponse->getBody()); 
     251        $this->assertSame($httpResponse, $httpResponse->readHeader()); 
     252        $this->assertType('stubHeaderList', $httpResponse->getHeader()); 
     253        $this->assertEquals('example.com', $httpResponse->getHeader()->get('Host')); 
     254    } 
     255 
     256    /** 
     257     * reading body without previous reading of headers throws exception 
     258     * 
     259     * @test 
     260     * @expectedException  stubIllegalAccessException 
     261     */ 
     262    public function readBodyWithoutReadingHeadersThrowsException() 
     263    { 
     264        $this->assertSame($this->httpResponse, $this->httpResponse->readBody()); 
    217265    } 
    218266