Skip to content

Instantly share code, notes, and snippets.

@zunda
Created June 16, 2011 05:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunda/1028700 to your computer and use it in GitHub Desktop.
Save zunda/1028700 to your computer and use it in GitHub Desktop.
Index: hiki/util.rb
===================================================================
--- hiki/util.rb (revision 1101)
+++ hiki/util.rb (working copy)
@@ -61,7 +61,7 @@
# # => "%27Stop%21%27+said+Fred"
def escape(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
- '%' + $1.unpack('H2' * $1.size).join('%').upcase
+ '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end.tr(' ', '+')
end
@@ -72,7 +72,7 @@
def unescape(string)
string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
[$1.delete('%')].pack('H*')
- end
+ end.force_encoding(Encoding.default_external)
end
# dead copy from cgi.rb (Ruby1.8)
@@ -118,7 +118,7 @@
else
"&#{match};"
end
- end
+ end.force_encoding(Encoding.default_external)
end
alias escape_html escapeHTML
Index: hiki/db/flatfile.rb
===================================================================
--- hiki/db/flatfile.rb (revision 1101)
+++ hiki/db/flatfile.rb (working copy)
@@ -81,7 +81,7 @@
def pages
Dir.glob( "#{@pages_path}/*" ).delete_if {|f| !test(?f, f.untaint)}.collect! {|f|
- unescape(File.basename( f ))
+ File.basename( f ).unescape
}
end
@@ -89,7 +89,7 @@
# info DB
# ==============
def info_exist? ( p )
- f = escape(p)
+ f = p.escape
@info.transaction(true) do
@info.root?( f )
end
@@ -100,7 +100,7 @@
end
def info( p )
- f = escape(p)
+ f = p.escape
@info.transaction(true) do
@info.root?(f) ? @info[f] : nil
end
@@ -109,13 +109,13 @@
def page_info
h = []
@info.transaction(true) do
- @info.roots.each { |a| h << { unescape(a) => @info[a]} }
+ @info.roots.each { |a| h << { a.unescape => @info[a]} }
end
h
end
def set_attribute(p, attr)
- f = escape(p)
+ f = p.escape
@info.transaction do
@info[f] = default unless @info[f]
attr.each do |attribute, value|
@@ -125,7 +125,7 @@
end
def get_attribute(p, attribute)
- f = escape(p)
+ f = p.escape
@info.transaction(true) do
if @info.root?(f)
@info[f][attribute] || default[attribute]
@@ -139,14 +139,14 @@
result = []
@info.transaction(true) do
@info.roots.each do |a|
- result << unescape(a) if yield(@info[a])
+ result << a.unescape if yield(@info[a])
end
end
result
end
def increment_hitcount ( p )
- f = escape(p)
+ f = p.escape
@info.transaction do
@info[f][:count] = @info[f][:count] + 1
end
@@ -197,7 +197,7 @@
end
def delete_info(p)
- f = escape(p)
+ f = p.escape
begin
@info.transaction do
@info.delete(f)
@@ -211,14 +211,14 @@
@info.transaction do
pages.each do |a|
r = default
- r[:last_modified] = File.mtime( "#{@pages_path}/#{escape(a)}".untaint )
- @info[escape(a)] = r
+ r[:last_modified] = File.mtime( "#{@pages_path}/#{a.escape}".untaint )
+ @info[a.escape] = r
end
end
end
def create_info_default(p)
- f = escape(p)
+ f = p.escape
@info.transaction do
@info[f] = default
end
@@ -235,11 +235,11 @@
end
def textdir(s)
- File.join(@pages_path, escape(s)).untaint
+ File.join(@pages_path, s.escape).untaint
end
def backupdir(s)
- File.join(@backup_path, escape(s)).untaint
+ File.join(@backup_path, s.escape).untaint
end
end
end
Index: hiki/db/tmarshal.rb
===================================================================
--- hiki/db/tmarshal.rb (revision 1101)
+++ hiki/db/tmarshal.rb (working copy)
@@ -3,6 +3,26 @@
# You can redistribute it and/or modify it under the terms of
# the Ruby's licence.
+class Hash
+ def force_encoding(encoding)
+ r = Hash.new
+ self.each_pair do |k,v|
+ k = k.dup.force_encoding(encoding) if k.respond_to?(:force_encoding)
+ v = v.dup.force_encoding(encoding) if v.respond_to?(:force_encoding)
+ r[k] = v
+ end
+ return r
+ end
+end
+
+class Array
+ def force_encoding(encoding)
+ self.map do |v|
+ v.dup.force_encoding(encoding) if v.respond_to?(:force_encoding)
+ end
+ end
+end
+
module TMarshal
module_function
def dump(obj, port = nil)
@@ -21,7 +41,7 @@
eval port.read.untaint
else
raise 'Wrong type!'
- end
+ end.force_encoding(Encoding.default_external)
end
def restore(port)
Index: hiki/storage.rb
===================================================================
--- hiki/storage.rb (revision 1101)
+++ hiki/storage.rb (working copy)
@@ -85,7 +85,7 @@
begin
tmp = Marshal.load( File.open( "#{cache_path}/#{escape(page)}".untaint, 'rb' ) {|f| f.read} )
if tmp[0] == Hiki::RELEASE_DATE
- return tmp[1]
+ return tmp[1].force_encoding(Encoding.default_external)
else
return nil
end
Index: hiki/config.rb
===================================================================
--- hiki/config.rb (revision 1101)
+++ hiki/config.rb (working copy)
@@ -38,7 +38,7 @@
# repository class
@repos = Hiki.const_get("Repos#{@repos_type.capitalize}").new(@repos_root, @data_path)
- self.class.__send__ :attr_accessor, *instance_variables.map{|v| v.sub('@', '') }
+ self.class.__send__ :attr_accessor, *instance_variables.map{|v| v.to_s.sub('@', '') }
bot = ["googlebot", "Hatena Antenna", "moget@goo.ne.jp"]
bot += @options['bot'] || []
Index: hiki.cgi
===================================================================
--- hiki.cgi (revision 1101)
+++ hiki.cgi (working copy)
@@ -8,6 +10,16 @@
# hikiconf.rb.sample.en).
Encoding.default_external = 'euc-jp'
rescue NameError
+ class Encoding
+ def Encoding.default_external
+ 'euc-jp'
+ end
+ end
+ class String
+ def force_encoding(arg)
+ self
+ end
+ end
$KCODE = 'e'
end
Index: plugin/00default.rb
===================================================================
--- plugin/00default.rb (revision 1101)
+++ plugin/00default.rb (working copy)
@@ -6,7 +6,7 @@
#==============================
def anchor( s )
s.sub!(/^\d+$/, '')
- p = h(escape(@page))
+ p = h(@page.escape)
p.gsub!(/%/, '%%')
%Q[#{@conf.cgi_name}?#{p}#{s}]
end
@@ -20,7 +20,7 @@
#==============================
#===== hiki_url
def hiki_url(page)
- "#{@conf.cgi_name}?#{escape(page)}"
+ "#{@conf.cgi_name}?#{page.escape}"
end
#===== hiki_anchor
@@ -96,7 +96,7 @@
ddd = cur_date
end
t = page_name(name)
- an = hiki_anchor(escape(name), t)
+ an = hiki_anchor(name.escape, t)
s << "<li>#{an}</li>\n"
end
s << "</ul>\n"
@@ -112,7 +112,7 @@
add_update_proc {
updating_mail if @conf.mail_on_update
if @user
- @conf.repos.commit(@page, escape(@user))
+ @conf.repos.commit(@page, @user.escape)
else
@conf.repos.commit(@page)
end
@@ -182,7 +182,7 @@
#===== edit_proc
add_edit_proc {
- hiki_anchor(escape(@page), "[#{page_name(@page)}]")
+ hiki_anchor(@page.escape, "[#{page_name(@page)}]")
}
#===== menu
@@ -193,8 +193,8 @@
menu << %Q!<a href="#{@conf.cgi_name}?c=index">#{@conf.msg_index}</a>!
else
menu << %Q!<a href="#{@conf.cgi_name}?c=create" rel="nofollow">#{@conf.msg_create}</a>! if creatable?
- menu << %Q!<a href="#{@conf.cgi_name}?c=edit;p=#{escape(@page)}" rel="nofollow">#{@conf.msg_edit}</a>! if @page && editable?
- menu << %Q!<a href="#{@conf.cgi_name}?c=diff;p=#{escape(@page)}" rel="nofollow">#{@conf.msg_diff}</a>! if @page && editable?
+ menu << %Q!<a href="#{@conf.cgi_name}?c=edit;p=#{@page.escape}" rel="nofollow">#{@conf.msg_edit}</a>! if @page && editable?
+ menu << %Q!<a href="#{@conf.cgi_name}?c=diff;p=#{@page.escape}" rel="nofollow">#{@conf.msg_diff}</a>! if @page && editable?
menu << %Q!#{hiki_anchor( 'FrontPage', page_name('FrontPage') )}!
menu << %Q!<a href="#{@conf.cgi_name}?c=index">#{@conf.msg_index}</a>!
menu << %Q!<a href="#{@conf.cgi_name}?c=search">#{@conf.msg_search}</a>!
@@ -203,14 +203,14 @@
next if c[:option].has_key?('p') && !(@page && editable?)
cmd = %Q!<a href="#{@conf.cgi_name}?c=#{c[:command]}!
c[:option].each do |key, value|
- value = escape(@page) if key == 'p'
+ value = @page.escape if key == 'p'
cmd << %Q!;#{key}=#{value}!
end
cmd << %Q!">#{c[:display_text]}</a>!
menu << cmd
end
menu_proc.each {|i| menu << i}
- menu << %Q!<a href="#{@conf.cgi_name}?c=login#{@page ? ";p=#{escape(@page)}" : ""}">#{@conf.msg_login}</a>! unless @user || @conf.password.empty?
+ menu << %Q!<a href="#{@conf.cgi_name}?c=login#{@page ? ";p=#{@page.escape}" : ""}">#{@conf.msg_login}</a>! unless @user || @conf.password.empty?
menu << %Q!<a href="#{@conf.cgi_name}?c=admin">#{@conf.msg_admin}</a>! if admin?
menu << %Q!<a href="#{@conf.cgi_name}?c=logout">#{@conf.msg_logout}</a>! if @user && !@conf.password.empty?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment