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
|
# Web::Interface::CGI
# Copyright(c) 2002-2004 MoonWolf <moonwolf@moonwolf.com>
require 'web/interface/basic'
module Web
module Interface
class CGI < Basic
EOL = "\r\n"
def initialize(opt={})
@opt = opt
@input = opt[:stdin] || $stdin
@output = opt[:stdout] || $stdout
@env = opt[:env] || ENV
#@persisent = opt[:storage] || Web::Persistent::File
@eof = false
end
# W͂RequestgݗĂ
def request(arg={})
return nil if @eof
@eof = true
super
req = Web::Request.new
#
req.request_id = @env['UNIQUE_ID'] || Web::Common::unique_id
req.method = @env['REQUEST_METHOD'] || 'GET'
req.query_string = @env['QUERY_STRING'] || ''
req.script_name = @env['SCRIPT_NAME']
req.path_info = @env['PATH_INFO']
req.path_translated = @env['PATH_TRANSLATED']
req.server_name = @env['SERVER_NAME']
req.server_port = @env['SERVER_PORT']
req.server_protocol = @env['SERVER_PROTOCOL']
req.host = @env['HTTP_HOST']
req.remote_addr = @env['REMOTE_ADDR'] || ''
req.remote_host = @env['REMOTE_HOST'] || req.remote_addr
req.auth_type = @env['AUTH_TYPE'] || ''
req.remote_ident = @env['REMOTE_IDENT'] || ''
req.remote_user = @env['REMOTE_USER']
req.https = @env['HTTPS'] ? true : false
# header
if len = @env['CONTENT_LENGTH']
req.header['CONTENT-LENGTH'] = len
end
type = @env['CONTENT_TYPE']
req.header['CONTENT-TYPE'] = type if type
@env.each {|key,value|
if key=~/^HTTP_([0-9a-zA-Z_]+)/
field = $1.upcase.tr("_","-")
req.header[field] = value
end
}
# cookie
if cookie = req.header['COOKIE']
req.cookies.parse(cookie, req)
end
## param
# QUERY_STRING
parse_query(req.query_string || '', req.query)
## body
case req.method
when 'GET'
# body
req.body = nil
when 'POST'
## form
case req.header['CONTENT-TYPE']
when /^multipart\/form-data\s*;\s*boundary="?(.*)"?/
boundary=$1
parse_multipart(@input, boundary, req.form)
when /^application\/x-www-form-urlencoded/
parse_query(@input.read.to_s, req.form)
else
req.body = @input.read
end
end
get_session_id req
req
end # CGI#request
def response(req, rsp)
super
out = @output
# wb_o
out << 'Status: ' << rsp.status << EOL
rsp.header.each {|key,value|
field = key.split('-').collect! {|i| i.capitalize }.join('-') << ': '
value.each {|v|
out << (field + v + EOL)
}
}
rsp.cookies.each {|key,cookie_ary|
cookie_ary.each {|cookie|
out << 'Set-Cookie: ' << cookie.to_response_header << EOL
}
}
out << EOL
#
out << rsp.body
req.cleanup
end # CGI#response
end # CGI
end # Interface
end # Web
|