small medium large xlarge

Generic-user-small
28 Jan 2009, 14:57
Alex Rudyk (5 posts)

Hi Adam

Got small problem with first exercise from first screencast.

Here is the code for exercise:

get '/:hash' do
  url = @shorten.lookup(params[:hash])
  redirect url.url
end

Final line redirects me to http://0.0.0.0:4567/example.com instead of http://example.com

And I see this sinatra page

Try this:
get '/example.com' do
  "Hello World"
end

I’ve tried original code from screencast and got the same result. My sinatra version is 0.9.0.4, ruby 1.8.6, mac os x

So I wonder, why my behavior is differ from your results. Do you have any idea?

thx

Generic-user-small
28 Jan 2009, 21:50
Matthew Hawkins (1 post)

Did you enter your original URL as http://example.com or just example.com?

If you entered the latter, the string ‘example.com’ is getting returned by ‘url.url’. Since there is no http in the url, the redirect assumes the string ‘example.com’ represents a path on the current site.

2223901045_12c5c7118b_s_pragsmall
30 Jan 2009, 15:30
Adam Keys (4 posts)

Alex, Matthew has the right idea here. The browser is interpreting the redirect as a URL relative to localhost, instead of an absolute URL. Exercise for the reader: add some validation to make sure the submitted URL starts with ‘http://’.

Great find Matthew!

Generic-user-small
03 Feb 2009, 18:49
Alex Rudyk (5 posts)

Thx a lot. It works now

You must be logged in to comment