Skip to content

Commit

Permalink
fixup! Fix serving files that clash with directories
Browse files Browse the repository at this point in the history
Try and make things more reasable by seperating assignment from if
conditions and removing early returns.

I still kept assignment in the while condition, because it is a nice and
simple way to assign the first non-"/" string popped off from the path
array.

You should be able to autosquash out this commit, since it is a fixup
commit.
  • Loading branch information
MattSturgeon committed Jul 19, 2017
1 parent 811ed0f commit 20a333f
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions lib/jekyll/commands/serve/servlet.rb
Expand Up @@ -29,31 +29,38 @@ def search_file(req, res, basename)
def search_index_file(req, res)
# /file/index.html -> /file.html

# First, let's see if the default implementation can figure it out.
# (Check for index files in the res.filename directory)
if file = super
return file
# First, let's see if the our super method can figure it out.
# (i.e Check for index.html files in the res.filename directory)
file = super

unless file
# Ok, I guess that didn't work, I guess there's no basename/index.html
# Let's look for basename.html instead...

# Keep a backup of res.filename in case we need to revert our changes to it
old_filename = res.filename

# We need to calculate the basename and remove it from the path (res.filename)
# so we'll turn res.filename into an array of path elements then pop off the
# basename.
#
# We use a while loop just in case res.filename has trailing slashes.
#
# Once we have popped off the basename, we can join up what's left and use it
# as the new res.filename.
path_arr = res.filename.scan(%r!/[^/]*!)
while basename = path_arr.pop
break unless basename == "/"
end
res.filename = path_arr.join

# Try and find a file named dirname.html in the parent directory.
file = search_file(req, res, basename + ".html")

# If we didn't find a file, revert our changes to res.filename .
res.filename = old_filename unless file
end

# Ok, I guess that didn't work, I guess there's no basename/index.html
# Let's look for basename.html instead...

# We need to extract the final part of the path
path_arr = res.filename.scan(%r!/[^/]*!)
while basename = path_arr.pop
break unless basename == "/"
end

# We need to change res.filename to the parent directory for
# search_file to work, so make a backup incase it doesn't work
old_filename = res.filename
res.filename = path_arr.join

# Try and find a file named dirname.html in the parent directory
unless file = search_file(req, res, basename + ".html")
# Don't modify filename unless we actually found a file to serve
res.filename = old_filename
end
return file
end
# rubocop:enable Lint/AssignmentInCondition
Expand Down

0 comments on commit 20a333f

Please sign in to comment.