Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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


  • Loading branch information...
commit 739b4a2e0754ad3bdabbf5ebee35744eb587125f 1 parent b434c43
@hukl authored
Showing with 50 additions and 0 deletions.
  1. +49 −0 lib/rig/http_upload.rb
  2. +1 −0  righttp.gemspec
View
49 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
View
1  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",
Please sign in to comment.
Something went wrong with that request. Please try again.