class Jekyll::Commands::Serve::Servlet

Constants

DEFAULTS

Public Class Methods

new(server, root, callbacks) click to toggle source
Calls superclass method
# File lib/jekyll/commands/serve/servlet.rb, line 134
def initialize(server, root, callbacks)
  # So we can access them easily.
  @jekyll_opts = server.config[:JekyllOptions]
  @mime_types_charset = server.config[:MimeTypesCharset]
  set_defaults
  super
end

Public Instance Methods

do_GET(req, res) click to toggle source

rubocop:disable Naming/MethodName

Calls superclass method
# File lib/jekyll/commands/serve/servlet.rb, line 160
def do_GET(req, res)
  rtn = super

  if @jekyll_opts["livereload"]
    return rtn if SkipAnalyzer.skip_processing?(req, res, @jekyll_opts)

    processor = BodyProcessor.new(res.body, @jekyll_opts)
    processor.process!
    res.body = processor.new_body
    res.content_length = processor.content_length.to_s

    if processor.livereload_added
      # Add a header to indicate that the page content has been modified
      res["X-Rack-LiveReload"] = "1"
    end
  end

  conditionally_inject_charset(res)
  res.header.merge!(@headers)
  rtn
end
search_file(req, res, basename) click to toggle source

Add the ability to tap file.html the same way that Nginx does on our Docker images (or on GitHub Pages.) The difference is that we might end up with a different preference on which comes first.

Calls superclass method
# File lib/jekyll/commands/serve/servlet.rb, line 152
def search_file(req, res, basename)
  # /file.* > /file/index.html > /file.html
  super ||
    super(req, res, "#{basename}.html") ||
    super(req, res, "#{basename}.xhtml")
end
search_index_file(req, res) click to toggle source
Calls superclass method
# File lib/jekyll/commands/serve/servlet.rb, line 142
def search_index_file(req, res)
  super ||
    search_file(req, res, ".html") ||
    search_file(req, res, ".xhtml")
end

Private Instance Methods

conditionally_inject_charset(res) click to toggle source

Inject charset based on Jekyll config only if our mime-types database contains the charset metadata.

Refer ‘script/vendor-mimes` in the repository for further details.

# File lib/jekyll/commands/serve/servlet.rb, line 189
def conditionally_inject_charset(res)
  typ = res.header["content-type"]
  return unless @mime_types_charset.key?(typ)
  return if %r!;\s*charset=!.match?(typ)

  res.header["content-type"] = "#{typ}; charset=#{@jekyll_opts["encoding"]}"
end
set_defaults() click to toggle source
# File lib/jekyll/commands/serve/servlet.rb, line 197
def set_defaults
  hash_ = @jekyll_opts.fetch("webrick", {}).fetch("headers", {})
  DEFAULTS.each_with_object(@headers = hash_) do |(key, val), hash|
    hash[key] = val unless hash.key?(key)
  end
end