Module: Spider::ControllerMixins::StaticContent

Includes:
Spider::ControllerMixin, HTTPMixin
Defined in:
lib/spiderfw/controller/mixins/static_content.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTTPMixin

base_url, #base_url, #before, #challenge_basic_auth, #challenge_digest_auth, #check_basic_auth, #check_digest_auth, #content_type, #digest_instance_key, #http_auth_realm, #http_auth_realm=, output_charset, #redirect, #request_full_url, #request_path, #request_url, reverse_proxy_mapping, #try_rescue

Methods included from Spider::ControllerMixin

extend_recipient

Class Method Details

.included(klass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 11

def self.included(klass)
    super
    @static_content_route ||= 'public/'
    klass.controller_actions(:serve_static)
    klass.route(@static_content_route, :serve_static, :do => lambda{ @serving_static = true })
    if (klass < Visual)
        klass.no_layout('public')
        klass.no_layout('serve_static')
    end
end

Instance Method Details

#output_static(full_path, file_name = nil) ⇒ Object



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
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 65

def output_static(full_path, file_name=nil)
    file_name ||= File.basename(full_path)
    @request.misc[:is_static] = true
    debug("Serving asset: #{full_path}")
    begin
        while File.symlink?(full_path)
            full_path = File.readlink(full_path)
        end
    rescue NotImplemented
    end
    mode = Spider.conf.get('static_content.mode')
    raise Spider::Controller::NotFound.new(full_path) unless File.exist?(full_path)
    stat = File.lstat(full_path)
    mtime = stat.mtime
    now = Time.now
    if @request.env['HTTP_IF_MODIFIED_SINCE'] && !@request.cache_control[:no_cache]
        if_modified = nil
        begin
          if_modified = Time.httpdate(@request.env['HTTP_IF_MODIFIED_SINCE'])
        rescue ArgumentError # Passenger with IE6 has this header wrong
        end
        max_age = nil
        fresh = true
        if fresh && if_modified && mtime <= if_modified
            debug("Not modified since #{if_modified}: #{full_path}")
            #@response.status = Spider::HTTP::NOT_MODIFIED
            @response.headers.delete("Content-Type")
            @response.headers['Date'] = mtime.httpdate
            @response.no_cookies
            raise HTTPStatus.new(Spider::HTTP::NOT_MODIFIED) 
            return
        end
    end
    if File.directory?(full_path)
        ct = "httpd/unix-directory"
    else
        ct = MIME::Types.type_for(file_name)
        ct = ct.first if ct.is_a?(Array)
        ct = ct.to_s if ct
        ct ||= "application/octet-stream"
    end
    @response.content_type = ct
    @response.content_length = stat.size
    @response.headers['Last-Modified'] = mtime.httpdate
    
    if mode == 'x-sendfile'
        @response.headers['X-Sendfile'] = full_path
    elsif mode == 'x-accel-redirect'
        @response.headers['X-Accel-Redirect'] = full_path
    else
        f = File.open(full_path, 'rb')
        while (block = f.read(1024)) do
            $out << block
        end
    end
end

#prepare_scene(scene) ⇒ Object



122
123
124
125
126
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 122

def prepare_scene(scene)
    scene = super
    scene.controller[:pub_url] = pub_url
    return scene
end

#pub_pathObject



39
40
41
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 39

def pub_path
    self.class.pub_path
end

#pub_urlObject



43
44
45
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 43

def pub_url
    self.class.pub_url
end

#sanitize_path(path) ⇒ Object



47
48
49
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 47

def sanitize_path(path)
    return path.gsub('..', '')
end

#serve_static(path = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 51

def serve_static(path=nil)
    path += ".#{@request.format}" if @request.format
    raise Spider::Controller::NotFound.new(path) unless path
    path = sanitize_path(path)
    mode = Spider.conf.get('static_content.mode')
    if mode == 'publish' && self.class != Spider::HomeController
        url = self.pub_url
        url += "/"+path
        return redirect(url)
    end
    full_path = pub_path+'/'+path
    output_static(full_path)
end

#serving_static?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 128

def serving_static?(action=nil)
    return @serving_static if @serving_static || !action
    n = dispatch_next(action)
    n && n.action == "serve_static"
end