Skip to content

Update to rust-url 1.0 - #9840

Merged
bors-servo merged 6 commits into
masterfrom
url-1.0
Apr 23, 2016
Merged

bors-servo merged 6 commits into
masterfrom
url-1.0

Conversation

@SimonSapin

@SimonSapin SimonSapin commented Mar 2, 2016 •

Copy link
Copy Markdown
Member

@highfive

highfive commented Mar 2, 2016

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @bholley: components/style/selector_matching.rs, components/style/parser.rs, components/style/font_face.rs

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 2, 2016
@highfive

highfive commented Mar 2, 2016

Copy link
Copy Markdown

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@asajeffrey

Copy link
Copy Markdown
Contributor

Removing the url! plugin means there's now urls being parsed at run-time rather than compile-time (e.g. about:blank). This is a step back in performance, and introduces some unwraps that weren't there before. The performance hit is probably pretty negligible, and the unwraps are annoying but most likely safe.

At some point, rust might get constant functions, then we could define (e.g) const ABOUT_BLANK: Url = Url::parse("about:blank").unwrap();.

@asajeffrey

Copy link
Copy Markdown
Contributor

Reviewed 63 of 63 files at r1.
Review status: all files reviewed at latest revision, 13 unresolved discussions, some commit checks failed.


components/devtools/lib.rs, line 278 [r1] (raw file):
It would be nice to be able to convert directly from a Url to String. I think this is unnecessarily cloning the URL string representation.


components/layout/layout_thread.rs, line 966 [r1] (raw file):
Since URL implements Debug shouldn't this be debug!("layout: received layout request for: {:?}", self.url);?


components/net/hsts.rs, line 126 [r1] (raw file):
Slightly unintuitive having set_scheme return a Result which needs to be unwrapped. Would it be worth exporting a Scheme type containing pre-parsed schemes, then having set_scheme just return ()? Or is this because of the error conditition "This URL is non-relative and the new scheme is one of http, https, ws, wss, ftp, or gopher" (whcih AFAICT is unenforced)?


components/net/http_loader.rs, line 691 [r1] (raw file):
Can we do better than reparsing the URL? Can rust-url export a helper method for this? Not sure about the unwrap() here.


components/net/storage_thread.rs, line 219 [r1] (raw file):
Much nicer!


components/net/fetch/methods.rs, line 541 [r1] (raw file):
Bikeshed the name join?


components/net_traits/hosts.rs, line 74 [r1] (raw file):
Should url export a helper function set_ip_host for this?


components/script/cors.rs, line 429 [r1] (raw file):
Inconsistent line break.


components/script/script_thread.rs, line 1864 [r1] (raw file):
What happened to steps 5-8? Are they now in lossy_utf8_percent_decode? If so, a comment would be good.


components/script/dom/element.rs, line 1081 [r1] (raw file):
I think this unnecessarily clones the url.


components/script/dom/macros.rs, line 102 [r1] (raw file):
Is this cloning the URL?


components/script/dom/urlhelper.rs, line 12 [r1] (raw file):
OK, this is much nicer!


tests/unit/util/opts.rs, line 18 [r1] (raw file):
Not obvious why path_segments() needs an unwrap(). Should it just return the empty iterator if there are none?


Comments from the review on Reviewable.io

@asajeffrey

Copy link
Copy Markdown
Contributor

There's only a handful of constant URLs. Put them in the URL crate?

@asajeffrey asajeffrey added S-awaiting-answer Someone asked a question that requires an answer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 2, 2016
@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 3, 2016
@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #9715) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added the S-needs-rebase There are merge conflict errors. label Mar 3, 2016
@asajeffrey

Copy link
Copy Markdown
Contributor

Reviewed 1 of 1 files at r2.
Review status: all files reviewed at latest revision, 13 unresolved discussions, some commit checks failed.


Comments from the review on Reviewable.io

@asajeffrey asajeffrey removed the S-awaiting-review There is new code that needs to be reviewed. label Mar 4, 2016
@SimonSapin

Copy link
Copy Markdown
Member Author

@frewsxcv said on IRC that he mostly implemented url! because it seemed easy and to learn Rust compiler plugin. The performance benefit was never measured, but I’d bet it’s negligible in Servo.

