Class: K8::RackRequest

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

Constant Summary collapse

MAX_FORM_SIZE =
10*1024*1024
MAX_JSON_SIZE =
10*1024*1024
MAX_MULTIPART_SIZE =
100*1024*1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ RackRequest

Returns a new instance of RackRequest.



1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
# File 'lib/keight.rb', line 1348

def initialize(env)
  #; [!yb9k9] sets @env.
  @env = env
  #; [!yo22o] sets @meth as Symbol value.
  @meth = 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.



1360
1361
1362
# File 'lib/keight.rb', line 1360

def env
  @env
end

#methObject (readonly)

Returns the value of attribute meth.



1360
1361
1362
# File 'lib/keight.rb', line 1360

def meth
  @meth
end

#pathObject (readonly)

Returns the value of attribute path.



1360
1361
1362
# File 'lib/keight.rb', line 1360

def path
  @path
end

Instance Method Details

#clearObject



1562
1563
1564
1565
1566
1567
# File 'lib/keight.rb', line 1562

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

#client_ip_addrObject



1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
# File 'lib/keight.rb', line 1422

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



1407
1408
1409
1410
1411
# File 'lib/keight.rb', line 1407

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

#content_typeObject



1402
1403
1404
1405
# File 'lib/keight.rb', line 1402

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

#cookiesObject



1557
1558
1559
1560
# File 'lib/keight.rb', line 1557

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



1375
1376
1377
1378
# File 'lib/keight.rb', line 1375

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

#method(name = nil) ⇒ Object

