Class: Hermeneutics::Cgi

Inherits:
Object
  • 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 (private)



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

.mainObject

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

#dataObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/hermeneutics/cgi.rb', line 149

def data
  case request_method
    when "GET", "HEAD" then
      Data::UrlEnc.new query_string
    when "POST"        then
      data = $stdin.binmode.read
      data.bytesize == content_length.to_i or
        warn "Content length #{content_length} is wrong (#{data.bytesize})."
      ct = ContentType.parse content_type
      data.force_encoding ct[ :charset]||Encoding::ASCII_8BIT
      case ct.fulltype
        when "application/x-www-form-urlencoded" then
          Data::UrlEnc.new data
        when "multipart/form-data" then
          Data::Multiparted.new data, ct.hash
        when "text/plain" then
          Data::Plain.new data
        when "application/json" then
          Data::Json.new data
        when "application/x-yaml", "application/yaml" then
          Data::Yaml.new data
        else
          Data::UrlEnc.new data
      end
    else
      Data::Lines.new read_interactive
  end
end

#document(cls = Html, *args, &block) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/hermeneutics/cgi.rb', line 304

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.headers.add :content_type, ct, charset: cs
    end
    if doc.respond_to? :cookies then
      doc.cookies do |c|
        res.headers.add :set_cookie, c
      end
    end
  }
end

#execute(out = nil) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/hermeneutics/cgi.rb', line 282

def execute out = nil
  @out ||= $stdout
  begin
    run
  rescue
    done { |res|
      res.body = if $!.class.const_defined? :HTTP_STATUS then
        res.headers.add :status, "%03d" % $!.class::HTTP_STATUS
        $!.message + "\n"
      else
        # Why doesn't Ruby provide the encoding of #message?
        ($!.full_message highlight: false, order: :top).force_encoding $!.message.encoding
      end
      res.headers.add :content_type, "text/plain", charset: res.body.encoding
    }
  end
rescue Done
  @out << $!.result.to_s
ensure
  @out = nil
end

#fullname(dest) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
# File 'lib/hermeneutics/cgi.rb', line 344

def fullname dest
  if dest then
    if dest =~ /\.\w+\z/ then
      dest
    else
      "#{dest}.rb"
    end
  else
    script_name
  end
end

#fullpath(dest) ⇒ Object



356
357
358
359
360
361
362
363
# File 'lib/hermeneutics/cgi.rb', line 356

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

Returns:

  • (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



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/hermeneutics/cgi.rb', line 332

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.headers.add "Location", url }
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
    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_stringObject

This has not been tested.



371
372
373
# File 'lib/hermeneutics/cgi.rb', line 371

def query_string
  Apache::request.args
end

#runObject

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



365
366
# File 'lib/hermeneutics/cgi.rb', line 365

def warn msg
end