Skip to content

Commit

Permalink
Enhance RateLimit header parsing
Browse files Browse the repository at this point in the history
This is now more readable and can deal with gitlab and github's variants
(e.g. with and without the 'X-'-Prefix) and is fully case-insensitive.
  • Loading branch information
theseer committed May 27, 2020
1 parent ef9fb06 commit ffa6a3b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/shared/http/CurlHttpClient.php
Expand Up @@ -74,13 +74,18 @@ public function handleProgressInfo($ch, int $expectedDown, int $received, int $e

public function handleHeaderInput($ch, string $line): int {
$parts = \explode(':', \trim($line));

if (\strtolower($parts[0]) === 'etag') {
$this->etag = new ETag(\trim($parts[1]));
if (!isset($parts[1])) {
return \mb_strlen($line);
}

if (\strpos($parts[0], 'X-RateLimit-') !== false) {
$this->rateLimitHeaders[\substr($parts[0], 12)] = \trim($parts[1]);
[$header, $value] = $parts;
$header = \ucfirst(\strtolower($header));
$value = \trim($value);

if ($header === 'Etag') {
$this->etag = new ETag($value);
} else if (preg_match('/^(X-)?RateLimit-(.*)$/i', $header, $matches) === 1) {
$this->rateLimitHeaders[$matches[2]] = $value;
}

return \mb_strlen($line);
Expand Down

0 comments on commit ffa6a3b

Please sign in to comment.