PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/innate/request.rb

https://github.com/fiber/innate
Ruby | 141 lines | 33 code | 10 blank | 98 comment | 4 complexity | 0a5d3e518cb01b56eb8d3c3f440858a8 MD5 | raw file
  1. module Innate
  2. # Subclass Rack::Request and add some convenient methods.
  3. #
  4. # An instance is available via the #request method in your Node.
  5. #
  6. # NOTE:
  7. # Please make sure to read the documentation of Rack::Request together with
  8. # this, as there are a lot of features available.
  9. #
  10. # A list of methods from Rack::Request so you get a gist of it:
  11. #
  12. # ## Generally
  13. #
  14. # * body
  15. # * cookies
  16. # * env
  17. # * fullpath
  18. # * host
  19. # * port
  20. # * scheme
  21. # * url
  22. #
  23. # ## ENV shortcuts
  24. #
  25. # * accept_encoding
  26. # * content_charset
  27. # * content_length
  28. # * content_type
  29. # * ip
  30. # * media_type
  31. # * media_type_params
  32. # * path_info
  33. # * path_info=
  34. # * query_string
  35. # * referrer
  36. # * script_name
  37. # * script_name=
  38. # * xhr?
  39. #
  40. # ## Query request method
  41. #
  42. # * delete?
  43. # * get?
  44. # * head?
  45. # * post?
  46. # * put?
  47. # * request_method
  48. #
  49. # ## parameter handling
  50. #
  51. # * []
  52. # * []=
  53. # * form_data?
  54. # * params
  55. # * values_at
  56. class Request < Rack::Request
  57. # Currently handled request from Thread.current[:request]
  58. # Call it from anywhere via Innate::Request.current
  59. def self.current
  60. Current.request
  61. end
  62. # Let's allow #[] to act like #values_at.
  63. #
  64. # Usage given a GET request like /hey?foo=duh&bar=doh
  65. #
  66. # request[:foo, :bar] # => ['duh', 'doh']
  67. #
  68. # Both +value+ and the elements of +keys+ will be turned into String by #to_s.
  69. def [](value, *keys)
  70. return super(value) if keys.empty?
  71. [value, *keys].map{|key| super(key) }
  72. end
  73. # the full request URI provided by Rack::Request
  74. # e.g. "http://localhost:7000/controller/action?foo=bar.xhtml"
  75. def request_uri
  76. env['REQUEST_URI'] || env['PATH_INFO']
  77. end
  78. # Answers with a subset of request.params with only the key/value pairs for
  79. # which you pass the keys.
  80. # Valid keys are objects that respond to :to_s
  81. #
  82. # @example usage
  83. #
  84. # request.params
  85. # # => {'name' => 'jason', 'age' => '45', 'job' => 'lumberjack'}
  86. # request.subset('name')
  87. # # => {'name' => 'jason'}
  88. # request.subset(:name, :job)
  89. # # => {'name' => 'jason', 'job' => 'lumberjack'}
  90. def subset(*keys)
  91. keys = keys.map{|key| key.to_s }
  92. params.reject{|key, value| not keys.include?(key) }
  93. end
  94. # Try to figure out the domain we are running on, this might work for some
  95. # deployments but fail for others, given the combination of servers in
  96. # front.
  97. #
  98. # @example usage
  99. #
  100. # domain
  101. # # => #<URI::HTTPS:0xb769ecb0 URL:https://localhost:7000/>
  102. # domain('/foo')
  103. # # => #<URI::HTTPS:0xb769ecb0 URL:https://localhost:7000/foo>
  104. #
  105. # @param [#to_s] path
  106. #
  107. # @return [URI]
  108. #
  109. # @api external
  110. # @author manveru
  111. def domain(path = nil, options = {})
  112. uri = URI(self.url)
  113. uri.path = path.to_s if path
  114. uri.query = nil unless options[:keep_query]
  115. uri
  116. end
  117. ipv4 = %w[ 127.0.0.1/32 192.168.0.0/16 172.16.0.0/12 10.0.0.0/8 169.254.0.0/16 ]
  118. ipv6 = %w[ fc00::/7 fe80::/10 fec0::/10 ::1 ]
  119. LOCAL = (ipv4 + ipv6).map{|range| IPAddr.new(range)} unless defined?(LOCAL)
  120. # Request is from a local network?
  121. # Checks both IPv4 and IPv6
  122. # Answer is true if the IP address making the request is from local network.
  123. # Optional argument address can be used to check any IP address.
  124. def local_net?(address = ip)
  125. addr = IPAddr.new(address)
  126. LOCAL.find{|range| range.include?(addr) }
  127. rescue ArgumentError => ex
  128. raise ArgumentError, ex unless ex.message == 'invalid address'
  129. end
  130. end
  131. end