Skip to content

Commit

Permalink
fix: remove http continue header if present
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Dec 4, 2021
1 parent 94d69f1 commit dd9ad1c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,24 @@ private function execute($handle): array
throw new \RuntimeException(sprintf('cURL error %s: %s', curl_errno($handle), curl_error($handle)));
}

$headers = explode("\n", preg_replace('/\n[ \t]/', ' ', str_replace("\r\n", "\n", $rawHeaders)));
$headers = (new Collection(explode("\n", preg_replace('/\n[ \t]/', ' ', str_replace("\r\n", "\n", $rawHeaders)))))->filter();
$matches = [];

preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)[ \t]+(.+)#i', array_shift($headers), $matches);
// Remove HTTP Continue header if present
if (preg_match('#^HTTP/1\.\d[ \t]+100[ \t]+Continue#i', $headers[0])) {
$headers->shift();
}

// Parse HTTP response
preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)[ \t]+(.+)#i', $headers->shift(), $matches);

if (!isset($matches[2], $matches[3])) {
throw new \RuntimeException('Unable to parse response code');
throw new \RuntimeException('Unable to parse HTTP response code');
}

return [
'body' => $body,
'headers' => (new Collection($headers))->filter()->mapWithKeys(function (string $header) {
'headers' => $headers->mapWithKeys(function (string $header) {
list($key, $value) = explode(':', $header, 2);

return [strtolower($key) => preg_replace('#(\s+)#i', ' ', trim($value))];
Expand Down
24 changes: 24 additions & 0 deletions src/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,30 @@ public function search($value, $strict = false)
return array_search($value, $this->items, $strict);
}

/**
* Get and remove the first N items from the collection.
*/
public function shift(int $count = 1)
{
if (1 === $count) {
return array_shift($this->items);
}

if ($this->isEmpty()) {
return new self();
}

$results = [];

$collectionCount = $this->count();

foreach (range(1, min($count, $collectionCount)) as $item) {
$results[] = array_shift($this->items);
}

return new self($results);
}

/**
* Extract a slice of the collection.
*/
Expand Down

0 comments on commit dd9ad1c

Please sign in to comment.