Bug #724: webrick httpresponse wrong content-length
http://redmine.ruby-lang.org/issues/show/724

Author: Maksim Melnikau
Status: Open, Priority: Normal
Category: lib, Target version: 1.9.x

Webrick(httpresponse) fills HTTP response body with wrong content-type.


---- my variant of fix ----

--- lib/webrick/httpresponse.rb (revision 20147)
+++ lib/webrick/httpresponse.rb (working copy)
@@ -142,7 +142,7 @@
         @header.delete('content-length')
       elsif @header['content-length'].nil?
         unless @body.is_a?(IO)
-          @header['content-length'] = @body ? @body.size : 0
+          @header['content-length'] = @body ? @body.bytesize : 0
         end
       end



----- sample webrick-based http server----
#!/usr/bin/ruby19
# coding : utf-8
 
require 'webrick'
 
include WEBrick
 
hello_proc = lambda do |req, resp|
 resp['Content-Type'] = "text/html"
 resp.body = %{
   <html>
     <head>
       <title> ????иве??, Rails! </title>
     </head>
     <body>
      <h1> ????иве??, Rails! </h1>
      <p>
        Сей??а?? Mon Jan 28 14:14:13 +0200 2008
      </p>
      <p>
        ??а????ало в??ем??
        поп??о??а????????!
      </p>
     </body>
   </html>
 }
end
 
hello = HTTPServlet::ProcHandler.new(hello_proc)

s = HTTPServer.new(:Port => 3000)
s.mount("/", hello)

trap("INT"){ s.shutdown }
s.start



---- testing with wget ----

 max_posedon@localhost ~ % wget http://0.0.0.0:3000 -O -
 --03:48:01--  http://0.0.0.0:3000/
           => `-'
 Connecting to 0.0.0.0:3000... connected.
 HTTP request sent, awaiting response... 200 OK
 Length: 318 [text/html]
 
  0% [                                                                                                                     ] 0             --.--K/s
    <html>
      <head>
        <title> ????иве??, Rails! </title>
      </head>
      <body>
       <h1> ????иве??, Rails! </h1>
       <p>
         Сей??а?? Mon Jan 28 14:14:13 +0200 2008
       </p>
       <p>
         ??а????ало в??ем??
 100%[====================================================================================================================>] 318           --.--K/s
 
 03:48:01 (10.49 MB/s) - `-' saved [318/318]
 
---- not hole document downloaded! ----


----------------------------------------
http://redmine.ruby-lang.org