rack 1.2.8 → 1.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rack might be problematic. Click here for more details.

Files changed (89) hide show
  1. data/README +9 -177
  2. data/Rakefile +2 -1
  3. data/SPEC +2 -2
  4. data/lib/rack.rb +2 -13
  5. data/lib/rack/auth/abstract/request.rb +7 -5
  6. data/lib/rack/auth/digest/md5.rb +6 -2
  7. data/lib/rack/auth/digest/params.rb +5 -7
  8. data/lib/rack/auth/digest/request.rb +1 -1
  9. data/lib/rack/backports/uri/common.rb +64 -0
  10. data/lib/rack/builder.rb +60 -3
  11. data/lib/rack/chunked.rb +29 -22
  12. data/lib/rack/conditionalget.rb +35 -16
  13. data/lib/rack/content_length.rb +3 -3
  14. data/lib/rack/deflater.rb +5 -2
  15. data/lib/rack/etag.rb +38 -10
  16. data/lib/rack/file.rb +76 -43
  17. data/lib/rack/handler.rb +13 -7
  18. data/lib/rack/handler/cgi.rb +0 -2
  19. data/lib/rack/handler/fastcgi.rb +13 -4
  20. data/lib/rack/handler/lsws.rb +0 -2
  21. data/lib/rack/handler/mongrel.rb +12 -2
  22. data/lib/rack/handler/scgi.rb +9 -1
  23. data/lib/rack/handler/thin.rb +7 -1
  24. data/lib/rack/handler/webrick.rb +12 -5
  25. data/lib/rack/lint.rb +2 -2
  26. data/lib/rack/lock.rb +29 -3
  27. data/lib/rack/methodoverride.rb +1 -1
  28. data/lib/rack/mime.rb +2 -2
  29. data/lib/rack/mock.rb +28 -33
  30. data/lib/rack/multipart.rb +34 -0
  31. data/lib/rack/multipart/generator.rb +93 -0
  32. data/lib/rack/multipart/parser.rb +164 -0
  33. data/lib/rack/multipart/uploaded_file.rb +30 -0
  34. data/lib/rack/request.rb +55 -19
  35. data/lib/rack/response.rb +10 -8
  36. data/lib/rack/sendfile.rb +14 -18
  37. data/lib/rack/server.rb +55 -8
  38. data/lib/rack/session/abstract/id.rb +233 -22
  39. data/lib/rack/session/cookie.rb +99 -46
  40. data/lib/rack/session/memcache.rb +30 -56
  41. data/lib/rack/session/pool.rb +22 -43
  42. data/lib/rack/showexceptions.rb +40 -11
  43. data/lib/rack/showstatus.rb +9 -2
  44. data/lib/rack/static.rb +29 -9
  45. data/lib/rack/urlmap.rb +6 -1
  46. data/lib/rack/utils.rb +67 -326
  47. data/rack.gemspec +2 -3
  48. data/test/builder/anything.rb +5 -0
  49. data/test/builder/comment.ru +4 -0
  50. data/test/builder/end.ru +3 -0
  51. data/test/builder/options.ru +2 -0
  52. data/test/cgi/lighttpd.conf +1 -1
  53. data/test/cgi/lighttpd.errors +412 -0
  54. data/test/multipart/content_type_and_no_filename +6 -0
  55. data/test/multipart/text +5 -0
  56. data/test/multipart/webkit +32 -0
  57. data/test/registering_handler/rack/handler/registering_myself.rb +8 -0
  58. data/test/spec_auth_digest.rb +20 -5
  59. data/test/spec_builder.rb +29 -0
  60. data/test/spec_cgi.rb +11 -0
  61. data/test/spec_chunked.rb +1 -1
  62. data/test/spec_commonlogger.rb +1 -1
  63. data/test/spec_conditionalget.rb +47 -0
  64. data/test/spec_content_length.rb +0 -6
  65. data/test/spec_content_type.rb +5 -5
  66. data/test/spec_deflater.rb +46 -2
  67. data/test/spec_etag.rb +68 -1
  68. data/test/spec_fastcgi.rb +11 -0
  69. data/test/spec_file.rb +54 -3
  70. data/test/spec_handler.rb +23 -5
  71. data/test/spec_lint.rb +2 -2
  72. data/test/spec_lock.rb +111 -5
  73. data/test/spec_methodoverride.rb +2 -2
  74. data/test/spec_mock.rb +3 -3
  75. data/test/spec_mongrel.rb +1 -2
  76. data/test/spec_multipart.rb +279 -0
  77. data/test/spec_request.rb +222 -38
  78. data/test/spec_response.rb +9 -3
  79. data/test/spec_server.rb +74 -0
  80. data/test/spec_session_abstract_id.rb +43 -0
  81. data/test/spec_session_cookie.rb +97 -15
  82. data/test/spec_session_memcache.rb +60 -50
  83. data/test/spec_session_pool.rb +63 -40
  84. data/test/spec_showexceptions.rb +64 -0
  85. data/test/spec_static.rb +23 -0
  86. data/test/spec_utils.rb +65 -351
  87. data/test/spec_webrick.rb +23 -4
  88. metadata +35 -15
  89. data/test/spec_auth.rb +0 -57
