cgi 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4807bd75c56012fd4b219cd83c6b0fb76006483f176b9a7b19b2658606b11503
4
- data.tar.gz: b49eebc85d48ac1f068dda9fd36570dbb83d2ee787d6d458680996d29554d8ce
3
+ metadata.gz: 0f7c88bb08e51fd6b7abd1855b87d05c8af6b8f4e89865f0315f512badcddcb9
4
+ data.tar.gz: fbe294b63d488ae16c123d325b2251a9561da5b474105a0ecbb1ecd880c4cdb6
5
5
  SHA512:
6
- metadata.gz: c85c60cdefbe541dd68ebc7c82be5a62635bb3861e7a717d828cff5e2072c583bdb0722d85eb36d18a6f1a28255cad60f8d2879d302c10c94f4c7e2c3100103f
7
- data.tar.gz: c7a7fccaf06fed4326df4fb5dc1bcab429e4c1430bffb010f5a3c46a4c1b53c102c32b7142b6bdda1a090d6c152da34cdc76e4f5a1d67e1bd37a6df6fc7b5f8e
6
+ metadata.gz: 9d4837752a514f02e88f8f3673fa0b0b9a31195df062d79c9d5b51175288f5053409fc337b1613ad27393b92c4394663d4cfd355d30c555b565f79a9102e2feb
7
+ data.tar.gz: 0c465bae726d291b6829b6f0bf9f86d05976bf61529dc8190da4fd73022b37924c7901c3b29fb3cddb7760829aa24ac5cd6999c1f1ad9b4ec825fe0afc63ef19
@@ -45,6 +45,7 @@ escaped_length(VALUE str)
45
45
  static VALUE
46
46
  optimized_escape_html(VALUE str)
47
47
  {
48
+ VALUE escaped;
48
49
  VALUE vbuf;
49
50
  char *buf = ALLOCV_N(char, vbuf, escaped_length(str));
50
51
  const char *cstr = RSTRING_PTR(str);
@@ -63,7 +64,6 @@ optimized_escape_html(VALUE str)
63
64
  }
64
65
  }
65
66
 