Re constant URLs, Rust’s const fn is currently very limited and can definitely not run at compile-time an algorithm like Url::parse.


Review status: all files reviewed at latest revision, 13 unresolved discussions, some commit checks failed.


components/devtools/lib.rs, line 278 [r1] (raw file):
I’ve added Url::into_string and used it here.


components/layout/layout_thread.rs, line 966 [r1] (raw file):
Now that I check I see that RefCell implements Debug so that would work. But it’s more verbose, printing something like Refcell { value: "http://example.net" }


components/net/hsts.rs, line 126 [r1] (raw file):
It’s not just set_scheme, some other setter methods return Result<(), ()> to indicate that they may fail (and do nothing) in some cases. As you mention for scheme there are parsing errors, but even for just that I’m not convinced that a pre-parsed Scheme type is more convenient than slapping .unwrap() here and there. There is an other case that I wrote up in the doc-comment and, you’re right, forgot to implement. I’ve just done that.


components/net/http_loader.rs, line 691 [r1] (raw file):
With an input of e.g. view-source:https://google.com?q=foo, there is no host and the path is https://google.com. This is completely different from what we want, so yes re-parsing is necessary.

unwrap() here can definitely panic, but it already could before.

Both issues could be solved by changing the pattern from view-source:{absolute url} to view-source+{absolute url}. That way we can check for url.scheme().startswith("view-source+") and extract the "inner" URL with url.set_scheme(&scheme["view-source+".len()..]). However that would be a functional change, so I’d prefer to do it in a separate PR.


components/net/fetch/methods.rs, line 541 [r1] (raw file):
There is precedent in std::path::Path with its join method and the urlparse.urljoin function in Python. And this method already exists in 0.5 servo/rust-url#153

Do you have another suggestion?


components/net_traits/hosts.rs, line 74 [r1] (raw file):
Done. I had removed it previously since std::net::IpAddr was not yet #[stable] in the stable channel, but it is now with Rust 1.7.


components/script/cors.rs, line 429 [r1] (raw file):
Fixed.


components/script/script_thread.rs, line 1864 [r1] (raw file):
This let statement does everything in 5 to 8. I’ve introduced an intermediate variable and rephrased the comment.


components/script/dom/element.rs, line 1081 [r1] (raw file):
Fixed.


components/script/dom/macros.rs, line 102 [r1] (raw file):
Fixed.


