summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/cgi-lib.rb57
-rw-r--r--lib/ftools.rb4
-rw-r--r--lib/parsedate.rb6
3 files changed, 62 insertions, 5 deletions
diff --git a/lib/cgi-lib.rb b/lib/cgi-lib.rb
index 7033f0f8c1..ef717b20bf 100644
--- a/lib/cgi-lib.rb
+++ b/lib/cgi-lib.rb
@@ -7,6 +7,24 @@
# foo['field'] <== value of 'field'
# foo.keys <== array of fields
# and foo has Hash class methods
+#
+# foo.cookie['name'] <== cookie value of 'name'
+# foo.cookie.keys <== all cookie names
+# and foo.cookie has Hash class methods
+#
+# make raw cookie string
+# cookie1 = CGI.cookie({'name' => 'name',
+# 'value' => 'value',
+# 'path' => 'path', # optional
+# 'domain' => 'domain', # optional
+# 'expires' => Time.now, # optional
+# 'secure' => true # optional
+# })
+#
+# print CGI.header("Content-Type: text/html", cookie1, cookie2)
+#
+# print CGI.header("HTTP/1.0 200 OK", "Content-Type: text/html")
+# print CGI.header # == print CGI.header("Content-Type: text/html")
# if running on Windows(IIS or PWS) then change cwd.
if ENV['SERVER_SOFTWARE'] =~ /^Microsoft-/ then
@@ -17,7 +35,12 @@ require "delegate"
class CGI < SimpleDelegator
+ CR = "\015"
+ LF = "\012"
+ EOL = CR + LF
+
attr("inputs")
+ attr("cookie")
# original is CGI.pm
def read_from_cmdline
@@ -40,7 +63,7 @@ class CGI < SimpleDelegator
# unescape url encoded
def unescape(str)
- str.gsub! /\+/, ' '
+ str.gsub!(/\+/, ' ')
str.gsub!(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") }
str
end
@@ -70,10 +93,38 @@ class CGI < SimpleDelegator
end
super(@inputs)
+
+ if ENV.has_key?('HTTP_COOKIE')
+ @cookie = {}
+ ENV['HTTP_COOKIE'].split("; ").each do |x|
+ key, val = x.split(/=/,2).collect{|x|unescape(x)}
+ if @cookie.include?(key)
+ @cookie[key] += "\0" + (val or "")
+ else
+ @cookie[key] = (val or "")
+ end
+ end
+ end
+ end
+
+ def CGI.header(*options)
+ options.push("Content-Type: text/html") if options.empty?
+ if options.find{|item| /^Expires: |^Set-Cookie: /i === item}
+ options.push("Date: " + Time.now.gmtime.strftime("%a, %d %b %Y %X %Z"))
+ end
+ options.join(EOL) + EOL + EOL
+ end
+
+ def CGI.cookie(options)
+ "Set-Cookie: " + options['name'] + '=' + escape(options['value']) +
+ (options['domain'] ? '; domain=' + options['domain'] : '') +
+ (options['path'] ? '; path=' + options['path'] : '') +
+ (options['expires'] ? '; expires=' + options['expires'].strftime("%a, %d %b %Y %X %Z") : '') +
+ (options['secure'] ? '; secure' : '')
end
- def CGI.message(msg, title = "")
- print "Content-type: text/html\n\n"
+ def CGI.message(msg, title = "", header = ["Content-Type: text/html"])
+ print CGI.header(*header)
print "<html><head><title>"
print title
print "</title></head><body>\n"
diff --git a/lib/ftools.rb b/lib/ftools.rb
index 7ccc7a4468..103e27adbd 100644
--- a/lib/ftools.rb
+++ b/lib/ftools.rb
@@ -137,7 +137,9 @@ class << File
parent = dirname(dir)
makedirs parent unless FileTest.directory? parent
$stderr.print "mkdir ", dir, "\n" if verbose
- Dir.mkdir dir, mode
+ if basename(dir) != ""
+ Dir.mkdir dir, mode
+ end
end
end
diff --git a/lib/parsedate.rb b/lib/parsedate.rb
index 68550c6505..5ffd2da47c 100644
--- a/lib/parsedate.rb
+++ b/lib/parsedate.rb
@@ -8,7 +8,7 @@ module ParseDate
'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3,
'thu' => 4, 'fri' => 5, 'sat' => 6 }
DAYPAT = DAYS.keys.join('|')
-
+
def parsedate(date)
# part of ISO 8601
# yyyy-mm-dd | yyyy-mm | yyyy
@@ -58,6 +58,10 @@ module ParseDate
if $3
year = $3.to_i
end
+ elsif date.sub!(/(\d+)-(#{MONTHPAT})-(\d+)/i, ' ')
+ mday = $1.to_i
+ mon = MONTHS[$2.downcase]
+ year = $3.to_i
end
return year, mon, mday, hour, min, sec, zone, wday
end