Module: Sprockets::Server

Included in:
Base
Defined in:
lib/sprockets/server.rb

Overview

‘Server` is a concern mixed into `Environment` and `CachedEnvironment` that provides a Rack compatible `call` interface and url generation helpers.

Constant Summary collapse

ALLOWED_REQUEST_METHODS =

Supported HTTP request methods.

['GET', 'HEAD'].to_set.freeze
X_CASCADE =
"x-cascade"
VARY =
"vary"

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

‘call` implements the Rack 1.x specification which accepts an `env` Hash and returns a three item tuple with the status code, headers, and body.

Mapping your environment at a url prefix will serve all assets in the path.

map "/assets" do
  run Sprockets::Environment.new
end

A request for ‘“/assets/foo/bar.js”` will search your environment for `“foo/bar.js”`.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sprockets/server.rb', line 37

def call(env)
  start_time = Time.now.to_f
  time_elapsed = lambda { ((Time.now.to_f - start_time) * 1000).to_i }

  unless ALLOWED_REQUEST_METHODS.include? env['REQUEST_METHOD']
    return method_not_allowed_response
  end

  msg = "Served asset #{env['PATH_INFO']} -"

  # Extract the path from everything after the leading slash
  full_path = Rack::Utils.unescape(env['PATH_INFO'].to_s.sub(/^\//, ''))
  path = full_path

  unless path.valid_encoding?
    return bad_request_response(env)
  end

  # Strip fingerprint
  if fingerprint = path_fingerprint(path)
    path = path.sub("-#{fingerprint}", '')
  end

  # URLs containing a `".."` are rejected for security reasons.
  if forbidden_request?(path)
    return forbidden_response(env)
  end

  if fingerprint
    if_match = fingerprint
  elsif env['HTTP_IF_MATCH']
    if_match = env['HTTP_IF_MATCH'][/"(\w+)"$/, 1]
  end

  if env['HTTP_IF_NONE_MATCH']
    if_none_match = env['HTTP_IF_NONE_MATCH'][/"(\w+)"$/, 1]
  end

  # Look up the asset.
  asset = find_asset(path)

  # Fallback to looking up the asset with the full path.
  # This will make assets that are hashed with webpack or
  # other js bundlers work consistently between production
  # and development pipelines.
  if asset.nil? && (asset = find_asset(full_path))
    if_match = asset.etag if fingerprint
    fingerprint = asset.etag
  end

  if asset.nil?
    status = :not_found
  elsif fingerprint && asset.etag != fingerprint
    status = :not_found
  elsif if_match && asset.etag != if_match
    status = :precondition_failed
  elsif if_none_match && asset.etag == if_none_match
    status = :not_modified
  else
    status = :ok
  end

  case status
  when :ok
    logger.info "#{msg} 200 OK (#{time_elapsed.call}ms)"
    ok_response(asset, env)
  when :not_modified
    logger.info "#{msg} 304 Not Modified (#{time_elapsed.call}ms)"
    not_modified_response(env, if_none_match)
  when :not_found
    logger.info "#{msg} 404 Not Found (#{time_elapsed.call}ms)"
    not_found_response(env)
  when :precondition_failed
    logger.info "#{msg} 412 Precondition Failed (#{time_elapsed.call}ms)"
    precondition_failed_response(env)
  end
rescue Exception => e
  logger.error "Error compiling asset #{path}:"
  logger.error "#{e.class.name}: #{e.message}"

  case File.extname(path)
  when ".js"
    # Re-throw JavaScript asset exceptions to the browser
    logger.info "#{msg} 500 Internal Server Error\n\n"
    return javascript_exception_response(e)
  when ".css"
    # Display CSS asset exceptions in the browser
    logger.info "#{msg} 500 Internal Server Error\n\n"
    return css_exception_response(e)
  else
    raise
  end
end