data/README CHANGED
@@ -1,4 +1,4 @@
1
- = Rack, a modular Ruby webserver interface {<img src="https://secure.travis-ci.org/rack/rack.png" alt="Build Status" />}[http://travis-ci.org/rack/rack] {<img src="https://gemnasium.com/rack/rack.png" alt="Dependency Status" />}[https://gemnasium.com/rack/rack]
1
+ = Rack, a modular Ruby webserver interface
2
2
 
3
3
  Rack provides a minimal, modular and adaptable interface for developing
4
4
  web applications in Ruby. By wrapping HTTP requests and responses in
@@ -27,11 +27,8 @@ These web servers include Rack handlers in their distributions:
27
27
  * Fuzed
28
28
  * Glassfish v3
29
29
  * Phusion Passenger (which is mod_rack for Apache and for nginx)
30
- * Puma
31
30
  * Rainbows!
32
31
  * Unicorn
33
- * unixrack
34
- * uWSGI
35
32
  * Zbatery
36
33
 
37
34
  Any valid Rack app will run the same on all these handlers, without
@@ -122,7 +119,7 @@ By default, the lobster is found at http://localhost:9292.
122
119
 
123
120
  == Installing with RubyGems
124
121
 
125
- A Gem of Rack is available at rubygems.org. You can install it with:
122
+ A Gem of Rack is available at gemcutter.org. You can install it with:
126
123
 
127
124
  gem install rack
128
125
 
@@ -320,7 +317,7 @@ run on port 11211) and memcache-client installed.
320
317
  * Security fix in Rack::Auth::Digest::MD5: when authenticator
321
318
  returned nil, permission was granted on empty password.
322
319
 
323
- * May 22nd, 2011: Thirteenth public release 1.3.0
320
+ * unknown: Thirteenth? public release 1.3.0.beta
324
321
  * Various performance optimizations
325
322
  * Various multipart fixes
326
323
  * Various multipart refactors
@@ -336,177 +333,14 @@ run on port 11211) and memcache-client installed.
336
333
  * Rack::Deflater now supports streaming
337
334
  * Improved Rack::Handler loading and searching
338
335
  * Support for the PATCH verb
