Module: Spider::ControllerMixins::HTTPMixin
- Included in:
- StaticContent
- Defined in:
- lib/spiderfw/controller/mixins/http_mixin.rb
Defined Under Namespace
Modules: ClassMethods Classes: HTTPStatus
Class Method Summary collapse
-
.base_url ⇒ String
The base URL to which the installation responds.
- .included(klass) ⇒ Object
-
.output_charset(val = nil) ⇒ String
Sets or returns the charset for the controller.
-
.reverse_proxy_mapping(url_path) ⇒ String
Takes a url path that the framework would accept, and transforms it into the url path the webserver would send, given the settings in http.proxy_mapping.
Instance Method Summary collapse
-
#base_url ⇒ String
The base URL to which the installation responds.
- #before(action = '', *arguments) ⇒ Object
- #challenge_basic_auth ⇒ Object
- #challenge_digest_auth(realm = nil) ⇒ Object
- #check_basic_auth(authenticator) ⇒ Object
- #check_digest_auth(authenticator) ⇒ Object
-
#content_type(ct) ⇒ String
Sets a Content type.
- #digest_instance_key ⇒ Object
- #http_auth_realm ⇒ Object
- #http_auth_realm=(val) ⇒ Object
- #prepare_scene(scene) ⇒ Object
-
#redirect(url, code = Spider::HTTP::SEE_OTHER) ⇒ Object
Sets the headers to redirect the browser to the given url, and calls Spider::Controller#done to terminate the execution of the Controller chain.
-
#request_full_url ⇒ String
The request_url with query params, if any.
-
#request_path ⇒ String
Returns the http path used to call the current controller & action.
-
#request_url ⇒ String
The request_path prefixed with http:// and the current host.
- #try_rescue(exc) ⇒ Object
Class Method Details
.base_url ⇒ String
Returns the base URL to which the installation responds.
113 114 115 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 113 def self.base_url HTTPMixin.reverse_proxy_mapping("") end |
.included(klass) ⇒ Object
11 12 13 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 11 def self.included(klass) klass.extend(ClassMethods) end |
.output_charset(val = nil) ⇒ String
Sets or returns the charset for the controller
70 71 72 73 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 70 def self.output_charset(val=nil) @output_charset = val if val @output_charset || Spider.conf.get('http.charset') end |
.reverse_proxy_mapping(url_path) ⇒ String
Takes a url path that the framework would accept, and transforms it into the url path the webserver would send, given the settings in http.proxy_mapping
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 34 def self.reverse_proxy_mapping(url_path) return '' unless url_path if (maps = Spider.conf.get('http.proxy_mapping')) maps.each do |proxy, spider| spider ||= '' return proxy + url_path[spider.length..-1] if (spider == "" || url_path[0..spider.length-1] == spider) end end return url_path end |
Instance Method Details
#base_url ⇒ String
Returns the base URL to which the installation responds.
118 119 120 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 118 def base_url HTTPMixin.base_url end |
#before(action = '', *arguments) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 96 def before(action='', *arguments) return super if self.is_a?(Spider::Widget) # FIXME: the Spider::Widget check # is needed because with _wt the widget executes without action # Redirect to url + slash if controller is called without action dest = HTTPMixin.reverse_proxy_mapping(@request.env['PATH_INFO']) if (action == '' && dest[-1].chr != '/' && !self.is_a?(Spider::Widget)) dest = dest += '/' if (@request.env['QUERY_STRING'] && !@request.env['QUERY_STRING'].empty?) dest += '?'+@request.env['QUERY_STRING'] end redirect(dest) end super end |
#challenge_basic_auth ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 147 def challenge_basic_auth() realm ||= http_auth_realm realm ||= 'Secure Area' @response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\"" @response.status = Spider::HTTP::UNAUTHORIZED render('errors/unauthorized') if self.is_a?(Visual) done end |
#challenge_digest_auth(realm = nil) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 164 def challenge_digest_auth(realm=nil) realm ||= http_auth_realm realm ||= 'Secure Area' # nonce now = "%012d" % @request.request_time pk = Digest::MD5.hexdigest("#{now}:#{digest_instance_key}")[0,32] nonce = [now + ":" + pk].pack("m*").chop # it has 60 length of chars. opaque = [UUIDTools::UUID.random_create.to_s].pack("m*").chop header = "Digest realm=\"#{realm}\", qop=\"auth\", nonce=\"#{nonce}\", opaque=\"#{opaque}\"" @response.headers['WWW-Authenticate'] = header @response.status = Spider::HTTP::UNAUTHORIZED done end |
#check_basic_auth(authenticator) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 156 def check_basic_auth(authenticator) if (@request.env['HTTP_AUTHORIZATION'] =~ /Basic (.+)/) pair = Base64.decode64($1) user, pass = pair.split(':') return authenticator.authenticate(:login, {:username => user, :password => pass}) end end |
#check_digest_auth(authenticator) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 180 def check_digest_auth(authenticator) # TODO: implement opaque, auth-int if (@request.env['HTTP_AUTHORIZATION'] =~ /Digest (.+)/) parts = $1.split(',') params = {} parts.each do |p| k, v = p.strip.split('=') v = v.sub(/^"+/, '').sub(/"+$/, '') params[k.downcase] = v end ['username', 'realm', 'nonce', 'uri', 'cnonce', 'qop', 'nc', 'response', 'opaque'].each{ |k| return unless params[k] } user = params['username'] user = $1 if params['username'] =~ /.+\\(.+)/ # FIXME: Temp fix for windows sending DOMAIN/User pub_time, pk = params['nonce'].unpack("m*")[0].split(":", 2) return unless pub_time && pk return unless Digest::MD5.hexdigest("#{pub_time}:#{digest_instance_key}")[0,32] == pk diff_time = @request.request_time.to_i - pub_time.to_i return if diff_time < 0 return if diff_time > Spider.conf.get('http.nonce_life') user = authenticator.load(:username => user, :realm => params['realm']) return unless user ha1 = user.ha1 return unless ha1 ha2 = Digest::MD5.hexdigest("#{@request.env['REQUEST_METHOD']}:#{params['uri']}") if (params['qop'] == "auth" || params['qop'] == "auth-int") param2 = ['nonce', 'nc', 'cnonce', 'qop'].map{|key| params[key] }.join(':') response = Digest::MD5.hexdigest([ha1, param2, ha2].join(':')) else response = Digest::MD5.hexdigest([ha1, params['nonce'], ha2].join(':')) end # FIXME: temporaneamente disabilitato controllo perché con il login DOMINIO/Utente non corrisponde #return unless response == params['response'] return user end end |
#content_type(ct) ⇒ String
Sets a Content type
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 79 def content_type(ct) if ct.is_a?(Symbol) ct = { :text => 'text/plain', :json => 'application/json', :js => 'application/x-javascript', :javascript => 'application/x-javascript', :html => 'text/html', :xml => 'text/xml' }[ct] end @response.headers["Content-Type"] = "#{ct};charset=utf-8" end |
#digest_instance_key ⇒ Object
216 217 218 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 216 def digest_instance_key Digest::MD5.hexdigest("#{Mac.addr}:plaw15x857m4p671") end |
#http_auth_realm ⇒ Object
226 227 228 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 226 def http_auth_realm @http_auth_realm || self.class.http_auth_realm end |
#http_auth_realm=(val) ⇒ Object
222 223 224 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 222 def http_auth_realm=(val) @http_auth_realm = val end |
#prepare_scene(scene) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 123 def prepare_scene(scene) scene = super scene.base_url = base_url scene.controller[:request_url] = request_url return scene end |
#redirect(url, code = Spider::HTTP::SEE_OTHER) ⇒ Object
Sets the headers to redirect the browser to the given url, and calls Spider::Controller#done to terminate the execution of the Controller chain
20 21 22 23 24 25 26 27 28 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 20 def redirect(url, code=Spider::HTTP::SEE_OTHER) debug "REDIRECTING TO #{url}" @request.session.persist if @request.session # It might be too late afterwards @response.status = code @response.headers["Location"] = url @response.headers.delete("Content-Type") @response.headers.delete("Set-Cookie") done end |
#request_full_url ⇒ String
Returns the request_url with query params, if any.
59 60 61 62 63 64 65 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 59 def request_full_url url = request_url if (@request.env['QUERY_STRING'] && !@request.env['QUERY_STRING'].empty?) url += '?'+@request.env['QUERY_STRING'] end return url end |
#request_path ⇒ String
Returns the http path used to call the current controller & action. Reverses any proxy mappings to the Controller#request_path.
48 49 50 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 48 def request_path HTTPMixin.reverse_proxy_mapping(super) end |
#request_url ⇒ String
Returns the request_path prefixed with http:// and the current host.
53 54 55 56 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 53 def request_url return request_path unless @request.env['HTTP_HOST'] 'http://'+@request.env['HTTP_HOST']+request_path end |
#try_rescue(exc) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/spiderfw/controller/mixins/http_mixin.rb', line 130 def try_rescue(exc) if (exc.is_a?(Spider::Controller::NotFound)) @response.status = Spider::HTTP::NOT_FOUND elsif (exc.is_a?(Spider::Controller::BadRequest)) @response.status = Spider::HTTP::BAD_REQUEST elsif (exc.is_a?(Spider::Controller::Forbidden)) @response.status = Spider::HTTP::FORBIDDEN elsif (exc.is_a?(HTTPStatus)) @response.status = exc.code Spider::Logger.debug("Sending HTTP status #{exc.code}") return else @response.status = Spider::HTTP::INTERNAL_SERVER_ERROR end super end |