cloudbridge 0.9.3 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cloudserver.rb +53 -48
- metadata +1 -1
data/lib/cloudserver.rb
CHANGED
@@ -20,58 +20,63 @@ module CloudBridge
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def accept()
|
23
|
-
# Connect to the cloudbridge and let it know we're available for a connection.
|
24
|
-
# This is all entirely syncronous.
|
25
23
|
begin
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
socket.write("Host: #{host}\r\n")
|
35
|
-
}
|
36
|
-
@listen_keys.each {|key|
|
37
|
-
socket.write("Host-Key: #{key}\r\n")
|
38
|
-
}
|
39
|
-
socket.write("\r\n")
|
40
|
-
code = nil
|
41
|
-
name = nil
|
42
|
-
headers = []
|
43
|
-
while (line = socket.gets())
|
44
|
-
line = line.strip
|
45
|
-
if (line == "")
|
46
|
-
case code.to_i
|
47
|
-
when 100 # 100 Continue, just a ping. Ignore.
|
48
|
-
code = name = nil
|
49
|
-
headers = []
|
50
|
-
next
|
51
|
-
when 101 # 101 Upgrade, successfuly got a connection.
|
52
|
-
socket.write("HTTP/1.1 100 Continue\r\n\r\n") # let the server know we're still here.
|
53
|
-
return socket
|
54
|
-
when 504 # 504 Gateway Timeout, just retry.
|
55
|
-
socket.close()
|
56
|
-
return accept()
|
57
|
-
else
|
58
|
-
raise "HTTP BRIDGE error #{code}: #{name} waiting for connection."
|
59
|
-
end
|
24
|
+
# Connect to the cloudbridge and let it know we're available for a connection.
|
25
|
+
# This is all entirely syncronous.
|
26
|
+
begin
|
27
|
+
socket = TCPSocket.new(@cloud_host, @cloud_port)
|
28
|
+
@sockopts.each {|opt| socket.setsockopt(*opt) }
|
29
|
+
rescue Errno::ECONNREFUSED
|
30
|
+
sleep(0.5)
|
31
|
+
retry
|
60
32
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
33
|
+
socket.write("BRIDGE / HTTP/1.1\r\n")
|
34
|
+
@listen_hosts.each {|host|
|
35
|
+
socket.write("Host: #{host}\r\n")
|
36
|
+
}
|
37
|
+
@listen_keys.each {|key|
|
38
|
+
socket.write("Host-Key: #{key}\r\n")
|
39
|
+
}
|
40
|
+
socket.write("\r\n")
|
41
|
+
code = nil
|
42
|
+
name = nil
|
43
|
+
headers = []
|
44
|
+
while (line = socket.gets())
|
45
|
+
line = line.strip
|
46
|
+
if (line == "")
|
47
|
+
case code.to_i
|
48
|
+
when 100 # 100 Continue, just a ping. Ignore.
|
49
|
+
code = name = nil
|
50
|
+
headers = []
|
51
|
+
next
|
52
|
+
when 101 # 101 Upgrade, successfuly got a connection.
|
53
|
+
socket.write("HTTP/1.1 100 Continue\r\n\r\n") # let the server know we're still here.
|
54
|
+
return socket
|
55
|
+
when 503, 504 # 503 Service Unavailable or 504 Gateway Timeout, just retry.
|
56
|
+
socket.close()
|
57
|
+
sleep_time = headers.find {|header| header["Retry-After"] } || 5
|
58
|
+
sleep(sleep_time)
|
59
|
+
puts("BRIDGE server timed out or is overloaded, waiting #{sleep_time}s to try again.")
|
60
|
+
retry
|
61
|
+
else
|
62
|
+
raise "HTTP BRIDGE error #{code}: #{name} waiting for connection."
|
63
|
+
end
|
69
64
|
end
|
70
|
-
|
71
|
-
if (
|
72
|
-
|
65
|
+
|
66
|
+
if (!code && !name) # This is the initial response line
|
67
|
+
if (match = line.match(%r{^HTTP/1\.[01] ([0-9]{3,3}) (.*)$}))
|
68
|
+
code = match[1]
|
69
|
+
name = match[2]
|
70
|
+
next
|
71
|
+
else
|
72
|
+
raise "Parse error in BRIDGE request reply."
|
73
|
+
end
|
73
74
|
else
|
74
|
-
|
75
|
+
if (match = line.match(%r{^(.+?):\s+(.+)$}))
|
76
|
+
headers.push({match[1] => match[2]})
|
77
|
+
else
|
78
|
+
raise "Parse error in BRIDGE request reply's headers."
|
79
|
+
end
|
75
80
|
end
|
76
81
|
end
|
77
82
|
end
|