Problem/Motivation

I'm a bit confused by #2372507-74: Remove _system_path from $request->attributes.4 (leading /) and the changes based on that. At least, we need to fix the documentation in CurrentPathStack::getPath() because that still says it returns it without a leading /, and given that it is 50/50 when we need a leading / it, maybe there should be an easier way to get the path without leading /?

IMHO, leading / was always a good way to differentiate between a url string (which is yet another thing, as commented above, it also includes the subfolder you're in) and the path. We need to better explain who should be using it and when, the current explanation there reads like an excuse for it's existence :)

Proposed resolution

Remaining tasks

User interface changes

API changes

Comments

dawehner’s picture

Issue tags: +Novice

Good catch, yeah that got forgotten after the review of crell.

zealfire’s picture

Assigned: Unassigned » zealfire
zealfire’s picture

I have made changes in the documentation after deducing that we just need to return path without mentioning that / will be trimmed.Have i deduced correct ?.Please review.
Thanks

zealfire’s picture

Status: Active » Needs review

Changing the status.

berdir’s picture

I think we should explicitly mention that it includes the leading /.

zealfire’s picture

Made changes according to comment 5.Please review.
Thanks.

berdir’s picture

Issue summary: View changes

After some discussions with @dawehner, it make make sense to have some more documentation on what this actually is. like aliased vs unaliased, and language prefix. Possibly explicitly describe the differences between getPathInfo() and this.

Also, the last sentence in the Note is a typical @dawehner sentence, I'm not sure we should document a service class like that :)

jhodgdon’s picture

Status: Needs review » Needs work

Hm. I was asked to comment here. The current patch looks fine as far as it goes.

I'll follow additional patches... I am all for more clear documentation on what is being returned, of course! Sounds like this is Needs Work for #7?

Crell’s picture

Agreed with Berdir and jhodgdon. Standardize on leading / everywhere, fix this doc, and possibly improve the doc for which path we're talking about.

kerby70’s picture

I am suggesting:

/**
   * Returns the path of the current request.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   (optional) The request.
   *
   * @return string
   *   Returns the non URI decoded request path, with leading slash,
   *   without queries or the base URL.
   */

getPath() calls getPathInfo() that has:

    /**
     * Returns the path being requested relative to the executed script.
     *
     * The path info always starts with a /.
     *
     * Suppose this request is instantiated from /mysite on localhost:
     *
     *  * http://localhost/mysite              returns an empty string
     *  * http://localhost/mysite/about        returns '/about'
     *  * http://localhost/mysite/enco%20ded   returns '/enco%20ded'
     *  * http://localhost/mysite/about?var=1  returns '/about'
     *
     * @return string The raw path (i.e. not urldecoded)
     *
     * @api
     */

Two more levels in getRequestUri() has:

    /**
     * Returns the requested URI (path and query string).
     *
     * @return string The raw URI (i.e. not URI decoded)
     *
     * @api
     */
jhodgdon’s picture

Thanks for the new patch... but it does not address the questions in #7. I am also not sure it is accurate... or at least I think using the term "URI decoded" is misleading (it sounds to me as though it's about urlencode/urldecode, and that is not related to what these functions do).

cilefen’s picture

Title: Fix CurrentPath::getPath() documentation that says it has no leading / (and make it easier to actually get that?) » Fix CurrentPathStack::getPath() documentation that says it has no leading / (and make it easier to actually get that?)
mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new581 bytes

Here is a patch which fixes documentation on trailing slash, and brings over urldecoded notes from Request::getPathInfo().

jhodgdon’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: -Novice

OK, I'm pretty confused now.

I was attempting to review the patch here to see if it was accurate... I decided that it doesn't merit a Novice tag until we sort out what is happening. Sorry mglaman -- you followed the instructions (thanks!) but I am not sure the patch is the correct documentation.

So.

CurrentPathStack::getPath(), if you look at the code, is returning the result of a \Symfony\Component\HttpFoundation\Request::getPathInfo() call, which says "always starts with \".