339
- * env['rack.session.options'] now contains session options
340
- * Cookies respect renew
341
- * Session middleware uses SecureRandom.hex
342
-
343
- * May 22nd, 2011: Fourteenth public release 1.2.3
344
- * Pulled in relevant bug fixes from 1.3
345
- * Fixed 1.8.6 support
346
-
347
- * July 13, 2011: Fifteenth public release 1.3.1
348
- * Fix 1.9.1 support
349
- * Fix JRuby support
350
- * Properly handle $KCODE in Rack::Utils.escape
351
- * Make method_missing/respond_to behavior consistent for Rack::Lock,
352
- Rack::Auth::Digest::Request and Rack::Multipart::UploadedFile
353
- * Reenable passing rack.session to session middleware
354
- * Rack::CommonLogger handles streaming responses correctly
355
- * Rack::MockResponse calls close on the body object
356
- * Fix a DOS vector from MRI stdlib backport
357
-
358
- * July 16, 2011: Sixteenth public release 1.3.2
359
- * Fix for Rails and rack-test, Rack::Utils#escape calls to_s
360
-
361
- * Not Yet Released: Seventeenth public release 1.3.3
362
- * Fix bug with broken query parameters in Rack::ShowExceptions
363
- * Rack::Request#cookies no longer swallows exceptions on broken input
364
- * Prevents XSS attacks enabled by bug in Ruby 1.8's regexp engine
365
- * Rack::ConditionalGet handles broken If-Modified-Since helpers
366
-
367
- * September 16, 2011: Eighteenth public release 1.2.4
368
- * Fix a bug with MRI regex engine to prevent XSS by malformed unicode
369
-
370
- * October 1, 2011: Nineteenth public release 1.3.4
371
- * Backport security fix from 1.9.3, also fixes some roundtrip issues in URI
372
- * Small documentation update
373
- * Fix an issue where BodyProxy could cause an infinite recursion
374
- * Add some supporting files for travis-ci
375
-
376
- * October 17, 2011: Twentieth public release 1.3.5
377
- * Fix annoying warnings caused by the backport in 1.3.4
378
-
379
- * December 28th, 2011: Twenty first public release: 1.1.3.
380
- * Security fix. http://www.ocert.org/advisories/ocert-2011-003.html
381
- Further information here: http://jruby.org/2011/12/27/jruby-1-6-5-1
382
-
383
- * December 28th, 2011: Twenty fourth public release 1.4.0
384
- * Ruby 1.8.6 support has officially been dropped. Not all tests pass.
385
- * Raise sane error messages for broken config.ru
386
- * Allow combining run and map in a config.ru
387
- * Rack::ContentType will not set Content-Type for responses without a body
388
- * Status code 205 does not send a response body
389
- * Rack::Response::Helpers will not rely on instance variables
390
- * Rack::Utils.build_query no longer outputs '=' for nil query values
391
- * Various mime types added
392
- * Rack::MockRequest now supports HEAD
393
- * Rack::Directory now supports files that contain RFC3986 reserved chars
394
- * Rack::File now only supports GET and HEAD requests
395
- * Rack::Server#start now passes the block to Rack::Handler::<h>#run
396
- * Rack::Static now supports an index option
397
- * Added the Teapot status code
398
- * rackup now defaults to Thin instead of Mongrel (if installed)
399
- * Support added for HTTP_X_FORWARDED_SCHEME
400
- * Numerous bug fixes, including many fixes for new and alternate rubies
401
-
402
- * January 22nd, 2012: Twenty fifth public release 1.4.1
403
- * Alter the keyspace limit calculations to reduce issues with nested params
404
- * Add a workaround for multipart parsing where files contain unescaped "%"
405
- * Added Rack::Response::Helpers#method_not_allowed? (code 405)
406
- * Rack::File now returns 404 for illegal directory traversals
407
- * Rack::File now returns 405 for illegal methods (non HEAD/GET)
408
- * Rack::Cascade now catches 405 by default, as well as 404
409
- * Cookies missing '--' no longer cause an exception to be raised
410
- * Various style changes and documentation spelling errors
411
- * Rack::BodyProxy always ensures to execute its block
412
- * Additional test coverage around cookies and secrets
413
- * Rack::Session::Cookie can now be supplied either secret or old_secret
414
- * Tests are no longer dependent on set order
415
- * Rack::Static no longer defaults to serving index files
416
- * Rack.release was fixed
417
-
418
- * January 6th, 2013: Twenty sixth public release 1.1.4
419
- * Add warnings when users do not provide a session secret
420
-
421
- * January 6th, 2013: Twenty seventh public release 1.2.6
422
- * Add warnings when users do not provide a session secret
423
- * Fix parsing performance for unquoted filenames
424
-
425
- * January 6th, 2013: Twenty eighth public release 1.3.7
426
- * Add warnings when users do not provide a session secret
427
- * Fix parsing performance for unquoted filenames
428
- * Updated URI backports
429
- * Fix URI backport version matching, and silence constant warnings
430
- * Correct parameter parsing with empty values
431
- * Correct rackup '-I' flag, to allow multiple uses
432
- * Correct rackup pidfile handling
433
- * Report rackup line numbers correctly
434
- * Fix request loops caused by non-stale nonces with time limits
435
- * Fix reloader on Windows
436
- * Prevent infinite recursions from Response#to_ary
437
- * Various middleware better conforms to the body close specification
438
- * Updated language for the body close specification
439
- * Additional notes regarding ECMA escape compatibility issues
440
- * Fix the parsing of multiple ranges in range headers
441
-
442
- * January 6th, 2013: Twenty ninth public release 1.4.2
443
- * Add warnings when users do not provide a session secret
444
- * Fix parsing performance for unquoted filenames
445
- * Updated URI backports
446
- * Fix URI backport version matching, and silence constant warnings
447
- * Correct parameter parsing with empty values
448
- * Correct rackup '-I' flag, to allow multiple uses
449
- * Correct rackup pidfile handling
450
- * Report rackup line numbers correctly
451
- * Fix request loops caused by non-stale nonces with time limits
452
- * Fix reloader on Windows
453
- * Prevent infinite recursions from Response#to_ary
454
- * Various middleware better conforms to the body close specification
455
- * Updated language for the body close specification
456
- * Additional notes regarding ECMA escape compatibility issues
457
- * Fix the parsing of multiple ranges in range headers
458
- * Prevent errors from empty parameter keys
459
- * Added PATCH verb to Rack::Request
460
- * Various documentation updates
461
- * Fix session merge semantics (fixes rack-test)
462
- * Rack::Static :index can now handle multiple directories
463
- * All tests now utilize Rack::Lint (special thanks to Lars Gierth)
464
- * Rack::File cache_control parameter is now deprecated, and removed by 1.5
465
- * Correct Rack::Directory script name escaping
466
- * Rack::Static supports header rules for sophisticated configurations
467
- * Multipart parsing now works without a Content-Length header
468
- * New logos courtesy of Zachary Scott!
469
- * Rack::BodyProxy now explicitly defines #each, useful for C extensions
470
- * Cookies that are not URI escaped no longer cause exceptions
471
-
472
- * January 7th, 2013: Thirtieth public release 1.3.8
473
- * Security: Prevent unbounded reads in large multipart boundaries
474
-
475
- * January 7th, 2013: Thirty first public release 1.4.3
476
- * Security: Prevent unbounded reads in large multipart boundaries
477
-
478
- * January 13th, 2013: Thirty second public release 1.4.4, 1.3.9, 1.2.7, 1.1.5
479
- * [SEC] Rack::Auth::AbstractRequest no longer symbolizes arbitrary strings
480
- * Fixed erroneous test case in the 1.3.x series
481
-
482
- * February 7th, Thirty fifth public release 1.1.6, 1.2.8, 1.3.10
483
- * Fix CVE-2013-0263, timing attack against Rack::Session::Cookie
484
-
485
- * February 7th, Thirty fifth public release 1.4.5
486
- * Fix CVE-2013-0263, timing attack against Rack::Session::Cookie
487
- * Fix CVE-2013-0262, symlink path traversal in Rack::File
488
-
489
- * February 7th, Thirty fifth public release 1.5.2
490
- * Fix CVE-2013-0263, timing attack against Rack::Session::Cookie
491
- * Fix CVE-2013-0262, symlink path traversal in Rack::File
492
- * Add various methods to Session for enhanced Rails compatibility
493
- * Request#trusted_proxy? now only matches whole stirngs
494
- * Add JSON cookie coder, to be default in Rack 1.6+ due to security concerns
495
- * URLMap host matching in environments that don't set the Host header fixed
496
- * Fix a race condition that could result in overwritten pidfiles
497
- * Various documentation additions
336
+
337
+
498
338
 
