Ruby TCPServer写给客户端的奇怪行为

时间:2016-06-14 22:27:32

标签: ruby tcpserver

我正在创建一个名为EmeraldCutter的Ruby gem,它本质上是一个非常简单的TCP服务器。碰巧我发现了一种非常奇怪的行为,如果有人能给我一些线索,我会很高兴。

正如您可能通过下面的代码看到的,它为每个收到的请求启动一个新线程,抓取发送到服务器的消息,分离其参数并将它们存储在哈希中,然后将具有这些元素的列表写入客户端表单列表中的此哈希值

key => parameter

然后,当我运行此服务器并导航到http://localhost:4321/something时,我的浏览器应该会收到类似

的内容
http_verb => GET
query_string => /something
http_version => HTTP/1.1
host => localhost:4321
connection => keep-alive
cache_control => max-age=0
accept => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
upgrade_insecure_requests => 1
user_agent => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
dnt => 1
accept_encoding => gzip, deflate, sdch
accept_language => pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,fr-CA;q=0.2,fr;q=0.2

但没有显示任何内容,当我访问开发人员控制台上的“网络”标签以检查此请求时,我看到一条消息Failed to load response data

这是我的代码:

LIB / emerald.rb

require "emeraldcutter/auxiliary"
require "emeraldcutter/version"

module EmeraldCutter

  def self.start
    require 'socket'

    server = TCPServer.new 4321
    loop do 
      Thread.start(server.accept) do |client|
        message = client.recvmsg[0]
        request = get_request_parameters(message)
        request.each{ |k,v| client.puts "#{k} => #{v}" }
        client.close
      end
    end
  end

  private

  def self.http_verb(msg_token)
    msg_token.split(' ')[0]
  end

  def self.http_protocol(msg_token)
    msg_token.split(' ')[2]
  end

  def self.query_string(msg_token)
    msg_token.split(' ')[1]
  end

  def self.get_request_parameters(message)
    result = Hash.new
    lines = message.split("\n");
    first_line = lines.shift
    result['http_verb'] = http_verb(first_line)
    result['query_string'] = query_string(first_line)
    result['http_version'] = http_protocol(first_line)
    lines.each{ |line|
      key_value = line.split(': ')
      key = EmeraldCutter::Auxiliary.normalize_hash_key(key_value[0].strip)
      result[key] = key_value[1].strip if not key.empty?
    }
    result
  end

end

LIB / emeraldcutter / auxiliary.rb

module EmeraldCutter
  module Auxiliary

    def self.normalize_hash_key(key)
      key.downcase.tr('-','_')
    end

  end
end

这不会那么奇怪。但是当我将client.puts "Teste"添加到Thread块中时,事情变得非常奇怪

    server = TCPServer.new 4321
    loop do 
      Thread.start(server.accept) do |client|
        client.puts "Test"
        message = client.recvmsg[0]
        request = get_request_parameters(message)
        request.each{ |k,v| client.puts "#{k} => #{v}" }
        client.close
      end
    end
  end

然后它运作得非常好,我得到了

Test
http_verb => GET
query_string => /something
http_version => HTTP/1.1
host => localhost:4321
connection => keep-alive
cache_control => max-age=0
accept => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
upgrade_insecure_requests => 1
user_agent => Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
dnt => 1
accept_encoding => gzip, deflate, sdch
accept_language => pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,fr-CA;q=0.2,fr;q=0.2
你见过这样的东西吗?如果是这样,原因是什么?

0 个答案:

没有答案