Class: Hermeneutics::Cgi
- Inherits:
-
Object
show all
- Defined in:
- lib/hermeneutics/cgi.rb
Overview
Example:
class MyCgi < Cgi
def run
p = parameters
if p.empty? then
location "/sorry.rb"
else
document MyHtml
end
rescue
document MyErrorPage
end
end Cgi.execute
Defined Under Namespace
Modules: Data
Classes: Done
Constant Summary
collapse
- CGIENV =
%w(content document gateway http query
remote request script server unique)
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/hermeneutics/cgi.rb', line 99
def method_missing sym, *args
if args.empty? and CGIENV.include? sym[ /\A(\w+?)_\w+\z/, 1] then
ENV[ sym.to_s.upcase]
else
super
end
end
|
Class Attribute Details
.main ⇒ Object
Returns the value of attribute main.
85
86
87
|
# File 'lib/hermeneutics/cgi.rb', line 85
def main
@main
end
|
Class Method Details
.execute(out = nil) ⇒ Object
89
90
91
|
# File 'lib/hermeneutics/cgi.rb', line 89
def execute out = nil
(@main||self).new.execute out
end
|
.inherited(cls) ⇒ Object
86
87
88
|
# File 'lib/hermeneutics/cgi.rb', line 86
def inherited cls
Cgi.main = cls
end
|
Instance Method Details
#document(cls = Html, *args, &block) ⇒ Object
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
# File 'lib/hermeneutics/cgi.rb', line 300
def document cls = Html, *args, &block
done { |res|
doc = cls.new
doc.cgi = self
res.body = ""
doc.generate res.body do
doc.document *args, &block
end
ct = if doc.respond_to? :content_type then doc.content_type
elsif cls.const_defined? :CONTENT_TYPE then doc.class::CONTENT_TYPE
end
if ct then
cs = if doc.respond_to? :charset then doc.charset
elsif cls.const_defined? :CHARSET then doc.class::CHARSET
else
res.body.encoding||Encoding.default_external
end
res..add :content_type, ct, charset: cs
end
if doc.respond_to? :cookies then
doc.cookies do |c|
res..add :set_cookie, c
end
end
}
end
|
#execute(out = nil) ⇒ Object
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/hermeneutics/cgi.rb', line 278
def execute out = nil
@out ||= $stdout
begin
run
rescue
done { |res|
res.body = if $!.class.const_defined? :HTTP_STATUS then
res..add :status, "%03d" % $!.class::HTTP_STATUS
$!.message + "\n"
else
($!.full_message highlight: false, order: :top).force_encoding $!.message.encoding
end
res..add :content_type, "text/plain", charset: res.body.encoding
}
end
rescue Done
@out << $!.result.to_s
ensure
@out = nil
end
|
#fullname(dest) ⇒ Object
340
341
342
343
344
345
346
347
348
349
350
|
# File 'lib/hermeneutics/cgi.rb', line 340
def fullname dest
if dest then
if dest =~ /\.\w+\z/ then
dest
else
"#{dest}.rb"
end
else
script_name
end
end
|
#fullpath(dest) ⇒ Object
352
353
354
355
356
357
358
359
|
# File 'lib/hermeneutics/cgi.rb', line 352
def fullpath dest
dest = fullname dest
unless File.absolute_path? dest then
dir = File.dirname script_name rescue ""
dest = File.join dir, dest
end
dest
end
|
#https? ⇒ Boolean
109
110
111
|
# File 'lib/hermeneutics/cgi.rb', line 109
def https?
ENV[ "HTTPS"].notempty?
end
|
#location(dest = nil, params = nil, anchor = nil) ⇒ Object
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/hermeneutics/cgi.rb', line 328
def location dest = nil, params = nil, anchor = nil
if Hash === dest then
dest, params, anchor = anchor, dest, params
end
utx = URLText.new mask_space: true
unless dest =~ %r{\A\w+://} then
dest = %Q'#{https? ? "https" : "http"}://#{http_host||"localhost"}#{fullpath dest}'
end
url = utx.mkurl dest, params, anchor
done { |res| res..add "Location", url }
end
|
#parameter_data ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/hermeneutics/cgi.rb', line 149
def parameter_data
case request_method
when "GET", "HEAD" then
dc, d = Data::UrlEnc, query_string
when "POST" then
d = $stdin.binmode.read
d.bytesize == content_length.to_i or
warn "Content length #{content_length} is wrong (#{d.bytesize})."
ct = ContentType.parse content_type
d.force_encoding ct[ :charset]||Encoding::ASCII_8BIT
dc = case ct.fulltype
when "application/x-www-form-urlencoded" then Data::UrlEnc
when "multipart/form-data" then a = [ ct.hash] ; Data::Multiparted
when "text/plain" then Data::Plain
when "application/json" then Data::Json
when "application/x-yaml", "application/yaml" then Data::Yaml
else Data::UrlEnc
end
else
dc, d = Data::Lines, read_interactive
end
dc.new d, *a
end
|
#parameters(nl: false, sym: false, strip: false) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/hermeneutics/cgi.rb', line 130
def parameters nl: false, sym: false, strip: false
if block_given? then
parameter_data.parse do |k,v,**kw|
k = k.to_sym if sym
if v then
v.strip! if strip
v.gsub! "\r\n", "\n" if nl
end
yield k, v.notempty?, **kw
end
else
p = {}
parameters nl: nl, sym: sym, strip: strip do |k,v|
p[ k] = v
end
p
end
end
|
#parameters!(nl: false, sym: false, strip: false) ⇒ Object
125
126
127
128
|
# File 'lib/hermeneutics/cgi.rb', line 125
def parameters! nl: false, sym: false, strip: false
@parameters ||= parameters nl: nl, sym: sym, strip: strip
nil
end
|
#query_string ⇒ Object
This has not been tested.
367
368
369
|
# File 'lib/hermeneutics/cgi.rb', line 367
def query_string
Apache::request.args
end
|
#run ⇒ Object
Overwrite this.
If you’re reacting to POST uploads, please consider limiting the upload size.
Apache: LimitRequestBody
Nginx: client_max_body_size
Lighttpd: server.max-request-size
121
122
123
|
# File 'lib/hermeneutics/cgi.rb', line 121
def run
document Html
end
|
#warn(msg) ⇒ Object
361
362
|
# File 'lib/hermeneutics/cgi.rb', line 361
def warn msg
end
|