Skip to content

Commit

Permalink
added upload class for direct file uploads - needs some care later

Browse files Browse the repository at this point in the history
  • Loading branch information
hukl committed Dec 1, 2010
1 parent b434c43 commit 739b4a2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 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
1 change: 1 addition & 0 deletions righttp.gemspec
Expand Up @@ -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",
Expand Down

0 comments on commit 739b4a2

Please sign in to comment.