Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into ValidatorMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej Szendi committed Nov 4, 2013
Show file tree
Hide file tree
Showing 143 changed files with 753 additions and 283 deletions.
22 changes: 12 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public function setAdapter($adapter)
*/
public function getAdapter()
{
if (! $this->adapter) {
$this->setAdapter($this->config['adapter']);
}

return $this->adapter;
}

Expand Down Expand Up @@ -812,10 +816,7 @@ public function send(Request $request = null)
$this->redirectCounter = 0;
$response = null;

// Make sure the adapter is loaded
if ($this->adapter == null) {
$this->setAdapter($this->config['adapter']);
}
$adapter = $this->getAdapter();

// Send the first request. If redirected, continue.
do {
Expand Down Expand Up @@ -868,7 +869,7 @@ public function send(Request $request = null)
}

// check that adapter supports streaming before using it
if (is_resource($body) && !($this->adapter instanceof Client\Adapter\StreamInterface)) {
if (is_resource($body) && !($adapter instanceof Client\Adapter\StreamInterface)) {
throw new Client\Exception\RuntimeException('Adapter does not support streaming');
}

Expand Down Expand Up @@ -896,15 +897,15 @@ public function send(Request $request = null)
rewind($stream);
}
// cleanup the adapter
$this->adapter->setOutputStream(null);
$adapter->setOutputStream(null);
$response = Response\Stream::fromStream($response, $stream);
$response->setStreamName($this->streamName);
if (!is_string($this->config['outputstream'])) {
// we used temp name, will need to clean up
$response->setCleanup(true);
}
} else {
$response = Response::fromString($response);
$response = $this->getResponse()->fromString($response);
}

// Get the cookies from response (if any)
Expand Down Expand Up @@ -1153,9 +1154,10 @@ protected function prepareHeaders($body, $uri)
}

// Merge the headers of the request (if any)
$requestHeaders = $this->getRequest()->getHeaders()->toArray();
foreach ($requestHeaders as $key => $value) {
$headers[$key] = $value;
// here we need right 'http field' and not lowercase letters
$requestHeaders = $this->getRequest()->getHeaders();
foreach ($requestHeaders as $requestHeaderElement) {
$headers[$requestHeaderElement->getFieldName()] = $requestHeaderElement->getFieldValue();
}
return $headers;
}
Expand Down
24 changes: 15 additions & 9 deletions src/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,15 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
if (isset($this->config['curloptions'][CURLOPT_INFILE])) {
// Now we will probably already have Content-Length set, so that we have to delete it
// from $headers at this point:
foreach ($headers AS $k => $header) {
if (preg_match('/Content-Length:\s*(\d+)/i', $header, $m)) {
if (is_resource($body)) {
$this->config['curloptions'][CURLOPT_INFILESIZE] = (int) $m[1];
}
unset($headers[$k]);
}
if (!isset($headers['Content-Length'])
&& !isset($this->config['curloptions'][CURLOPT_INFILESIZE])
) {
throw new AdapterException\RuntimeException("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
}

if (!isset($this->config['curloptions'][CURLOPT_INFILESIZE])) {
throw new AdapterException\RuntimeException("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
if (isset($headers['Content-Length'])) {
$this->config['curloptions'][CURLOPT_INFILESIZE] = (int) $headers['Content-Length'];
unset($headers['Content-Length']);
}

if (is_resource($body)) {
Expand Down Expand Up @@ -427,6 +425,14 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
$this->response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->response);
}

// cURL can automatically handle content encoding; prevent double-decoding from occurring
if (isset($this->config['curloptions'][CURLOPT_ENCODING])
&& '' == $this->config['curloptions'][CURLOPT_ENCODING]
&& stripos($this->response, "Content-Encoding: gzip\r\n")
) {
$this->response = str_ireplace("Content-Encoding: gzip\r\n", '', $this->response);
}

// Eliminate multiple HTTP responses.
do {
$parts = preg_split('|(?:\r?\n){2}|m', $this->response, 2);
Expand Down
2 changes: 1 addition & 1 deletion src/Header/AbstractDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function fromString($headerLine)
{
$dateHeader = new static();

list($name, $date) = explode(': ', $headerLine, 2);
list($name, $date) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== strtolower($dateHeader->getFieldName())) {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/AbstractLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function fromString($headerLine)
$locationHeader = new static();

// ZF-5520 - IIS bug, no space after colon
list($name, $uri) = explode(':', $headerLine, 2);
list($name, $uri) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== strtolower($locationHeader->getFieldName())) {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/AcceptRanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'accept-ranges') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Age.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'age') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Allow.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'allow') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/AuthenticationInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'authentication-info') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'authorization') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/CacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'cache-control') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'connection') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentDisposition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-disposition') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentEncoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-encoding') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-language') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-length') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentMD5.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-md5') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-range') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentTransferEncoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-transfer-encoding') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-type') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'cookie') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Etag.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'etag') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Expect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'expect') {
Expand Down
2 changes: 1 addition & 1 deletion src/Header/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function fromString($headerLine)
{
$header = new static();

list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'from') {
Expand Down
40 changes: 33 additions & 7 deletions src/Header/GenericHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,30 @@ class GenericHeader implements HeaderInterface
*/
public static function fromString($headerLine)
{
list($fieldName, $fieldValue) = explode(': ', $headerLine, 2);
list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine);
$header = new static($fieldName, $fieldValue);
return $header;
}

/**
* Splits the header line in `name` and `value` parts.
*
* @param string $headerLine
* @return string[] `name` in the first index and `value` in the second.
* @throws Exception\InvalidArgumentException If header does not match with the format ``name:value``
*/
public static function splitHeaderLine($headerLine)
{
$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name:value"');
}

$parts[1] = ltrim($parts[1]);

return $parts;
}

/**
* Constructor
*
Expand All @@ -61,7 +80,7 @@ public function __construct($fieldName = null, $fieldValue = null)
*
* @param string $fieldName
* @return GenericHeader
* @throws Exception\InvalidArgumentException(
* @throws Exception\InvalidArgumentException If the name does not match with RFC 2616 format.
*/
public function setFieldName($fieldName)
{
Expand All @@ -70,12 +89,19 @@ public function setFieldName($fieldName)
}

// Pre-filter to normalize valid characters, change underscore to dash
$fieldName = str_replace(' ', '-', ucwords(str_replace(array('_', '-'), ' ', $fieldName)));

// Validate what we have
if (!preg_match('/^[a-z][a-z0-9-]*$/i', $fieldName)) {
$fieldName = str_replace('_', '-', $fieldName);

/*
* Following RFC 2616 section 4.2
*
* message-header = field-name ":" [ field-value ]
* field-name = token
*
* @see http://tools.ietf.org/html/rfc2616#section-2.2 for token definition.
*/
if (!preg_match('/^[!#-\'*+\-\.0-9A-Z\^-z|~]+$/', $fieldName)) {
throw new Exception\InvalidArgumentException(
'Header name must start with a letter, and consist of only letters, numbers, and dashes'
'Header name must be a valid RFC 2616 (section 4.2) field-name.'
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Header/GenericMultiHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GenericMultiHeader extends GenericHeader implements MultipleHeaderInterfac
{
public static function fromString($headerLine)
{
list($fieldName, $fieldValue) = explode(': ', $headerLine, 2);
list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine);

if (strpos($fieldValue, ',')) {
$headers = array();
Expand Down
Loading

0 comments on commit a05e97f

Please sign in to comment.