Skip to content

Commit

Permalink
fixup! Fix serving files that clash with directories
Browse files Browse the repository at this point in the history
Replace the array spltting and while looping system with a simpler(?)
index check for the last `/`.

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 20a333f commit bb94deb
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions lib/jekyll/commands/serve/servlet.rb
Expand Up @@ -25,7 +25,6 @@ def search_file(req, res, basename)
super || super(req, res, "#{basename}.html")
end

# rubocop:disable Lint/AssignmentInCondition
def search_index_file(req, res)
# /file/index.html -> /file.html

Expand All @@ -37,33 +36,21 @@ def search_index_file(req, res)
# 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
# Split the basename and split it from the path (res.filename)
# Regex matches a / that isn't followed by another / or EOL
index = res.filename.rindex %r!/(?!/|$)!
basename = res.filename[index..-1]
res.filename = res.filename[0, index]

# 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
# If we didn't find a file, revert our changes to res.filename.
res.filename << basename unless file
end

return file
end
# rubocop:enable Lint/AssignmentInCondition

# rubocop:disable Style/MethodName
def do_GET(req, res)
Expand Down

0 comments on commit bb94deb

Please sign in to comment.