Changeset 1486
- Timestamp:
- 04/02/08 23:58:46 (8 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/peer/http/stubHTTPConnection.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/peer/http/stubHTTPRequest.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/peer/http/stubHTTPResponse.php (modified) (5 diffs)
- trunk/src/main/php/net/stubbles/peer/http/stubHTTPURL.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/peer/stubHeaderList.php (modified) (11 diffs)
- trunk/src/test/php/net/stubbles/peer/http/stubHTTPURLTestCase.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/peer/stubHeaderListTestCase.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/peer/http/stubHTTPConnection.php
r1485 r1486 22 22 * request object to open connection 23 23 * 24 * @var stubHTTP Request24 * @var stubHTTPURL 25 25 */ 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; 27 39 /** 28 40 * end-of-line marker 29 41 */ 30 const END_OF_LINE = "\r\n";42 const END_OF_LINE = "\r\n"; 31 43 32 44 /** … … 37 49 public function __construct(stubHTTPURL $http) 38 50 { 39 $this->request = new stubHTTPRequest($http); 51 $this->url = $http; 52 $this->headers = new stubHeaderList(); 40 53 } 41 54 … … 43 56 * set timeout for connection 44 57 * 45 * @param int $timeout timeout for connection in seconds 58 * @param int $timeout timeout for connection in seconds 59 * @return stubHTTPConnection 46 60 */ 47 public function setTimeout($timeout)61 public function timeout($timeout) 48 62 { 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; 50 127 } 51 128 … … 53 130 * returns response object for given URL after GET request 54 131 * 55 * @param stubHeaderList $headers optional list of headers to use56 132 * @return stubHTTPResponse 57 133 */ 58 public function get( stubHeaderList $headers = null)134 public function get() 59 135 { 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); 66 138 } 67 139 … … 69 141 * returns response object for given URL after HEAD request 70 142 * 71 * @param stubHeaderList $headers optional list of headers to use72 143 * @return stubHTTPResponse 73 144 */ 74 public function head( stubHeaderList $headers = null)145 public function head() 75 146 { 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); 82 149 } 83 150 … … 85 152 * returns response object for given URL after POST request 86 153 * 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 89 155 * @return stubHTTPResponse 90 156 */ 91 public function post($postValues , stubHeaderList $headers)157 public function post($postValues) 92 158 { 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); 100 162 } 101 163 } trunk/src/main/php/net/stubbles/peer/http/stubHTTPRequest.php
r1485 r1486 24 24 * @var stubHTTPURL 25 25 */ 26 protected $http = null;26 protected $http = null; 27 27 /** 28 28 * contains request headers … … 30 30 * @var stubHeaderList 31 31 */ 32 protected $headers = null;32 protected $headers = null; 33 33 /** 34 34 * contains body for request … … 36 36 * @var string 37 37 */ 38 protected $body = '';38 protected $body = ''; 39 39 /** 40 40 * timeout … … 42 42 * @var int 43 43 */ 44 protected $timeout = 30;44 protected $timeout = 30; 45 45 /** 46 46 * request method type: GET 47 47 */ 48 const METHOD_GET = 'GET';48 const METHOD_GET = 'GET'; 49 49 /** 50 50 * request method type: POST 51 51 */ 52 const METHOD_POST = 'POST';52 const METHOD_POST = 'POST'; 53 53 /** 54 54 * request method type: HEAD 55 55 */ 56 const METHOD_HEAD = 'HEAD';56 const METHOD_HEAD = 'HEAD'; 57 57 /** 58 58 * HTTP version: 1.0 59 59 */ 60 const VERSION_1_0 = 'HTTP/1.0';60 const VERSION_1_0 = 'HTTP/1.0'; 61 61 /** 62 62 * HTTP version: 1.1 63 63 */ 64 const VERSION_1_1 = 'HTTP/1.1';64 const VERSION_1_1 = 'HTTP/1.1'; 65 65 66 66 /** 67 * constructor for this class.67 * constructor 68 68 * 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 70 72 */ 71 public function __construct(stubHTTPURL $http )73 public function __construct(stubHTTPURL $http, stubHeaderList $header, $timeout) 72 74 { 73 75 $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; 94 77 $this->timeout = $timeout; 95 78 } 96 79 97 80 /** 98 * creates needed headers for post request and encodes post values81 * factory method for more fluent use 99 82 * 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 101 87 */ 102 public function preparePost(array $post_value)88 public static function create(stubHTTPURL $http, stubHeaderList $header, $timeout) 103 89 { 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) { 105 102 $this->body .= urlencode($key) . '=' . urlencode($value) . '&'; 106 103 } … … 108 105 $this->headers->put('Content-Type', 'application/x-www-form-urlencoded'); 109 106 $this->headers->put('Content-Length', strlen($this->body)); 107 return $this; 110 108 } 111 109 … … 135 133 // prepare last headers and write all headers to socket 136 134 $this->headers->putDate(); 137 if ($this->headers->containsKey('User-Agent') == false) {135 if ($this->headers->containsKey('User-Agent') === false) { 138 136 $this->headers->putUserAgent('stubbles HTTP Client'); 139 137 } trunk/src/main/php/net/stubbles/peer/http/stubHTTPResponse.php
r1485 r1486 24 24 * @var stubSocket 25 25 */ 26 protected $socket = null;26 protected $socket = null; 27 27 /** 28 28 * list of response parts … … 30 30 * @var array 31 31 */ 32 protected $response = array();32 protected $response = array(); 33 33 /** 34 34 * contains headers of response … … 36 36 * @var stubHeaderList 37 37 */ 38 protected $headers = null;38 protected $headers = null; 39 39 /** 40 40 * contains body of response … … 42 42 * @var string 43 43 */ 44 protected $body = '';44 protected $body = ''; 45 45 /** 46 46 * response type data: status line … … 103 103 public function __destruct() 104 104 { 105 $this->socket-> __destruct();105 $this->socket->disconnect(); 106 106 } 107 107 trunk/src/main/php/net/stubbles/peer/http/stubHTTPURL.php
r1485 r1486 161 161 * creates a stubHTTPConnection for this URL 162 162 * 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 * 163 171 * @return stubHTTPConnection 164 172 */ 165 public function getConnection()173 public function connect() 166 174 { 167 175 stubClassLoader::load('net::stubbles::peer::http::stubHTTPConnection'); 168 $httpconnection = new stubHTTPConnection($this); 169 return $httpconnection; 176 return new stubHTTPConnection($this); 170 177 } 171 178 trunk/src/main/php/net/stubbles/peer/stubHeaderList.php
r1485 r1486 46 46 * creates header with value for key 47 47 * 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 50 51 * @throws stubIllegalArgumentException 51 52 */ … … 61 62 62 63 $this->headers[$key] = (string) $value; 64 return $this; 63 65 } 64 66 … … 66 68 * removes header with given key 67 69 * 68 * @param string $key name of header 70 * @param string $key name of header 71 * @return stubHeaderList 69 72 */ 70 73 public function remove($key) … … 73 76 unset($this->headers[$key]); 74 77 } 78 79 return $this; 75 80 } 76 81 … … 78 83 * creates header for user agent 79 84 * 80 * @param string $userAgent name of user agent 85 * @param string $userAgent name of user agent 86 * @return stubHeaderList 81 87 */ 82 88 public function putUserAgent($userAgent) 83 89 { 84 90 $this->put('User-Agent', $userAgent); 91 return $this; 85 92 } 86 93 … … 88 95 * creates header for referer 89 96 * 90 * @param string $referer referer url 97 * @param string $referer referer url 98 * @return stubHeaderList 91 99 */ 92 100 public function putReferer($referer) 93 101 { 94 102 $this->put('Referer', $referer); 103 return $this; 95 104 } 96 105 … … 98 107 * creates header for cookie 99 108 * 100 * @param array $cookieValues cookie values 109 * @param array $cookieValues cookie values 110 * @return stubHeaderList 101 111 */ 102 112 public function putCookie(array $cookieValues) … … 108 118 109 119 $this->put('Cookie', $cookieValue); 120 return $this; 110 121 } 111 122 … … 113 124 * creates header for authorization 114 125 * 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 117 129 */ 118 130 public function putAuthorization($user, $password) 119 131 { 120 132 $this->put('Authorization', 'BASIC ' . base64_encode($user . ':' . $password)); 133 return $this; 121 134 } 122 135 … … 124 137 * adds a date header 125 138 * 126 * @param int $date optional timestamp to use as date 139 * @param int $date optional timestamp to use as date 140 * @return stubHeaderList 127 141 */ 128 142 public function putDate($date = null) … … 135 149 136 150 $this->put('Date', $date . ' GMT'); 151 return $this; 137 152 } 138 153 139 154 /** 140 155 * creates X-Binford header 156 * 157 * @return stubHeaderList 141 158 */ 142 159 public function enablePower() 143 160 { 144 161 $this->put('X-Binford', 'More power!'); 162 return $this; 145 163 } 146 164 147 165 /** 148 166 * removes all headers 167 * 168 * @return stubHeaderList 149 169 */ 150 170 public function clear() 151 171 { 152 172 $this->headers = array(); 173 return $this; 153 174 } 154 175 trunk/src/test/php/net/stubbles/peer/http/stubHTTPURLTestCase.php
r1485 r1486 239 239 { 240 240 $http = stubHTTPURL::fromString('http://example.com/'); 241 $httpconnection = $http-> getConnection();241 $httpconnection = $http->connect(); 242 242 $this->assertType('stubHTTPConnection', $httpconnection); 243 243 } trunk/src/test/php/net/stubbles/peer/stubHeaderListTestCase.php
r1485 r1486 48 48 public function put() 49 49 { 50 $this-> headerList->put('Binford', 6100);50 $this->assertSame($this->headerList, $this->headerList->put('Binford', 6100)); 51 51 $this->assertEquals(1, $this->headerList->size()); 52 52 $this->assertTrue($this->headerList->containsKey('Binford')); … … 94 94 public function remove() 95 95 { 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')); 98 98 $this->assertFalse($this->headerList->containsKey('Binford')); 99 99 } … … 106 106 public function putX() 107 107 { 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')); 112 112 $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()); 115 115 116 116 $this->assertTrue($this->headerList->containsKey('User-Agent')); … … 137 137 public function clear() 138 138 { 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 ); 145 147 146 148 $this->assertEquals(6, $this->headerList->size());
