> Ok, brilliant, that all works.
> Here's the new problem ;) :
> The content length never seems to get set on the response.
> This means that when the response checks if it's persistent or not, it
> always reverts to a 'Connection: close' since it checks to see whether
> the response has a length or not (and it never seems to).
> putting this at the top of the method headers_output on response.rb
> seems to solve the problem:
> @headers[CONTENT_LENGTH] = @body.join('').length if @body
> btw, any idea why rack requires the body to be an array?
> Also, do you know how do make the method .eof? on a socket non
> blocking, it seems to block until the socket is actually at eof.
> Thanks
> Alex
> On May 16, 2:47 pm, macournoyer <macourno...@gmail.com> wrote:
> > Hey Alex,
> > What you're doing here is sending 2 requests to Thin at the same time
> > (pipelining them) so Thin replies w/ 2 responses one after the other,
> > like you did w/ the requests. This is called http pipelining:http://en.wikipedia.org/wiki/HTTP_pipelining
> > Keep-alive is just a something that removes the need to close the
> > connection between each request.
> > So if you'd normally do:
> > # 1st request
> > socket = TCPSocket.new('0.0.0.0', 3000)
> > socket.write(...)
> > socket.read
> > socket.close
> > # 2nd request
> > socket = TCPSocket.new('0.0.0.0', 3000)
> > socket.write(...)
> > socket.read
> > socket.close
> > w/ keep-alive, it's the same, w/out the close, but you need to use
> > readpartial since the FIN segment has not been received yet (hm, not
> > sure it's FIN):
> > # 1st request
> > socket = TCPSocket.new('0.0.0.0', 3000)
> > socket.write(...)
> > socket.readpartial(1024)
> > # 2nd request
> > socket.write(...)
> > socket.readpartial(1024)
> > socket.close
> > Let me know if this doesn't works for you.
> > Marc
> > On May 16, 7:31 am, Alex MacCaw <macc...@gmail.com> wrote:
> > > Sorry, that might not be clear (google groups formatting problem).
> > > Here's a slightly cleaner client:
> > > require 'socket'
> > > socket = TCPSocket.new('0.0.0.0', 3000)
> > > params = 'data=123'
> > > socket.write %{POST /rpc HTTP/1.1\r\nContent-Length: #{params.length}\r
> > > \n\r\n#{params}}
> > > socket.flush
> > > socket.write %{POST /rpc HTTP/1.1\r\nContent-Length: #{params.length}\r
> > > \n\r\n#{params}}
> > > response = socket.read
> > > p response
> > > socket.close
> > > On May 16, 11:09 am, Alex MacCaw <macc...@gmail.com> wrote:
> > > > I'm trying to get keep alive support working in Thin (for a JSON RPC
> > > > server)
> > > > The following code is based on Thin's tests:
> > > > require 'socket'
> > > > socket = TCPSocket.new('0.0.0.0', 3000)
> > > > def urlencode(str)
> > > > str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
> > > > end
> > > > params = {:data => %{{"id":1,"method":"test","params":
> > > > [],"version":"0.1"}}}
> > > > data = params.map {|k,v|
> > > > "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }.join('&')
> > > > socket.write %{POST /rpc HTTP/1.1\r\nConnection: keep-alive\r\nContent-
> > > > Type: application/x-www-form-urlencoded\r\nContent-Length:
> > > > #{data.length}\r\n\r\n#{data}}
> > > > socket.flush
> > > > socket.write %{POST /rpc HTTP/1.1\r\nConnection: keep-alive\r\nContent-
> > > > Type: application/x-www-form-urlencoded\r\nContent-Length:
> > > > #{data.length}\r\n\r\n#{data}}
> > > > response = socket.read
> > > > p response
> > > > socket.close
> > > > However, Thin seems to mangle the second request into the first:
> > > > >> @env['rack.input'].read
> > > > => "data=%7b%22id%22%3a1%2c%22method%22%3a%22test%22%2c%22params%22%3a
> > > > %5b%5d%2c%22version%22%3a%220.1%22%7dPOST /rpc HTTP/1.1\r\nConnection:
> > > > keep-alive\r\nContent-Type: application/x-www-form-urlencoded\r
> > > > \nContent-Length: 103\r\n\r\ndata=%7b%22id%22%3a1%2c%22method%22%3a
> > > > %22test%22%2c%22params%22%3a%5b%5d%2c%22version%22%3a%220.1%22%7d"
> > > > Is this expected behavior? Am I doing something stupid?