tests/unit/util/opts.rs, line 18 [r1] (raw file):
Non-relative URLs (soon to be called "URLs that cannot be a base" whatwg/url#89) don’t have a path made of slash-separated segments, they only have a single unstructured string. This is different from a path made of zero segments.


Comments from the review on Reviewable.io

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 24, 2016
@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #9887) made this pull request unmergeable. Please resolve the merge conflicts.

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #10188) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added the S-needs-rebase There are merge conflict errors. label Mar 26, 2016
@asajeffrey

Copy link
Copy Markdown
Contributor

@SimonSapin what's the status on this PR?

@asajeffrey asajeffrey removed the S-awaiting-review There is new code that needs to be reviewed. label Apr 8, 2016
@SimonSapin

Copy link
Copy Markdown
Member Author

@asajeffrey This needs to happen before it can be merged:

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 45acaea with merge 882ce08...

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. S-needs-rebase There are merge conflict errors. S-awaiting-answer Someone asked a question that requires an answer. labels Apr 23, 2016
@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - mac-dev-unit

@highfive highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Apr 23, 2016
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-tests-failed The changes caused existing tests to fail. labels Apr 23, 2016
@SimonSapin

Copy link
Copy Markdown
Member Author

Updated lockfile.

@bors-servo r=asajeffrey

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit e662605 has been approved by asajeffrey

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Apr 23, 2016
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit e662605 with merge 84ab7e9...

@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel, mac-dev-unit, mac-rel-css, mac-rel-wpt

@bors-servo
bors-servo merged commit e662605 into master Apr 23, 2016
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 23, 2016
@SimonSapin
SimonSapin deleted the url-1.0 branch April 23, 2016 20:08
aphrody-dev pushed a commit to aphrody-labs/browser that referenced this pull request Nov 16, 2026
Version 1.0, rewrite the data structure and API

**Do not merge yet**, I’d like to get feedback on API design.

This is a large rewrite of the data structures and API that aims to eventually become version 1.0.0. Motivations are:

* I designed rust-url when I was relatively new to Rust, coming from Python where memory allocation and ownership are not concerns. The URL Standard also writes algorithms to optimize for human comprehension, not computer resources. As a result, each component of the `Url` struct is a `String` or a `Vec<String>`.
* At the same time, I tried to use enums to enforce some invariants in the type system. For example, “non-relative” like `data:` don’t have a host or port number. In practice I think this makes the API a pain to use more than it’s worth.
* The spec [has changed](whatwg/url@b266a43) a lot (notably around dealing with protocols/schemes outside the small list it knows about), and I’m not sure the API can be adapted backward-compatibly anyway.

I think the API can be more "rusty", and a bunch of long-standing issues be fixed along the way. The big idea is that the `Url` struct now contains a single `String` of the URL’s serialization (the only heap memory allocation), plus some indices into it (and some other meta-data). The indices allow accessor methods for URL components to return `&str` in O(1) time.

The fields of `Url` are now private, as they need to maintain a number of invariants. Getting invariants wrong is not a memory-safety hazard (e.g. string indexing is still checked) but can cause non-sensical results.

Rather than multiple ad-hoc methods like `serialize_authority` and `serialize_without_fragment`, `Url` implements indexing with ranges of `Position`, which is an enum indicating a position between URL components. For example, `serialize_without_fragment` is now `&url[..Position::AfterQuery]`, takes O(1) time, and returns a `&str` slice.

While we’re making breaking changes, this is an opportunity to clean up APIs that didn’t turn out so great:

* Drop `UrlParser`. The builder pattern works well for `std::process::Command`, but for URLs in practice almost only `base_url` is ever used. v0.5.2 already has [`base_url.join(str)`](https://github.com/servo/rust-url/blob/v0.5.2/src/lib.rs#L867-L874), which stays the same.
* Drop all the `lossy_percent_decode_*` methods. They don’t seem to be used at all. Maybe accessors could return a `PercentEncodedStr` wrapper that dereferences to `&str` and has a `percent_decode()` method?
* In the `percent_encoding` module, there was “duplicated” APIs that returned a new `String` or `Vec<u8>` for convenience, or pushed to an existing `&mut String` or `&mut Vec<u8>` for flexibility. This was a bit awkward, so I’ve changed it to return iterators of `char` or `u8` which can be used with `.extend(…)` or `.collect()`. I’m not sure this an improvement and might revert it. Feedback appreciated.

This new design is great for accessing components, but not so much for modifying them. We need to make changes in the middle of the string, and with percent-encoding we don’t know the encoded size of the new thing until we’ve encoded it. Then we need to fix up indices. As a result, the `set_*` methods have lots of fiddly code where it’s easy to make subtle mistakes. This code should be well-tested before this PR is merged, but it’s not at all yet.

To see what the new API looks like more easily than by reading the code, you can find the output of `cargo doc` at https://simonsapin.github.io/rust-url-1.0-docs/url/

Too see what the API usage is like in practice, I’ve updated some crates that use rust-url for this PR:

* https://github.com/alexcrichton/git2-rs/pull/115
* rust-lang/cargo#2428
* rwf2/cookie-rs#42
* hyperium/hyper#740
* https://github.com/cyderize/rust-websocket/pull/70
* servo/servo#9840

Since this PR isn’t merged yet, to do the same in your crates you’ll need a clone of the git repository at the right branch:

```
git clone https://github.com/servo/rust-url --branch 1.0
```

… and a Cargo [path override](http://doc.crates.io/guide.html#overriding-dependencies) in the directory of your crate:

`.cargo/config`
```
path = [
    "../rust-url",
]
```

Since we’re making breaking changes, this is the opportunity to make *more* (or different) breaking changes before version 1.0 is published and we commit to this new API.

Please comment here to say what are your pain points with the 0.5.x API, what could be improved, what I’ve done in this PR that doesn’t seem like a good idea, etc.

Thanks!

<!-- Reviewable:start -->
<!--  (Simon) Hidden for now
[<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/176)
-->
<!-- Reviewable:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants