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

RequestTrait #153

Open
easy-system opened this issue Mar 8, 2016 · 2 comments
Open

RequestTrait #153

easy-system opened this issue Mar 8, 2016 · 2 comments

Comments

@easy-system
Copy link

::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;
@easy-system
Copy link
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;
    }

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-diactoros; a new issue has been opened at laminas/laminas-diactoros#25.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants