Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Weird routing behavior #582

Closed
josemota opened this Issue · 3 comments

4 participants

@josemota

Hello everyone, this is striking me as severely odd and I need help in finding its root cause.

Scenario

I have the following Sinatra app:

require "sinatra/base"

IMAGES = [
  { title: "Utopia"     , url: "http://fantasyartdesign.com/free-wallpapers/imgs/mid/68landscapes04-m241.jpg" },
  { title: "Alaska"     , url: "http://www.davidsfotos.com/LANDSCAPESpage_files/LANDSCAPES2.jpg" },
  { title: "The Unknown", url: "http://www.beautifullife.info/wp-content/uploads/2010/12/31/the-unknown.jpg" },
]

class App < Sinatra::Base
  # ...
  get "/images" do
    @images = IMAGES
    erb :images
  end

  #
  # The route under test
  #
  get "/images/:index.?:format?" do |index, format|
    index = index.to_i
    @image = IMAGES[index]
    @format = params[:format]

    haml :"images/show", layout: true
  end
  # ...
end

Basic manual tracing

| Route          | Status                      |
------------------------------------------------
| /images/0.jpeg | 200                         |
| /images/1.jpeg | 200                         |
| /images/2.jpeg | 404                         |
| /images/3.jpeg | 500 # No index for that one |

Can anyone explain me why this is happening? I don't have any sort of clue on this...!

Fresh clone of master, by the way. Let me know if you need anything else. Thanks.

@mtoledo

I can confirm something is going on. There's a difference in behavior between sinatra 1.3.3 and master in how it treats the /images/2.jpg request

Here's the app I used:

require "sinatra/base"

IMAGES = [
  { title: "Utopia"     , url: "http://fantasyartdesign.com/free-wallpapers/imgs/mid/68landscapes04-m241.jpg" },
  { title: "Alaska"     , url: "http://www.davidsfotos.com/LANDSCAPESpage_files/LANDSCAPES2.jpg" },
  { title: "The Unknown", url: "http://www.beautifullife.info/wp-content/uploads/2010/12/31/the-unknown.jpg" },
]

class App < Sinatra::Base

  get "/images/:index.?:format?" do |index, format|
    index = index.to_i
    @image = IMAGES[index]
    @format = params[:format]

    "#{@image.inspect} #{format.inspect}"
  end
end

App.run!

Here's 1.3.3

[ruby-1.9.3-p194]~ % curl -sI localhost:4567/images/2.jpeg | head -1
HTTP/1.1 200 OK 

[ruby-1.9.3-p194]~/Projects/test % bundle exec ruby app.rb
[2012-10-10 22:34:01] INFO  WEBrick 1.3.1
[2012-10-10 22:34:01] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin11.3.0]
== Sinatra/1.3.3 has taken the stage on 4567 for development with backup from WEBrick
[2012-10-10 22:34:01] INFO  WEBrick::HTTPServer#start: pid=86469 port=4567
localhost - - [10/Oct/2012:22:34:07 BRT] "HEAD /images/2.jpeg HTTP/1.1" 200 0
- -> /images/2.jpeg
^C
== Sinatra has ended his set (crowd applauds)
[2012-10-10 22:34:39] INFO  going to shutdown ...
[2012-10-10 22:34:39] INFO  WEBrick::HTTPServer#start done.

Here's master

[ruby-1.9.3-p194]~ % curl -sI localhost:4567/images/2.jpeg | head -1
HTTP/1.1 404 Not Found

[ruby-1.9.3-p194]~/Projects/test % bundle exec ruby app.rb
[2012-10-10 22:34:59] INFO  WEBrick 1.3.1
[2012-10-10 22:34:59] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin11.3.0]
== Sinatra/1.4.0 has taken the stage on 4567 for development with backup from WEBrick
[2012-10-10 22:34:59] INFO  WEBrick::HTTPServer#start: pid=86556 port=4567
localhost - - [10/Oct/2012:22:35:15 BRT] "HEAD /images/2.jpeg HTTP/1.1" 404 0
- -> /images/2.jpeg
^C
== Sinatra has ended his set (crowd applauds)
[2012-10-10 22:37:05] INFO  going to shutdown ...
[2012-10-10 22:37:05] INFO  WEBrick::HTTPServer#start done.
@josemota

Important to say that in 1.3.3 the index variable is greedy and takes format as well, hence having to use master.

@kgrz

@josemota #611 This has the reason why its happening.

@rkh rkh closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.