From b36b892c735407bba34502e71e95d7481fc3ad79 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 17 Sep 2008 18:34:15 -0700 Subject: Move http_get_sock into its own function We'll be reusing it in the big file module --- lib/mogilefs/mogilefs.rb | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'lib/mogilefs/mogilefs.rb') diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb index 7ed66bd..fcb33d8 100644 --- a/lib/mogilefs/mogilefs.rb +++ b/lib/mogilefs/mogilefs.rb @@ -76,18 +76,8 @@ class MogileFS::MogileFS < MogileFS::Client next unless path case path when /^http:\/\// then - path = URI.parse(path) - sock = nil begin - timeout @get_file_data_timeout, MogileFS::Timeout do - sock = TCPSocket.new(path.host, path.port) - sock.sync = true - sock.syswrite("GET #{path.request_uri} HTTP/1.0\r\n\r\n") - buf = sock.recv(4096, Socket::MSG_PEEK) - head, body = buf.split(/\r\n\r\n/, 2) - head = sock.recv(head.size + 4) - end - + sock = http_get_sock(URI.parse(path)) return block_given? ? yield(sock) : sock.read rescue MogileFS::Timeout, Errno::ECONNREFUSED, EOFError, SystemCallError @@ -270,5 +260,21 @@ class MogileFS::MogileFS < MogileFS::Client return keys, res['next_after'] end + protected + + def http_get_sock(uri) + sock = nil + timeout @get_file_data_timeout, MogileFS::Timeout do + sock = TCPSocket.new(uri.host, uri.port) + sock.sync = true + sock.syswrite("GET #{uri.request_uri} HTTP/1.0\r\n\r\n") + buf = sock.recv(4096, Socket::MSG_PEEK) + head, body = buf.split(/\r\n\r\n/, 2) + head = sock.recv(head.size + 4) + end + + sock + end # def http_get_sock + end -- cgit v1.2.3-24-ge0c7