Class: Staticd::HTTPServer

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd/http_server.rb

Overview

Simple HTTP server Rack app.

If the resource is readable from the root folder at the path given by the request, the resource is sent to the client. Otherwise a 404 Not Found HTTP error is sent.

Constant Summary collapse

EXT_MIME_TYPE =

Mime types served by the webserver (from NGiNX mime.types file).

{
  ".html"    => "text/html",
  ".htm"     => "text/html",
  ".shtml"   => "text/html",
  ".css"     => "text/css",
  ".xml"     => "text/xml",
  ".rss"     => "text/xml",
  ".gif"     => "image/gif",
  ".jpeg"    => "image/jpeg",
  ".jpg"     => "image/jpeg",
  ".js"      => "application/x-javascript",
  ".txt"     => "text/plain",
  ".htc"     => "text/x-component",
  ".mml"     => "text/mathml",
  ".png"     => "image/png",
  ".ico"     => "image/x-icon",
  ".jng"     => "image/x-jng",
  ".wbmp"    => "image/vnd.wap.wbmp",
  ".jar"     => "application/java-archive",
  ".war"     => "application/java-archive",
  ".ear"     => "application/java-archive",
  ".hqx"     => "application/mac-binhex40",
  ".pdf"     => "application/pdf",
  ".cco"     => "application/x-cocoa",
  ".jardiff" => "application/x-java-archive-diff",
  ".jnlp"    => "application/x-java-jnlp-file",
  ".run"     => "application/x-makeself",
  ".pm"      => "application/x-perl",
  ".pl"      => "application/x-perl",
  ".prc"     => "application/x-pilot",
  ".pdb"     => "application/x-pilot",
  ".rar"     => "application/x-rar-compressed",
  ".rpm"     => "application/x-redhat-package-manager",
  ".sea"     => "application/x-sea",
  ".swf"     => "application/x-shockwave-flash",
  ".sit"     => "application/x-stuffit",
  ".tcl"     => "application/x-tcl",
  ".tk"      => "application/x-tcl",
  ".der"     => "application/x-x509-ca-cert",
  ".pem"     => "application/x-x509-ca-cert",
  ".crt"     => "application/x-x509-ca-cert",
  ".xpi"     => "application/x-xpinstall",
  ".zip"     => "application/zip",
  ".deb"     => "application/octet-stream",
  ".bin"     => "application/octet-stream",
  ".exe"     => "application/octet-stream",
  ".dll"     => "application/octet-stream",
  ".dmg"     => "application/octet-stream",
  ".eot"     => "application/octet-stream",
  ".iso"     => "application/octet-stream",
  ".img"     => "application/octet-stream",
  ".msi"     => "application/octet-stream",
  ".msp"     => "application/octet-stream",
  ".msm"     => "application/octet-stream",
  ".mp3"     => "audio/mpeg",
  ".ra"      => "audio/x-realaudio",
  ".mpeg"    => "video/mpeg",
  ".mpg"     => "video/mpeg",
  ".mov"     => "video/quicktime",
  ".flv"     => "video/x-flv",
  ".avi"     => "video/x-msvideo",
  ".wmv"     => "video/x-ms-wmv",
  ".asx"     => "video/x-ms-asf",
  ".asf"     => "video/x-ms-asf",
  ".mng"     => "video/x-mng"
}
DEFAULT_MIME_TYPE =

Mime type used when no type has been identified.

"application/octet-stream"

Instance Method Summary collapse

Constructor Details

#initialize(http_root, access_logger = nil) ⇒ HTTPServer

Returns a new instance of HTTPServer.



83
84
85
86
87
88
89
90
91
# File 'lib/staticd/http_server.rb', line 83

def initialize(http_root, access_logger=nil)
  @http_root = http_root
  unless (@access_logger = access_logger)
    @access_logger = Logger.new(STDOUT)
    @access_logger.formatter = proc { |_, _, _, msg| "#{msg}\n"}
  end

  raise "No HTTP root folder provided" unless @http_root
end

Instance Method Details

#_call(env) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/staticd/http_server.rb', line 97

def _call(env)
  @env = env
  req = Rack::Request.new(@env)
  file_path = @http_root + req.path
  res = File.readable?(file_path) ? serve(file_path) : send_404
  log(req, res)
end

#call(env) ⇒ Object



93
94
95
# File 'lib/staticd/http_server.rb', line 93

def call(env)
  dup._call(env)
end