-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_upload.rb
49 lines (37 loc) · 992 Bytes
/
http_upload.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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