class Request

Utopia's application-facing request wrapper.

Protocol request methods are delegated to the underlying request; parsing and application conveniences live here rather than on protocol-http itself.

Definitions

def self.[](*arguments)

Build a Utopia request from the given protocol request arguments.

Implementation

def self.[](*arguments)
	self.new(Protocol::HTTP::Request[*arguments])
end

def initialize(delegate)

Initialize the request.

Signature

parameter delegate Protocol::HTTP::Request

The underlying protocol request.

Implementation

def initialize(delegate)
	@delegate = delegate
	
	@url = nil
	@session = nil
	@variables = nil
	@localization = nil
	@exception = nil
	
	@query_parameters = nil
	@cookies = nil
end

attr :delegate

The underlying protocol request.

attr_accessor :session

The session associated with this request, if installed.

attr_accessor :variables

The controller variables associated with this request, if installed.

attr_accessor :localization

The immutable localization preferences associated with this request, if installed.

attr_accessor :exception

The exception associated with this request, if any.

def url

The structured request URL.

Signature

returns Protocol::URL::Absolute | Protocol::URL::Relative | Nil

The request URL.

Implementation

def url
	@url ||= self.parse_url.normalize!
end

def path

Signature

returns Utopia::Path

The path components.

Implementation

def path
	Path.new(self.url.path.components(Protocol::URL::Encoding::System))
end

def path=(value)

Assign the decoded application path while preserving the current query string.

Signature

parameter value Utopia::Path | Protocol::URL::Path | String

The path.

Implementation

def path= value
	if value.is_a?(Protocol::URL::Path)
		self.url.path = value
	else
		self.url.path = Path[value].to_url_path
	end
end

def query?

Whether the request method is QUERY.

Signature

returns Boolean

Whether the request method is QUERY.

Implementation

def query?
	@delegate.method == "QUERY"
end

def post?

Whether the request method is POST.

Implementation

def post?
	@delegate.method == "POST"
end

def request_path

The normalized original request path, before any internal request rewrites.

Signature

returns Utopia::Path

The decoded application path.

Implementation

def request_path
	path = @delegate.path.split("?", 2).first
	path = Protocol::URL::Path[path].normalize.simplify
	
	return Path.new(path.components(Protocol::URL::Encoding::System))
end

def query_parameters

Decoded query arguments.

Implementation

def query_parameters
	@query_parameters ||= parse_query_parameters(self.url.query)
end

def cookies

Decoded request cookies.

Implementation

def cookies
	@cookies ||= parse_cookies(self.headers["cookie"])
end

def user_agent

The request user agent.

Implementation

def user_agent
	@delegate.headers["user-agent"]
end

def referrer

The request referrer.

Implementation

def referrer
	@delegate.headers["referer"]
end

def ip

The remote peer IP address, if available.

Implementation

def ip
	@delegate.peer&.ip_address
end

def url=(url)

Assign the structured request URL.

Signature

parameter url Protocol::URL::Absolute | Protocol::URL::Relative | String | Nil

The request URL.

Implementation

def url= url
	@url = Protocol::URL[url]
	@query_parameters = nil
end

def with(method: nil, path: self.path)

Build a derived request with updated protocol fields.

Implementation

def with(method: nil, path: self.path)
	delegate = @delegate
	
	if method
		delegate = @delegate.dup
		delegate.method = method
	end
	
	request = self.class.new(delegate)
	request.session = @session
	request.variables = @variables
	request.localization = @localization
	request.exception = @exception
	request.path = path
	
	return request
end