499
339
  == Contact
500
340
 
501
341
  Please post bugs, suggestions and patches to
502
342
  the bug tracker at <http://github.com/rack/rack/issues>.
503
343
 
504
- Please post security related bugs and suggestions to the core team at
505
- <https://groups.google.com/group/rack-core> or rack-core@googlegroups.com. Due
506
- to wide usage of the library, it is strongly preferred that we manage timing in
507
- order to provide viable patches at the time of disclosure. Your assistance in
508
- this matter is greatly appreciated.
509
-
510
344
  Mailing list archives are available at
511
345
  <http://groups.google.com/group/rack-devel>.
512
346
 
@@ -526,8 +360,6 @@ The Rack Core Team, consisting of
526
360
  * Michael Fellinger (manveru)
527
361
  * Ryan Tomayko (rtomayko)
528
362
  * Scytrin dai Kinthra (scytrin)
529
- * Aaron Patterson (tenderlove)
530
- * Konstantin Haase (rkh)
531
363
 
532
364
  would like to thank:
533
365
 
@@ -557,7 +389,7 @@ would like to thank:
557
389
  * Marcus Rückert, for help with configuring and debugging lighttpd.
558
390
  * The WSGI team for the well-done and documented work they've done and
559
391
  Rack builds up on.
560
- * All bug reporters and patch contributors not mentioned above.
392
+ * All bug reporters and patch contributers not mentioned above.
561
393
 
