Skip to content

Commit 5d50fbb

Browse files
[DOC] Enhanced doc for CGI.new (#80)
- Fix accept_charset option description to clarify HTTP header vs config behavior - Add note about max_multipart_length being internal-only (no public getter) - Update pretty_inspect example to show accurate @accept_charset value - Add 'Options vs Public Methods' section explaining internal vs external APIs - Improve clarity around class default encoding vs method behavior - Add test_cgi_new.rb with 11 tests covering all CGI.new functionality - Test all documented options: accept_charset, max_multipart_length, tag_maker - Validate all HTML versions: html3, html4, html4Tr, html4Fr, html5 - Test offline mode, encoding error handling, and option combinations - Verify accept_charset method behavior (HTTP header vs configuration) The test suite validates both the functionality and the accuracy of the enhanced CGI.new documentation. --------- Co-authored-by: Samuel Williams <samuel.williams@oriontransfer.co.nz>
1 parent 80ec7ae commit 5d50fbb

File tree

2 files changed

+393
-50
lines changed

2 files changed

+393
-50
lines changed

lib/cgi/core.rb

Lines changed: 127 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -778,75 +778,152 @@ def self.accept_charset=(accept_charset)
778778
#
779779
@@max_multipart_length= 128 * 1024 * 1024
780780

781-
# Create a new CGI instance.
782-
#
783781
# :call-seq:
784-
# CGI.new(tag_maker) { block }
785-
# CGI.new(options_hash = {}) { block }
782+
# CGI.new(options = {}) -> new_cgi
783+
# CGI.new(tag_maker) -> new_cgi
784+
# CGI.new(options = {}) {|name, value| ... } -> new_cgi
785+
# CGI.new(tag_maker) {|name, value| ... } -> new_cgi
786+
#
787+
# Returns a new \CGI object.
788+
#
789+
# The behavior of this method depends _strongly_ on whether it is called
790+
# within a standard \CGI call environment;
791+
# that is, whether <tt>ENV['REQUEST_HEADER']</tt> is defined.
792+
#
793+
# <b>Within a Standard Call Environment</b>
794+
#
795+
# This section assumes that <tt>ENV['REQUEST_HEADER']</tt> is defined;
796+
# for example:
797+
#
798+
# ENV['REQUEST_METHOD'] # => "GET"
799+
#
800+
# With no argument and no block given, returns a new \CGI object with default values:
801+
#
802+
# cgi = CGI.new
803+
# puts cgi.pretty_inspect
804+
# #<CGI:0x000002b0ea237bc8
805+
# @accept_charset=#<Encoding:UTF-8>,
806+
# @accept_charset_error_block=nil,
807+
# @cookies={},
808+
# @max_multipart_length=134217728,
809+
# @multipart=false,
810+
# @output_cookies=nil,
811+
# @output_hidden=nil,
812+
# @params={}>
813+
#
814+
# With hash argument +options+ given and no block given,
815+
# returns a new \CGI object with the given options.
816+
#
817+
# The options may be:
818+
#
819+
# - <tt>accept_charset: _encoding_</tt>:
820+
# specifies the encoding of the received query string.
821+
#
822+
# Value _encoding_ may be
823+
# an {Encoding object}[https://docs.ruby-lang.org/en/master/encodings_rdoc.html#label-Encoding+Objects]
824+
# or an {encoding name}[https://docs.ruby-lang.org/en/master/encodings_rdoc.html#label-Names+and+Aliases]:
825+
#
826+
# CGI.new(accept_charset: 'EUC-JP')
827+
#
828+
# If the option is not given,
829+
# the default value is the class default encoding.
830+
#
831+
# <em>Note:</em> The <tt>accept_charset</tt> method returns the HTTP Accept-Charset
832+
# header value, not the configured encoding. The configured encoding is used
833+
# internally for query string parsing.
834+
#
835+
# - <tt>max_multipart_length: _size_</tt>:
836+
# specifies maximum size (in bytes) of multipart data.
837+
#
838+
# The _size_ may be:
839+
#
840+
# - A positive integer.
841+
#
842+
# CGI.new(max_multipart_length: 1024 * 1024)
843+
#
844+
#
845+
# - A lambda to be evaluated when the request is parsed.
846+
# This is useful when determining whether to accept multipart data
847+
# (e.g. by consulting a registered user's upload allowance).
848+
#
849+
# CGI.new(max_multipart_length: -> {check_filesystem})
850+
#
851+
# If the option is not given, the default is +134217728+, specifying a maximum size of 128 megabytes.
852+
#
853+
# <em>Note:</em> This option configures internal behavior only.
854+
# There is no public method to retrieve this value after initialization.
855+
#
856+
# - <tt>tag_maker: _html_version_</tt>:
857+
# specifies which version of HTML to use in generating tags.
858+
#
859+
# Value _html_version_ may be one of:
860+
#
861+
# - <tt>'html3'</tt>: {HTML version 3}[https://en.wikipedia.org/wiki/HTML#HTML_3].
862+
# - <tt>'html4'</tt>: {HTML version 4}[https://en.wikipedia.org/wiki/HTML#HTML_4].
863+
# - <tt>'html4Tr'</tt>: HTML 4.0 Transitional.
864+
# - <tt>'html4Fr'</tt>: HTML 4.0 with Framesets.
865+
# - <tt>'html5'</tt>: {HTML version 5}[https://en.wikipedia.org/wiki/HTML#HTML_5].
786866
#
867+
# Example:
787868
#
788-
# <tt>tag_maker</tt>::
789-
# This is the same as using the +options_hash+ form with the value <tt>{
790-
# :tag_maker => tag_maker }</tt> Note that it is recommended to use the
791-
# +options_hash+ form, since it also allows you specify the charset you
792-
# will accept.
793-
# <tt>options_hash</tt>::
794-
# A Hash that recognizes three options:
869+
# CGI.new(tag_maker: 'html5')
795870
#
796-
# <tt>:accept_charset</tt>::
797-
# specifies encoding of received query string. If omitted,
798-
# <tt>@@accept_charset</tt> is used. If the encoding is not valid, a
799-
# CGI::InvalidEncoding will be raised.
871+
# If the option is not given,
872+
# no HTML generation methods are loaded.
800873
#
801-
# Example. Suppose <tt>@@accept_charset</tt> is "UTF-8"
874+
# With string argument +tag_maker+ given as _tag_maker_ and no block given,
875+
# equivalent to <tt>CGI.new(tag_maker: _tag_maker_)</tt>:
802876
#
803-
# when not specified:
877+
# CGI.new('html5')
804878
#
805-
# cgi=CGI.new # @accept_charset # => "UTF-8"
879+
# <b>Outside a Standard Call Environment</b>
806880
#
807-
# when specified as "EUC-JP":
881+
# This section assumes that <tt>ENV['REQUEST_HEADER']</tt> is not defined;
882+
# for example:
808883
#
809-
# cgi=CGI.new(:accept_charset => "EUC-JP") # => "EUC-JP"
884+
# ENV['REQUEST_METHOD'] # => nil
810885
#
811-
# <tt>:tag_maker</tt>::
812-
# String that specifies which version of the HTML generation methods to
813-
# use. If not specified, no HTML generation methods will be loaded.
886+
# In this mode, the method reads its parameters
887+
# from the command line or (failing that) from standard input;
888+
# returns a new \CGI object.
814889
#
815-
# The following values are supported:
890+
# Otherwise, cookies and other parameters are parsed automatically from the standard CGI locations,
891+
# which vary according to the request method.
816892
#
817-
# "html3":: HTML 3.x
818-
# "html4":: HTML 4.0
819-
# "html4Tr":: HTML 4.0 Transitional
820-
# "html4Fr":: HTML 4.0 with Framesets
821-
# "html5":: HTML 5
893+
# <b>Options vs Public Methods</b>
822894
#
823-
# <tt>:max_multipart_length</tt>::
824-
# Specifies maximum length of multipart data. Can be an Integer scalar or
825-
# a lambda, that will be evaluated when the request is parsed. This
826-
# allows more complex logic to be set when determining whether to accept
827-
# multipart data (e.g. consult a registered users upload allowance)
895+
# Some initialization options configure internal behavior only and do not provide
896+
# corresponding public getter methods:
828897
#
829-
# Default is 128 * 1024 * 1024 bytes
898+
# - <tt>accept_charset</tt>: Configures internal encoding for parsing.
899+
# The <tt>accept_charset</tt> method returns the HTTP Accept-Charset header.
900+
# - <tt>max_multipart_length</tt>: Configures internal multipart size limits.
901+
# No public getter method is available.
902+
# - <tt>tag_maker</tt>: Loads HTML generation methods (publicly accessible).
830903
#
831-
# cgi=CGI.new(:max_multipart_length => 268435456) # simple scalar
904+
# <b>Block</b>
832905
#
833-
# cgi=CGI.new(:max_multipart_length => -> {check_filesystem}) # lambda
906+
# If a block is given, its code is stored as a Proc;
907+
# whenever CGI::InvalidEncoding would be raised, the proc is called instead.
834908
#
835-
# <tt>block</tt>::
836-
# If provided, the block is called when an invalid encoding is
837-
# encountered. For example:
909+
# In this example, the proc simply saves the error:
838910
#
839-
# encoding_errors={}
840-
# cgi=CGI.new(:accept_charset=>"EUC-JP") do |name,value|
841-
# encoding_errors[name] = value
842-
# end
911+
# encoding_errors={}
912+
# CGI.new(accept_charset: 'EUC-JP') do |name,value|
913+
# encoding_errors[name] = value
914+
# end
915+
# # =>
916+
# #<CGI:0x000002b0ec11bcd8
917+
# @accept_charset="EUC-JP",
918+
# @accept_charset_error_block=#<Proc:0x000002b0ed2ee190 (irb):146>,
919+
# @cookies={},
920+
# @max_multipart_length=134217728,
921+
# @multipart=false,
922+
# @options={accept_charset: "EUC-JP", max_multipart_length: 134217728},
923+
# @output_cookies=nil,
924+
# @output_hidden=nil,
925+
# @params={}>
843926
#
844-
# Finally, if the CGI object is not created in a standard CGI call
845-
# environment (that is, it can't locate REQUEST_METHOD in its environment),
846-
# then it will run in "offline" mode. In this mode, it reads its parameters
847-
# from the command line or (failing that) from standard input. Otherwise,
848-
# cookies and other parameters are parsed automatically from the standard
849-
# CGI locations, which varies according to the REQUEST_METHOD.
850927
def initialize(options = {}, &block) # :yields: name, value
851928
@accept_charset_error_block = block_given? ? block : nil
852929
@options={

0 commit comments

Comments
 (0)