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

feat(server): add path() and query() to Request #899

Merged
merged 1 commit into from
Aug 29, 2016
Merged

feat(server): add path() and query() to Request #899

merged 1 commit into from
Aug 29, 2016

Conversation

ahmedcharles
Copy link
Contributor

Fixes #896 and #897.

Also improve RequestUri tests.

@ahmedcharles
Copy link
Contributor Author

Are there tests for Request that I should update? I didn't find any obvious ones. Alternatively, I can write unit tests for Request.

pub fn path(&self) -> Option<Cow<str>> {
match self.uri {
RequestUri::AbsolutePath(ref s) => {
let url = Url::parse("http://example.com").ok().and_then(|u| u.join(s).ok());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was imagine using something more primitive:

let end = s.find('?').unwrap_or(s.len());
&s[..end]

Is there a reason that this is incorrect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going for consistency and doing it in a way that I don't have to read the RFC and implement correct parsing and interpretation of urls. Url::parse normalizes the urls, and therefore, it makes a copy. It would be weird if normalized paths and queries were returned for AbsoluteUri but not for AbsolutePath.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, good call. I'm wondering whether it's better to run the Url parser
when constructing the RequestUri, and thus be able to return a reference,
or do it on demand, as this patch does.

On Tue, Aug 23, 2016, 7:30 AM Ahmed Charles [email protected]
wrote:

In src/server/request.rs
#899 (comment):

 /// The target path of this Request.
 #[inline]
  • pub fn path(&self) -> Option<&str> {
  •    match *self.uri {
    
  •        RequestUri::AbsolutePath(ref s) => Some(s),
    
  •        RequestUri::AbsoluteUri(ref url) => Some(&url[::url::Position::BeforePath..]),
    
  •        _ => None
    
  • pub fn path(&self) -> Option<Cow> {
  •    match self.uri {
    
  •        RequestUri::AbsolutePath(ref s) => {
    
  •            let url = Url::parse("http://example.com").ok().and_then(|u| u.join(s).ok());
    

I was going for consistency and doing it in a way that I don't have to
read the RFC and implement correct parsing and interpretation of urls.
Url::parse normalizes the urls, and therefore, it makes a copy. It would be
weird if normalized paths and queries were returned for AbsoluteUri but not
for AbsolutePath.


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/hyperium/hyper/pull/899/files/e4ec7e0644b36b81407ec1899036c5c63b1dc2b9#r75877722,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AADJF2Xxk2msmuotBxxEluLzk2527Onvks5qiwQMgaJpZM4JqgGd
.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also found that appealing as an option. Though I didn't want to change the RequestUri enum. Also, I'm not sure what the appropriate design would be. It'd be nice if Url supported parsing partial urls, then it could ignore the domain.

Perhaps it would be best to implement a custom Url to return as part of AbsolutePath instead of just a String. That way we can return references to owned data while only parsing once, up front.

I'll take a look and see what I come up with.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec claims this variant has "absolute-path [ "?" query ]". Maybe its worth adjusting the variant to AbsolutePath { path: String, query: Option<String> }?

@ahmedcharles
Copy link
Contributor Author

Did you notice the test changes and do you know if they are actually correct or is the implementation buggy? It seems like some things that are accepted should be rejected.

read("hyper.rs", RequestUri::Authority("hyper.rs".to_owned()));
read("/", RequestUri::AbsolutePath("/".to_owned()));
read("*", Some(RequestUri::Star));
read("**", Some(RequestUri::Authority("**".to_owned())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is surprising. I would expect it to be a parse error.

@ahmedcharles
Copy link
Contributor Author

I implemented all of the suggested changes, though it doesn't fix one of the test cases, since I'm not entirely sure how to go about changing the request uri parser.

@seanmonstar
Copy link
Member

travis logs say the code examples in the guide are now failing.

@ahmedcharles
Copy link
Contributor Author

I guess I didn't run the doc tests. I'll do that. :)

Fixes #896 and #897.

Also improve RequestUri tests.
@seanmonstar
Copy link
Member

Excellent, thanks for sticking through to the end!

@seanmonstar seanmonstar merged commit 8b3c120 into hyperium:master Aug 29, 2016
@ahmedcharles ahmedcharles deleted the request branch August 29, 2016 20:52
@ahmedcharles
Copy link
Contributor Author

No problem. I really like that Rust has a tool for testing documentation, since that's something that's always out of date.

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.

2 participants