Changeset 1493
- Timestamp:
- 04/04/08 17:08:47 (3 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/peer/http/stubHTTPConnection.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/peer/http/stubHTTPRequest.php (modified) (7 diffs)
- trunk/src/main/php/net/stubbles/peer/http/stubHTTPResponse.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/peer/PeerTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/peer/http/stubHTTPConnectionTestCase.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/peer/http/stubHTTPRequestTestCase.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/peer/http/stubHTTPConnection.php
r1492 r1493 155 155 * returns response object for given URL after GET request 156 156 * 157 * @param string $version optional HTTP version 157 158 * @return stubHTTPResponse 158 159 */ 159 public function get( )160 { 161 return $this->createRequest()-> send(stubHTTPRequest::METHOD_GET);160 public function get($version = null) 161 { 162 return $this->createRequest()->get($version); 162 163 } 163 164 … … 165 166 * returns response object for given URL after HEAD request 166 167 * 168 * @param string $version optional HTTP version 167 169 * @return stubHTTPResponse 168 170 */ 169 public function head( )170 { 171 return $this->createRequest()-> send(stubHTTPRequest::METHOD_HEAD);171 public function head($version = null) 172 { 173 return $this->createRequest()->head($version); 172 174 } 173 175 … … 176 178 * 177 179 * @param array<string,string> $postValues post data to send with POST request 180 * @param string $version optional HTTP version 178 181 * @return stubHTTPResponse 179 182 */ 180 public function post( $postValues)181 { 182 return $this->createRequest()->preparePost($postValues)-> send(stubHTTPRequest::METHOD_POST);183 public function post(array $postValues, $version = null) 184 { 185 return $this->createRequest()->preparePost($postValues)->post($version); 183 186 } 184 187 trunk/src/main/php/net/stubbles/peer/http/stubHTTPRequest.php
r1486 r1493 9 9 stubClassLoader::load('net::stubbles::peer::stubHeaderList', 10 10 'net::stubbles::peer::stubConnectionException', 11 'net::stubbles::peer::stubSocket' 11 'net::stubbles::peer::stubSocket', 12 'net::stubbles::peer::http::stubHTTPConnection', 13 'net::stubbles::peer::http::stubHTTPResponse', 14 'net::stubbles::peer::http::stubHTTPURL' 12 15 ); 13 16 /** … … 24 27 * @var stubHTTPURL 25 28 */ 26 protected $http = null;29 protected $httpURL = null; 27 30 /** 28 31 * contains request headers … … 67 70 * constructor 68 71 * 69 * @param stubHTTPURL $http HTTP URL to perform a request to72 * @param stubHTTPURL $httpURL HTTP URL to perform a request to 70 73 * @param stubHeaderList $header list of request headers 71 74 * @param int $timeout timeout for connection in seconds 72 75 */ 73 public function __construct(stubHTTPURL $http , stubHeaderList $header, $timeout)76 public function __construct(stubHTTPURL $httpURL, stubHeaderList $header, $timeout) 74 77 { 75 $this->http = $http;78 $this->httpURL = $httpURL; 76 79 $this->headers = $header; 77 80 $this->timeout = $timeout; … … 79 82 80 83 /** 81 * factory method for more fluent use84 * initializes a get request 82 85 * 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 86 * @param string $version optional HTTP version 87 * @return stubHTTPResponse 87 88 */ 88 public static function create(stubHTTPURL $http, stubHeaderList $header, $timeout)89 public function get($version = null) 89 90 { 90 return new self($http, $header, $timeout); 91 $socket = $this->createSocket(); 92 $this->processHeader($socket, self::METHOD_GET, $version); 93 $socket->write(stubHTTPConnection::END_OF_LINE); 94 return new stubHTTPResponse($socket); 95 } 96 97 /** 98 * initializes a head request 99 * 100 * @param string $version optional HTTP version 101 * @return stubHTTPResponse 102 */ 103 public function head($version = null) 104 { 105 $socket = $this->createSocket(); 106 $this->processHeader($socket, self::METHOD_HEAD, $version); 107 $socket->write('Connection: close' . stubHTTPConnection::END_OF_LINE . stubHTTPConnection::END_OF_LINE); 108 return new stubHTTPResponse($socket); 91 109 } 92 110 … … 94 112 * creates required headers for post request and encodes post values 95 113 * 96 * @param array<string,string> $postValue post values to submit114 * @param array<string,string> $postValues post values to submit 97 115 * @return stubHTTPRequest 98 116 */ 99 public function preparePost(array $postValue )117 public function preparePost(array $postValues) 100 118 { 101 foreach ($postValue as $key => $value) {119 foreach ($postValues as $key => $value) { 102 120 $this->body .= urlencode($key) . '=' . urlencode($value) . '&'; 103 121 } … … 109 127 110 128 /** 111 * do a request on the given URL129 * initializes a post request 112 130 * 113 * @param string $method request type, allowed: POST, GET, PUT, standard: GET 114 * @param string $version optional version of HTTP-protocol to use, standard: HTTP/1.0 115 * @return stubHTTPResponse response of given request 131 * @param string $version optional HTTP version 132 * @return stubHTTPResponse 116 133 */ 117 public function send($method, $version = null) 134 public function post($version = null) 135 { 136 $socket = $this->createSocket(); 137 $this->processHeader($socket, self::METHOD_POST, $version); 138 $socket->write(stubHTTPConnection::END_OF_LINE); 139 $socket->write($this->body); 140 return new stubHTTPResponse($socket); 141 } 142 143 /** 144 * creates the socket 145 * 146 * @return stubSocket 147 */ 148 protected function createSocket() 149 { 150 return new stubSocket($this->httpURL->getHost(), $this->httpURL->getPort()); 151 } 152 153 /** 154 * helper method to send the headers 155 * 156 * @param stubSocket $socket the socket to write headers to 157 * @param string $method HTTP method 158 * @param string $version HTTP version 159 */ 160 protected function processHeader(stubSocket $socket, $method, $version) 118 161 { 119 162 if (self::METHOD_POST != $method && self::METHOD_GET != $method && self::METHOD_HEAD != $method) { … … 125 168 } 126 169 127 $socket = $this->createSocket();128 170 $socket->setTimeout($this->timeout); 129 171 $socket->connect(); 130 $socket->write($method . ' ' . $this->http ->getPath() . ' ' . $version . stubHTTPConnection::END_OF_LINE);131 $socket->write('Host: ' . $this->http ->getHost() . stubHTTPConnection::END_OF_LINE);172 $socket->write($method . ' ' . $this->httpURL->getPath() . ' ' . $version . stubHTTPConnection::END_OF_LINE); 173 $socket->write('Host: ' . $this->httpURL->getHost() . stubHTTPConnection::END_OF_LINE); 132 174 133 175 // prepare last headers and write all headers to socket 134 $this->headers->putDate();135 176 if ($this->headers->containsKey('User-Agent') === false) { 136 177 $this->headers->putUserAgent('stubbles HTTP Client'); 137 178 } 138 179 139 $this->headers->putPower(); 180 $this->headers->enablePower(); 181 $this->headers->putDate(); 140 182 foreach ($this->headers as $key => $value) { 141 183 $socket->write($key . ': ' . $value . stubHTTPConnection::END_OF_LINE); 142 184 } 143 144 switch ($method) {145 // on GET: write one further end of line146 case self::METHOD_GET:147 $socket->write(stubHTTPConnection::END_OF_LINE);148 break;149 150 // on POST we have to write the body151 case self::METHOD_POST:152 $socket->write(stubHTTPConnection::END_OF_LINE);153 $socket->write($this->body);154 break;155 156 // on HEAD we write one additional header and close the connection157 case self::METHOD_HEAD:158 $socket->write('Connection: close' . stubHTTPConnection::END_OF_LINE . stubHTTPConnection::END_OF_LINE);159 break;160 161 default:162 // same as GET163 $socket->write(stubHTTPConnection::END_OF_LINE);164 }165 166 $response = new stubHTTPResponse($socket);167 return $response;168 }169 170 /**171 * creates the socket172 *173 * @return stubSocket174 */175 protected function createSocket()176 {177 return new stubSocket($this->http->getHost(), $this->http->getPort());178 185 } 179 186 } trunk/src/main/php/net/stubbles/peer/http/stubHTTPResponse.php
r1486 r1493 107 107 108 108 /** 109 * returns the used socket 110 * 111 * @return stubSocket 112 */ 113 public function getSocket() 114 { 115 return $this->socket; 116 } 117 118 /** 109 119 * read the response from socket and parse it 110 120 */ trunk/src/test/php/net/stubbles/peer/PeerTestSuite.php
r1492 r1493 32 32 // http classes 33 33 $suite->addTestFile($dir . '/http/stubHTTPConnectionTestCase.php'); 34 $suite->addTestFile($dir . '/http/stubHTTPRequestTestCase.php'); 34 35 $suite->addTestFile($dir . '/http/stubHTTPURLTestCase.php'); 35 36 trunk/src/test/php/net/stubbles/peer/http/stubHTTPConnectionTestCase.php
r1492 r1493 107 107 ); 108 108 $mockHTTPRequest->expects($this->once()) 109 ->method(' send')110 ->with(stubHTTPRequest:: METHOD_GET)109 ->method('get') 110 ->with(stubHTTPRequest::VERSION_1_1) 111 111 ->will($this->returnValue($mockHTTPResponse)); 112 $this->assertSame($mockHTTPResponse, $this->httpConnection->get( ));112 $this->assertSame($mockHTTPResponse, $this->httpConnection->get(stubHTTPRequest::VERSION_1_1)); 113 113 } 114 114 … … 134 134 ); 135 135 $mockHTTPRequest->expects($this->once()) 136 ->method(' send')137 ->with(stubHTTPRequest:: METHOD_HEAD)136 ->method('head') 137 ->with(stubHTTPRequest::VERSION_1_1) 138 138 ->will($this->returnValue($mockHTTPResponse)); 139 $this->assertSame($mockHTTPResponse, $this->httpConnection->head( ));139 $this->assertSame($mockHTTPResponse, $this->httpConnection->head(stubHTTPRequest::VERSION_1_1)); 140 140 } 141 141 142 142 /** 143 * initializing a headrequest should return a response object143 * initializing a post request should return a response object 144 144 * 145 145 * @test … … 163 163 ); 164 164 $mockHTTPRequest->expects($this->once()) 165 ->method(' send')166 ->with(stubHTTPRequest:: METHOD_POST)165 ->method('post') 166 ->with(stubHTTPRequest::VERSION_1_1) 167 167 ->will($this->returnValue($mockHTTPResponse)); 168 $this->assertSame($mockHTTPResponse, $this->httpConnection->post(array('foo' => 'bar') ));168 $this->assertSame($mockHTTPResponse, $this->httpConnection->post(array('foo' => 'bar'), stubHTTPRequest::VERSION_1_1)); 169 169 } 170 170 }
