Getting list of browser cookies with ruby
<p>I'm trying to code a tiny ruby server using TCPServer but can't get the hang of getting the list of cookies from the browser's response. Below is the code for the server - it will serve the same html for all connections except js.</p>

<pre><code>require 'socket'
server = TCPServer.new('localhost', 2000)

class Serveit
  def initialize(socket, content)
    @socket = socket
    @content = content
  end

  def content
    # @content
    @content = File.read('./partials/header.html.erb')
  end

  def socket
    @socket
  end

  def url
    @url = @socket.gets.split(' ')[1]
  end

  def request
    @socket.gets
  end
end

loop do
  s = Serveit.new(server.accept, rand(1..100000000).to_s)

  if s.url == "/js"
    content = File.read('app.js').to_s
    s.socket.print "HTTP/1.1 200 OK\r\n" + "Content-Type: application/javascript\r\n" + "Content-Length: #{content.bytesize}\r\n" + "Connection: close\r\n"
    s.socket.print "\r\n"
    s.socket.print content
    s.socket.close
  else
    s.socket.print "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Content-Length: #{s.content.bytesize}\r\n" + "Connection: close\r\n"
    s.socket.print "\r\n"
    s.socket.print s.content
    s.socket.close
  end
end
</code></pre>

<p>The .js file contents is a simple document.cookie to set a cookie. I've tried getting the list using CGI but all i can manage is a a prompt on the terminal to manualy insert cookies.</p>

<p>My understanding is that you're supposed to capture the browser response headers that will include the cookies but i can't quite figure out how to do that.</p>