namshi / cuzzle

This library let's you dump a Guzzle request to a cURL command for debug and log purpose.
https://github.com/namshi/cuzzle
330 stars 67 forks source link

[Guzzle 6] Exception : Query and fragment must be a string #13

Closed bmancone closed 8 years ago

bmancone commented 8 years ago

In CurlFormatter::extractUrlArgument, calling withFragment with null throws an exception because the PSR-7 Uri implementation of Guzzle immediatly calls filterQueryAndFragment which checks if the fragment is a string :

// CurlFormatter.php

$uri = $request->getUri();
$uri = $uri->withFragment(null); /// <----------------

$this->addCommandPart(escapeshellarg((string)$uri));
// Uri.php

public function withFragment($fragment)
{
    $fragment = $this->filterQueryAndFragment($fragment); /// <----------------

    if ($this->fragment === $fragment) {
        return $this;
    }

    $new = clone $this;
    $new->fragment = $fragment;
    return $new;
}

private function filterQueryAndFragment($str)
{
    if (!is_string($str)) { /// <----------------
        throw new \InvalidArgumentException('Query and fragment must be a string');
    }

    return preg_replace_callback(
        '/(?:[^' . self::$charUnreserved . self::$charSubDelims . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/',
        [$this, 'rawurlencodeMatchZero'],
        $str
    );
}

Should an empty string be passed instead ? Or not be called at all (depending if the string representation of the Uri holds the fragment, as it's the case for Guzzle) ?

Brice

xkobal commented 8 years ago

I create a Pull Request to fix the fragment set to null.

bmancone commented 8 years ago

Fixed in #14