Skip to content

Instantly share code, notes, and snippets.

@nikushi

nikushi/chunk-test.md

Last active Dec 30, 2015
Embed
What would you like to do?
Transfer-Encoding: chunkedを返答するときでもWebRickだとContent-Lengthが付いてしまう

WebRick

$ telnet localhost 9292
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
Host: localhost:9292

HTTP/1.1 200 OK
Content-Type: text/html
Transfer-Encoding: chunked
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-05-14)
Date: Tue, 03 Dec 2013 05:06:11 GMT
Content-Length: 155
Connection: Keep-Alive

a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
0

puma

 telnet localhost 9292
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
Host: localhost:9292

HTTP/1.1 200 OK
Content-Type: text/html
Transfer-Encoding: chunked

a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
0

thin

 $ telnet localhost 9292
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
Host: localhost:9292

HTTP/1.1 200 OK
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Server: thin 1.5.1 codename Straight Razor

a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
0

Connection closed by foreign host.

Unicorn

$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
Host: localhost:8080

HTTP/1.1 200 OK
Date: Tue, 03 Dec 2013 05:07:54 GMT
Status: 200 OK
Connection: close
Content-Type: text/html
Transfer-Encoding: chunked

a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
a
**********
0

Connection closed by foreign host.

dive into WebRick


[105, 114] in /Users/nikushi.nobuhiro/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb
   105:         ensure
   106:           if req.request_line
   107:             if req.keep_alive? && res.keep_alive?
   108:               req.fixup()
   109:             end
=> 110:             res.send_response(sock)
   111:             server.access_log(@config, req, res)
   112:           end
   113:         end
   114:         break if @http_version < "1.1"
(byebug) p res.chunked?
false
(byebug) res.body
"a\r\n**********\r\na\r\n**********\r\na\r\n**********\r\na\r\n**********\r\na\r\n**********\r\na\r\n**********\r\na\r\n**********\r\na\r\n**********\r\na\r\n**********\r\na\r\n**********\r\n0\r\n\r\n"
(byebug) res.body.length
155

Chunkデータではあるが、全て読まれたbodyなのでchunkにしてる意味がない気がする...

RackはどこでChunkデータを生成するのか


[47, 56] in /Users/nikushi.nobuhiro/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/chunked.rb
   47:          STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
   48:          headers['Content-Length'] ||
   49:          headers['Transfer-Encoding']
   50:         [status, headers, body]
   51:       else
=> 52:         headers.delete('Content-Length')
   53:         headers['Transfer-Encoding'] = 'chunked'
   54:         [status, headers, Body.new(body)]
   55:       end
   56:     end

(byebug) p Body.new(body)
#<Rack::Chunked::Body:0x007fcbfad9ea18 @body=#<Rack::BodyProxy:0x007fcbfacaec20 @body=#<Rack::Lint:0x007fcbfaa35f20 @app=#<Proc:0x007fcbfabb4770@/Users/nikushi.nobuhiro/prj/chunk-response-test/config.ru:3 (lambda)>, @content_length=nil, @body=["**********", "**********", "**********", "**********", "**********", "**********", "**********", "**********", "**********", "**********"], @head_request=false>, @block=#<Proc:0x007fcbfacaeb58@/Users/nikushi.nobuhiro/.rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/commonlogger.rb:35>, @closed=false>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.