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

def self.included(klass)
    super
    klass.route('public/', :serve_static, :do => lambda{ @serving_static = true })
    klass.route('w/', :serve_widget_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



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

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
          if_modified = 0
        end
        max_age = nil
        fresh = true
        if fresh && 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.headers['Content-Type'] = ct
    @response.headers['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, 'r')
        while (block = f.read(1024)) do
            $out << block
        end
    end
end

#prepare_scene(scene) ⇒ Object



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

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

#pub_pathObject



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

def pub_path
    self.class.pub_path
end

#pub_urlObject



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

def pub_url
    self.class.pub_url
end

#sanitize_path(path) ⇒ Object



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

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

#serve_static(path = nil) ⇒ Object



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

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

#serve_widget_static(path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 64

def serve_widget_static(path)
    path = sanitize_path(path)
    parts = path.split('/public/', 2)
    raise Spider::Controller::NotFound.new(path) unless parts[1]
    full_path = self.class.app.widgets_path+'/'+parts[0]+'/public/'+parts[1]
    output_static(full_path)
end

#serving_static?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
140
# File 'lib/spiderfw/controller/mixins/static_content.rb', line 136

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