562
394
  == Copyright
563
395
 
@@ -582,11 +414,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
582
414
 
583
415
  == Links
584
416
 
585
- Rack:: <http://rack.github.com/>
417
+ Rack:: <http://rack.rubyforge.org/>
418
+ Rack's Rubyforge project:: <http://rubyforge.org/projects/rack>
586
419
  Official Rack repositories:: <http://github.com/rack>
587
- Rack Bug Tracking:: <http://github.com/rack/rack/issues>
420
+ Rack Lighthouse Bug Tracking:: <http://rack.lighthouseapp.com/>
588
421
  rack-devel mailing list:: <http://groups.google.com/group/rack-devel>
589
- Rack's Rubyforge project:: <http://rubyforge.org/projects/rack>
590
422
 
591
423
  Christian Neukirchen:: <http://chneukirchen.org/>
592
424
 
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  # Rakefile for Rack. -*-ruby-*-
2
+ require 'rake/rdoctask'
2
3
 
3
4
  desc "Run all the tests"
4
5
  task :default => [:test]
@@ -68,7 +69,7 @@ desc "Run all the fast tests"
68
69
  task :test => 'SPEC' do
69
70
  opts = ENV['TEST'] || '-a'
70
71
  specopts = ENV['TESTOPTS'] ||
71
- "-q -t '^(?!Rack::Handler|Rack::Adapter|Rack::Session::Memcache|rackup)'"
72
+ "-q -t '^(?!Rack::Adapter|Rack::Session::Memcache|Rack::Server)'"
72
73
 
73
74
  sh "bacon -I./lib:./test -w #{opts} #{specopts}"
74
75
  end
data/SPEC CHANGED
@@ -3,7 +3,7 @@ can (and should) use Rack::Lint to enforce it.
3
3
  When you develop middleware, be sure to add a Lint before and
4
4
  after to catch all mistakes.
5
5
  = Rack applications
