class Qwik::Request

Constants

DEFAULT_ACCEPT_LANGUAGE

Attributes

accept_language[RW]
auth[RW]
base[RW]
cookies[R]
ext[R]
ext_args[R]
fromhost[R]
header[R]
pass[R]
path_args[RW]
path_query[R]
plugin[RW]
query[R]
request_line[R]
request_method[R]
sessionid[RW]
sitename[R]
start_time[RW]
unparsed_uri[R]
user[RW]
useragent[R]

Public Class Methods

get_fromhost(request, forwarded_from) click to toggle source
# File vendor/qwik/lib/qwik/request-webrick.rb, line 46
def self.get_fromhost(request, forwarded_from)
  if forwarded_from
    forwarded_from.gsub!(/, /, ',')
    return forwarded_from
  end
  peer = request.peeraddr
  return peer[2] if peer
  return '127.0.0.1'
end
get_request_line(request) click to toggle source
# File vendor/qwik/lib/qwik/request-webrick.rb, line 56
def self.get_request_line(request)
  return '' if request.request_line.nil?
  request_line = request.request_line.sub(/\x0d?\x0a\z/o, '')
  request_line = request_line.sub(/ HTTP\/1\..\z/o, '')
  return request_line
end
new(config) click to toggle source
# File vendor/qwik/lib/qwik/request.rb, line 15
def initialize(config)
  @config = config

  @start_time = Time.now
  @start_time = Time.at(0) if defined?($test) && $test

  init_path

  # init_host
  @fromhost = nil

  @request_method = nil
  @cookies = {}
  @header = {}
  @query = {}
  @user = @pass = nil
  @auth = nil
  @useragent = UserAgent.new(self)
  @sessionid = nil
  @accept_language = DEFAULT_ACCEPT_LANGUAGE
  @request_line = nil
end
parse_path(path, default_sitename) click to toggle source
# File vendor/qwik/lib/qwik/request-path.rb, line 31
def self.parse_path(path, default_sitename)
  raise "first character must be '/'" unless path[0] == ?/

  path = remove_quote(path)

  path = normalize_path(path)       # for security

  # OBSOLETE: Patch for redirect plugin (old format).
  if /\A\/((?:http|https|ftp|file):\/.+)\z/ =~ path
    return [default_sitename, '', '', 'redirect', [$1], {}, []]
  end

  pas = path.split('/')
  pas.shift         # Drop the first null element.

  # Ad hoc: Error handling.
  if pas.length == 1
    if ! pas.first.include?('.') && /\/\z/ !~ path
      return [default_sitename, pas.first, '', nil, [], {}, []]
    end
  end

  sitename = base = ext = plugin = nil
  path_args = []
  path_query = {}
  ext_args = []

  pas.each_with_index {|pa, i|
    if plugin || ext
      # path_args is catch all.
      path_args << pa
      next
    end

    # Maybe sitename.
    if i == 0
      if Request.sitename?(pa)
        sitename = pa
      end
      next if sitename     # Skip.
    end

    ff = pa.split('.')

    # No end with dot.
    if ff[1].nil? || ff[1].empty?
      if ff[0] == 'theme' || ff[0] == 'attach'
        ff = ['', ff[0]]
      else
        base = ''
        ext = ''
        return [sitename, base, ext, plugin, path_args, path_query, ext_args]
      end
    end

    # Start with dot -> Action plugin.
    if ff[0].empty?
      raise 'no two action' unless plugin.nil?     # No two plugins.
      plugin = ff[1]
      next
    end

    # Accept only two.
    if 2 < ff.length
      base = ff.shift
      ext = ff.pop
      ext_args = ff
      next
    end

    raise 'base should be one.' if base    # base should be one.
    base = ff[0]
    ext = ff[1]
  }

  # sitename is not specified.
  sitename = default_sitename if sitename.nil?

  if base.nil?
    base, ext = ['FrontPage', 'html']
  end

  path_args.each {|pa|
    ff = pa.split('=')
    if ff.length == 2
      path_query[ff.first] = ff.last
    end
  }

  base.set_url_charset

  return [sitename, base, ext, plugin, path_args, path_query, ext_args]
end

Private Class Methods

normalize_path(path) click to toggle source

copied from webrick/httputils.rb

# File vendor/qwik/lib/qwik/request-path.rb, line 147
def self.normalize_path(path)
  raise "abnormal path `#{path}'" if path[0] != ?/
  ret = path.dup

  ret.gsub!(%r{/+}o, '/')                    # //      => /
  while ret.sub!(%r:/\.(/|\z):o, '/'); end   # /.      => /
  begin                                      # /foo/.. => /foo
    match = ret.sub!(%r{/([^/]+)/\.\.(/|\z)}o){
      if $1 == '..'
        raise "abnormal path `#{path}'"
      else
        '/'
      end
    }
  end while match

  raise "abnormal path `#{path}'" if %r{/\.\.(/|\z)} =~ ret
  return ret
end
remove_quote(path) click to toggle source

For Excite Translate Bug.

# File vendor/qwik/lib/qwik/request-path.rb, line 139
def self.remove_quote(path)
  if /\A(.+)\"(.+)\"\z/ =~ path
    return $1+$2
  end
  return path
end
sitename?(pa) click to toggle source
# File vendor/qwik/lib/qwik/request-path.rb, line 167
def self.sitename?(pa)
  ff = pa.split('.')
  return false if ff.first.empty? # action
  len = ff.length
  return true if len == 1
  # contain dot, and last is com or jp is external site
  return true if 1 < len && %w(com jp).include?(ff.last)
  return false
end

Public Instance Methods

[](key) click to toggle source
# File vendor/qwik/lib/qwik/request.rb, line 56
def [](key)
  return nil if @header.nil?
  value = @header[key.downcase]
  return nil if value.nil? || value.empty?
  return value.join(', ')
end
is_post?() click to toggle source
# File vendor/qwik/lib/qwik/request.rb, line 52
def is_post?
  return @request_method == 'POST'
end
parse_path(path) click to toggle source
# File vendor/qwik/lib/qwik/request-path.rb, line 20
def parse_path(path)
  if @unparsed_uri.nil?
    @unparsed_uri = path   # for test
  end

  @sitename, @base, @ext, @plugin, @path_args, @path_query, @ext_args =
    Request.parse_path(path, @config.default_sitename)

  return [@sitename, @base, @ext]   # only for test
end
parse_webrick(request) click to toggle source

request is a WEBrick::HTTPRequest

# File vendor/qwik/lib/qwik/request-webrick.rb, line 12
def parse_webrick(request)
  host = ''
  host = request.request_uri.host if request.request_uri

  @unparsed_uri = request.unparsed_uri

  begin
    parse_path(request.path)
  rescue
  end

  @header = request.header.dup if request.header

  @useragent = UserAgent.new(self)

  if request.accept_language && ! request.accept_language.empty?
    @accept_language = request.accept_language
  elsif @useragent.mobile
    @accept_language = ['ja']
  end

  @request_method = request.request_method
  request.cookies.each {|c| @cookies[c.name] = c.value }
  @query = request.query.dup
  @query.each {|k, v|
    v.set_url_charset
  }

  @user = @pass = nil

  @fromhost = Request.get_fromhost(request, self['x-forwarded-for'])
  @request_line = Request.get_request_line(request)
end

Private Instance Methods

init_path() click to toggle source
# File vendor/qwik/lib/qwik/request-path.rb, line 127
def init_path
  @sitename = nil
  @base = nil
  @ext = nil
  @plugin = nil
  @ext_args = []
  @path_args = []
  @path_query = {}
  @unparsed_uri = nil
end