Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: cgi.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# cgi.rb - cgi support library
[2] Fix | Delete
#
[3] Fix | Delete
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
[4] Fix | Delete
#
[5] Fix | Delete
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
[6] Fix | Delete
#
[7] Fix | Delete
# Author: Wakou Aoyama <wakou@ruby-lang.org>
[8] Fix | Delete
#
[9] Fix | Delete
# Documentation: Wakou Aoyama (RDoc'd and embellished by William Webber)
[10] Fix | Delete
#
[11] Fix | Delete
[12] Fix | Delete
# == Overview
[13] Fix | Delete
#
[14] Fix | Delete
# The Common Gateway Interface (CGI) is a simple protocol for passing an HTTP
[15] Fix | Delete
# request from a web server to a standalone program, and returning the output
[16] Fix | Delete
# to the web browser. Basically, a CGI program is called with the parameters
[17] Fix | Delete
# of the request passed in either in the environment (GET) or via $stdin
[18] Fix | Delete
# (POST), and everything it prints to $stdout is returned to the client.
[19] Fix | Delete
#
[20] Fix | Delete
# This file holds the CGI class. This class provides functionality for
[21] Fix | Delete
# retrieving HTTP request parameters, managing cookies, and generating HTML
[22] Fix | Delete
# output.
[23] Fix | Delete
#
[24] Fix | Delete
# The file CGI::Session provides session management functionality; see that
[25] Fix | Delete
# class for more details.
[26] Fix | Delete
#
[27] Fix | Delete
# See http://www.w3.org/CGI/ for more information on the CGI protocol.
[28] Fix | Delete
#
[29] Fix | Delete
# == Introduction
[30] Fix | Delete
#
[31] Fix | Delete
# CGI is a large class, providing several categories of methods, many of which
[32] Fix | Delete
# are mixed in from other modules. Some of the documentation is in this class,
[33] Fix | Delete
# some in the modules CGI::QueryExtension and CGI::HtmlExtension. See
[34] Fix | Delete
# CGI::Cookie for specific information on handling cookies, and cgi/session.rb
[35] Fix | Delete
# (CGI::Session) for information on sessions.
[36] Fix | Delete
#
[37] Fix | Delete
# For queries, CGI provides methods to get at environmental variables,
[38] Fix | Delete
# parameters, cookies, and multipart request data. For responses, CGI provides
[39] Fix | Delete
# methods for writing output and generating HTML.
[40] Fix | Delete
#
[41] Fix | Delete
# Read on for more details. Examples are provided at the bottom.
[42] Fix | Delete
#
[43] Fix | Delete
# == Queries
[44] Fix | Delete
#
[45] Fix | Delete
# The CGI class dynamically mixes in parameter and cookie-parsing
[46] Fix | Delete
# functionality, environmental variable access, and support for
[47] Fix | Delete
# parsing multipart requests (including uploaded files) from the
[48] Fix | Delete
# CGI::QueryExtension module.
[49] Fix | Delete
#
[50] Fix | Delete
# === Environmental Variables
[51] Fix | Delete
#
[52] Fix | Delete
# The standard CGI environmental variables are available as read-only
[53] Fix | Delete
# attributes of a CGI object. The following is a list of these variables:
[54] Fix | Delete
#
[55] Fix | Delete
#
[56] Fix | Delete
# AUTH_TYPE HTTP_HOST REMOTE_IDENT
[57] Fix | Delete
# CONTENT_LENGTH HTTP_NEGOTIATE REMOTE_USER
[58] Fix | Delete
# CONTENT_TYPE HTTP_PRAGMA REQUEST_METHOD
[59] Fix | Delete
# GATEWAY_INTERFACE HTTP_REFERER SCRIPT_NAME
[60] Fix | Delete
# HTTP_ACCEPT HTTP_USER_AGENT SERVER_NAME
[61] Fix | Delete
# HTTP_ACCEPT_CHARSET PATH_INFO SERVER_PORT
[62] Fix | Delete
# HTTP_ACCEPT_ENCODING PATH_TRANSLATED SERVER_PROTOCOL
[63] Fix | Delete
# HTTP_ACCEPT_LANGUAGE QUERY_STRING SERVER_SOFTWARE
[64] Fix | Delete
# HTTP_CACHE_CONTROL REMOTE_ADDR
[65] Fix | Delete
# HTTP_FROM REMOTE_HOST
[66] Fix | Delete
#
[67] Fix | Delete
#
[68] Fix | Delete
# For each of these variables, there is a corresponding attribute with the
[69] Fix | Delete
# same name, except all lower case and without a preceding HTTP_.
[70] Fix | Delete
# +content_length+ and +server_port+ are integers; the rest are strings.
[71] Fix | Delete
#
[72] Fix | Delete
# === Parameters
[73] Fix | Delete
#
[74] Fix | Delete
# The method #params() returns a hash of all parameters in the request as
[75] Fix | Delete
# name/value-list pairs, where the value-list is an Array of one or more
[76] Fix | Delete
# values. The CGI object itself also behaves as a hash of parameter names
[77] Fix | Delete
# to values, but only returns a single value (as a String) for each
[78] Fix | Delete
# parameter name.
[79] Fix | Delete
#
[80] Fix | Delete
# For instance, suppose the request contains the parameter
[81] Fix | Delete
# "favourite_colours" with the multiple values "blue" and "green". The
[82] Fix | Delete
# following behavior would occur:
[83] Fix | Delete
#
[84] Fix | Delete
# cgi.params["favourite_colours"] # => ["blue", "green"]
[85] Fix | Delete
# cgi["favourite_colours"] # => "blue"
[86] Fix | Delete
#
[87] Fix | Delete
# If a parameter does not exist, the former method will return an empty
[88] Fix | Delete
# array, the latter an empty string. The simplest way to test for existence
[89] Fix | Delete
# of a parameter is by the #has_key? method.
[90] Fix | Delete
#
[91] Fix | Delete
# === Cookies
[92] Fix | Delete
#
[93] Fix | Delete
# HTTP Cookies are automatically parsed from the request. They are available
[94] Fix | Delete
# from the #cookies() accessor, which returns a hash from cookie name to
[95] Fix | Delete
# CGI::Cookie object.
[96] Fix | Delete
#
[97] Fix | Delete
# === Multipart requests
[98] Fix | Delete
#
[99] Fix | Delete
# If a request's method is POST and its content type is multipart/form-data,
[100] Fix | Delete
# then it may contain uploaded files. These are stored by the QueryExtension
[101] Fix | Delete
# module in the parameters of the request. The parameter name is the name
[102] Fix | Delete
# attribute of the file input field, as usual. However, the value is not
[103] Fix | Delete
# a string, but an IO object, either an IOString for small files, or a
[104] Fix | Delete
# Tempfile for larger ones. This object also has the additional singleton
[105] Fix | Delete
# methods:
[106] Fix | Delete
#
[107] Fix | Delete
# #local_path():: the path of the uploaded file on the local filesystem
[108] Fix | Delete
# #original_filename():: the name of the file on the client computer
[109] Fix | Delete
# #content_type():: the content type of the file
[110] Fix | Delete
#
[111] Fix | Delete
# == Responses
[112] Fix | Delete
#
[113] Fix | Delete
# The CGI class provides methods for sending header and content output to
[114] Fix | Delete
# the HTTP client, and mixes in methods for programmatic HTML generation
[115] Fix | Delete
# from CGI::HtmlExtension and CGI::TagMaker modules. The precise version of HTML
[116] Fix | Delete
# to use for HTML generation is specified at object creation time.
[117] Fix | Delete
#
[118] Fix | Delete
# === Writing output
[119] Fix | Delete
#
[120] Fix | Delete
# The simplest way to send output to the HTTP client is using the #out() method.
[121] Fix | Delete
# This takes the HTTP headers as a hash parameter, and the body content
[122] Fix | Delete
# via a block. The headers can be generated as a string using the #http_header()
[123] Fix | Delete
# method. The output stream can be written directly to using the #print()
[124] Fix | Delete
# method.
[125] Fix | Delete
#
[126] Fix | Delete
# === Generating HTML
[127] Fix | Delete
#
[128] Fix | Delete
# Each HTML element has a corresponding method for generating that
[129] Fix | Delete
# element as a String. The name of this method is the same as that
[130] Fix | Delete
# of the element, all lowercase. The attributes of the element are
[131] Fix | Delete
# passed in as a hash, and the body as a no-argument block that evaluates
[132] Fix | Delete
# to a String. The HTML generation module knows which elements are
[133] Fix | Delete
# always empty, and silently drops any passed-in body. It also knows
[134] Fix | Delete
# which elements require matching closing tags and which don't. However,
[135] Fix | Delete
# it does not know what attributes are legal for which elements.
[136] Fix | Delete
#
[137] Fix | Delete
# There are also some additional HTML generation methods mixed in from
[138] Fix | Delete
# the CGI::HtmlExtension module. These include individual methods for the
[139] Fix | Delete
# different types of form inputs, and methods for elements that commonly
[140] Fix | Delete
# take particular attributes where the attributes can be directly specified
[141] Fix | Delete
# as arguments, rather than via a hash.
[142] Fix | Delete
#
[143] Fix | Delete
# === Utility HTML escape and other methods like a function.
[144] Fix | Delete
#
[145] Fix | Delete
# There are some utility tool defined in cgi/util.rb .
[146] Fix | Delete
# And when include, you can use utility methods like a function.
[147] Fix | Delete
#
[148] Fix | Delete
# == Examples of use
[149] Fix | Delete
#
[150] Fix | Delete
# === Get form values
[151] Fix | Delete
#
[152] Fix | Delete
# require "cgi"
[153] Fix | Delete
# cgi = CGI.new
[154] Fix | Delete
# value = cgi['field_name'] # <== value string for 'field_name'
[155] Fix | Delete
# # if not 'field_name' included, then return "".
[156] Fix | Delete
# fields = cgi.keys # <== array of field names
[157] Fix | Delete
#
[158] Fix | Delete
# # returns true if form has 'field_name'
[159] Fix | Delete
# cgi.has_key?('field_name')
[160] Fix | Delete
# cgi.has_key?('field_name')
[161] Fix | Delete
# cgi.include?('field_name')
[162] Fix | Delete
#
[163] Fix | Delete
# CAUTION! <code>cgi['field_name']</code> returned an Array with the old
[164] Fix | Delete
# cgi.rb(included in Ruby 1.6)
[165] Fix | Delete
#
[166] Fix | Delete
# === Get form values as hash
[167] Fix | Delete
#
[168] Fix | Delete
# require "cgi"
[169] Fix | Delete
# cgi = CGI.new
[170] Fix | Delete
# params = cgi.params
[171] Fix | Delete
#
[172] Fix | Delete
# cgi.params is a hash.
[173] Fix | Delete
#
[174] Fix | Delete
# cgi.params['new_field_name'] = ["value"] # add new param
[175] Fix | Delete
# cgi.params['field_name'] = ["new_value"] # change value
[176] Fix | Delete
# cgi.params.delete('field_name') # delete param
[177] Fix | Delete
# cgi.params.clear # delete all params
[178] Fix | Delete
#
[179] Fix | Delete
#
[180] Fix | Delete
# === Save form values to file
[181] Fix | Delete
#
[182] Fix | Delete
# require "pstore"
[183] Fix | Delete
# db = PStore.new("query.db")
[184] Fix | Delete
# db.transaction do
[185] Fix | Delete
# db["params"] = cgi.params
[186] Fix | Delete
# end
[187] Fix | Delete
#
[188] Fix | Delete
#
[189] Fix | Delete
# === Restore form values from file
[190] Fix | Delete
#
[191] Fix | Delete
# require "pstore"
[192] Fix | Delete
# db = PStore.new("query.db")
[193] Fix | Delete
# db.transaction do
[194] Fix | Delete
# cgi.params = db["params"]
[195] Fix | Delete
# end
[196] Fix | Delete
#
[197] Fix | Delete
#
[198] Fix | Delete
# === Get multipart form values
[199] Fix | Delete
#
[200] Fix | Delete
# require "cgi"
[201] Fix | Delete
# cgi = CGI.new
[202] Fix | Delete
# value = cgi['field_name'] # <== value string for 'field_name'
[203] Fix | Delete
# value.read # <== body of value
[204] Fix | Delete
# value.local_path # <== path to local file of value
[205] Fix | Delete
# value.original_filename # <== original filename of value
[206] Fix | Delete
# value.content_type # <== content_type of value
[207] Fix | Delete
#
[208] Fix | Delete
# and value has StringIO or Tempfile class methods.
[209] Fix | Delete
#
[210] Fix | Delete
# === Get cookie values
[211] Fix | Delete
#
[212] Fix | Delete
# require "cgi"
[213] Fix | Delete
# cgi = CGI.new
[214] Fix | Delete
# values = cgi.cookies['name'] # <== array of 'name'
[215] Fix | Delete
# # if not 'name' included, then return [].
[216] Fix | Delete
# names = cgi.cookies.keys # <== array of cookie names
[217] Fix | Delete
#
[218] Fix | Delete
# and cgi.cookies is a hash.
[219] Fix | Delete
#
[220] Fix | Delete
# === Get cookie objects
[221] Fix | Delete
#
[222] Fix | Delete
# require "cgi"
[223] Fix | Delete
# cgi = CGI.new
[224] Fix | Delete
# for name, cookie in cgi.cookies
[225] Fix | Delete
# cookie.expires = Time.now + 30
[226] Fix | Delete
# end
[227] Fix | Delete
# cgi.out("cookie" => cgi.cookies) {"string"}
[228] Fix | Delete
#
[229] Fix | Delete
# cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
[230] Fix | Delete
#
[231] Fix | Delete
# require "cgi"
[232] Fix | Delete
# cgi = CGI.new
[233] Fix | Delete
# cgi.cookies['name'].expires = Time.now + 30
[234] Fix | Delete
# cgi.out("cookie" => cgi.cookies['name']) {"string"}
[235] Fix | Delete
#
[236] Fix | Delete
# === Print http header and html string to $DEFAULT_OUTPUT ($>)
[237] Fix | Delete
#
[238] Fix | Delete
# require "cgi"
[239] Fix | Delete
# cgi = CGI.new("html4") # add HTML generation methods
[240] Fix | Delete
# cgi.out do
[241] Fix | Delete
# cgi.html do
[242] Fix | Delete
# cgi.head do
[243] Fix | Delete
# cgi.title { "TITLE" }
[244] Fix | Delete
# end +
[245] Fix | Delete
# cgi.body do
[246] Fix | Delete
# cgi.form("ACTION" => "uri") do
[247] Fix | Delete
# cgi.p do
[248] Fix | Delete
# cgi.textarea("get_text") +
[249] Fix | Delete
# cgi.br +
[250] Fix | Delete
# cgi.submit
[251] Fix | Delete
# end
[252] Fix | Delete
# end +
[253] Fix | Delete
# cgi.pre do
[254] Fix | Delete
# CGI.escapeHTML(
[255] Fix | Delete
# "params: #{cgi.params.inspect}\n" +
[256] Fix | Delete
# "cookies: #{cgi.cookies.inspect}\n" +
[257] Fix | Delete
# ENV.collect do |key, value|
[258] Fix | Delete
# "#{key} --> #{value}\n"
[259] Fix | Delete
# end.join("")
[260] Fix | Delete
# )
[261] Fix | Delete
# end
[262] Fix | Delete
# end
[263] Fix | Delete
# end
[264] Fix | Delete
# end
[265] Fix | Delete
#
[266] Fix | Delete
# # add HTML generation methods
[267] Fix | Delete
# CGI.new("html3") # html3.2
[268] Fix | Delete
# CGI.new("html4") # html4.01 (Strict)
[269] Fix | Delete
# CGI.new("html4Tr") # html4.01 Transitional
[270] Fix | Delete
# CGI.new("html4Fr") # html4.01 Frameset
[271] Fix | Delete
# CGI.new("html5") # html5
[272] Fix | Delete
#
[273] Fix | Delete
# === Some utility methods
[274] Fix | Delete
#
[275] Fix | Delete
# require 'cgi/util'
[276] Fix | Delete
# CGI.escapeHTML('Usage: foo "bar" <baz>')
[277] Fix | Delete
#
[278] Fix | Delete
#
[279] Fix | Delete
# === Some utility methods like a function
[280] Fix | Delete
#
[281] Fix | Delete
# require 'cgi/util'
[282] Fix | Delete
# include CGI::Util
[283] Fix | Delete
# escapeHTML('Usage: foo "bar" <baz>')
[284] Fix | Delete
# h('Usage: foo "bar" <baz>') # alias
[285] Fix | Delete
#
[286] Fix | Delete
#
[287] Fix | Delete
[288] Fix | Delete
class CGI
[289] Fix | Delete
VERSION = "0.3.6"
[290] Fix | Delete
end
[291] Fix | Delete
[292] Fix | Delete
require 'cgi/core'
[293] Fix | Delete
require 'cgi/cookie'
[294] Fix | Delete
require 'cgi/util'
[295] Fix | Delete
CGI.autoload(:HtmlExtension, 'cgi/html')
[296] Fix | Delete
[297] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function