Static Page Routing in Sinatra (Ruby)

Static page routing in Sinatra (Ruby)

Probably a better answer will eventually come, until then this is my shot at it.

If this is not what you want:

get '/' do
redirect '/index.html'
end

You might do something like this:

get '/' do
File.new('public/index.html').readlines
end

I'd go with the first one though, Not sure why you want to avoid that redirect

Serving static files with Sinatra

Without any additional configuration, Sinatra will serve assets in public. For the empty route, you'll want to render the index document.

require 'rubygems'
require 'sinatra'

get '/' do
File.read(File.join('public', 'index.html'))
end

Routes should return a String which become the HTTP response body. File.read opens a file, reads the file, closes the file and returns a String.

View routes in Sinatra

I'm not sure if this is a security concern here (I'm not that into all the details of Sinatra) but I tend to be paranoid when using user specified data like for example params['page'] in your example. As said I'm not sure if Sinatra sanitizes the content and would make this example impossible, but imagine it said something like ../db_connection.yml. So Sinatra would be told to load the haml file pages/../db_connection.yml which might actually exist and display it to the user showing them your database configuration.

If you don't have any weird symlinks in your pages directory it would probably be enough to replace all double occurences of a dot with something like .gsub(/\.+/, ".") of the passed string (or replace all dots if you don't need them in the name to be even more paranoid). I'm not sure if there are any multi-byte insecurities where someone could do something ugly with encodings though and it might be useless to do the replacing at all because the exploit would work nevertheless.

Edit: Short reading into the Sinatra manual yielded that

By the way, unless you disable the path traversal attack protection (see below), the request path might be modified before matching against your routes.

So it seams that it should be secure to just use the params value without any special filtering, you may however like to look into the documentation some more (especially the security sections). However I don't think it's too much of a security issue if it's possible to figure out if a file in your pages directory exists or not.

Serving static files in Sinatra... with beautiful routes?

Got it!

get '/path_to_file/:number/:file' do
File.read(File.join('path_to_file', "#{params[:number]}", "#{params[:file]}"))
end

get '/path_to_file/:number' do
File.read(File.join('path_to_file', "#{params[:number]}", "index.html"))
end

Order is important, since if these two methods are reversed, get '/path_to_file/:number' becomes a superset of get '/path_to_file/:number/:file'.

Sinatra and a client-side routing

Use wildcard route to return index.html to all route.

Something like this:

get '/*' do
#return index.html
end

Serving static files from Sinatra with case-insensitive URLs

Here is an idea for your routing:

get '/' do
send_file(File.join(File.dirname(__FILE__), 'public', 'index.html'))
end

get '*' do
file_path = File.join(File.dirname(__FILE__), 'public', request.path.downcase)
File.exist?(file_path) ? send_file(file_path) : halt 404
end

not_found do
send_file(File.join(File.dirname(__FILE__), 'public', '404.html'))
end

This is untested, since I probably won't be able to reproduce your environment anyways. But I think that it would work for your purpose, assuming all your pages are lowercase (as you mentioned) and that you also have an actual 404.html page.

Serving large static files with Sinatra

I think I can just use send_file (see here) - but if anyone has any other suggestions I have open ears!

How to render a plain HTML file with Sinatra?

Does send_file do what you want?

e.g.

  get '/myspecialroute' do
send_file 'special.html'
end

What is a better way to convert a simple sinatra app to static html pages?

Try Httrack



Related Topics



Leave a reply



Submit