Object
Rack::Request provides a convenient interface to a Rack environment. It is stateless, the environment env passed to the constructor will be directly modified.
req = Rack::Request.new(env) req.post? req.params["data"]
The environment hash passed will store a reference to the Request object instantiated so that it will only instantiate if an instance of the Request object doesn’t already exist.
The set of form-data media-types. Requests that do not indicate one of the media types presents in this list will not be eligible for form-data / param parsing.
The set of media-types. Requests that do not indicate one of the media types presents in this list will not be eligible for param parsing like soap attachments or generic multiparts
Returns the data received in the query string.
# File lib/rack/request.rb, line 181 181: def GET 182: if @env["rack.request.query_string"] == query_string 183: @env["rack.request.query_hash"] 184: else 185: @env["rack.request.query_string"] = query_string 186: @env["rack.request.query_hash"] = parse_query(query_string) 187: end 188: end
Returns the data received in the request body.
This method support both application/x-www-form-urlencoded and multipart/form-data.
# File lib/rack/request.rb, line 194 194: def POST 195: if @env["rack.input"].nil? 196: raise "Missing rack.input" 197: elsif @env["rack.request.form_input"].eql? @env["rack.input"] 198: @env["rack.request.form_hash"] 199: elsif form_data? || parseable_data? 200: @env["rack.request.form_input"] = @env["rack.input"] 201: unless @env["rack.request.form_hash"] = parse_multipart(env) 202: form_vars = @env["rack.input"].read 203: 204: # Fix for Safari Ajax postings that always append \0 205: # form_vars.sub!(/\0\z/, '') # performance replacement: 206: form_vars.slice!(1) if form_vars[1] == \00 207: \ 208: 209: @env["rack.request.form_vars"] = form_vars 210: @env["rack.request.form_hash"] = parse_query(form_vars) 211: 212: @env["rack.input"].rewind 213: end 214: @env["rack.request.form_hash"] 215: else 216: {} 217: end 218: end
shortcut for request.params[key]
# File lib/rack/request.rb, line 227 227: def [](key) 228: params[key.to_s] 229: end
shortcut for request.params[key] = value
# File lib/rack/request.rb, line 232 232: def []=(key, value) 233: params[key.to_s] = value 234: end
# File lib/rack/request.rb, line 300 300: def accept_encoding 301: @env["HTTP_ACCEPT_ENCODING"].to_s.split(/\s*,\s*/).map do |part| 302: encoding, parameters = part.split(/\s*;\s*/, 2) 303: quality = 1.0 304: if parameters and /\Aq=([\d.]+)/ =~ parameters 305: quality = $1.to_f 306: end 307: [encoding, quality] 308: end 309: end
# File lib/rack/request.rb, line 275 275: def base_url 276: url = scheme + "://" 277: url << host 278: 279: if scheme == "https" && port != 443 || 280: scheme == "http" && port != 80 281: url << ":#{port}" 282: end 283: 284: url 285: end
# File lib/rack/request.rb, line 24 24: def body; @env["rack.input"] end
The character set of the request body if a “charset” media type parameter was given, or nil if no “charset” was specified. Note that, per RFC2616, text/* media types that specify no explicit charset are to be considered ISO-8859-1.
# File lib/rack/request.rb, line 66 66: def content_charset 67: media_type_params['charset'] 68: end
# File lib/rack/request.rb, line 29 29: def content_length; @env['CONTENT_LENGTH'] end
# File lib/rack/request.rb, line 31 31: def content_type 32: content_type = @env['CONTENT_TYPE'] 33: content_type.nil? || content_type.empty? ? nil : content_type 34: end
Checks the HTTP request method (or verb) to see if it was of type DELETE
# File lib/rack/request.rb, line 120 120: def delete?; request_method == "DELETE" end
Determine whether the request body contains form-data by checking the request Content-Type for one of the media-types: “application/x-www-form-urlencoded“ or “multipart/form-data“. The list of form-data media types can be modified through the FORM_DATA_MEDIA_TYPES array.
A request body is also assumed to contain form-data when no Content-Type header is provided and the request_method is POST.
# File lib/rack/request.rb, line 168 168: def form_data? 169: type = media_type 170: meth = env["rack.methodoverride.original_method"] || env['REQUEST_METHOD'] 171: (meth == 'POST' && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type) 172: end
# File lib/rack/request.rb, line 296 296: def fullpath 297: query_string.empty? ? path : "#{path}?#{query_string}" 298: end
Checks the HTTP request method (or verb) to see if it was of type GET
# File lib/rack/request.rb, line 123 123: def get?; request_method == "GET" end
Checks the HTTP request method (or verb) to see if it was of type HEAD
# File lib/rack/request.rb, line 126 126: def head?; request_method == "HEAD" end
# File lib/rack/request.rb, line 110 110: def host 111: # Remove port number. 112: host_with_port.to_s.gsub(/:\d+\z/, '') 113: end
# File lib/rack/request.rb, line 88 88: def host_with_port 89: if forwarded = @env["HTTP_X_FORWARDED_HOST"] 90: forwarded.split(/,\s?/).last 91: else 92: @env['HTTP_HOST'] || "#{@env['SERVER_NAME'] || @env['SERVER_ADDR']}:#{@env['SERVER_PORT']}" 93: end 94: end
# File lib/rack/request.rb, line 315 315: def ip 316: remote_addrs = @env['REMOTE_ADDR'] ? @env['REMOTE_ADDR'].split(/[,\s]+/) : [] 317: remote_addrs.reject! { |addr| trusted_proxy?(addr) } 318: 319: return remote_addrs.first if remote_addrs.any? 320: 321: forwarded_ips = @env['HTTP_X_FORWARDED_FOR'] ? @env['HTTP_X_FORWARDED_FOR'].strip.split(/[,\s]+/) : [] 322: 323: if client_ip = @env['HTTP_CLIENT_IP'] 324: # If forwarded_ips doesn't include the client_ip, it might be an 325: # ip spoofing attempt, so we ignore HTTP_CLIENT_IP 326: return client_ip if forwarded_ips.include?(client_ip) 327: end 328: 329: return forwarded_ips.reject { |ip| trusted_proxy?(ip) }.last || @env["REMOTE_ADDR"] 330: end
# File lib/rack/request.rb, line 38 38: def logger; @env['rack.logger'] end
The media type (type/subtype) portion of the CONTENT_TYPE header without any media type parameters. e.g., when CONTENT_TYPE is “text/plain;charset=utf-8”, the media-type is “text/plain“.
For more information on the use of media types in HTTP, see: www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
# File lib/rack/request.rb, line 46 46: def media_type 47: content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase 48: end
The media type parameters provided in CONTENT_TYPE as a Hash, or an empty Hash if no CONTENT_TYPE or media-type parameters were provided. e.g., when the CONTENT_TYPE is “text/plain;charset=utf-8”, this method responds with the following Hash:
{ 'charset' => 'utf-8' }
# File lib/rack/request.rb, line 55 55: def media_type_params 56: return {} if content_type.nil? 57: Hash[*content_type.split(/\s*[;,]\s*/)[1..1]. 58: collect { |s| s.split('=', 2) }. 59: map { |k,v| [k.downcase, v] }.flatten] 60: end
Checks the HTTP request method (or verb) to see if it was of type OPTIONS
# File lib/rack/request.rb, line 129 129: def options?; request_method == "OPTIONS" end
Determine whether the request body contains data by checking the request media_type against registered parse-data media-types
# File lib/rack/request.rb, line 176 176: def parseable_data? 177: PARSEABLE_DATA_MEDIA_TYPES.include?(media_type) 178: end
Checks the HTTP request method (or verb) to see if it was of type PATCH
# File lib/rack/request.rb, line 132 132: def patch?; request_method == "PATCH" end
# File lib/rack/request.rb, line 292 292: def path 293: script_name + path_info 294: end
# File lib/rack/request.rb, line 26 26: def path_info; @env["PATH_INFO"].to_s end
# File lib/rack/request.rb, line 116 116: def path_info=(s); @env["PATH_INFO"] = s.to_s end
# File lib/rack/request.rb, line 96 96: def port 97: if port = host_with_port.split(/:/)[1] 98: port.to_i 99: elsif port = @env['HTTP_X_FORWARDED_PORT'] 100: port.to_i 101: elsif ssl? 102: 443 103: elsif @env.has_key?("HTTP_X_FORWARDED_HOST") 104: 80 105: else 106: @env["SERVER_PORT"].to_i 107: end 108: end
Checks the HTTP request method (or verb) to see if it was of type POST
# File lib/rack/request.rb, line 135 135: def post?; request_method == "POST" end
Checks the HTTP request method (or verb) to see if it was of type PUT
# File lib/rack/request.rb, line 138 138: def put?; request_method == "PUT" end
# File lib/rack/request.rb, line 28 28: def query_string; @env["QUERY_STRING"].to_s end
the referer of the client
# File lib/rack/request.rb, line 242 242: def referer 243: @env['HTTP_REFERER'] 244: end
# File lib/rack/request.rb, line 27 27: def request_method; @env["REQUEST_METHOD"] end
# File lib/rack/request.rb, line 70 70: def scheme 71: if @env['HTTPS'] == 'on' 72: 'https' 73: elsif @env['HTTP_X_FORWARDED_SSL'] == 'on' 74: 'https' 75: elsif @env['HTTP_X_FORWARDED_SCHEME'] 76: @env['HTTP_X_FORWARDED_SCHEME'] 77: elsif @env['HTTP_X_FORWARDED_PROTO'] 78: @env['HTTP_X_FORWARDED_PROTO'].split(',')[0] 79: else 80: @env["rack.url_scheme"] 81: end 82: end
# File lib/rack/request.rb, line 25 25: def script_name; @env["SCRIPT_NAME"].to_s end
# File lib/rack/request.rb, line 115 115: def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
# File lib/rack/request.rb, line 36 36: def session; @env['rack.session'] ||= {} end
# File lib/rack/request.rb, line 37 37: def session_options; @env['rack.session.options'] ||= {} end
# File lib/rack/request.rb, line 84 84: def ssl? 85: scheme == 'https' 86: end
Checks the HTTP request method (or verb) to see if it was of type TRACE
# File lib/rack/request.rb, line 141 141: def trace?; request_method == "TRACE" end
# File lib/rack/request.rb, line 311 311: def trusted_proxy?(ip) 312: ip =~ /^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|^::1$|^fd[0-9a-f]{2}:.+|^localhost$/ 313: end
Tries to return a remake of the original request URL as a string.
# File lib/rack/request.rb, line 288 288: def url 289: base_url + fullpath 290: end
# File lib/rack/request.rb, line 247 247: def user_agent 248: @env['HTTP_USER_AGENT'] 249: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.