Class: Bijou::WEBrick::Adapter

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

Overview

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

Class Method Summary collapse

Class Method Details

.handle(req, res) ⇒ Object



17
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
# File 'lib/bijou/webrick/adapter.rb', line 17

def self.handle(req, res)
  bijou_types = 
    [
     [ 'rbb',  'text/html'  ],
     [ 'htm',  'text/html'  ],
     [ 'html', 'text/html'  ],
    ]

  path_info = req.path
  document_root = ENV['DOCUMENT_ROOT']
  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(res, "Error", 
                              "Error loading configuration.", error)
    end

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

  # The config file overrides the environment settings.
  if !config.document_root || config.document_root.empty?
    if document_root
      config.document_root = document_root
    else
      config.document_root = Dir.getwd
    end
  end

  if !config.cache_root && bijou_cache
    config.cache_root = bijou_cache
  end

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

  # Remove any leading slashes so expand_path doesn't see it as absolute.
  if path_info[0,1] == '/'
    path_info = path_info[1..-1]
  end

  found = false
  bijou_types.each {|type|
    ext = type[0]
    if path_info =~ /\.#{ext}$/
      found = true
    end
  }

  if !found
    response = Bijou::Processor.handle_other(config, path_info)
    
    res.status          = response['status']
    res['Content-Type'] = response['type']
    res.body            = response['body']
    return
  end

  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(res, "Error loading page", msg)
  end

  #
  # Prepare the request data.
  #

  context.request = Bijou::WEBrick::Request.new(req)
  context.response = HttpResponse.new

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

  begin
    context.render(args)
  rescue Exception
    msg = Bijou::ErrorFormatter.format_error :html, 4, context
    return self.formatted_error(res, "Error rendering page", msg)
  end

  res.status = 200
  res['Content-Type'] = "text/html"
  res.body = context.output;

  #        print_environment cgi
end