However, what it actually does is to return the result of a call to its preparePathInfo() method. This method returns "\" if it can't figure out the path, and otherwise it strips getBaseUrl() off the request URL and returns that.

The base URL comes from the prepareBaseUrl() method. It looks as though this method always strips off the last / so that getPath() should always start with a / no matter what.

So I'm very confused about this issue. What makes you think that CurrentPathStack::getPath() has the wrong documentation? I must have missed something...

dawehner’s picture

Adding a related issue

Status: Postponed (maintainer needs more info) » Needs review

mgifford queued 13: fix-2430805-13.patch for re-testing.

mgifford’s picture

Assigned: zealfire » Unassigned

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

deviantintegral’s picture

I'm seeing different behaviour if a node has a path alias. When the path alias is used, the returned path has a leading slash. I'm not 100% sure, but since \Drupal\Core\Url::fromUserInput requires a leading slash this gets pretty confusing.

Here's two patches - one with the docs fix, and the other that throws a \LogicException. Since this method itself doesn't I'm not 100% sure that the path alias is the cause, so I'd like to see if this fails any tests.

Status: Needs review » Needs work

The last submitted patch, 24: 2430805.24-leading-slash-with-exception.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

deviantintegral’s picture

Status: Needs work » Needs review
StatusFileSize
new1.27 KB

Huh, not running tests on CS fails is new. That's too bad, it adds a roadblock to running tests like this.

\Drupal\Core\Path\CurrentPathStack::setPath() doesn't ensure that the leading slash is set, unlike \Symfony\Component\HttpFoundation\Request::getPathInfo. That means that for routes, \Drupal\Core\Routing\RouteProvider::getRouteCollectionForRequest ends up setting paths with the leading slash. The easiest way to see this is to set a breakpoint with strpos($path, '/') === 0; at the uncached setPath call in getRouteCollectionForRequest. Clear caches from the UI, and you'll see them being set.

My initial thought was to treat the docs as correct, but stripping the leading slash in setPath() hoses the site.

Here's a patch that validates incoming paths in setPath. If all the tests pass I think we can call this a docs bug. I don't think this is a hard BC break because there's no interface defined, but it's certainly one worth calling out.

dawehner’s picture

My initial thought was to treat the docs as correct, but stripping the leading slash in setPath() hoses the site.

Do you mind elaborating a bit more on that? I could imagine that throwing an exception here is problematic too. Maybe triggering an error / having an assert would be a better approach to not cause 500s on production, which leads to really problematic behaviours on reverse proxies.

deviantintegral’s picture

StatusFileSize
new993 bytes

Here's the exception thrown when clearing caches with the attached patch that instead removes any leading slashes. I'm guessing this will cause a ton of test fails.

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Symfony\Component\HttpKernel\Exception\NotFoundHttpException</em>: No route found for &quot;POST /admin/config/development/performance&quot; (from &quot;http://d8.local/admin/config/development/performance&quot;) in <em class="placeholder">Symfony\Component\HttpKernel\EventListener\RouterListener-&gt;onKernelRequest()</em> (line <em class="placeholder">139</em> of <em class="placeholder">vendor/symfony/http-kernel/EventListener/RouterListener.php</em>). <pre class="backtrace">Drupal\Core\Routing\AccessAwareRouter-&gt;matchRequest(Object) (Line: 115)
Symfony\Component\HttpKernel\EventListener\RouterListener-&gt;onKernelRequest(Object, &#039;kernel.request&#039;, Object)
call_user_func(Array, Object, &#039;kernel.request&#039;, Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&gt;dispatch(&#039;kernel.request&#039;, Object) (Line: 127)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 665)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
</pre>

Status: Needs review » Needs work

The last submitted patch, 28: 2430805.28-remove-leading-slash.patch, failed testing. View results

deviantintegral’s picture

Status: Needs work » Needs review

Back to review for the patch in #26.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Seems there were some errors in #28 from what I can tell. Least the tests aren't green.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.