2

今、私はこのコードでユニコーンによって作成された私のソケットに接続しようとします

require 'socket'

def foo
  socket = UNIXSocket.new("path_to_socket/tmp/unicorn.sock")

  data = "GET /time HTTP/1.1\n" 
  data << "Connection: Close\n" 
  data << "User-Agent: Mozilla/5.0\n" 
  data << "Accept: */*\n" 
  data << "Content-Type: application/x-www-form-urlencoded\n" 
  data << "\n\r\n\r"

  socket.puts(data)

  while(line = socket.gets) do
    puts line
  end 
end

foo

ただし、常に「HTTP / 1.1400BadRequest」を取得してください

どうか、私が間違っていることを誰もが言うことができますか?

4

2 に答える 2

5

ネット/httpを使用...

require "net/http"
require "socket"

sock = Net::BufferedIO.new(UNIXSocket.new("path_to_socket/tmp/unicorn.sock"))
request = Net::HTTP::Get.new("/time")
request.exec(sock, "1.1", "/time")

begin
  response = Net::HTTPResponse.read_new(sock)
end while response.kind_of?(Net::HTTPContinue)
response.reading_body(sock, request.response_body_permitted?) { }

response.body
response.code
于 2013-03-26T02:55:14.680 に答える
2

これは非常に役立ちますが、Net :: HTTP#execメソッドは内部使用のみにマークされていることに注意してください。リソース管理などを行っていないためと思われます。

次の作業は、提案された戦略を適応させて、Net :: HTTP#connectをオーバーライドします(ソケットに接続するため)。HTTPリクエストの処理にHTTPartygemを使用するのが好きです。したがって、ここでの戦略は、HTTParty用のカスタムConnectionAdaptorを利用します。これで、インクルードクラスの:: default_params =呼び出しを変更して、UnixまたはTCP/HTTPソケットのどちらを使用しているかを制御できます。

###########################################################
#  net/socket_http.rb
###########################################################

module Net
  #  Overrides the connect method to simply connect to a unix domain socket.
  class SocketHttp < HTTP
    attr_reader :socket_path

    #  URI should be a relative URI giving the path on the HTTP server.
    #  socket_path is the filesystem path to the socket the server is listening to.
    def initialize(uri, socket_path)
      @socket_path = socket_path
      super(uri)
    end

    #  Create the socket object.
    def connect
      @socket = Net::BufferedIO.new UNIXSocket.new socket_path
      on_connect
    end

    #  Override to prevent errors concatenating relative URI objects.
    def addr_port
      File.basename(socket_path)
    end
  end
end


###########################################################
#  sock_party.rb, a ConnectionAdapter class
###########################################################
require "net/http"
require "socket"

class SockParty < HTTParty::ConnectionAdapter
  #  Override the base class connection method.
  #  Only difference is that we'll create a Net::SocketHttp rather than a Net::HTTP.
  #  Relies on :socket_path in the
  def connection
    http = Net::SocketHttp.new(uri, options[:socket_path])

    if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float))
      http.open_timeout = options[:timeout]
      http.read_timeout = options[:timeout]
    end

    if options[:debug_output]
      http.set_debug_output(options[:debug_output])
    end

    if options[:ciphers]
      http.ciphers = options[:ciphers]
    end

    return http
  end
end


###########################################################
#  class MockSockParty, a really *nix-y HTTParty
###########################################################
class MockSockParty
  include HTTParty
  self.default_options = {connection_adapter: SockParty, socket_path: '/tmp/thin.sock'}

  def party_hard
    self.class.get('/client').body
  end
end

###########################################################
#  sock_party_spec.rb
###########################################################

require 'spec_helper'

describe SockParty do
  it "should party until its socks fall off." do
    puts MockSockParty.new.party_hard
  end
end
于 2013-11-28T12:50:13.340 に答える