Module: PrestoCore::Utils

Included in:
App, Controller, Mapper, Slice, PrestoHTTP::API, PrestoHTTP::BasicAuth, PrestoHTTP::Config, PrestoHTTP::DigestAuth, PrestoHTTP::HTMLAuth, PrestoHTTP::Response, PrestoView::API, PrestoView::Config
Defined in:
lib/presto/core/utils.rb

Constant Summary

STATUS__OK =
200
STATUS__REDIRECT =
302
STATUS__PERMANENT_REDIRECT =
301
STATUS__NOT_FOUND =
404
STATUS__SERVER_ERROR =
500
STATUS__RESTRICTED =
401
PATH_MODIFIERS =
[
    /^\.\.$/,
    '../', '/../', '/..',
    '..%2F', '%2F..%2F', '%2F..',
    '..\\', '\\..\\', '\\..',
    '..%5C', '%5C..%5C', '%5C..',
].freeze

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (String) build_path(path = nil, *args)

takes an arbitrary number of arguments and builds an HTTP path. Hash arguments will transformed into HTTP params. empty hash elements will be ignored.

Examples:

::PrestoCore::Utils.build_path :some, :page, and: :some_param
#=> some/page?and=some_param
::PrestoCore::Utils.build_path 'another', 'page', with: {'nested' => 'params'}
#=> another/page?with[nested]=params
::PrestoCore::Utils.build_path 'page', with: 'param-added', an_ignored_param: nil
#=> page?with=param-added

Parameters:

  • args (Array)

Returns:

  • (String)


68
69
70
71
72
# File 'lib/presto/core/utils.rb', line 68

def build_path path = nil, *args
  path = '' << path.to_s
  query = (Hash === args.last ? '?' << ::Rack::Utils.build_nested_query(args.pop) : '')
  path << (args.size == 0 || path =~ /\/$/ ? '' : '/') << args.compact.join('/') << query
end

+ (Boolean) is_controller?(obj)

Returns:

  • (Boolean)


86
87
88
# File 'lib/presto/core/utils.rb', line 86

def is_controller? obj
  obj.is_a?(::Class) && obj.include?(::Presto) && obj.http.path
end

+ (String) normalize_path(path)

Note:

it will also remove duplicating slashes.

"fluffing" potentially hostile paths. to avoid paths traversing, it basically removes ../ /../ /.. ..\ ..\ .. and for cases when given path is escaped ..%2F %2F..%2F %2F.. ..%5C %5C..%5C %5C..

backslashes could be replaced by a backslash, however, it is not strictly necessary to add one more step to already heavy regexp operation, cause windows-es accepts / as a path separator.

Parameters:

  • *chunks (String, Symbol)

Returns:

  • (String)


43
44
45
# File 'lib/presto/core/utils.rb', line 43

def normalize_path path
  path.gsub Regexp.union(*PATH_MODIFIERS, /\\+/, /\/+/), '/'
end

+ (Object) rootify_url(url)



49
50
51
# File 'lib/presto/core/utils.rb', line 49

def rootify_url url
  '/' << normalize_path(url).gsub(/^\/+|\/+$/, '')
end

Instance Method Details

- (Object) extract_controllers(namespace)



76
77
78
79
80
81
82
83
84
# File 'lib/presto/core/utils.rb', line 76

def extract_controllers namespace
  @controllers << namespace if is_controller?(namespace)
  namespace.constants.each do |c|
    c = namespace.const_get(c)
    next unless c.is_a?(Module) # do not check for class type - any class is a module
    @controllers << c if is_controller?(c)
    extract_controllers c
  end
end