# Q: send\_file with Ruby 1.9.1 only works for text files

**URL:** <https://www.ruby-forum.com/t/q-send-file-with-ruby-1-9-1-only-works-for-text-files/159162>\
**Category:** Ruby\
**Created:** [February 1, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/q-send-file-with-ruby-1-9-1-only-works-for-text-files/159162 "2009-02-01T18:11:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [February 1, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/q-send-file-with-ruby-1-9-1-only-works-for-text-files/159162/1 "2009-02-01T18:11:00Z")

</div>

When I use Ruby 1.8.6 my Rails app works fine. Under Ruby 1.9.1 in  
development mode Webrick is trying to “split” binary data and throws  
an error:

#\<ArgumentError: invalid byte sequence in UTF-8\>  
["/Users/markw/bin/ruby19/lib/ruby/gems/1.9.1/gems/rails-2.2.2/lib/  
webrick\_server.rb:136:in `split'", "/Users/markw/bin/ruby19/lib/ruby/ gems/1.9.1/gems/rails-2.2.2/lib/webrick_server.rb:136:in`extract\_header\_and\_body’", “/Users/markw/bin/ruby19/lib/ruby/gems/  
1.9.1/gems/rails-2.2.2/lib/webrick\_server.rb:109:in  
`handle_dispatch'", "/Users/markw/bin/ruby19/lib/ruby/gems/1.9.1/gems/ rails-2.2.2/lib/webrick_server.rb:74:in`service’”, “/Users/markw/bin/  
ruby19/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'", "/Users/ markw/bin/ruby19/lib/ruby/1.9.1/webrick/httpserver.rb:70:in`run’”, “/  
Users/markw/bin/ruby19/lib/ruby/1.9.1/webrick/server.rb:183:in `block  
in start\_thread’”]

As much as possible I am trying convert all my Ruby code and Rails  
apps to Ruby 1.9.x and this is one of the last problems that I am  
having problems working around.

I have tried several combinations of send\_file calls like:

send\_file(filename, :filename =\> asset.filename, :type =\> :jpeg)

and none work - unless I am sending a plain text file.

This is obviously a unicode problem.

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [February 1, 2009, 7:35pm UTC](https://www.ruby-forum.com/t/q-send-file-with-ruby-1-9-1-only-works-for-text-files/159162/2 "2009-02-01T19:35:43Z")

</div>

On Mon, 2 Feb 2009 02:10:00 +0900, Mark Watson wrote:

> When I use Ruby 1.8.6 my Rails app works fine. Under Ruby 1.9.1 in  
> development mode Webrick is trying to “split” binary data and throws  
> an error:
> 
> #\<ArgumentError: invalid byte sequence in UTF-8\>

Is webrick part of the standard distribution? (been years since I used  
it!) Does its tests run?

Does Thin work for you? I’ve been running Ruby 1.9.1 for a while now on  
Thin and it works pretty well. I haven’t sent any binary data yet  
though.

---

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [February 2, 2009, 5:56am UTC](https://www.ruby-forum.com/t/q-send-file-with-ruby-1-9-1-only-works-for-text-files/159162/3 "2009-02-02T05:56:02Z")

</div>

On Feb 1, 11:34 am, David P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > in start\_thread’"]
> 
> > This is obviously a unicode problem.
> 
> > Thanks,  
> > Mark

Thanks for the reference David.

I could not build thin on OS X using Ruby 1.9.1:  
rubymain.cpp: In function ‘VALUE t\_invoke\_popen(VALUE, VALUE)’:  
rubymain.cpp:466: error: ‘struct RArray’ has no member named ‘len’

Looks like the code is not updated for the unicode implementation in  
Ruby 1.9.1

---

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [February 2, 2009, 5:56am UTC](https://www.ruby-forum.com/t/q-send-file-with-ruby-1-9-1-only-works-for-text-files/159162/4 "2009-02-02T05:56:03Z")

</div>

On Feb 1, 10:08 am, Mark Watson [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> `handle\_dispatch’", "/Users/markw/bin/ruby19/lib/ruby/gems/1.9.1/gems/  
> I have tried several combinations of send\_file calls like:
> 
> send\_file(filename, :filename =\> asset.filename, :type =\> :jpeg)
> 
> and none work - unless I am sending a plain text file.
> 
> This is obviously a unicode problem.
> 
> Thanks,  
> Mark

BTW, I can patch webrick\_server.rb to make everything work:

```
def extract_header_and_body(data)
  data.rewind
  data = data.read

  # MLW 2/1/2009
  if data.ascii_only?
    raw_header, body = *data.split(/^[\xd\xa]{2}/on, 2)
  else
    aa = data.lines.to_a
    ind = aa.index("\r\n")
    raw_header = aa[0...ind].join
    body = aa[ind+1..-1].join
  end

  #raw_header, body = *data.split(/^[\xd\xa]{2}/on, 2)
  header = WEBrick::HTTPUtils::parse_header(raw_header)

  return header, body
end

```
