Class: CGI

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

Overview

We’ll add three methods to the cgi class in order to separete get/post requests:

  • parameters – all (mixed) get and post

  • query_parameters – get

  • request_parameters – post

Direct Known Subclasses

CGIEmu

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fix_env(ec) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cuca/cgi_fix.rb', line 12

def fix_env(ec)
  if (ec['PATH_INFO'].nil? || ec['PATH_INFO'] == '') then
     pi =  ec['REQUEST_URI']
     pi = pi[0..(pi.index('?')-1)] if pi.include?('?')
     ec['PATH_INFO'] = pi
  end

  if (ec['QUERY_STRING'].nil? || ec['QUERY_STRING'] == '') then
     ec['QUERY_STRING'] = ec['REQUEST_URI'].include?('?') ?
         ec['REQUEST_URI'].scan(/.?\?(.*)/)[0][0] :
         ""
  end
  ec
end

Instance Method Details

#cgidumpObject

cgi implementation test



59
60
61
62
63
64
65
66
# File 'lib/cuca/cgi_fix.rb', line 59

def cgidump
  s = ""
  s << "-----ENV------<br>"
  self.env_table.each_pair { |k,v| s << "#{k} => #{v}<br>" }
  s << "--------------<br>"
  s << "Server Software: #{self.server_software}<br>"
  s << "PATH INFO: #{self.path_info}<br>"      
end

#env_tableObject



29
30
31
32
# File 'lib/cuca/cgi_fix.rb', line 29

def env_table
  CGI::fix_env(ENV)
  ENV
end

#parametersObject



34
35
36
# File 'lib/cuca/cgi_fix.rb', line 34

def parameters
  query_parameters.merge(request_parameters)
end

#query_parametersObject



38
39
40
41
42
43
44
45
46
# File 'lib/cuca/cgi_fix.rb', line 38

def query_parameters
   res = {}
   query_string.split(/[&;]/).each do |p|
            k, v = p.split("=")
            v = '' if v.nil?
            res[CGI.unescape(k)] = CGI.unescape(v)
   end
   res
end

#request_parametersObject



48
49
50
51
52
53
54
55
56
# File 'lib/cuca/cgi_fix.rb', line 48

def request_parameters
 if request_method == 'POST' then
   res = {}
   params.each_pair { |k,v| res[k] = v[0] }
   return res
 else
  return {}
 end
end