Class: Jimmu::Request

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

Overview

A parsed HTTP request. Constructed once per connection by Application's connection-handling code; passed into Context.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method:, path:, query_string:, headers:, body:, ip:) ⇒ Request

Returns a new instance of Request.



499
500
501
502
503
504
505
506
507
# File 'lib/jimmu.rb', line 499

def initialize(http_method:, path:, query_string:, headers:, body:, ip:)
  @http_method = http_method
  @path = path
  @query_string = query_string || ''
  @raw_headers = headers # Hash, lowercase String keys => String values
  @body = body || ''.b
  @ip = ip
  @route_params = {}
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



496
497
498
# File 'lib/jimmu.rb', line 496

def body
  @body
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



496
497
498
# File 'lib/jimmu.rb', line 496

def http_method
  @http_method
end

#http_versionObject (readonly)

Returns the value of attribute http_version.



496
497
498
# File 'lib/jimmu.rb', line 496

def http_version
  @http_version
end

#ipObject (readonly)

Returns the value of attribute ip.



496
497
498
# File 'lib/jimmu.rb', line 496

def ip
  @ip
end

#pathObject (readonly)

Returns the value of attribute path.



496
497
498
# File 'lib/jimmu.rb', line 496

def path
  @path
end

#query_stringObject (readonly)

Returns the value of attribute query_string.



496
497
498
# File 'lib/jimmu.rb', line 496

def query_string
  @query_string
end

#raw_headersObject (readonly)

Returns the value of attribute raw_headers.



496
497
498
# File 'lib/jimmu.rb', line 496

def raw_headers
  @raw_headers
end

#route_paramsObject

Returns the value of attribute route_params.



497
498
499
# File 'lib/jimmu.rb', line 497

def route_params
  @route_params
end

Instance Method Details

#body_paramsObject



532
533
534
# File 'lib/jimmu.rb', line 532

def body_params
  @body_params ||= compute_body_params
end

#content_typeObject



518
519
520
# File 'lib/jimmu.rb', line 518

def content_type
  header('content-type')
end

#header(name) ⇒ Object



514
515
516
# File 'lib/jimmu.rb', line 514

def header(name)
  @raw_headers[name.to_s.downcase]
end

#methodObject

Public DSL name (spec: request.method)



510
511
512
# File 'lib/jimmu.rb', line 510

def method
  @http_method
end

#paramsObject

Query params, then body params, then route (file-based dynamic segment) params, then uploaded files -- later sources win on key collision, with route params taking precedence as the most specific/intentional source, and uploaded files layered on top since their field names are defined by the form itself.



548
549
550
# File 'lib/jimmu.rb', line 548

def params
  @params ||= query_params.merge(body_params).merge(route_params).merge(uploaded_files)
end

#query_paramsObject



522
523
524
525
526
527
528
529
530
# File 'lib/jimmu.rb', line 522

def query_params
  @query_params ||= begin
    h = {}
    URI.decode_www_form(@query_string, Encoding::UTF_8).each { |k, v| h[k.to_sym] = v } unless @query_string.empty?
    h
  rescue ArgumentError
    {}
  end
end

#uploaded_filesObject



536
537
538
539
540
541
# File 'lib/jimmu.rb', line 536

def uploaded_files
  @uploaded_files ||= begin
    compute_body_params
    @uploaded_files_cache || {}
  end
end