Class: Landline::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/landline/request.rb

Overview

Request wrapper for Rack protocol

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Returns a new instance of Request.

Parameters:

  • env (Array)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/landline/request.rb', line 12

def initialize(env)
  # Should not be used under regular circumstances or depended upon.
  @_original_env = env
  # Rack environment variable bindings. Should be public and frozen.
  init_request_params(env)
  # Cookie hash
  @cookies = Landline::Cookie.from_cookie_string(@headers['cookie'])
  # Query parsing
  @query = Landline::Util::Query.new(@query_string)
  # Pattern matching parameters. Public, readable, unfrozen.
  @param = {}
  @splat = []
  # Traversal route. Public and writable.
  @path = URI.decode_www_form_component(env["PATH_INFO"])
  # File serving path. Public and writable.
  @filepath = "/"
  # Encapsulates all rack variables. Is no longer private, but usually should not be used directly
  @rack = init_rack_vars(env)
  # Internal navigation states. Private.
  @states = []
  # Postprocessors for current request
  @postprocessors = []
  # Execution context
  @context = init_context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



95
96
97
# File 'lib/landline/request.rb', line 95

def context
  @context
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



95
96
97
# File 'lib/landline/request.rb', line 95

def cookies
  @cookies
end

#filepathObject

Returns the value of attribute filepath.



98
99
100
# File 'lib/landline/request.rb', line 98

def filepath
  @filepath
end

#headersObject (readonly)

Returns the value of attribute headers.



95
96
97
# File 'lib/landline/request.rb', line 95

def headers
  @headers
end

#paramObject (readonly)

Returns the value of attribute param.



95
96
97
# File 'lib/landline/request.rb', line 95

def param
  @param
end

#pathObject

Returns the value of attribute path.



98
99
100
# File 'lib/landline/request.rb', line 98

def path
  @path
end

#path_infoObject (readonly)

Returns the value of attribute path_info.



95
96
97
# File 'lib/landline/request.rb', line 95

def path_info
  @path_info
end

#postprocessorsObject (readonly)

Returns the value of attribute postprocessors.



95
96
97
# File 'lib/landline/request.rb', line 95

def postprocessors
  @postprocessors
end

#queryObject

Returns the value of attribute query.



98
99
100
# File 'lib/landline/request.rb', line 98

def query
  @query
end

#rackObject (readonly)

Returns the value of attribute rack.



95
96
97
# File 'lib/landline/request.rb', line 95

def rack
  @rack
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



95
96
97
# File 'lib/landline/request.rb', line 95

def request_method
  @request_method
end

#script_nameObject (readonly)

Returns the value of attribute script_name.



95
96
97
# File 'lib/landline/request.rb', line 95

def script_name
  @script_name
end

#server_nameObject (readonly)

Returns the value of attribute server_name.



95
96
97
# File 'lib/landline/request.rb', line 95

def server_name
  @server_name
end

#server_portObject (readonly)

Returns the value of attribute server_port.



95
96
97
# File 'lib/landline/request.rb', line 95

def server_port
  @server_port
end

#server_protocolObject (readonly)

Returns the value of attribute server_protocol.



95
96
97
# File 'lib/landline/request.rb', line 95

def server_protocol
  @server_protocol
end

#splatObject (readonly)

Returns the value of attribute splat.



95
96
97
# File 'lib/landline/request.rb', line 95

def splat
  @splat
end

Instance Method Details

#bodynil, String

Note:

reads data from rack.input, which is not rewindable. .body data is memoized.

Returns request body (if POST data exists)

Returns:

  • (nil, String)


51
52
53
# File 'lib/landline/request.rb', line 51

def body
  @body ||= @rack.input&.read
end

#envObject

Reconstructs rack env after modification



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/landline/request.rb', line 83

def env
  path = @path
  @_original_env.merge(reconstruct_headers)
                .merge({
                         'PATH_INFO' => path,
                         'REQUEST_PATH' => path,
                         'QUERY_STRING' => query.query,
                         'REQUEST_URI' => "#{path}?#{query.query}"
                       })
                .merge(reconstruct_cookie)
end

#hijackObject

Returns full hijack callback



78
79
80
# File 'lib/landline/request.rb', line 78

def hijack
  @_original_env['rack.hijack']
end

#hijack?Boolean

Checks if response stream can be partially hijacked

Returns:

  • (Boolean)


73
74
75
# File 'lib/landline/request.rb', line 73

def hijack?
  @_original_env['rack.hijack?']
end

#inputIO

Note:

Rack IO is not always rewindable - if it is read once, the data is gone (i.e. request.body will return nothing).

Returns raw Rack input object

Returns:

  • (IO)

    (May not entirely be compatible with IO, see Rack/SPEC.rdoc)



58
59
60
# File 'lib/landline/request.rb', line 58

def input
  @rack.input
end

#pop_stateObject

Load last navigation state (path, splat, param) from state stack



68
69
70
# File 'lib/landline/request.rb', line 68

def pop_state
  @path, @param, @splat, @filepath = @states.pop
end

#push_stateObject

Push current navigation state (path, splat, param) onto state stack



63
64
65
# File 'lib/landline/request.rb', line 63

def push_state
  @states.push([@path, @param.dup, @splat.dup, @filepath.dup])
end

#run_postprocessors(response) ⇒ Object

Run postprocessors

Parameters:



40
41
42
43
44
45
46
# File 'lib/landline/request.rb', line 40

def run_postprocessors(response)
  @context.origin.properties.lookup = {}
  @postprocessors.reverse_each do |postproc|
    @context.instance_exec(self, response, &postproc)
  end
  @postprocessors = []
end