Skip to content

Commit

Permalink
Merge pull request #2269 from jekyll/fix-up-serve
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Apr 27, 2014
2 parents 4a40187 + 513e776 commit 49dd674
Showing 1 changed file with 45 additions and 22 deletions.
67 changes: 45 additions & 22 deletions lib/jekyll/commands/serve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,22 @@ def init_with_program(prog)
# Boot up a WEBrick server which points to the compiled site's root.
def process(options)
options = configuration_from_options(options)

require 'webrick'

destination = options['destination']

FileUtils.mkdir_p(destination)

# monkey patch WEBrick using custom 404 page (/404.html)
if File.exist?(File.join(destination, '404.html'))
WEBrick::HTTPResponse.class_eval do
def create_error_page
@body = IO.read(File.join(@config[:DocumentRoot], '404.html'))
end
end
end

# recreate NondisclosureName under utf-8 circumstance
fh_option = WEBrick::Config::FileHandler
fh_option[:NondisclosureName] = ['.ht*','~*']
setup(destination)

s = WEBrick::HTTPServer.new(webrick_options(options))
s.unmount("")

s.config.store(:DirectoryIndex, s.config[:DirectoryIndex] << "index.xml")
s.mount(
options['baseurl'],
WEBrick::HTTPServlet::FileHandler,
destination,
file_handler_options
)

s.mount(options['baseurl'], WEBrick::HTTPServlet::FileHandler, destination, fh_option)
Jekyll.logger.info "Server address:", server_address(s, options)

Jekyll.logger.info "Server address:", "http://#{s.config[:BindAddress]}:#{s.config[:Port]}"
p s

if options['detach'] # detach the server
pid = Process.fork { s.start }
Expand All @@ -68,14 +57,30 @@ def create_error_page
end
end

def setup(destination)
require 'webrick'

FileUtils.mkdir_p(destination)

# monkey patch WEBrick using custom 404 page (/404.html)
if File.exist?(File.join(destination, '404.html'))
WEBrick::HTTPResponse.class_eval do
def create_error_page
@body = IO.read(File.join(@config[:DocumentRoot], '404.html'))
end
end
end
end

def webrick_options(config)
opts = {
:DocumentRoot => config['destination'],
:Port => config['port'],
:BindAddress => config['host'],
:MimeTypes => mime_types,
:DoNotReverseLookup => true,
:StartCallback => start_callback(config['detach'])
:StartCallback => start_callback(config['detach']),
:DirectoryIndex => %w(index.html index.htm index.cgi index.rhtml index.xml)
}

if !config['verbose']
Expand All @@ -99,6 +104,24 @@ def mime_types
WEBrick::HTTPUtils::load_mime_types(mime_types_file)
end

def server_address(server, options)
baseurl = "#{options['baseurl']}/" if options['baseurl']
[
"http://",
server.config[:BindAddress],
":",
server.config[:Port],
baseurl || ""
].map(&:to_s).join("")
end

# recreate NondisclosureName under utf-8 circumstance
def file_handler_options
fh_option = WEBrick::Config::FileHandler
fh_option[:NondisclosureName] = ['.ht*','~*']
fh_option
end

end

end
Expand Down

0 comments on commit 49dd674

Please sign in to comment.