Class: K8::Request

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

Constant Summary collapse

MAX_POST_SIZE =
10*1024*1024
MAX_MULTIPART_SIZE =
100*1024*1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Returns a new instance of Request.



447
448
449
450
451
452
453
454
455
456
457
# File 'lib/keight.rb', line 447

def initialize(env)
  #; [!yb9k9] sets @env.
  @env = env
  #; [!yo22o] sets @method as Symbol value.
  @method = HTTP_REQUEST_METHODS[env['REQUEST_METHOD']]  or
    raise HTTPException.new(400, "#{env['REQUEST_METHOD'].inspect}: unknown request method.")
  #; [!twgmi] sets @path.
  @path = (x = env['PATH_INFO'])
  #; [!ae8ws] uses SCRIPT_NAME as urlpath when PATH_INFO is not provided.
  @path = env['SCRIPT_NAME'] if x.nil? || x.empty?
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



459
460
461
# File 'lib/keight.rb', line 459

def env
  @env
end

#method(name = nil) ⇒ Object (readonly)

Returns the value of attribute method.



459
460
461
# File 'lib/keight.rb', line 459

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



459
460
461
# File 'lib/keight.rb', line 459

def path
  @path
end

Instance Method Details

#clearObject



652
653
654
655
656
# File 'lib/keight.rb', line 652

def clear
  #; [!0jdal] removes uploaded files.
  d = nil
  d.each {|_, uploaded| uploaded.clean() } if (d = @params_file)
end

#client_ip_addrObject



514
515
516
517
518
519
520
521
522
523
524
# File 'lib/keight.rb', line 514

def client_ip_addr
  #; [!e1uvg] returns 'X-Real-IP' header value if provided.
  addr = @env['HTTP_X_REAL_IP']          # nginx
  return addr if addr
  #; [!qdlyl] returns first item of 'X-Forwarded-For' header if provided.
  addr = @env['HTTP_X_FORWARDED_FOR']    # apache, squid, etc
  return addr.split(',').first if addr
  #; [!8nzjh] returns 'REMOTE_ADDR' if neighter 'X-Real-IP' nor 'X-Forwarded-For' provided.
  addr = @env['REMOTE_ADDR']             # http standard
  return addr
end

#content_lengthObject



499
500
501
502
503
# File 'lib/keight.rb', line 499

def content_length
  #; [!0wbek] returns env['CONTENT_LENGHT'] as integer.
  len = @env['CONTENT_LENGTH']
  return len ? len.to_i : len
end

#content_typeObject



494
495
496
497
# File 'lib/keight.rb', line 494

def content_type
  #; [!95g9o] returns env['CONTENT_TYPE'].
  return @env['CONTENT_TYPE']
end

#cookiesObject



647
648
649
650
# File 'lib/keight.rb', line 647

def cookies
  #; [!c9pwr] parses cookie data and returns it as hash object.
  return @cookies ||= Util.parse_cookie_string(@env['HTTP_COOKIE'] || "")
end

#header(name) ⇒ Object



467
468
469
470
# File 'lib/keight.rb', line 467

def header(name)
  #; [!1z7wj] returns http header value from environment.
  return @env["HTTP_#{name.upcase.sub('-', '_')}"]
end

#paramsObject



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/keight.rb', line 627

def params
  #; [!erlc7] parses QUERY_STRING when request method is GET or HEAD.
  #; [!cr0zj] parses JSON when content type is 'application/json'.
  #; [!j2lno] parses form parameters when content type is 'application/x-www-form-urlencoded'.
  #; [!4rmn9] parses multipart when content type is 'multipart/form-data'.
  if @method == :GET || @method == :HEAD
    return params_query()
  end
  case @env['CONTENT_TYPE']
  when /\Aapplication\/json\b/
    return params_json()
  when /\Aapplication\/x-www-form-urlencoded\b/
    return params_form()
  when /\Amultipart\/form-data\b/
    return params_multipart()
  else
    return {}
  end
end

#params_formObject Also known as: form



556
557
558
559
560
561
562
# File 'lib/keight.rb', line 556

def params_form
  d = @params_form
  return d if d
  #
  d = @params_form = _parse_post_data(:form)
  return d
end

#params_jsonObject Also known as: json



575
576
577
578
579
580
# File 'lib/keight.rb', line 575

def params_json
  d = @params_json
  return d if d
  d = @params_json = _parse_post_data(:json)
  return d
end

#params_multipartObject Also known as: multipart



565
566
567
568
569
570
571
572
# File 'lib/keight.rb', line 565

def params_multipart
  d1 = @params_form
  d2 = @params_file
  return d1, d2 if d1 && d2
  d1, d2 = _parse_post_data(:multipart)
  @params_form = d1; @params_file = d2
  return d1, d2
