-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.rb
executable file
·137 lines (131 loc) · 3.89 KB
/
index.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env ruby
# -*- coding: utf-8; -*-
#
# index.rb $Revision: 1.35 $
#
# Copyright (C) 2001-2006, TADA Tadashi <sho@spc.gr.jp>
# You can redistribute it and/or modify it under GPL2.
#
BEGIN { $stdout.binmode }
begin
Encoding::default_external = 'UTF-8'
rescue NameError
$KCODE = 'n'
end
begin
if FileTest::symlink?( __FILE__ ) then
org_path = File::dirname( File::readlink( __FILE__ ) )
else
org_path = File::dirname( __FILE__ )
end
$:.unshift( org_path.untaint )
require 'tdiary'
@cgi = CGI::new
conf = TDiary::Config::new(@cgi)
tdiary = nil
status = nil
if %r[/\d{4,8}(-\d+)?\.html?$] =~ @cgi.redirect_url and not @cgi.valid?( 'date' ) then
@cgi.params['date'] = [@cgi.redirect_url.sub( /.*\/(\d+)(-\d+)?\.html$/, '\1\2' )]
status = CGI::HTTP_STATUS['OK']
end
begin
if @cgi.valid?( 'comment' ) then
tdiary = TDiary::TDiaryComment::new( @cgi, "day.rhtml", conf )
elsif @cgi.valid?( 'date' )
date = @cgi.params['date'][0]
if /^\d{8}-\d+$/ =~ date then
tdiary = TDiary::TDiaryLatest::new( @cgi, "latest.rhtml", conf )
elsif /^\d{8}$/ =~ date
tdiary = TDiary::TDiaryDay::new( @cgi, "day.rhtml", conf )
elsif /^\d{6}$/ =~ date then
tdiary = TDiary::TDiaryMonth::new( @cgi, "month.rhtml", conf )
elsif /^\d{4}$/ =~ date then
tdiary = TDiary::TDiaryNYear::new( @cgi, "month.rhtml", conf )
end
elsif @cgi.valid?( 'category' )
tdiary = TDiary::TDiaryCategoryView::new( @cgi, "category.rhtml", conf )
elsif @cgi.valid?( 'q' )
tdiary = TDiary::TDiarySearch::new( @cgi, "search.rhtml", conf )
else
tdiary = TDiary::TDiaryLatest::new( @cgi, "latest.rhtml", conf )
end
rescue TDiary::PermissionError
raise
rescue TDiary::TDiaryError
end
tdiary = TDiary::TDiaryLatest::new( @cgi, "latest.rhtml", conf ) if not tdiary
begin
head = {
'type' => 'text/html',
'Vary' => 'User-Agent'
}
head['status'] = status if status
body = ''
head['Last-Modified'] = CGI::rfc1123_date( tdiary.last_modified )
if /HEAD/i =~ @cgi.request_method then
head['Pragma'] = 'no-cache'
head['Cache-Control'] = 'no-cache'
print @cgi.header( head )
else
if @cgi.mobile_agent? then
body = conf.to_mobile( tdiary.eval_rhtml( 'i.' ) )
head['charset'] = conf.mobile_encoding
head['Content-Length'] = body.bytesize.to_s
else
require 'digest/md5'
body = tdiary.eval_rhtml
head['ETag'] = %Q["#{Digest::MD5.hexdigest( body )}"]
if ENV['HTTP_IF_NONE_MATCH'] == head['ETag'] and /^GET$/i =~ @cgi.request_method then
head['status'] = CGI::HTTP_STATUS['NOT_MODIFIED']
body = ''
else
head['charset'] = conf.encoding
head['Content-Length'] = body.bytesize.to_s
end
head['Pragma'] = 'no-cache'
head['Cache-Control'] = 'no-cache'
end
head['cookie'] = tdiary.cookies if tdiary.cookies.size > 0
print @cgi.header( head )
print body
end
rescue TDiary::NotFound
if @cgi then
print @cgi.header( 'status' => CGI::HTTP_STATUS['NOT_FOUND'], 'type' => 'text/html' )
else
print "Status: 404 Not Found\n"
print "Content-Type: text/html\n\n"
end
puts "<h1>404 Not Found</h1>"
puts "<div>#{' ' * 500}</div>"
end
rescue TDiary::ForceRedirect
head = {
#'Location' => $!.path
'type' => 'text/html',
}
head['cookie'] = tdiary.cookies if tdiary && tdiary.cookies.size > 0
print @cgi.header( head )
print %Q[
<html>
<head>
<meta http-equiv="refresh" content="1;url=#{$!.path}">
<title>moving...</title>
</head>
<body>Wait or <a href="#{$!.path}">Click here!</a></body>
</html>]
rescue Exception
if @cgi then
print @cgi.header( 'status' => CGI::HTTP_STATUS['SERVER_ERROR'], 'type' => 'text/html' )
else
print "Status: 500 Internal Server Error\n"
print "Content-Type: text/html\n\n"
end
puts "<h1>500 Internal Server Error</h1>"
puts "<pre>"
puts CGI::escapeHTML( "#{$!} (#{$!.class})" )
puts ""
puts CGI::escapeHTML( $@.join( "\n" ) )
puts "</pre>"
puts "<div>#{' ' * 500}</div>"
end