a cookie handling patch for HTTP_Request
| From: | Alexey Borzov | Date: | Wed, 11 Sep 2002 11:33:51 +0000 |
| Subject: | a cookie handling patch for HTTP_Request | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-9014@lists.php.net to get a copy of this message | ||
Greetings!
I tried to use HTTP_Request/HTTP_Response classes in my project and
found out that they did not work quite well with cookies set in server's
response. HTTP_Response kept all response headers in associative array
(header name => header value). As the response can contain _multiple_
Set-Cookie headers, their values got overwritten. Besides, there
was no easy way to add cookies to request or to extract their
values from response.
This patch adds two methods: addCookie() and getResponseCookies()
to HTTP_Request class and Set-Cookie parsing logic to
HTTP_Response's constructor. Cookies are kept (and returned via
getResponseCookies()) in an array with numerical keys (as multiple
cookies can have the same name).
--
С уважением, Алексей Борзов
отдел интернет-проектов ООО "РДВ-Медиа"
http://www.rdw.ru
http://www.vashdosug.ru
--- /usr/local/share/pear/HTTP/Request.php Wed Aug 14 23:13:57 2002 +++ /home/alex/Request.php Wed Sep 11 14:09:05 2002 @@ -326,6 +326,19 @@ } /** + * Appends a cookie to "Cookie:" header + * + * @param string $name cookie name + * @param string $value cookie value + * @access public + */ + function addCookie($name, $value) + { + $cookies = isset($this->_requestHeaders['Cookie']) ? $this->_requestHeaders['Cookie']. '; ' : ''; + $this->addHeader('Cookie', $cookies . $name . '=' . $value); + } + + /** * Sends the request * * @access public @@ -385,6 +398,16 @@ } /** + * Returns cookies set in response + * + * @access public + */ + function getResponseCookies() + { + return isset($this->_response->_cookies) ? $this->_response->_cookies : false; + } + + /** * Builds the request string * * @access private @@ -470,7 +493,13 @@ * @var array */ var $_headers; - + + /** + * Cookies set in response + * @var array + */ + var $_cookies; + /** * Response body * @var string @@ -504,7 +533,36 @@ $headername = substr($value, 0, strpos($value, ':')); $headervalue = ltrim(substr($value, strpos($value, ':') + 1)); - $this->_headers[$headername] = $headervalue; + if ('Set-Cookie' != $headername) { + $this->_headers[$headername] = $headervalue; + } else { + // parse a SetCookie header to fill _cookies array + $cookie = array( + 'expires' => null, + 'domain' => null, + 'path' => null, + 'secure' => false + ); + // only a name=value pair + if (!strpos($headervalue, ';')) { + list($cookie['name'], $cookie['value']) = array_map('trim', explode('=', $headervalue)); + // some optional parameters are supplied + } else { + $elements = explode(';', $headervalue); + list($cookie['name'], $cookie['value']) = array_map('trim', explode('=', $elements[0])); + for ($i = 1; $i < count($elements); $i++) { + list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); + if ('secure' == $elName) { + $cookie['secure'] = true; + } elseif ('expires' == $elName) { + $cookie['expires'] = str_replace('"', '', $elValue); + } else { + $cookie[$elName] = $elValue; + } + } + } + $this->_cookies[] = $cookie; + } } // Store body
--- /usr/local/share/pear/HTTP/Request.php Wed Aug 14 23:13:57 2002 +++ /home/alex/Request.php Wed Sep 11 14:09:05 2002 @@ -326,6 +326,19 @@ } /** + * Appends a cookie to "Cookie:" header + * + * @param string $name cookie name + * @param string $value cookie value + * @access public + */ + function addCookie($name, $value) + { + $cookies = isset($this->_requestHeaders['Cookie']) ? $this->_requestHeaders['Cookie']. '; ' : ''; + $this->addHeader('Cookie', $cookies . $name . '=' . $value); + } + + /** * Sends the request * * @access public @@ -385,6 +398,16 @@ } /** + * Returns cookies set in response + * + * @access public + */ + function getResponseCookies() + { + return isset($this->_response->_cookies) ? $this->_response->_cookies : false; + } + + /** * Builds the request string * * @access private @@ -470,7 +493,13 @@ * @var array */ var $_headers; - + + /** + * Cookies set in response + * @var array + */ + var $_cookies; + /** * Response body * @var string @@ -504,7 +533,36 @@ $headername = substr($value, 0, strpos($value, ':')); $headervalue = ltrim(substr($value, strpos($value, ':') + 1)); - $this->_headers[$headername] = $headervalue; + if ('Set-Cookie' != $headername) { + $this->_headers[$headername] = $headervalue; + } else { + // parse a SetCookie header to fill _cookies array + $cookie = array( + 'expires' => null, + 'domain' => null, + 'path' => null, + 'secure' => false + ); + // only a name=value pair + if (!strpos($headervalue, ';')) { + list($cookie['name'], $cookie['value']) = array_map('trim', explode('=', $headervalue)); + // some optional parameters are supplied + } else { + $elements = explode(';', $headervalue); + list($cookie['name'], $cookie['value']) = array_map('trim', explode('=', $elements[0])); + for ($i = 1; $i < count($elements); $i++) { + list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i])); + if ('secure' == $elName) { + $cookie['secure'] = true; + } elseif ('expires' == $elName) { + $cookie['expires'] = str_replace('"', '', $elValue); + } else { + $cookie[$elName] = $elValue; + } + } + } + $this->_cookies[] = $cookie; + } } // Store body