end

#params_queryObject Also known as: query



546
547
548
549
550
# File 'lib/keight.rb', line 546

def params_query
  #; [!6ezqw] parses QUERY_STRING and returns it as Hash object.
  #; [!o0ws7] unquotes both keys and values.
  return @params_query ||= Util.parse_query_string(@env['QUERY_STRING'] || "")
end

#path_infoObject

may be empty



489
# File 'lib/keight.rb', line 489

def path_info    ; @env['PATH_INFO'   ] || ''; end

#query_stringObject

may be empty



490
# File 'lib/keight.rb', line 490

def query_string ; @env['QUERY_STRING'] || ''; end

#rack_errorsObject

ex: $stderr



536
# File 'lib/keight.rb', line 536

def rack_errors       ; @env['rack.errors']       ; end

#rack_hijackObject

ex: callable object



542
# File 'lib/keight.rb', line 542

def rack_hijack       ; @env['rack.hijack']       ; end

#rack_hijack?Boolean

ex: true or false

Returns:

  • (Boolean)


543
# File 'lib/keight.rb', line 543

def rack_hijack?      ; @env['rack.hijack?']      ; end

#rack_hijack_ioObject

ex: socket object



544
# File 'lib/keight.rb', line 544

def rack_hijack_io    ; @env['rack.hijack_io']    ; end

#rack_inputObject

ex: $stdout



535
# File 'lib/keight.rb', line 535

def rack_input        ; @env['rack.input']        ; end

#rack_loggerObject

ex: Logger.new



541
# File 'lib/keight.rb', line 541

def rack_logger       ; @env['rack.logger']       ; end

#rack_multiprocessObject

ex: true



538
# File 'lib/keight.rb', line 538

def rack_multiprocess ; @env['rack.multiprocess'] ; end

#rack_multithreadObject

ex: true



537
# File 'lib/keight.rb', line 537

def rack_multithread  ; @env['rack.multithread']  ; end

#rack_run_onceObject

ex: false



539
# File 'lib/keight.rb', line 539

def rack_run_once     ; @env['rack.run_once']     ; end

#rack_sessionObject

ex: {}



540
# File 'lib/keight.rb', line 540

def rack_session      ; @env['rack.session']      ; end

#rack_url_schemeObject

ex: ‘http’ or ‘https’



534
# File 'lib/keight.rb', line 534

def rack_url_scheme   ; @env['rack.url_scheme']   ; end

#rack_versionObject

ex: [1, 3]



533
# File 'lib/keight.rb', line 533

def rack_version      ; @env['rack.version']      ; end

#refererObject



505
# File 'lib/keight.rb', line 505

def referer          ; @env['HTTP_REFERER']         ; end

#request_methodObject



472
473
474
475
# File 'lib/keight.rb', line 472

def request_method
  #; [!y8eos] returns env['REQUEST_METHOD'] as string.
  return @env['REQUEST_METHOD']
end

#schemeObject



526
527
528
529
530
531
# File 'lib/keight.rb', line 526

def scheme
  #; [!jytwy] returns 'https' when env['HTTPS'] is 'on'.
  return 'https' if @env['HTTPS'] == 'on'
  #; [!zg8r2] returns env['rack.url_scheme'] ('http' or 'https').
  return @env['rack.url_scheme']
end

#script_nameObject

– def get? ; @method == :GET ; end def post? ; @method == :POST ; end def put? ; @method == :PUT ; end def delete? ; @method == :DELETE ; end def head? ; @method == :HEAD ; end def patch? ; @method == :PATCH ; end def options? ; @method == :OPTIONS ; end def trace? ; @method == :TRACE ; end ++



488
# File 'lib/keight.rb', line 488

def script_name  ; @env['SCRIPT_NAME' ] || ''; end

#server_nameObject

should NOT be empty



491
# File 'lib/keight.rb', line 491

def server_name  ; @env['SERVER_NAME' ]      ; end

#server_portObject

should NOT be empty



492
# File 'lib/keight.rb', line 492

def server_port  ; @env['SERVER_PORT' ].to_i ; end

#user_agentObject



506
# File 'lib/keight.rb', line 506

def user_agent       ; @env['HTTP_USER_AGENT']      ; end

#x_requested_withObject



507
# File 'lib/keight.rb', line 507

def x_requested_with ; @env['HTTP_X_REQUESTED_WITH']; end

#xhr?Boolean

Returns:

  • (Boolean)


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

def xhr?
  #; [!hsgkg] returns true when 'X-Requested-With' header is 'XMLHttpRequest'.
  return self.x_requested_with == 'XMLHttpRequest'
end