Class: Merb::Request
- Includes:
- ControllerExceptions
- Defined in:
- lib/merb-core/dispatch/request.rb,
lib/merb-core/dispatch/dispatcher.rb
Direct Known Subclasses
Constant Summary collapse
- METHODS =
%w{get post put delete head options}
- NAME_REGEX =
/Content-Disposition:.* name="?([^\";]*)"?/ni.freeze
- CONTENT_TYPE_REGEX =
/Content-Type: (.*)\r\n/ni.freeze
- FILENAME_REGEX =
/Content-Disposition:.* filename="?([^\";]*)"?/ni.freeze
- CRLF =
"\r\n".freeze
- EOL =
CRLF
- @@mutex =
Mutex.new
Constants included from ControllerExceptions
ControllerExceptions::STATUS_CODES
Instance Attribute Summary collapse
-
#env ⇒ Object
def env def exceptions def route_params.
-
#exceptions ⇒ Object
def env def exceptions def route_params.
-
#route ⇒ Object
def env def exceptions def route_params.
-
#route_params ⇒ Object
readonly
Returns the value of attribute route_params.
Class Method Summary collapse
-
.escape(s) ⇒ Object
private
Parameters s<String>:: String to URL escape.
-
.normalize_params(parms, name, val = nil) ⇒ Object
private
Converts a query string snippet to a hash and adds it to existing parameters.
-
.params_to_query_string(value, prefix = nil) ⇒ Object
private
Parameters value<Array, Hash, Dictionary ~to_s>:: The value for the query string.
-
.parse_multipart(request, boundary, content_length) ⇒ Object
private
Parameters request<IO>:: The raw request.
-
.query_parse(query_string, delimiter = '&;', preserve_order = false) ⇒ Object
private
Parameters query_string<String>:: The query string.
-
.unescape(s) ⇒ Object
private
Parameter s<String>:: String to URL unescape.
Instance Method Summary collapse
-
#_process_block_return(retval) ⇒ Object
private
Notes Processes the return value of a deferred router block and returns the current route params for the current request evaluation.
-
#accept ⇒ Object
private
Returns String:: The accepted response types.
-
#accept_charset ⇒ Object
Returns String:: The accepted character sets.
-
#accept_encoding ⇒ Object
private
Returns String:: The accepted encodings.
-
#accept_language ⇒ Object
Returns String:: The accepted language.
-
#cache_control ⇒ Object
Returns String:: HTTP cache control.
-
#connection ⇒ Object
private
Returns String:: The HTTP connection.
-
#content_length ⇒ Object
Returns Fixnum:: The request content length.
-
#content_type ⇒ Object
private
Returns String:: The request content type.
-
#controller ⇒ Object
private
Returns the controller object for initialization and dispatching the request.
-
#domain(tld_length = 1) ⇒ Object
Parameters tld_length<Fixnum>:: Number of domains levels to inlclude in the top level domain.
-
#find_route! ⇒ Object
private
Notes Find route using requested URI and merges route parameters (:action, :controller and named segments) into request params hash.
-
#full_uri ⇒ Object
Returns String:: The full URI, including protocol and host.
-
#gateway ⇒ Object
Returns String:: The gateway.
-
#handle ⇒ Object
private
Handles request routing and action dispatch.
-
#handled? ⇒ Boolean
private
If @route_params is an Array, then it will be the rack response.
-
#host ⇒ Object
Returns String:: The full hostname including the port.
-
#if_modified_since ⇒ Object
private
Returns Value of If-Modified-Since request header.
-
#if_none_match ⇒ Object
private
Returns Value of If-None-Match request header.
-
#initialize(rack_env) ⇒ Request
constructor
private
Initialize the request object.
-
#keep_alive ⇒ Object
Returns String:: Value of HTTP_KEEP_ALIVE.
-
#matched! ⇒ Object
private
Sets the request as matched.
-
#matched? ⇒ Boolean
private
Checks whether or not the request has been matched to a route.
-
#message ⇒ Object
Returns String:: Returns the redirect message Base64 unencoded.
-
#method ⇒ Object
Returns Symbol:: The name of the request method, e.g.
-
#params ⇒ Object
Returns Mash:: All request parameters.
-
#path ⇒ Object
Returns String:: The URI without the query string.
-
#path_info ⇒ Object
Returns String:: The path info.
-
#port ⇒ Object
Returns Fixnum:: The server port.
-
#protocol ⇒ Object
Returns String:: The protocol, i.e.
-
#query_string ⇒ Object
private
Returns String:: The query string.
-
#rack_response ⇒ Object
private
Returns (Array, Hash):: the route params for the matched route.
-
#raw_post ⇒ Object
private
Returns String:: The raw post.
-
#referer ⇒ Object
Returns String:: The HTTP referer.
-
#remote_ip ⇒ Object
Returns String:: The remote IP address.
-
#reset_params! ⇒ Object
private
Notes Resets the params to a nil value.
-
#script_name ⇒ Object
Returns String:: The script name.
-
#server_name ⇒ Object
Returns String:: The server name.
-
#server_software ⇒ Object
Returns String:: The server software.
-
#ssl? ⇒ Boolean
Returns Boolean::: True if the request is an SSL request.
-
#subdomains(tld_length = 1) ⇒ Object
Parameters tld_length<Fixnum>:: Number of domains levels to inlclude in the top level domain.
-
#uri ⇒ Object
Returns String:: The request URI.
-
#user_agent ⇒ Object
Returns String:: The HTTP user agent.
-
#version ⇒ Object
private
Returns String:: The HTTP version.
-
#xml_http_request? ⇒ Boolean
(also: #xhr?, #ajax?)
Returns Boolean:: If the request is an XML HTTP request.
Constructor Details
#initialize(rack_env) ⇒ Request
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize the request object.
Parameters
- http_request<~params:~[], ~body:IO>
-
An object like an HTTP Request.
34 35 36 37 38 |
# File 'lib/merb-core/dispatch/request.rb', line 34 def initialize(rack_env) @env = rack_env @body = rack_env['rack.input'] @route_params = {} end |
Instance Attribute Details
#env ⇒ Object
def env def exceptions def route_params
7 8 9 |
# File 'lib/merb-core/dispatch/request.rb', line 7 def env @env end |
#exceptions ⇒ Object
def env def exceptions def route_params
7 8 9 |
# File 'lib/merb-core/dispatch/request.rb', line 7 def exceptions @exceptions end |
#route ⇒ Object
def env def exceptions def route_params
7 8 9 |
# File 'lib/merb-core/dispatch/request.rb', line 7 def route @route end |
#route_params ⇒ Object (readonly)
Returns the value of attribute route_params.
8 9 10 |
# File 'lib/merb-core/dispatch/request.rb', line 8 def route_params @route_params end |
Class Method Details
.escape(s) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parameters
- s<String>
-
String to URL escape.
returns
- String
-
The escaped string.
644 645 646 647 648 |
# File 'lib/merb-core/dispatch/request.rb', line 644 def escape(s) s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) { '%'+$1.unpack('H2'*$1.size).join('%').upcase }.tr(' ', '+') end |
.normalize_params(parms, name, val = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts a query string snippet to a hash and adds it to existing parameters.
Parameters
- parms<Hash>
-
Parameters to add the normalized parameters to.
- name<String>
-
The key of the parameter to normalize.
- val<String>
-
The value of the parameter.
Returns
- Hash
-
Normalized parameters
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 |
# File 'lib/merb-core/dispatch/request.rb', line 797 def normalize_params(parms, name, val=nil) name =~ %r([\[\]]*([^\[\]]+)\]*) key = $1 || '' after = $' || '' if after == "" parms[key] = val elsif after == "[]" (parms[key] ||= []) << val elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) child_key = $1 parms[key] ||= [] if parms[key].last.is_a?(Hash) && !parms[key].last.key?(child_key) parms[key].last.update(child_key => val) else parms[key] << { child_key => val } end else parms[key] ||= {} parms[key] = normalize_params(parms[key], after, val) end parms end |
.params_to_query_string(value, prefix = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parameters
- value<Array, Hash, Dictionary ~to_s>
-
The value for the query string.
- prefix<~to_s>
-
The prefix to add to the query string keys.
Returns
- String
-
The query string.
Alternatives
If the value is a string, the prefix will be used as the key.
Examples
params_to_query_string(10, "page")
# => "page=10"
params_to_query_string({ :page => 10, :word => "ruby" })
# => "page=10&word=ruby"
params_to_query_string({ :page => 10, :word => "ruby" }, "search")
# => "search[page]=10&search[word]=ruby"
params_to_query_string([ "ice-cream", "cake" ], "shopping_list")
# => "shopping_list[]=ice-cream&shopping_list[]=cake"
622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
# File 'lib/merb-core/dispatch/request.rb', line 622 def params_to_query_string(value, prefix = nil) case value when Array value.map { |v| params_to_query_string(v, "#{prefix}[]") } * "&" when Hash, Dictionary value.map { |k, v| params_to_query_string(v, prefix ? "#{prefix}[#{Merb::Request.escape(k)}]" : Merb::Request.escape(k)) } * "&" else "#{prefix}=#{Merb::Request.escape(value)}" end end |
.parse_multipart(request, boundary, content_length) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parameters
- request<IO>
-
The raw request.
- boundary<String>
-
The boundary string.
- content_length<Fixnum>
-
The length of the content.
Raises
- ControllerExceptions::MultiPartParseError
-
Failed to parse request.
Returns
- Hash
-
The parsed request.
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 |
# File 'lib/merb-core/dispatch/request.rb', line 708 def parse_multipart(request, boundary, content_length) boundary = "--#{boundary}" paramhsh = {} buf = "" input = request input.binmode if defined? input.binmode boundary_size = boundary.size + EOL.size bufsize = 16384 content_length -= boundary_size status = input.read(boundary_size) return {} if status == nil || status.empty? raise ControllerExceptions::MultiPartParseError, "bad content body:\n'#{status}' should == '#{boundary + EOL}'" unless status == boundary + EOL rx = /(?:#{EOL})?#{Regexp.quote(boundary,'n')}(#{EOL}|--)/ loop { head = nil body = '' filename = content_type = name = nil read_size = 0 until head && buf =~ rx i = buf.index("\r\n\r\n") if( i == nil && read_size == 0 && content_length == 0 ) content_length = -1 break end if !head && i head = buf.slice!(0, i+2) # First \r\n buf.slice!(0, 2) # Second \r\n filename = head[FILENAME_REGEX, 1] content_type = head[CONTENT_TYPE_REGEX, 1] name = head[NAME_REGEX, 1] if filename && !filename.empty? body = Tempfile.new(:Merb) body.binmode if defined? body.binmode end next end # Save the read body part. if head && (boundary_size+4 < buf.size) body << buf.slice!(0, buf.size - (boundary_size+4)) end read_size = bufsize < content_length ? bufsize : content_length if( read_size > 0 ) c = input.read(read_size) raise ControllerExceptions::MultiPartParseError, "bad content body" if c.nil? || c.empty? buf << c content_length -= c.size end end # Save the rest. if i = buf.index(rx) body << buf.slice!(0, i) buf.slice!(0, boundary_size+2) content_length = -1 if $1 == "--" end if filename && !filename.empty? body.rewind data = { :filename => File.basename(filename), :content_type => content_type, :tempfile => body, :size => File.size(body.path) } else data = body end paramhsh = normalize_params(paramhsh,name,data) break if buf.empty? || content_length == -1 } paramhsh end |
.query_parse(query_string, delimiter = '&;', preserve_order = false) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parameters
- query_string<String>
-
The query string.
- delimiter<String>
-
The query string divider. Defaults to “&”.
- preserve_order<Boolean>
-
Preserve order of args. Defaults to false.
Returns
- Mash
-
The parsed query string (Dictionary if preserve_order is set).
Examples
query_parse("bar=nik&post[body]=heya")
# => { :bar => "nik", :post => { :body => "heya" } }
676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'lib/merb-core/dispatch/request.rb', line 676 def query_parse(query_string, delimiter = '&;', preserve_order = false) query = preserve_order ? Dictionary.new : {} for pair in (query_string || '').split(/[#{delimiter}] */n) key, value = unescape(pair).split('=',2) next if key.nil? if key.include?('[') normalize_params(query, key, value) else query[key] = value end end preserve_order ? query : query.to_mash end |
.unescape(s) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parameter
- s<String>
-
String to URL unescape.
returns
- String
-
The unescaped string.
657 658 659 660 661 |
# File 'lib/merb-core/dispatch/request.rb', line 657 def unescape(s) s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') } end |
Instance Method Details
#_process_block_return(retval) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Notes
Processes the return value of a deferred router block and returns the current route params for the current request evaluation
122 123 124 125 126 127 128 129 |
# File 'lib/merb-core/dispatch/request.rb', line 122 def _process_block_return(retval) # If the return value is an array, then it is a redirect # so we must set the request as a redirect and extract # the redirect params and return it as a hash so that the # dispatcher can handle it matched! if retval.is_a?(Array) retval end |
#accept ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- String
-
The accepted response types. Defaults to “/”.
482 483 484 |
# File 'lib/merb-core/dispatch/request.rb', line 482 def accept @env['HTTP_ACCEPT'].blank? ? "*/*" : @env['HTTP_ACCEPT'] end |
#accept_charset ⇒ Object
Returns
- String
-
The accepted character sets.
458 459 460 |
# File 'lib/merb-core/dispatch/request.rb', line 458 def accept_charset @env['HTTP_ACCEPT_CHARSET'] end |
#accept_encoding ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- String
-
The accepted encodings.
410 411 412 |
# File 'lib/merb-core/dispatch/request.rb', line 410 def accept_encoding @env['HTTP_ACCEPT_ENCODING'] end |
#accept_language ⇒ Object
Returns
- String
-
The accepted language.
434 435 436 |
# File 'lib/merb-core/dispatch/request.rb', line 434 def accept_language @env['HTTP_ACCEPT_LANGUAGE'] end |
#cache_control ⇒ Object
Returns
- String
-
HTTP cache control.
426 427 428 |
# File 'lib/merb-core/dispatch/request.rb', line 426 def cache_control @env['HTTP_CACHE_CONTROL'] end |
#connection ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- String
-
The HTTP connection.
490 491 492 |
# File 'lib/merb-core/dispatch/request.rb', line 490 def connection @env['HTTP_CONNECTION'] end |
#content_length ⇒ Object
Returns
- Fixnum
-
The request content length.
514 515 516 |
# File 'lib/merb-core/dispatch/request.rb', line 514 def content_length @content_length ||= @env[Merb::Const::CONTENT_LENGTH].to_i end |
#content_type ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- String
-
The request content type.
506 507 508 |
# File 'lib/merb-core/dispatch/request.rb', line 506 def content_type @env['CONTENT_TYPE'] end |
#controller ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the controller object for initialization and dispatching the request.
Returns
- Class
-
The controller class matching the routed request,
e.g. Posts.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/merb-core/dispatch/request.rb', line 48 def controller unless params[:controller] raise ControllerExceptions::NotFound, "Route matched, but route did not specify a controller.\n" + "Did you forgot to add :controller => \"people\" or :controller " + "segment to route definition?\nHere is what's specified:\n" + route.inspect end path = [params[:namespace], params[:controller]].compact.join("/") controller = path.snake_case.to_const_string begin Object.full_const_get(controller) rescue NameError => e msg = "Controller class not found for controller `#{path}'" Merb.logger.warn!(msg) raise ControllerExceptions::NotFound, msg end end |
#domain(tld_length = 1) ⇒ Object
Parameters
- tld_length<Fixnum>
-
Number of domains levels to inlclude in the top level domain. Defaults to 1.
Returns
- String
-
The full domain name without the port number.
577 578 579 |
# File 'lib/merb-core/dispatch/request.rb', line 577 def domain(tld_length = 1) host.split('.').last(1 + tld_length).join('.').sub(/:\d+$/,'') end |
#find_route! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Notes
Find route using requested URI and merges route parameters (:action, :controller and named segments) into request params hash.
111 112 113 114 |
# File 'lib/merb-core/dispatch/request.rb', line 111 def find_route! @route, @route_params = Merb::Router.route_for(self) params.merge! @route_params if @route_params.is_a?(Hash) end |
#full_uri ⇒ Object
Returns
- String
-
The full URI, including protocol and host
378 379 380 |
# File 'lib/merb-core/dispatch/request.rb', line 378 def full_uri protocol + "://" + host + uri end |
#gateway ⇒ Object
Returns
- String
-
The gateway.
474 475 476 |
# File 'lib/merb-core/dispatch/request.rb', line 474 def gateway @env['GATEWAY_INTERFACE'] end |
#handle ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Handles request routing and action dispatch.
Returns
- Merb::Controller
-
the controller that handled the action dispatch.
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 |
# File 'lib/merb-core/dispatch/dispatcher.rb', line 51 def handle start = Time.now Merb.logger.info "Started request handling: #{start.to_s}" find_route! return rack_response if handled? klass = controller Merb.logger.debug("Routed to: #{params.inspect}") unless klass < Controller raise NotFound, "Controller '#{klass}' not found.\n" \ "If Merb tries to find a controller for static files, " \ "you may need to check your Rackup file, see the Problems " \ "section at: http://wiki.merbivore.com/pages/rack-middleware" end if klass.abstract? raise NotFound, "The '#{klass}' controller has no public actions" end controller = dispatch_action(klass, params[:action]) controller._benchmarks[:dispatch_time] = Time.now - start Merb.logger.info controller._benchmarks.inspect Merb.logger.flush controller.rack_response rescue Object => exception dispatch_exception(exception).rack_response end |
#handled? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If @route_params is an Array, then it will be the rack response. In this case, the request is considered handled.
Returns
- Boolean
-
true if @route_params is an Array, false otherwise.
166 167 168 |
# File 'lib/merb-core/dispatch/request.rb', line 166 def handled? @route_params.is_a?(Array) end |
#host ⇒ Object
Returns
- String
-
The full hostname including the port.
550 551 552 |
# File 'lib/merb-core/dispatch/request.rb', line 550 def host @env['HTTP_X_FORWARDED_HOST'] || @env['HTTP_HOST'] end |
#if_modified_since ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
Value of If-Modified-Since request header.
593 594 595 596 597 |
# File 'lib/merb-core/dispatch/request.rb', line 593 def if_modified_since if time = @env[Merb::Const::HTTP_IF_MODIFIED_SINCE] Time.rfc2822(time) end end |
#if_none_match ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
Value of If-None-Match request header.
585 586 587 |
# File 'lib/merb-core/dispatch/request.rb', line 585 def if_none_match @env[Merb::Const::HTTP_IF_NONE_MATCH] end |
#keep_alive ⇒ Object
Returns
- String
-
Value of HTTP_KEEP_ALIVE.
450 451 452 |
# File 'lib/merb-core/dispatch/request.rb', line 450 def keep_alive @env['HTTP_KEEP_ALIVE'] end |
#matched! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the request as matched. This will abort evaluating any further deferred procs.
135 136 137 |
# File 'lib/merb-core/dispatch/request.rb', line 135 def matched! @matched = true end |
#matched? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks whether or not the request has been matched to a route.
142 143 144 |
# File 'lib/merb-core/dispatch/request.rb', line 142 def matched? @matched end |
#message ⇒ Object
Returns
- String
-
Returns the redirect message Base64 unencoded.
294 295 296 297 298 299 300 301 |
# File 'lib/merb-core/dispatch/request.rb', line 294 def return {} unless params[:_message] begin Marshal.load(Merb::Request.unescape(params[:_message]).unpack("m").first) rescue ArgumentError {} end end |
#method ⇒ Object
Returns
- Symbol
-
The name of the request method, e.g. :get.
Notes
If the method is post, then the blocks specified in http_method_overrides will be checked for the masquerading method. The block will get the controller yielded to it. The first matching workaround wins. To disable this behavior, set http_method_overrides = []
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/merb-core/dispatch/request.rb', line 80 def method @method ||= begin request_method = @env['REQUEST_METHOD'].downcase.to_sym case request_method when :get, :head, :put, :delete, :options request_method when :post m = nil self.class.http_method_overrides.each do |o| m ||= o.call(self); break if m end m.downcase! if m METHODS.include?(m) ? m.to_sym : :post else raise "Unknown REQUEST_METHOD: #{@env['REQUEST_METHOD']}" end end end |
#params ⇒ Object
Returns
- Mash
-
All request parameters.
Notes
The order of precedence for the params is XML, JSON, multipart, body and request string.
280 281 282 283 284 285 286 287 288 |
# File 'lib/merb-core/dispatch/request.rb', line 280 def params @params ||= begin h = body_and_query_params.merge(route_params) h.merge!(multipart_params) if self.class.parse_multipart_params && multipart_params h.merge!(json_params) if self.class.parse_json_params && json_params h.merge!(xml_params) if self.class.parse_xml_params && xml_params h end end |
#path ⇒ Object
Returns
- String
-
The URI without the query string. Strips trailing “/” and reduces duplicate “/” to a single “/”.
524 525 526 527 528 |
# File 'lib/merb-core/dispatch/request.rb', line 524 def path path = (uri.empty? ? '/' : uri.split('?').first).squeeze("/") path = path[0..-2] if (path[-1] == ?/) && path.size > 1 path end |
#path_info ⇒ Object
Returns
- String
-
The path info.
534 535 536 |
# File 'lib/merb-core/dispatch/request.rb', line 534 def path_info @path_info ||= self.class.unescape(@env['PATH_INFO']) end |
#port ⇒ Object
Returns
- Fixnum
-
The server port.
542 543 544 |
# File 'lib/merb-core/dispatch/request.rb', line 542 def port @env['SERVER_PORT'].to_i end |
#protocol ⇒ Object
Returns
- String
-
The protocol, i.e. either “https” or “http” depending on the HTTPS header.
354 355 356 |
# File 'lib/merb-core/dispatch/request.rb', line 354 def protocol ssl? ? 'https' : 'http' end |
#query_string ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- String
-
The query string.
498 499 500 |
# File 'lib/merb-core/dispatch/request.rb', line 498 def query_string @env['QUERY_STRING'] end |
#rack_response ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- (Array, Hash)
-
the route params for the matched route.
Notes
If the response is an Array then it is considered a direct Rack response to be sent back as a response. Otherwise, the route_params is a Hash with routing data (controller, action, et al).
155 156 157 |
# File 'lib/merb-core/dispatch/request.rb', line 155 def rack_response @route_params end |
#raw_post ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- String
-
The raw post.
315 316 317 318 |
# File 'lib/merb-core/dispatch/request.rb', line 315 def raw_post @body.rewind if @body.respond_to?(:rewind) @raw_post ||= @body.read end |
#referer ⇒ Object
Returns
- String
-
The HTTP referer.
370 371 372 |
# File 'lib/merb-core/dispatch/request.rb', line 370 def referer @env['HTTP_REFERER'] end |
#remote_ip ⇒ Object
Returns
- String
-
The remote IP address.
334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/merb-core/dispatch/request.rb', line 334 def remote_ip return @env['HTTP_CLIENT_IP'] if @env.include?('HTTP_CLIENT_IP') if @env.include?(Merb::Const::HTTP_X_FORWARDED_FOR) then remote_ips = @env[Merb::Const::HTTP_X_FORWARDED_FOR].split(',').reject do |ip| ip =~ /^unknown$|^(127|10|172\.16|192\.168)\./i end return remote_ips.first.strip unless remote_ips.empty? end return @env[Merb::Const::REMOTE_ADDR] end |
#reset_params! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Notes
Resets the params to a nil value.
307 308 309 |
# File 'lib/merb-core/dispatch/request.rb', line 307 def reset_params! @params = nil end |
#script_name ⇒ Object
Returns
- String
-
The script name.
418 419 420 |
# File 'lib/merb-core/dispatch/request.rb', line 418 def script_name @env['SCRIPT_NAME'] end |
#server_name ⇒ Object
Returns
- String
-
The server name.
402 403 404 |
# File 'lib/merb-core/dispatch/request.rb', line 402 def server_name @env['SERVER_NAME'] end |
#server_software ⇒ Object
Returns
- String
-
The server software.
442 443 444 |
# File 'lib/merb-core/dispatch/request.rb', line 442 def server_software @env['SERVER_SOFTWARE'] end |
#ssl? ⇒ Boolean
Returns
- Boolean:
-
True if the request is an SSL request.
362 363 364 |
# File 'lib/merb-core/dispatch/request.rb', line 362 def ssl? @env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' end |
#subdomains(tld_length = 1) ⇒ Object
Parameters
- tld_length<Fixnum>
-
Number of domains levels to inlclude in the top level domain. Defaults to 1.
Returns
- Array
-
All the subdomain parts of the host.
563 564 565 566 |
# File 'lib/merb-core/dispatch/request.rb', line 563 def subdomains(tld_length = 1) parts = host.split('.') parts[0..-(tld_length+2)] end |
#uri ⇒ Object
Returns
- String
-
The request URI.
386 387 388 |
# File 'lib/merb-core/dispatch/request.rb', line 386 def uri @env['REQUEST_PATH'] || @env['REQUEST_URI'] || path_info end |
#user_agent ⇒ Object
Returns
- String
-
The HTTP user agent.
394 395 396 |
# File 'lib/merb-core/dispatch/request.rb', line 394 def user_agent @env['HTTP_USER_AGENT'] end |
#version ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns
- String
-
The HTTP version
466 467 468 |
# File 'lib/merb-core/dispatch/request.rb', line 466 def version @env['HTTP_VERSION'] end |
#xml_http_request? ⇒ Boolean Also known as: xhr?, ajax?
Returns
- Boolean
-
If the request is an XML HTTP request.
324 325 326 |
# File 'lib/merb-core/dispatch/request.rb', line 324 def xml_http_request? not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil? end |