Module: Watobo::HTTPSocket
- Defined in:
- lib/watobo/sockets/agent.rb,
lib/watobo/sockets/ntlm_auth.rb,
lib/watobo/sockets/connection.rb,
lib/watobo/sockets/http_socket.rb,
lib/watobo/sockets/client_socket.rb
Overview
Defined Under Namespace
Modules: NTLMAuth
Classes: Agent_UNUSED, ClientSocket, ClientSocket_ORIG, Connection_UNUSED
Class Method Summary
collapse
Class Method Details
.close(socket) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/watobo/sockets/http_socket.rb', line 13
def self.close(socket)
begin
if socket.respond_to? :sysclose
socket.sysclose
elsif socket.respond_to? :shutdown
socket.shutdown(Socket::SHUT_RDWR)
end
if socket.respond_to? :close
socket.close
end
return true
rescue => bang
puts bang
puts bang.backtrace if $DEBUG
end
false
end
|
.get_peer_subject(socket) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/watobo/sockets/http_socket.rb', line 148
def self.get_peer_subject(socket)
begin
ctx = OpenSSL::SSL::SSLContext.new()
ctx.tmp_dh_callback = proc { |*args|
OpenSSL::PKey::DH.new(128)
}
ssl_sock = OpenSSL::SSL::SSLSocket.new(socket, ctx)
subject = ssl_sock.peer_cert.subject
return subject
rescue => bang
puts bang
puts bang.backtrace
end
return nil
end
|
.get_ssl_cert_cn(host, port) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/watobo/sockets/http_socket.rb', line 117
def self.get_ssl_cert_cn( host, port)
cn = ""
begin
tcp_socket = TCPSocket.new( host, port )
tcp_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
tcp_socket.sync = true
ctx = OpenSSL::SSL::SSLContext.new()
ctx.tmp_dh_callback = proc { |*args|
OpenSSL::PKey::DH.new(128)
}
socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ctx)
socket.connect
cert = socket.peer_cert
if cert.subject.to_s =~ /cn=([^\/]*)/i
cn = $1
end
puts "Peer-Cert CN: #{cn}"
socket.io.shutdown(2)
rescue => bang
puts bang
cn = host
ensure
socket.close if socket.respond_to? :close
end
cn
end
|
.read_body(socket, prefs = nil) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/watobo/sockets/http_socket.rb', line 164
def self.read_body(socket, prefs=nil)
buf = nil
max_bytes = -1
unless prefs.nil?
max_bytes = prefs[:max_bytes] unless prefs[:max_bytes].nil?
end
bytes_to_read = max_bytes >= 0 ? max_bytes : 1024
bytes_read = 0
while max_bytes < 0 or bytes_to_read > 0
begin
buf = socket.readpartial(bytes_to_read)
bytes_read += buf.length
rescue EOFError
if $DEBUG
puts "#{buf.class} - #{buf}"
end
break
rescue Timeout::Error
puts "!!! Timeout: read_body (max_bytes=#{max_bytes})"
puts $!.backtrace if $DEBUG
break
rescue => bang
print "E!"
puts bang.backtrace if $DEBUG
break
end
break if buf.nil?
yield buf if block_given?
break if max_bytes >= 0 and bytes_read >= max_bytes
bytes_to_read -= bytes_read if max_bytes >= 0 && bytes_to_read >= bytes_read
end
return
end
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
# File 'lib/watobo/sockets/http_socket.rb', line 295
def self.(socket)
buf = ''
while true
begin
buf = socket.gets
rescue EOFError => e
puts "EOFError: #{e}"
return true
rescue Errno::ECONNRESET => e
puts "ECONNRESET: #{e}"
return false
rescue Errno::ECONNABORTED => e
puts "ECONNABORTED: #{e}"
return false
rescue Timeout::Error => e
puts "TIMEOUT: #{e}"
return false
rescue => bang
puts bang
puts bang.backtrace
raise
end
return false if buf.nil?
yield buf if block_given?
return if buf.strip.empty?
end
end
|
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/watobo/sockets/http_socket.rb', line 259
def self.(socket)
buf = ''
while true
begin
buf = socket.gets
rescue EOFError
puts "!!! EOF: reading header"
return
rescue Errno::ECONNRESET
raise
rescue Errno::ECONNABORTED
raise
rescue Timeout::Error
raise
rescue => bang
puts bang
puts bang.backtrace
raise
end
return if buf.nil?
yield buf if block_given?
return if buf.strip.empty?
end
end
|
.readChunkedBody(socket, &block) ⇒ Object
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/watobo/sockets/http_socket.rb', line 209
def self.readChunkedBody(socket, &block)
buf = nil
while (chunk_size = socket.gets)
if chunk_size.strip.empty?
yield chunk_size
next
end
next unless chunk_size.strip =~/^[a-fA-F0-9]+$/
yield "#{chunk_size.strip}\n" if block_given?
bytes_to_read = num_bytes = chunk_size.strip.hex
return if num_bytes == 0
bytes_read = 0
while bytes_read < num_bytes
begin
bytes_to_read = num_bytes - bytes_read
buf = socket.readpartial(bytes_to_read)
bytes_read += buf.length
rescue EOFError
return
rescue Timeout::Error
puts "!!! Timeout: readChunkedBody (bytes_to_read=#{bytes_to_read}"
return
rescue => bang
print "E!"
return
end
yield buf if block_given?
end
yield "\r\n" if block_given?
end
end
|
.siteAlive?(chat) ⇒ Boolean
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/watobo/sockets/http_socket.rb', line 37
def self.siteAlive?(chat)
site = nil
host = nil
port = nil
site = chat.request.site
proxy = Watobo::ForwardingProxy.get site
unless proxy.nil?
Watobo.print_debug("Using Proxy","#{proxy.to_yaml}") if $DEBUG
puts "* testing proxy connection:"
puts "#{proxy.name} (#{proxy.host}:#{proxy.port})"
host = proxy.host
port = proxy.port
else
print "* check if site is alive (#{site}) ... "
host = chat.request.host
port = chat.request.port
end
return false if host.nil? or port.nil?
begin
tcp_socket = nil
tcp_socket = TCPSocket.new( host, port)
tcp_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
tcp_socket.sync = true
socket = tcp_socket
if socket.class.to_s =~ /SSLSocket/
socket.io.shutdown(2)
else
socket.shutdown(2)
end
socket.close
print "[OK]\n"
return true
rescue Errno::ECONNREFUSED
p "* connection refused (#{host}:#{port})"
rescue Errno::ECONNRESET
puts "* connection reset"
rescue Errno::EHOSTUNREACH
p "* host unreachable (#{host}:#{port})"
rescue Timeout::Error
p "* TimeOut (#{host}:#{port})\n"
rescue Errno::ETIMEDOUT
p "* TimeOut (#{host}:#{port})"
rescue Errno::ENOTCONN
puts "!!!ENOTCONN"
rescue OpenSSL::SSL::SSLError
p "* ssl error"
socket = nil
print "E"
rescue => bang
puts bang
puts bang.backtrace if $DEBUG
end
print "[FALSE]\n"
return false
end
|