Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ruby获取浏览器cookie列表_Ruby_Cookies_Tcpserver - Fatal编程技术网

使用ruby获取浏览器cookie列表

使用ruby获取浏览器cookie列表,ruby,cookies,tcpserver,Ruby,Cookies,Tcpserver,我正在尝试使用TCPServer编写一个小型ruby服务器,但无法从浏览器的响应中获取cookie列表。下面是服务器的代码-它将为除js之外的所有连接提供相同的html require 'socket' server = TCPServer.new('localhost', 2000) class Serveit def initialize(socket, content) @socket = socket @content = content end def

我正在尝试使用TCPServer编写一个小型ruby服务器,但无法从浏览器的响应中获取cookie列表。下面是服务器的代码-它将为除js之外的所有连接提供相同的html

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
js文件内容是一个简单的document.cookie,用于设置cookie。我已经尝试使用CGI获取列表,但我所能做的就是在终端上提示手动插入cookies


我的理解是,您应该捕获包含cookies的浏览器响应头,但我不太清楚如何做到这一点。

要获取cookies,您需要
从套接字获取所有头:

headers = []
while (line = @socket.gets) != "\r\n"
  headers << line
end
headers=[]
而(line=@socket.gets)!=“\r\n”

头文件也可以看到内置的,一种制作小型HTTP服务器的简单方法。但是,如果您的目标是了解HTTP服务器如何工作,那么请继续。您是否需要另一个类似的库来完成大部分工作?