6
- A Rack application is a Ruby object (not a class) that
6
+ A Rack application is an Ruby object (not a class) that
7
7
  responds to +call+.
8
8
  It takes exactly one argument, the *environment*
9
9
  and returns an Array of exactly three values:
@@ -106,7 +106,7 @@ The input stream must respond to +gets+, +each+, +read+ and +rewind+.
106
106
  * +gets+ must be called without arguments and return a string,
107
107
  or +nil+ on EOF.
108
108
  * +read+ behaves like IO#read. Its signature is <tt>read([length, [buffer]])</tt>.
109
- If given, +length+ must be a non-negative Integer (>= 0) or +nil+, and +buffer+ must
109
+ If given, +length+ must be an non-negative Integer (>= 0) or +nil+, and +buffer+ must
110
110
  be a String and may not be nil. If +length+ is given and not nil, then this method
111
111
  reads at most +length+ bytes from the input stream. If +length+ is not given or nil,
112
112
  then this method reads all data until EOF.
@@ -20,7 +20,7 @@ module Rack
20
20
 
21
21
  # Return the Rack release as a dotted string.
22
22
  def self.release
23
- "1.2"
23
+ "1.3"
24
24
  end
25
25
 
26
26
  autoload :Builder, "rack/builder"
@@ -54,6 +54,7 @@ module Rack
54
54
  autoload :Static, "rack/static"
55
55
  autoload :URLMap, "rack/urlmap"
56
56
  autoload :Utils, "rack/utils"
57
+ autoload :Multipart, "rack/multipart"
57
58
 
58
59
  autoload :MockRequest, "rack/mock"
59
60
  autoload :MockResponse, "rack/mock"
@@ -71,18 +72,6 @@ module Rack
71
72
  autoload :Params, "rack/auth/digest/params"
72
73
  autoload :Request, "rack/auth/digest/request"
73
74
  end
74
-
75
- # Not all of the following schemes are "standards", but they are used often.
76
- @schemes = %w[basic digest bearer mac token oauth oauth2]
77
-
78
- def self.add_scheme scheme
79
- @schemes << scheme
80
- @schemes.uniq!
81
- end
82
-
83
- def self.schemes
84
- @schemes.dup
85
- end
86
75
  end
87
76
 
88
77
  module Session
@@ -1,3 +1,5 @@
1
+ require 'rack/request'
2
+
1
3
  module Rack
2
4
  module Auth
3
5
  class AbstractRequest
@@ -6,6 +8,10 @@ module Rack
6
8
  @env = env
7
9
  end
8
10
 
11
+ def request
12
+ @request ||= Request.new(@env)
13
+ end
14
+
9
15
  def provided?
10
16
  !authorization_key.nil?
11
17
  end
@@ -15,11 +21,7 @@ module Rack
15
21
  end
16
22
 
17
23
  def scheme
18
- @scheme ||=
19
- begin
20
- s = parts.first.downcase
21
- Rack::Auth.schemes.include?(s) ? s.to_sym : s
22
- end
24
+ @scheme ||= parts.first.downcase.to_sym
23
25
  end
24
26
 
25
27
  def params
@@ -21,9 +21,13 @@ module Rack
21
21
 
22
22
  attr_writer :passwords_hashed
23
23
 
24
- def initialize(*args)
25
- super
24
+ def initialize(app, realm=nil, opaque=nil, &authenticator)
26
25
  @passwords_hashed = nil
26
+ if opaque.nil? and realm.respond_to? :values_at
27
+ realm, opaque, @passwords_hashed = realm.values_at :realm, :opaque, :passwords_hashed
28
+ end
29
+ super(app, realm, &authenticator)
30
+ @opaque = opaque
27
31
  end
28
32
 
29
33
  def passwords_hashed?
@@ -4,11 +4,10 @@ module Rack
4
4
  class Params < Hash
5
5
 
6
6
  def self.parse(str)
