From 739b4a2e0754ad3bdabbf5ebee35744eb587125f Mon Sep 17 00:00:00 2001 From: hukl Date: Wed, 1 Dec 2010 11:35:54 +0100 Subject: [PATCH] =?UTF-8?q?added=20upload=20class=20for=20direct=20file=20?= =?UTF-8?q?uploads=20-=20needs=20some=20care=20later=E2=80=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/rig/http_upload.rb | 49 ++++++++++++++++++++++++++++++++++++++++++ righttp.gemspec | 1 + 2 files changed, 50 insertions(+) create mode 100644 lib/rig/http_upload.rb diff --git a/lib/rig/http_upload.rb b/lib/rig/http_upload.rb new file mode 100644 index 0000000..c93f6b3 --- /dev/null +++ b/lib/rig/http_upload.rb @@ -0,0 +1,49 @@ +require 'uri' +require 'cgi' +require 'socket' + +module Rig + + class Upload + + def initialize options = {} + filename = uri_escape( File.basename( options[:path] ) ) + filesize = File.stat( options[:path] ).size + + @options = options + + @header = [ + "PUT /#{filename} HTTP/1.1", + 'User-Agent: Rig-HTTP', + "Host: #{options[:host]}", + 'Accept: */*', + "Content-Length: #{filesize}", + 'Connection: close', + "\r\n" + ] + + @header = @header.join("\r\n") + end + + def send + begin + tcp_socket = TCPSocket.new( @options[:host], @options[:port] ) + tcp_socket.write( @header + File.open(@options[:path]) {|f| f.read} ) + + response = tcp_socket.read + rescue => excetption + puts exception.message + ensure + tcp_socket.close + end + + HTTPResponse.new( response ) || exception.message + end + + def uri_escape url + URI.escape(CGI.escape(url),'.') + end + + end + +end diff --git a/righttp.gemspec b/righttp.gemspec index 781479e..1499631 100644 --- a/righttp.gemspec +++ b/righttp.gemspec @@ -29,6 +29,7 @@ Gem::Specification.new do |s| "lib/rig/http_exceptions.rb", "lib/rig/http_header.rb", "lib/rig/http_response.rb", + "lib/rig/http_upload.rb", "lib/righttp.rb", "righttp.gemspec", "test/fixtures/yay.gif",