66
- VALUE escaped;
67
67
  if (RSTRING_LEN(str) < (dest - buf)) {
68
68
  escaped = rb_str_new(buf, dest - buf);
69
69
  preserve_original_state(str, escaped);
data/lib/cgi/cookie.rb CHANGED
@@ -40,9 +40,11 @@ class CGI
40
40
  class Cookie < Array
41
41
  @@accept_charset="UTF-8" unless defined?(@@accept_charset)
42
42
 
43
+ # :stopdoc:
43
44
  TOKEN_RE = %r"\A[[!-~]&&[^()<>@,;:\\\"/?=\[\]{}]]+\z"
44
45
  PATH_VALUE_RE = %r"\A[[ -~]&&[^;]]*\z"
45
46
  DOMAIN_VALUE_RE = %r"\A\.?(?<label>(?!-)[-A-Za-z0-9]+(?<!-))(?:\.\g<label>)*\z"
47
+ # :startdoc:
46
48
 
47
49
  # Create a new CGI::Cookie object.
48
50
  #
data/lib/cgi/core.rb CHANGED
@@ -384,11 +384,14 @@ class CGI
384
384
  stdoutput.print(*options)
385
385
  end
386
386
 
387
- # Parse an HTTP query string into a hash of key=>value pairs.
387
+ # :call-seq:
388
+ # CGI.parse(query_string) -> hash
389
+ #
390
+ # Returns a new hash built from name/value pairs in the given +query_string+:
388
391
  #
389
- # params = CGI.parse("query_string")
390
- # # {"name1" => ["value1", "value2", ...],
391
- # # "name2" => ["value1", "value2", ...], ... }
392
+ # query = 'foo=0&bar=1&foo=2&bar=3'
393
+ # CGI.parse(query)
394
+ # # => {"foo" => ["0", "2"], "bar" => ["1", "3"]}
392
395
  #
393
396
  def self.parse(query)
394
397
  params = {}
@@ -629,8 +632,8 @@ class CGI
629
632
  string = unless ARGV.empty?
630
633
  ARGV.join(' ')
631
634
  else
632
- if STDIN.tty?
633
- STDERR.print(
635
+ if stdinput.tty?
636
+ $stderr.print(
634
637
  %|(offline mode: enter name=value pairs on standard input)\n|
635
638
  )
636
639
  end
@@ -778,75 +781,152 @@ class CGI
778
781
  #
779
782
  @@max_multipart_length= 128 * 1024 * 1024
780
783
 
781
- # Create a new CGI instance.
782
- #
783
784
  # :call-seq:
784
- # CGI.new(tag_maker) { block }
785
- # CGI.new(options_hash = {}) { block }
785
+ # CGI.new(options = {}) -> new_cgi
786
+ # CGI.new(tag_maker) -> new_cgi
787
+ # CGI.new(options = {}) {|name, value| ... } -> new_cgi
788
+ # CGI.new(tag_maker) {|name, value| ... } -> new_cgi
789
+ #
790
+ # Returns a new \CGI object.
791
+ #
792
+ # The behavior of this method depends _strongly_ on whether it is called
793
+ # within a standard \CGI call environment;
794
+ # that is, whether <tt>ENV['REQUEST_METHOD']</tt> is defined.
795
+ #
796
+ # <b>Within a Standard Call Environment</b>
797
+ #
798
+ # This section assumes that <tt>ENV['REQUEST_METHOD']</tt> is defined;
799
+ # for example:
800
+ #
801
+ # ENV['REQUEST_METHOD'] # => "GET"
802
+ #
803
+ # With no argument and no block given, returns a new \CGI object with default values:
804
+ #
805
+ # cgi = CGI.new
806
+ # puts cgi.pretty_inspect
807
+ # #<CGI:0x000002b0ea237bc8
808
+ # @accept_charset=#<Encoding:UTF-8>,
809
+ # @accept_charset_error_block=nil,
810
+ # @cookies={},
811
+ # @max_multipart_length=134217728,
812
+ # @multipart=false,
813
+ # @output_cookies=nil,
814
+ # @output_hidden=nil,
815
+ # @params={}>
816
+ #
817
+ # With hash argument +options+ given and no block given,
818
+ # returns a new \CGI object with the given options.
819
+ #
820
+ # The options may be:
821
+ #
822
+ # - <tt>accept_charset: _encoding_</tt>:
823
+ # specifies the encoding of the received query string.
824
+ #
825
+ # Value _encoding_ may be
826
+ # an {Encoding object}[https://docs.ruby-lang.org/en/master/encodings_rdoc.html#label-Encoding+Objects]
827
+ # or an {encoding name}[https://docs.ruby-lang.org/en/master/encodings_rdoc.html#label-Names+and+Aliases]:
828
+ #
829
+ # CGI.new(accept_charset: 'EUC-JP')
830
+ #
831
+ # If the option is not given,
832
+ # the default value is the class default encoding.
833
+ #
834
+ # <em>Note:</em> The <tt>accept_charset</tt> method returns the HTTP Accept-Charset
835
+ # header value, not the configured encoding. The configured encoding is used
836
+ # internally for query string parsing.
837
+ #
838
+ # - <tt>max_multipart_length: _size_</tt>:
839
+ # specifies maximum size (in bytes) of multipart data.
840
+ #
841
+ # The _size_ may be:
842
+ #
843
+ # - A positive integer.
844
+ #
845
+ # CGI.new(max_multipart_length: 1024 * 1024)
846
+ #
847
+ #
848
+ # - A lambda to be evaluated when the request is parsed.
849
+ # This is useful when determining whether to accept multipart data
850
+ # (e.g. by consulting a registered user's upload allowance).
851
+ #
852
+ # CGI.new(max_multipart_length: -> {check_filesystem})
853
+ #
854
+ # If the option is not given, the default is +134217728+, specifying a maximum size of 128 megabytes.
855
+ #
856
+ # <em>Note:</em> This option configures internal behavior only.
857
+ # There is no public method to retrieve this value after initialization.
858
+ #
859
+ # - <tt>tag_maker: _html_version_</tt>:
860
+ # specifies which version of HTML to use in generating tags.
861
+ #
862
+ # Value _html_version_ may be one of:
863
+ #
864
+ # - <tt>'html3'</tt>: {HTML version 3}[https://en.wikipedia.org/wiki/HTML#HTML_3].
865
+ # - <tt>'html4'</tt>: {HTML version 4}[https://en.wikipedia.org/wiki/HTML#HTML_4].
866
+ # - <tt>'html4Tr'</tt>: HTML 4.0 Transitional.
867
+ # - <tt>'html4Fr'</tt>: HTML 4.0 with Framesets.
868
+ # - <tt>'html5'</tt>: {HTML version 5}[https://en.wikipedia.org/wiki/HTML#HTML_5].
786
869
  #
870
+ # Example:
787
871
  #
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:
872
+ # CGI.new(tag_maker: 'html5')
795
873
  #
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.
874
+ # If the option is not given,
875
+ # no HTML generation methods are loaded.
800
876
  #
801
- # Example. Suppose <tt>@@accept_charset</tt> is "UTF-8"
877
+ # With string argument +tag_maker+ given as _tag_maker_ and no block given,
878
+ # equivalent to <tt>CGI.new(tag_maker: _tag_maker_)</tt>:
802
879
  #
803
- # when not specified:
880
+ # CGI.new('html5')
804
881
  #
805
- # cgi=CGI.new # @accept_charset # => "UTF-8"
882
+ # <b>Outside a Standard Call Environment</b>
806
883
  #
807
- # when specified as "EUC-JP":
884
+ # This section assumes that <tt>ENV['REQUEST_METHOD']</tt> is not defined;
885
+ # for example:
808
886
  #
809
- # cgi=CGI.new(:accept_charset => "EUC-JP") # => "EUC-JP"
887
+ # ENV['REQUEST_METHOD'] # => nil
810
888
  #
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.
889
+ # In this mode, the method reads its parameters
890
+ # from the command line or (failing that) from standard input;
891
+ # returns a new \CGI object.
814
892
  #
815
- # The following values are supported:
893
+ # Otherwise, cookies and other parameters are parsed automatically from the standard CGI locations,
894
+ # which vary according to the request method.
816
895
  #
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
896
+ # <b>Options vs Public Methods</b>
822
897
  #
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)
898
+ # Some initialization options configure internal behavior only and do not provide
899
+ # corresponding public getter methods:
828
900
  #
829
- # Default is 128 * 1024 * 1024 bytes
901
+ # - <tt>accept_charset</tt>: Configures internal encoding for parsing.
902
+ # The <tt>accept_charset</tt> method returns the HTTP Accept-Charset header.
903
+ # - <tt>max_multipart_length</tt>: Configures internal multipart size limits.
904
+ # No public getter method is available.
905
+ # - <tt>tag_maker</tt>: Loads HTML generation methods (publicly accessible).
830
906
  #
831
- # cgi=CGI.new(:max_multipart_length => 268435456) # simple scalar
907
+ # <b>Block</b>
832
908
  #
833
- # cgi=CGI.new(:max_multipart_length => -> {check_filesystem}) # lambda
909
+ # If a block is given, its code is stored as a Proc;
910
+ # whenever CGI::InvalidEncoding would be raised, the proc is called instead.
834
911
  #
835
- # <tt>block</tt>::
836
- # If provided, the block is called when an invalid encoding is
837
- # encountered. For example:
912
+ # In this example, the proc simply saves the error:
838
913
  #
839
- # encoding_errors={}
840
- # cgi=CGI.new(:accept_charset=>"EUC-JP") do |name,value|
841
- # encoding_errors[name] = value
842
- # end
914
+ # encoding_errors={}
915
+ # CGI.new(accept_charset: 'EUC-JP') do |name,value|
916
+ # encoding_errors[name] = value
917
+ # end
918
+ # # =>
919
+ # #<CGI:0x000002b0ec11bcd8
920
+ # @accept_charset="EUC-JP",
921
+ # @accept_charset_error_block=#<Proc:0x000002b0ed2ee190 (irb):146>,
922
+ # @cookies={},
923
+ # @max_multipart_length=134217728,
924
+ # @multipart=false,
925
+ # @options={accept_charset: "EUC-JP", max_multipart_length: 134217728},
926
+ # @output_cookies=nil,
927
+ # @output_hidden=nil,
928
+ # @params={}>
843
929
  #
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.
850
930
  def initialize(options = {}, &block) # :yields: name, value
851
931
  @accept_charset_error_block = block_given? ? block : nil
852
932
  @options={
data/lib/cgi/escape.rb CHANGED
@@ -1,11 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :stopdoc
3
4
  class CGI
4
5
  module Escape; end
5
6
  include Escape
6
7
  extend Escape
8
+ module EscapeExt; end # :nodoc:
7
9
  end
10
+ # :startdoc:
8
11
 
12
+ # Escape/unescape for CGI, HTML, URI.
9
13
  module CGI::Escape
10
14
  @@accept_charset = Encoding::UTF_8 unless defined?(@@accept_charset)
11
15
 
data/lib/cgi/html.rb CHANGED
@@ -1006,27 +1006,32 @@ class CGI
1006
1006
 
1007
1007
  end # Html5
1008
1008
 
1009
+ # HTML version 3 generation class.
1009
1010
  class HTML3
1010
1011
  include Html3
1011
1012
  include HtmlExtension
1012
1013
  end
1013
1014
 
1015
+ # HTML version 4 generation class.
1014
1016
  class HTML4
1015
1017
  include Html4
1016
1018
  include HtmlExtension
1017
1019
  end
1018
1020
 
1021
+ # HTML version 4 transitional generation class.
1019
1022
  class HTML4Tr
1020
1023
  include Html4Tr
1021
1024
  include HtmlExtension
1022
1025
  end
1023
1026
 
1027
+ # HTML version 4 with framesets generation class.
1024
1028
  class HTML4Fr
1025
1029
  include Html4Tr
1026
1030
  include Html4Fr
1027
1031
  include HtmlExtension
1028
1032
  end
1029
1033
 
1034
+ # HTML version 5 generation class.
1030
1035
  class HTML5
1031
1036
  include Html5
1032
1037
  include HtmlExtension
data/lib/cgi/session.rb CHANGED
@@ -214,11 +214,11 @@ class CGI
214
214
  dir = option['tmpdir'] || Dir::tmpdir
215
215
  prefix = option['prefix']
216
216
  suffix = option['suffix']
217
- require 'digest/md5'
218
- md5 = Digest::MD5.hexdigest(session_id)[0,16]
217
+ require 'digest'
218
+ sha256 = Digest::SHA256.hexdigest(session_id)[0,16]
219
219
  path = dir+"/"
220
220
  path << prefix if prefix
221
- path << md5
221
+ path << sha256
222
222
  path << suffix if suffix
223
223
  if File::exist? path
224
224
  hash = nil
data/lib/cgi/util.rb CHANGED
@@ -5,6 +5,7 @@ class CGI
5
5
  extend Util
6
6
  end
7
7
 
8
+ # Utility methods for CGI.
8
9
  module CGI::Util
9
10
  # Format a +Time+ object as a String using the format specified by RFC 1123.
10
11
  #
data/lib/cgi.rb CHANGED
@@ -42,6 +42,17 @@
42
42
  #
43
43
  # Read on for more details. Examples are provided at the bottom.
44
44
  #
45
+ # == About the Examples
46
+ #
47
+ # Examples on this page assume that \CGI has been required:
48
+ #
49
+ # require 'cgi'
50
+ #
51
+ # Unless otherwise stated, examples also assume that environment variable 'REQUEST_METHOD' exists
52
+ # (which prevents CGI.new from entering its online mode):
53
+ #
54
+ # ENV.include?('REQUEST_METHOD') # => true
55
+ #
45
56
  # == Queries
46
57
  #
47
58
  # The CGI class dynamically mixes in parameter and cookie-parsing
@@ -148,59 +159,62 @@
148
159
  # Escape and unescape methods are defined in cgi/escape.rb.
149
160
  # And when include, you can use utility methods like a function.
150
161
  #
151
- # == Examples of use
162
+ # == Examples of Use
152
163
  #
153
- # === Get form values
164
+ # === Form Values
154
165
  #
155
- # require "cgi"
156
- # cgi = CGI.new
157
- # value = cgi['field_name'] # <== value string for 'field_name'
158
- # # if not 'field_name' included, then return "".
159
- # fields = cgi.keys # <== array of field names
160
- #
161
- # # returns true if form has 'field_name'
162
- # cgi.has_key?('field_name')
163
- # cgi.has_key?('field_name')
164
- # cgi.include?('field_name')
166
+ # ==== Get Form Values
165
167
  #
166
- # CAUTION! <code>cgi['field_name']</code> returned an Array with the old
167
- # cgi.rb(included in Ruby 1.6)
168
+ # You can use method +cgi.params+ to retrieve form values
169
+ # in a {Hash}[https://docs.ruby-lang.org/en/3.4/Hash.html]:
168
170
  #
169
- # === Get form values as hash
170
- #
171
- # require "cgi"
171
+ # ENV.update(
172
+ # 'REQUEST_METHOD' => 'GET',
173
+ # 'QUERY_STRING' => 'a=111&&b=222&c&d='
174
+ # )
172
175
  # cgi = CGI.new
173
- # params = cgi.params
174
- #
175
- # cgi.params is a hash.
176
- #
177
- # cgi.params['new_field_name'] = ["value"] # add new param
178
- # cgi.params['field_name'] = ["new_value"] # change value
179
- # cgi.params.delete('field_name') # delete param
180
- # cgi.params.clear # delete all params
181
- #
182
- #
183
- # === Save form values to file
184
- #
185
- # require "pstore"
186
- # db = PStore.new("query.db")
187
- # db.transaction do
188
- # db["params"] = cgi.params
176
+ # cgi.params.class # => Hash
177
+ # cgi.params # => {"a" => ["111"], "b" => ["222"], "c" => [], "d" => [""]}
178
+ # cgi.params.keys # => ["a", "b", "c", "d"]
179
+ # cgi.params['a'] # => ["111"] # Returns an array.
180
+ # cgi.params['d'] # => [""] # Returns empty string in array if no value.
181
+ # cgi.params['x'] # => [] # Returns empty array if no key.
182
+ #
183
+ # A \CGI instance has these convenience methods:
184
+ #
185
+ # # Convenience method for cgi.params.keys.
186
+ # cgi.keys # => ["a", "b", "c", "d"]
187
+ # # Convenience method for cgi.params[key].first.
188
+ # cgi['a'] # => "111" # Returns string, not array.
189
+ # cgi['d'] # => "" # Returns empty string if no value.
190
+ # cgi['x'] # => "" # Returns empty string if no key.
191
+ # # Convenience method for cgi.params.include?.
192
+ # cgi.include?('a') # => true
193
+ # cgi.include?('x') # => false
194
+ #
195
+ # ==== Save and Restore Form Values
196
+ #
197
+ # This example uses {Pstore}[https://docs.ruby-lang.org/en/3.4/PStore.html]
198
+ # to store and retrieve form values:
199
+ #
200
+ # ENV.update(
201
+ # 'REQUEST_METHOD' => 'GET',
202
+ # 'QUERY_STRING' => 'a=111&&b=222&c&d='
203
+ # )
204
+ # cgi = CGI.new
205
+ # require 'pstore'
206
+ # store = PStore.new('params.store')
207
+ # store.transaction do
208
+ # store['params'] = cgi.params
189
209
  # end
190
- #
191
- #
192
- # === Restore form values from file
193
- #
194
- # require "pstore"
195
- # db = PStore.new("query.db")
196
- # db.transaction do
197
- # cgi.params = db["params"]
210
+ # cgi.params.clear # Oops! Lost my params!
211
+ # store.transaction do
212
+ # cgi.params = store['params']
198
213
  # end
214
+ # cgi.params # => {"a" => ["111"], "b" => ["222"], "c" => [], "d" => [""]}
199
215
  #
216
+ # ==== Get multipart form values
200
217
  #
201
- # === Get multipart form values
202
- #
203
- # require "cgi"
204
218
  # cgi = CGI.new
205
219
  # value = cgi['field_name'] # <== value string for 'field_name'
206
220
  # value.read # <== body of value
@@ -212,7 +226,6 @@
212
226
  #
213
227
  # === Get cookie values
214
228
  #
215
- # require "cgi"
216
229
  # cgi = CGI.new
217
230
  # values = cgi.cookies['name'] # <== array of 'name'
218
231
  # # if not 'name' included, then return [].
@@ -222,7 +235,6 @@
222
235
  #
223
236
  # === Get cookie objects
224
237
  #
225
- # require "cgi"
226
238
  # cgi = CGI.new
227
239
  # for name, cookie in cgi.cookies
228
240
  # cookie.expires = Time.now + 30
@@ -231,14 +243,12 @@
231
243
  #
232
244
  # cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
233
245
  #
234
- # require "cgi"
235
246
  # cgi = CGI.new
236
247
  # cgi.cookies['name'].expires = Time.now + 30
237
248
  # cgi.out("cookie" => cgi.cookies['name']) {"string"}
238
249
  #
239
250
  # === Print http header and html string to $DEFAULT_OUTPUT ($>)
240
251
  #
241
- # require "cgi"
242
252
  # cgi = CGI.new("html4") # add HTML generation methods
243
253
  # cgi.out do
244
254
  # cgi.html do
@@ -289,7 +299,8 @@
289
299
  #
290
300
 
291
301
  class CGI
292
- VERSION = "0.5.0"
302
+ # The version string
303
+ VERSION = "0.5.1"
293
304
  end
294
305
 
295
306
  require 'cgi/util'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cgi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-06-04 00:00:00.000000000 Z
10
+ date: 2025-12-10 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Support for the Common Gateway Interface protocol.
13
13
  email: