Skip to content
This repository
Browse code

monkey patching FCGI::each_cgi so that we can accept both UTF-8 input…

… and Shift_JIS input.
  • Loading branch information...
commit 5c8bcc051c1d8b0ac5e1e4a44233865358c35fcf 1 parent 67f2db1
Kazuhiko fdiary authored

Showing 3 changed files with 100 additions and 0 deletions. Show diff stats Hide diff stats

  1. +3 0  ChangeLog
  2. +7 0 index.fcgi
  3. +90 0 misc/lib/fcgi_patch.rb
3  ChangeLog
... ... @@ -1,3 +1,6 @@
  1 +2011-09-26 Kazuhiko <kazuhiko@fdiary.net>
  2 + * index.fcgi, misc/lib/fcgi_patch.rb: monkey patching FCGI::each_cgi so that we can accept both UTF-8 input and Shift_JIS input.
  3 +
1 4 2011-09-13 Kazuhiko <kazuhiko@fdiary.net>
2 5 * tdiary/config.rb (to_native): fix a typo that may cause NoMethodError, pointed out by hsbt.
3 6
7 index.fcgi
@@ -12,6 +12,13 @@ require 'fcgi'
12 12 # workaround untaint LOAD_PATH for rubygems library path is always tainted.
13 13 $:.each{|path| path.untaint if path.include?('fcgi') && path.tainted? }
14 14
  15 +if FileTest::symlink?( __FILE__ ) then
  16 + org_path = File::dirname( File::readlink( __FILE__ ) ).untaint
  17 +else
  18 + org_path = File::dirname( __FILE__ ).untaint
  19 +end
  20 +load "#{org_path}/misc/lib/fcgi_patch.rb"
  21 +
15 22 FCGI.each_cgi do |cgi|
16 23 begin
17 24 ENV.clear
90 misc/lib/fcgi_patch.rb
... ... @@ -0,0 +1,90 @@
  1 +=begin
  2 +
  3 +Patched version of FCGI::each_cgi from fcgi-0.8.8.
  4 +* if encoding error happens with UTF-8, try Shift_JIS.
  5 +
  6 +Original copyright notices:
  7 + fastcgi.rb Copyright (C) 2001 Eli Green
  8 + fcgi.rb Copyright (C) 2002-2003 MoonWolf <moonwolf@moonwolf.com>
  9 + fcgi.rb Copyright (C) 2004 Minero Aoki
  10 +
  11 +Patch written by:
  12 + Copyright (C) 2011 Kazuhiko <kazuhiko@fdiary.net>
  13 +
  14 +=end
  15 +
  16 +class FCGI
  17 + def self::each_cgi(*args)
  18 + require 'cgi'
  19 +
  20 + eval(<<-EOS,TOPLEVEL_BINDING)
  21 + class CGI
  22 + public :env_table
  23 + def self::remove_params
  24 + if (const_defined?(:CGI_PARAMS))
  25 + remove_const(:CGI_PARAMS)
  26 + remove_const(:CGI_COOKIES)
  27 + end
  28 + end
  29 + end # ::CGI class
  30 +
  31 + class FCGI
  32 + class CGI < ::CGI
  33 + def initialize(request, *args)
  34 + ::CGI.remove_params
  35 + @request = request
  36 + # PATCH BEGIN : if encoding error happens with UTF-8, try
  37 + # Shift_JIS.
  38 + encoding_error = {}
  39 + super(*(args+[:accept_charset => "UTF-8"])) do |name, value|
  40 + encoding_error[name] = value
  41 + end
  42 + params = @params
  43 + unless encoding_error.empty?
  44 + super(*(args+[:accept_charset => "Shift_JIS"]))
  45 + @params = params
  46 + end
  47 + # PATCH END : if encoding error happens with UTF-8, try
  48 + # Shift_JIS.
  49 + @args = *args
  50 + end
  51 + def args
  52 + @args
  53 + end
  54 + def env_table
  55 + @request.env
  56 + end
  57 + def stdinput
  58 + @request.in
  59 + end
  60 + def stdoutput
  61 + @request.out
  62 + end
  63 + end # FCGI::CGI class
  64 + end # FCGI class
  65 + EOS
  66 +
  67 + if FCGI::is_cgi?
  68 + yield ::CGI.new(*args)
  69 + else
  70 + exit_requested = false
  71 + FCGI::each {|request|
  72 + $stdout, $stderr = request.out, request.err
  73 +
  74 + yield CGI.new(request, *args)
  75 +
  76 + request.finish
  77 + }
  78 + end
  79 + end
  80 +end
  81 +
  82 +# The following indentation rule is not same as tDiary's by intention.
  83 +# It is same as the original (fcgi-0.8.8/lib/fcgi.rb) so that we can
  84 +# easily see the difference by diff command.
  85 +
  86 +# Local Variables:
  87 +# mode: ruby
  88 +# indent-tabs-mode: nil
  89 +# ruby-indent-level: 2
  90 +# End:

0 comments on commit 5c8bcc0

Please sign in to comment.
Something went wrong with that request. Please try again.