7
- split_header_value(str).inject(new) do |header, param|
7
+ Params[*split_header_value(str).map do |param|
8
8
  k, v = param.split('=', 2)
9
- header[k] = dequote(v)
10
- header
11
- end
9
+ [k, dequote(v)]
10
+ end.flatten]
12
11
  end
13
12
 
14
13
  def self.dequote(str) # From WEBrick::HTTPUtils
@@ -38,9 +37,8 @@ module Rack
38
37
  UNQUOTED = ['nc', 'stale']
39
38
 
40
39
  def to_s
41
- inject([]) do |parts, (k, v)|
42
- parts << "#{k}=" + (UNQUOTED.include?(k) ? v.to_s : quote(v))
43
- parts
40
+ map do |k, v|
41
+ "#{k}=" + (UNQUOTED.include?(k) ? v.to_s : quote(v))
44
42
  end.join(', ')
45
43
  end
46
44
 
@@ -16,7 +16,7 @@ module Rack
16
16
  end
17
17
 
18
18
  def correct_uri?
19
- (@env['SCRIPT_NAME'].to_s + @env['PATH_INFO'].to_s) == uri
19
+ request.fullpath == uri
20
20
  end
21
21
 
22
22
  def nonce
@@ -0,0 +1,64 @@
1
+ # :stopdoc:
2
+
3
+ # Stolen from ruby core's uri/common.rb, with modifications to support 1.8.x
4
+ #
5
+ # https://github.com/ruby/ruby/blob/trunk/lib/uri/common.rb
6
+ #
7
+ #
8
+
9
+ module URI
10
+ TBLENCWWWCOMP_ = {} # :nodoc:
11
+ TBLDECWWWCOMP_ = {} # :nodoc:
12
+
13
+ # Encode given +str+ to URL-encoded form data.
14
+ #
15
+ # This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP
16
+ # (ASCII space) to + and converts others to %XX.
17
+ #
18
+ # This is an implementation of
19
+ # http://www.w3.org/TR/html5/forms.html#url-encoded-form-data
20
+ #
21
+ # See URI.decode_www_form_component, URI.encode_www_form
22
+ def self.encode_www_form_component(str)
23
+ if TBLENCWWWCOMP_.empty?
24
+ tbl = {}
25
+ 256.times do |i|
26
+ tbl[i.chr] = '%%%02X' % i
27
+ end
28
+ tbl[' '] = '+'
29
+ begin
30
+ TBLENCWWWCOMP_.replace(tbl)
31
+ TBLENCWWWCOMP_.freeze
32
+ rescue
33
+ end
34
+ end
35
+ str = str.to_s
36
+ str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]}
37
+ end
38
+
39
+ # Decode given +str+ of URL-encoded form data.
40
+ #
41
+ # This decods + to SP.
42
+ #
43
+ # See URI.encode_www_form_component, URI.decode_www_form
44
+ def self.decode_www_form_component(str, enc=nil)
45
+ if TBLDECWWWCOMP_.empty?
46
+ tbl = {}
47
+ 256.times do |i|
48
+ h, l = i>>4, i&15
49
+ tbl['%%%X%X' % [h, l]] = i.chr
50
+ tbl['%%%x%X' % [h, l]] = i.chr
51
+ tbl['%%%X%x' % [h, l]] = i.chr
52
+ tbl['%%%x%x' % [h, l]] = i.chr
53
+ end
54
+ tbl['+'] = ' '
55
+ begin
56
+ TBLDECWWWCOMP_.replace(tbl)
57
+ TBLDECWWWCOMP_.freeze
58
+ rescue
59
+ end
60
+ end
61
+ raise ArgumentError, "invalid %-encoding (#{str})" unless /\A(?:%[0-9a-fA-F]{2}|[^%]+)*\z/ =~ str
62
+ str.gsub(/\+|%[0-9a-fA-F]{2}/) {|m| TBLDECWWWCOMP_[m]}
63
+ end
64
+ end