Git Product home page Git Product logo

Comments (7)

eode avatar eode commented on August 23, 2024

btw -- nice work, overall!

from uri.

eode avatar eode commented on August 23, 2024

Actually, you can't reject paths with one. What an awkward state to be in. Is the extra slash in http://foo.bar//blah possibly meaningful?

I suppose there's no route to rejection. I guess expected behavior would be to implicitly add a slash, and if path is set manually with a slash, it results in a url like http://foo.com//blah.

from uri.

amcgregor avatar amcgregor commented on August 23, 2024

Hmm; it is true that you can provide paths that have no root. Technically, your own code above is buggy, though the URI class is not helping. Specifically, assign /blah, not blah, or use the division helper:

>>> uri = URI('http://foo.com') # no path part at all
>>> uri.path
PurePosixPath('.')
>>> uri.path = '/foo'
>>> uri
URI('http://foo.com/foo')
>>> uri = URI('http://foo.com')

# "natural" syntax for "extending" the path / resolving relative references
>>> uri / 'foo'
URI('http://foo.com/foo')

# scheme + authority part, plus a path part = rooted path at that authority over that protocol
>>> URI('http://foo.com') / URI('bar/baz')
URI('http://foo.com/bar/baz')

I'll leave this open for now as a reminder that non-root paths should be rooted for presentation, or, if there is an authority present, to only accept rooted paths. foo/bar isn't a valid URI path, where /foo/bar is. Good catch!

Edited to add: non-rooted paths remain useful, at least for URI without other, earlier components defined. This would allow you to, for example, use the division operator to combine two URI, the second being a URI fragment. (The second having uri.relative == True.). Think of relative links on a page; those are very much fragments, usually of non-root paths, and part of the reason why the division operator was added, reference issue #3.

Edited to add: I'm not seeing double slashes if you assign a rooted path. How are you getting that as a result?

from uri.

amcgregor avatar amcgregor commented on August 23, 2024
>>> from uri import URI
>>> uri = URI('http://foo.com')  # not a fragment; has an authority
>>> uri.path = '/foo'; uri  # rooted path assignment is OK
URI('http://foo.com/foo')
>>> uri.path = 'foo'  # rootless path assignment is NOT OK

ValueError                                Traceback (most recent call last)
<ipython-input-3-d765cb2bdfb3> in <module>()
----> 1 uri.path = 'foo'

~/Projects/marrow/web/uri/uri/part/path.py in __set__(self, obj, value)
     28 
     29                 if obj.authority and not value.startswith('/'):
---> 30                         raise ValueError("Can only assign rooted paths to URI with authority.")
     31 
     32                 super(PathPart, self).__set__(obj, value)

ValueError: Can only assign rooted paths to URI with authority.

>>> uri = URI('http://foo.com')
>>> uri2 = URI()  # let's construct a fragment this time
>>> uri2.path = 'foo'; uri2
URI('foo')
>>> uri.resolve(uri2)  # relative reference resolution against an authority becomes rooted
URI('http://foo.com/foo')

Available in 84e0787 for testing prior to release. (Tests added in 74a16f6.)

from uri.

eode avatar eode commented on August 23, 2024

from uri.

amcgregor avatar amcgregor commented on August 23, 2024

Marking as resolved. (Rejection is preferable to me; from the Zen of Python: errors should never pass silently, esp. as if you aren't expecting a relative path it might have security implications.). Edited to add: URI are not just URL. This is not a URL object, nor a URL-handling library, specifically. Your bug was intentionally trying to write "http://foo.com" + "blah", which URI happily (and incorrectly) allowed you to do. (2+2==42 → if you want this to be true, it's not a bug in Python's addition operator…)

from uri.

eode avatar eode commented on August 23, 2024

I most definitely would not want that to be true -- what I want is for there to be an implicit slash if an unrooted path is attached to a host, or better yet, for the library to reject unrooted paths in that case. Which it does now, so everything's roly-poly.

from uri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.