(experimental; use #meth instead)



1363
1364
1365
1366
1367
# File 'lib/keight.rb', line 1363

def method(name=nil)    # :nodoc:
  #; [!084jo] returns current request method when argument is not specified.
  #; [!gwskf] calls Object#method() when argument specified.
  return name ? super : @meth
end

#paramsObject



1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
# File 'lib/keight.rb', line 1535

def params
  #; [!erlc7] parses QUERY_STRING when request method is GET or HEAD.
  #; [!j2lno] parses form parameters when content type is 'application/x-www-form-urlencoded'.
  #; [!z5w4k] raises error when content type is 'multipart/form-data' (because params_multipart() returns two values).
  #; [!td6fw] raises error when content type is 'application/json' (because JSON data can contain non-string values).
  if @meth == :GET || @meth == :HEAD
    return params_query()       # hash object
  end
  case @env['CONTENT_TYPE']
  when /\Aapplication\/x-www-form-urlencoded\b/
    return params_form()        # hash object
  when /\Amultipart\/form-data\b/
    #return params_multipart()  # array of hash objects
    raise PayloadParseError.new("don't use `@req.params' for multipart data; use `@req.params_multipart' instead.")
  when /\Aapplication\/json\b/
    #return params_json()       # hash object
    raise PayloadParseError.new("use `@req.json' for JSON data instead of `@req.params'.")
  else
    return {}                   # hash object
  end
end

#params_form(max_content_length = nil) ⇒ Object Also known as: form



1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
# File 'lib/keight.rb', line 1466

def params_form(max_content_length=nil)
  #; [!iultp] returns same value when called more than once.
  d = @params_form
  return d if d
  #; [!uq46o] raises 400 error when payload is not form data.
  self.content_type == 'application/x-www-form-urlencoded'  or
    raise HttpException.new(400, "expected form data, but Content-Type header is #{self.content_type.inspect}.")
  #; [!puxlr] raises 400 error when content length is too large (> 10MB).
  clen = get_content_length(max_content_length || MAX_FORM_SIZE)
  #; [!59ad2] parses form parameters and returns it as Hash object.
  payload = get_input_stream().read(clen)
  d = @params_form = Util.parse_query_string(payload)
  return d
end

#params_json(max_content_length = nil) ⇒ Object Also known as: json



1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
# File 'lib/keight.rb', line 1502

def params_json(max_content_length=nil)
  #; [!5kwij] returns same value when called more than once.
  d = @params_json
  return d if d
  #; [!qjgfz] raises 400 error when not JSON data.
  self.content_type =~ /\Aapplication\/json\b/  or
    raise HttpException.new(400, "expected JSON data, but Content-Type header is #{self.content_type.inspect}.")
  #; [!on107] raises error when content length of JSON is too large (> 10MB).
  clen = get_content_length(max_content_length || MAX_JSON_SIZE)
  #; [!ugik5] parses json data and returns it as hash object when json data is sent.
  payload = get_input_stream().read(clen)
  d = @params_json = JSON.parse(payload)
  return d
end

#params_multipart(max_content_length = nil) ⇒ Object Also known as: multipart



1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'lib/keight.rb', line 1482

def params_multipart(max_content_length=nil)
  #; [!gbdxu] returns same values when called more than once.
  d1 = @params_form
  d2 = @params_file
  return d1, d2 if d1 && d2
  #; [!ho5ii] raises 400 error when not multipart data.
  self.content_type =~ /\Amultipart\/form-data(?:;\s*boundary=(.+))?/  or
    raise HttpException.new(400, "expected multipart data, but Content-Type header is #{self.content_type.inspect}.")
  #; [!davzs] raises 400 error when boundary is missing.
  boundary = $1  or
    raise HttpException.new(400, 'bounday attribute of multipart required.')
  #; [!mtx6t] raises error when content length of multipart is too large (> 100MB).
  clen = get_content_length(max_content_length || MAX_MULTIPART_SIZE)
  #; [!y1jng] parses multipart when multipart data posted.
  d1, d2 = Util.parse_multipart(get_input_stream(), boundary, clen, nil, nil)
  @params_form = d1; @params_file = d2
  return d1, d2
end

#params_queryObject Also known as: query



1458
1459
1460
1461
1462
1463
# File 'lib/keight.rb', line 1458

def params_query
  #; [!6ezqw] parses QUERY_STRING and returns it as Hash/dict object.
  #; [!o0ws7] unquotes both keys and values.
  #; [!2fhrk] returns same value when called more than once.
  return @params_query ||= Util.parse_query_string(@env['QUERY_STRING'] || "")
end

#path_extObject



1369
1370
1371
1372
1373
# File 'lib/keight.rb', line 1369

def path_ext
  #; [!tf6yz] returns extension of request path such as '.html' or '.json'.
  #; [!xnurj] returns empty string when no extension.
  return File.extname(@path)
end

#path_infoObject

may be empty



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

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

#query_stringObject

may be empty



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

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

#rack_errorsObject

ex: $stderr



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

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

#rack_hijackObject

ex: callable object



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

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

#rack_hijack?Boolean

ex: true or false

Returns:

  • (Boolean)


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

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

#rack_hijack_ioObject

ex: socket object



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

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

#rack_inputObject

ex: $stdout



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

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

#rack_loggerObject

ex: Logger.new



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

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

#rack_multiprocessObject

ex: true



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

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

#rack_multithreadObject

ex: true



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

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

#rack_run_onceObject

ex: false



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

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

#rack_sessionObject

ex: {}



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

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

#rack_url_schemeObject

ex: ‘http’ or ‘https’



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

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

#rack_versionObject

ex: [1, 3]



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

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

#refererObject



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

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

#request_methodObject



1380
1381
1382
1383
# File 'lib/keight.rb', line 1380

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

#schemeObject



1434
1435
1436
1437
1438
1439
# File 'lib/keight.rb', line 1434

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? ; @meth == :GET ; end def post? ; @meth == :POST ; end def put? ; @meth == :PUT ; end def delete? ; @meth == :DELETE ; end def head? ; @meth == :HEAD ; end def patch? ; @meth == :PATCH ; end def options? ; @meth == :OPTIONS ; end def trace? ; @meth == :TRACE ; end ++



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

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

#server_nameObject

should NOT be empty



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

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

#server_portObject

should NOT be empty



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

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

#user_agentObject



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

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

#x_requested_withObject



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

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

#xhr?Boolean

Returns:

  • (Boolean)


1417
1418
1419
1420
# File 'lib/keight.rb', line 1417

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