Class: Bijou::CGI::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/bijou/cgi/adapter.rb

Overview

This adapter encapsulates the differences between the CGI and the interfaces expected within the Bijou environment.

Class Method Summary collapse

Class Method Details

.diagnostic(message, diagnostic) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/bijou/cgi/adapter.rb', line 143

def self.diagnostic(message, diagnostic)
  print "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"

  puts '<html><h1>500 Internal server error</h1>' +
    "<h3>#{message}</h3>" +
    "<pre>#{::CGI::escapeHTML(diagnostic)}</pre>" +
    '</html>'
  return nil
end

.error(message) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/bijou/cgi/adapter.rb', line 134

def self.error(message)
  print "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"

  puts '<html><h1>500 Internal server error</h1>' +
    "<p>#{message}</p>" +
    '</html>'
  return nil
end

.formatted_error(title, message) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bijou/cgi/adapter.rb', line 153

def self.formatted_error(title, message)
  print "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  puts '<html>'
  puts '<head><title>Bijou Error</title>'
  puts '<style type="text/css">'
  puts Bijou::ErrorFormatterHTML.style
  puts '</style>'
  puts '</head>'
  puts '<body>'
  puts "<h1>#{title}</h1>"
  puts message
  puts '</body>'
  puts '</html>'
  return nil
end

.handleObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bijou/cgi/adapter.rb', line 18

def self.handle
  t0 = Time.now

  # Use binmode so CR\LF pairs aren't translated on Windows.
  $stdout.binmode if defined? $stdout.binmode

  #
  # Initialize the configuration.
  #

  document_root = ENV['DOCUMENT_ROOT']

  # The document root must be specified.
  if !FileTest.directory?(document_root)
    return self::error("The document root is invalid. " +
                       "Please check the CGI configuration.")
  end

  path_info = ENV['PATH_INFO']
  bijou_cache = ENV['BIJOU_CACHE']
  bijou_config = ENV['BIJOU_CONFIG']

  if bijou_config
    begin
      config = Bijou::Config.load_file(bijou_config)
    rescue SyntaxError
      error = $!.to_s
      error << "\nStack: " + $@.join("\n")

      return self::diagnostic("Error loading configuration.", error)
    end

    if (!config)
      return self::error("The configuration file path is invalid. " +
                         "Please check the CGI configuration.")
    end
  else
    config = Bijou::Config.new
  end

  # The CGI value is always used for the document root because the 
  # server may have expectations about it.
  config.document_root = document_root

  # The config file overrides the CGI cache root value
  if !config.cache_root && bijou_cache
    config.cache_root = bijou_cache
  end

  config.includes.each do |inc|
    $:.push inc
  end

  #
  # Load the requested page.
  #

  processor = Bijou::Processor.new

  # Build the page from the request.
  begin
    context = processor.load(path_info, config)
  rescue Exception
    msg = Bijou::ErrorFormatter.format_error :html, 4
    return self.formatted_error("Error loading page", msg)
  end

  #
  # Prepare the request data.
  #

  context.request = Bijou::CGI::Request.new(true)
  context.response = HttpResponse.new

  args = {}
  args.replace(context.request.params);

  #
  # Handle the request.
  #

  begin
    context.render(args)
  rescue EndRequest
    # Normal end request; possibly a redirect.
  rescue Exception
    msg = Bijou::ErrorFormatter.format_error :html, 4, context
    return self.formatted_error("Error rendering page", msg)
  end

  #
  # Return the response.
  #

  # print "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  print context.response.render_headers

  if !context.response.suppress_content
    puts context.output

    if config.debug
      t1 = Time.now

      # self.print_request(context)
      # self.print_environment

      puts "<pre>" + "Time: #{t1 - t0}"
      puts "Log:"
      puts ::CGI.escapeHTML(context.get_log)
      puts "Trace:"
      puts ::CGI.escapeHTML(context.get_trace)
      puts "</pre>"
    end
  end
end


169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bijou/cgi/adapter.rb', line 169

def self.print_environment()
  #        print "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  #        print "Hello, world!"
  #        puts $:

  print "<pre>" +
    ::CGI::escapeHTML(
                      ENV.collect() do |key, value|
                        key + " --> " + value + "\n"
                      end.join("")
                      ) +
    "</pre>"
end


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/bijou/cgi/adapter.rb', line 183

def self.print_request(context)
  #        print "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  #        print "Hello, world!"
  #        puts $:

  cgi = context.request.cgi

  print "<pre>" +
    ::CGI::escapeHTML(
                      "query: " + cgi.query_string.inspect + "\n" +
                      "form: " + cgi.form.inspect + "\n" +
                      "cookies: " + cgi.cookies.inspect + "\n" +
                      "params: " + cgi.params.inspect + "\n"
                      ) +
    "</pre>"
end