Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix FilePart length calculation for Ruby 1.9 when filename contains multibyte characters #21

Merged
merged 2 commits into from

2 participants

@hexfet

Applicable RFCs (http://en.wikipedia.org/wiki/MIME#Encoded-Word,
http://stackoverflow.com/questions/1361604) indicate only ASCII8BIT is allowed
in headers and suggest encoding options, but Chrome and Firefox put multibyte
characters in the header and the Rails stack handles it correctly, so just
fix the length.

The test is written the way it is to avoid embedding multibyte characters in the source file.

hexfet added some commits
@hexfet hexfet Fix FilePart length calculation for Ruby 1.9 when filename contains m…
…ultibyte

characters.

Applicable RFCs (http://en.wikipedia.org/wiki/MIME#Encoded-Word,
http://stackoverflow.com/questions/1361604) indicate only ASCII8BIT is allowed
in headers and suggest encoding options, but Chrome and Firefox put multibyte
characters in the header and the Rails stack handles it correctly, so ... just
fix the length.
8b07e7b
@hexfet hexfet Fix multibyte filename test for Ruby 1.8 and Jruby 460bc8b
@nicksieger nicksieger merged commit b940e62 into nicksieger:master
@nicksieger
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 14, 2012
  1. @hexfet

    Fix FilePart length calculation for Ruby 1.9 when filename contains m…

    hexfet authored
    …ultibyte
    
    characters.
    
    Applicable RFCs (http://en.wikipedia.org/wiki/MIME#Encoded-Word,
    http://stackoverflow.com/questions/1361604) indicate only ASCII8BIT is allowed
    in headers and suggest encoding options, but Chrome and Firefox put multibyte
    characters in the header and the Rails stack handles it correctly, so ... just
    fix the length.
Commits on Nov 15, 2012
  1. @hexfet
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 1 deletion.
  1. +1 −1  lib/parts.rb
  2. +8 −0 test/test_parts.rb
View
2  lib/parts.rb
@@ -52,7 +52,7 @@ def initialize(boundary, name, io)
@head = build_head(boundary, name, io.original_filename, io.content_type, file_length,
io.respond_to?(:opts) ? io.opts : {})
@foot = "\r\n"
- @length = @head.length + file_length + @foot.length
+ @length = @head.bytesize + file_length + @foot.length
@io = CompositeReadIO.new(StringIO.new(@head), io, StringIO.new(@foot))
end
View
8 test/test_parts.rb
@@ -9,6 +9,7 @@
require 'parts'
require 'stringio'
require 'composite_io'
+require 'tempfile'
MULTIBYTE = File.dirname(__FILE__)+'/multibyte.txt'
@@ -42,6 +43,13 @@ def test_correct_length
def test_multibyte_file_length
assert_part_length Parts::FilePart.new("boundary", "multibyte", UploadIO.new(MULTIBYTE, "text/plain"))
end
+
+ def test_multibyte_filename
+ name = File.read(MULTIBYTE, 300)
+ file = Tempfile.new(name.respond_to?(:force_encoding) ? name.force_encoding("UTF-8") : name)
+ assert_part_length Parts::FilePart.new("boundary", "multibyte", UploadIO.new(file, "text/plain"))
+ file.close
+ end
end
class ParamPartTest < Test::Unit::TestCase
Something went wrong with that request. Please try again.