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

Update README to flag issue with optional routes. #1025

Merged
merged 1 commit into from
Jan 24, 2016

Conversation

JonRowe
Copy link
Contributor

@JonRowe JonRowe commented Aug 13, 2015

The original example would also match GET /posts_anything as GET /posts with a format of "_anything" because of the optional format separator, forcing the separator is kind of an ugly example so switch to using a trailing slash which should be recognised either way (I think). It'd be nice to be able to create optional groups e.g. (.:format)?

get '/posts.?:format?' do
# matches "GET /posts" and any extension "GET /posts.json", "GET /posts.xml" etc.
get '/post/:format?' do
# matches "GET /posts" and any extension "GET /posts/json", "GET /posts/xml" etc
Copy link
Member

Choose a reason for hiding this comment

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

I feel the description quite doesn't fit here since it looks like a path fragment rather than an extension. Perhaps this should be reworded?

Copy link
Contributor

Choose a reason for hiding this comment

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

Additionally, the proposed change would not match "GET /posts", but "GET /post/".

How about something that would be more typically used as a path fragment? Perhaps

get '/posts/?:year?' do
  # matches "GET /posts" and any year "GET /posts/2009", "GET /posts/2010", etc
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

get /posts/:year? would be ok but the the /? means the problem would remain, as it'd match posts_2005 as a :year of _2005

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right. Damn, I missed that.

OK, so if we do get '/posts/:year?, then the comment would have to say that it `matches "GET /posts/", which sucks as a route, since people typing things in would tend to leave off the trailing slash.

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 believe most HTTP servers automatically mismatch the trailing ‘/‘, which is how the /folder/index.html trick works as a /folder url (e.g. with middleman), or at least thats what I was lead to believe when I picked the example :)

Copy link
Contributor

Choose a reason for hiding this comment

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

This is what I get:

$ cat app.rb 
require 'sinatra'

get '/hello/' do
  'Hello world!'
end

$ ruby app.rb
$ curl -v 'localhost:4567/hello/'
HTTP/1.1 200 OK

$ curl -v 'localhost:4567/hello/'
HTTP/1.1 404 Not Found

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah it's true webbrick doesn't but nginx and apache fronting it would, tricky.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, wow. I didn't realize that nginx/apache would treat these differently. Yeah, that is tricky :/

Thin behaves the same way as webrick. I didn't check puma and unicorn.

@kytrinyx
Copy link
Contributor

kytrinyx commented Sep 1, 2015

@mhutter left a comment in #1023 (comment) which I think is the right way to go for fixing the README. What do you think @JonRowe ?

@mhutter
Copy link

mhutter commented Sep 1, 2015

Sooo question here, is it really expected for the . in strings to behave like a Regex-.?

@JonRowe
Copy link
Contributor Author

JonRowe commented Sep 1, 2015

I actually tried that route at fist @kytrinyx but couldn't make it work, which is why I went down the rewording the readme to avoid the issue entirely route.

@mhutter
Copy link

mhutter commented Sep 1, 2015

I would actually leave the README as-is - It's only an example.

Or replace by a completely generic scenario, such as

get '/hello/:name?' do
  name = params[:name] || "World!"
  "Hello, #{name}"
end

But then again this example will behave differently depending on the webserver...

@JonRowe
Copy link
Contributor Author

JonRowe commented Sep 2, 2015

Thats what this PR is about, changing the example to a more correct if more generic example, just to prevent confusion as in #1023

@JonRowe
Copy link
Contributor Author

JonRowe commented Sep 22, 2015

Anything I can do to move this along?

The original example would also match `GET /posts_anything` as `GET /posts` with a format of `"_anything"`
@zzak
Copy link
Member

zzak commented Jan 24, 2016

We have to update the translations too, let me see if I can fix this.

zzak pushed a commit that referenced this pull request Jan 24, 2016
Update README to flag issue with optional routes.
@zzak zzak merged commit c4c6e2a into sinatra:master Jan 24, 2016
zzak pushed a commit that referenced this pull request Jan 24, 2016
@JonRowe JonRowe deleted the patch-1 branch January 24, 2016 12:05
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