51. class HelloApp
def call(env)
[200, {quot;Content-Typequot; => quot;text/plainquot;},
quot;Hello, Sapporo!quot;]
end
end
require 'hello_app'
run HelloApp.new
$ rackup hello.ru
52.
53.
54.
55. if /HEAD/i =~ @cgi.request_method then
head['Pragma'] = 'no-cache'
head['Cache-Control'] = 'no-cache'
print @cgi.header( head )
else
#!/usr/bin/env ruby if @cgi.mobile_agent? then
# body = conf.to_mobile( tdiary.eval_rhtml( 'i.' ) )
# index.rb $Revision: 1.35 $ head['charset'] = conf.mobile_encoding
# head['Content-Length'] = body.size.to_s
# Copyright (C) 2001-2006, TADA Tadashi <sho@spc.gr.jp> else
# You can redistribute it and/or modify it under GPL2. require 'digest/md5'
# body = tdiary.eval_rhtml
BEGIN { $defout.binmode } head['ETag'] = %Q[quot;#{Digest::MD5.hexdigest( body )}quot;]
$KCODE = 'n' if ENV['HTTP_IF_NONE_MATCH'] == head['ETag'] and /^GET$/i =~ @cgi.
head['status'] = CGI::HTTP_STATUS['NOT_MODIFIED']
begin body = ''
if FileTest::symlink?( __FILE__ ) then else
org_path = File::dirname( File::readlink( __FILE__ ) ) head['charset'] = conf.encoding
else head['Content-Length'] = body.size.to_s
org_path = File::dirname( __FILE__ ) end
end head['Pragma'] = 'no-cache'
$:.unshift( org_path.untaint ) head['Cache-Control'] = 'no-cache'
require 'tdiary' end
head['cookie'] = tdiary.cookies if tdiary.cookies.size > 0
@cgi = CGI::new print @cgi.header( head )
conf = TDiary::Config::new(@cgi) print body
tdiary = nil end
status = nil rescue TDiary::ForceRedirect
if %r[/d{4,8}(-d+)?.html?$] =~ @cgi.redirect_url and not @cgi.valid?( 'date' ) then head = {
@cgi.params['date'] = [@cgi.redirect_url.sub( /.*/(d+)(-d+)?.html$/, '12' )] #'Location' => $!.path
status = CGI::HTTP_STATUS['OK'] 'type' => 'text/html',
end }
head['cookie'] = tdiary.cookies if tdiary.cookies.size > 0
begin print @cgi.header( head )
if @cgi.valid?( 'comment' ) then print %Q[
tdiary = TDiary::TDiaryComment::new( @cgi, quot;day.rhtmlquot;, conf ) <html>
elsif @cgi.valid?( 'date' ) <head>
date = @cgi.params['date'][0] <meta http-equiv=quot;refreshquot; content=quot;1;url=#{$!.path}quot;>
if /^d{8}-d+$/ =~ date then <title>moving...</title>
tdiary = TDiary::TDiaryLatest::new( @cgi, quot;latest.rhtmlquot;, conf ) </head>
elsif /^d{8}$/ =~ date <body>Wait or <a href=quot;#{$!.path}quot;>Click here!</a></body>
tdiary = TDiary::TDiaryDay::new( @cgi, quot;day.rhtmlquot;, conf ) </html>]
elsif /^d{6}$/ =~ date then rescue TDiary::NotFound
tdiary = TDiary::TDiaryMonth::new( @cgi, quot;month.rhtmlquot;, conf ) if @cgi then
elsif /^d{4}$/ =~ date then print @cgi.header( 'status' => CGI::HTTP_STATUS['NOT_FOUND'], 'type'
tdiary = TDiary::TDiaryNYear::new( @cgi, quot;month.rhtmlquot;, conf ) else
end print quot;Status: 404 Not Foundnquot;
elsif @cgi.valid?( 'category' ) print quot;Content-Type: text/htmlnnquot;
tdiary = TDiary::TDiaryCategoryView::new( @cgi, quot;category.rhtmlquot;, conf ) end
elsif @cgi.valid?( 'q' ) puts quot;<h1>404 Not Found</h1>quot;
tdiary = TDiary::TDiarySearch::new( @cgi, quot;search.rhtmlquot;, conf ) puts quot;<div>#{' ' * 500}</div>quot;
else end
tdiary = TDiary::TDiaryLatest::new( @cgi, quot;latest.rhtmlquot;, conf ) rescue Exception
end if @cgi then
rescue TDiary::PermissionError print @cgi.header( 'status' => CGI::HTTP_STATUS['SERVER_ERROR'], 'type'
raise else
rescue TDiary::TDiaryError print quot;Status: 500 Internal Server Errornquot;
end print quot;Content-Type: text/htmlnnquot;
tdiary = TDiary::TDiaryLatest::new( @cgi, quot;latest.rhtmlquot;, conf ) if not tdiary end
puts quot;<h1>500 Internal Server Error</h1>quot;
begin puts quot;<pre>quot;
head = { puts CGI::escapeHTML( quot;#{$!} (#{$!.class})quot; )
'type' => 'text/html', puts quot;quot;
'Vary' => 'User-Agent' puts CGI::escapeHTML( $@.join( quot;nquot; ) )
} puts quot;</pre>quot;
head['status'] = status if status puts quot;<div>#{' ' * 500}</div>quot;
body = '' end
113. # -*- coding: utf-8 -*-
Given quot; tdiary.confquot; do
...
@driver = TDiaryDriver.configure do
data_path File.expand_path(
quot;../fixtures/just_installed/tdiary.confquot;,
File.dirname(__FILE__))
end
end
...
Given /(?: | ) (.*)/ do |param|
@driver.append_param(param)
end
When /(.*) (?: | ) / do |uri|
@status, @header, @response = @driver.invoke(uri)
end
...
130. module Rack
class TDiaryApp
class << self
def index
new(TDiaryDriver.index)
end
...
end
...
def call(env)
# req = Request.new(env)
tdiary_conf = ::File.expand_path(
quot;../../tdiary.confquot;, ::File.dirname(__FILE__))
driver = @driver.configure {
data_path tdiary_conf
}
driver.invoke
end
end
end
131. ...
use Rack::ShowExceptions
use Rack::CommonLogger
use Rack::Lint
use Rack::Reloader
use Rack::Static, :urls => [quot;/themequot;], :root => parent_dir
map quot;/quot; do
run Rack::TDiaryApp.index
end
map quot;/index.rbquot; do
run Rack::TDiaryApp.index
end
map quot;/update.rbquot; do
run Rack::TDiaryApp.update
end