Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request target is invalid when uri path is empty #25

Open
weierophinney opened this issue Dec 31, 2019 · 1 comment
Open

Request target is invalid when uri path is empty #25

weierophinney opened this issue Dec 31, 2019 · 1 comment
Labels
Bug Something isn't working

Comments

@weierophinney
Copy link
Member

::getRequestTarget()

http://tools.ietf.org/html/rfc7230#section-5.3.1

The most common form of request-target is the origin-form.
origin-form = absolute-path [ "?" query ]

The query component is optional, the path is required.
But in code if exists only query - return it

        $target = $this->uri->getPath();
        if ($this->uri->getQuery()) {
            $target .= '?' . $this->uri->getQuery();
        }
        if (empty($target)) {
            $target = '/';
        }

probably it would be correct:

        $target = $this->uri->getPath();
        if (! $target) {
            $target = '/';
        }
        if ($this->uri->getQuery()) {
            $target .= '?' . $this->uri->getQuery();
        }
        return $target;

Originally posted by @easy-system at zendframework/zend-diactoros#153

@weierophinney
Copy link
Member Author

::withRequestTarget()

if (preg_match('#\s#', $requestTarget)) {
...

What if my path contains not ASCII characters?
Probably, the URI class must have public methods encode and decode?
Moreover, the PSR-7 almost directly talking about it:

Users can provide both encoded and decoded path characters.

That is, if the user wants to decode the string - there are decoding method.
If we need to encode a string (eg, target) - there is an encoding method.

I think that something like this:

    public static function encode($value, $ignore = null)
    {
        if (null === $ignore) {
            $ignore = static::CHAR_UNRESERVED
                    . static::CHAR_GEN_DELIMS
                    . static::CHAR_SUB_DELIMS;
        }
        $rawurlencode = function (array $matches) {
            return rawurlencode($matches[0]);
        };

        return preg_replace_callback(
            '/(?:[^' . $ignore . '%]+|%(?![A-Fa-f0-9]{2}))/',
            $rawurlencode,
            $value
        );
    }

    /**
     * Decode URI-encoded strings.
     *
     * @param string $value The encoded string
     *
     * @return string The decoded string
     */
    public static function decode($value)
    {
        return rawurldecode($value);
    }

Or even easier

    protected function setTarget($target)
    {
        if ('*' == $target) {
            $this->target = $target;

            return $this;
        }
        $uri = new Uri($target);
        $this->target = (string) $uri;

        return $this;
    }

Originally posted by @easy-system at zendframework/zend-diactoros#153 (comment)

@Xerkus Xerkus changed the title RequestTrait Request target is invalid when uri path is empty May 1, 2023
@Xerkus Xerkus added the Bug Something isn't working label May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants