Class: HttpStreamingClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/http_streaming_client/client.rb

Constant Summary collapse

ALLOWED_MIME_TYPES =
["application/json", "text/plain", "text/html"]
MAX_RECONNECT_ATTEMPTS =
10
RECONNECT_INTERVAL_SEC =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/http_streaming_client/client.rb', line 58

def initialize(opts = {})
  logger.debug("Client.new: #{opts}")
  @socket = nil
  @interrupted = false

  @compression_requested = opts[:compression].nil? ? true : opts[:compression]
  logger.debug("compression is #{@compression_requested}")

  @reconnect_requested = opts[:reconnect].nil? ? false : opts[:reconnect]
  logger.debug("reconnect is #{@reconnect_requested}")

  if @reconnect_requested then
	@reconnect_count = 0

	@reconnect_attempts = opts[:reconnect_attempts].nil? ? MAX_RECONNECT_ATTEMPTS : opts[:reconnect_attempts]
	logger.debug("reconnect_attempts is #{@reconnect_attempts}")

	@reconnect_interval = opts[:reconnect_interval].nil? ? RECONNECT_INTERVAL_SEC : opts[:reconnect_interval]
	logger.debug("reconnect_interval is #{@reconnect_interval}")

  end

end

Instance Attribute Details

#compression_requestedObject

Returns the value of attribute compression_requested.



43
44
45
# File 'lib/http_streaming_client/client.rb', line 43

def compression_requested
  @compression_requested
end

#interruptedObject

Returns the value of attribute interrupted.



43
44
45
# File 'lib/http_streaming_client/client.rb', line 43

def interrupted
  @interrupted
end

#reconnect_attemptsObject

Returns the value of attribute reconnect_attempts.



43
44
45
# File 'lib/http_streaming_client/client.rb', line 43

def reconnect_attempts
  @reconnect_attempts
end

#reconnect_intervalObject

Returns the value of attribute reconnect_interval.



43
44
45
# File 'lib/http_streaming_client/client.rb', line 43

def reconnect_interval
  @reconnect_interval
end

#reconnect_requestedObject

Returns the value of attribute reconnect_requested.



43
44
45
# File 'lib/http_streaming_client/client.rb', line 43

def reconnect_requested
  @reconnect_requested
end

#socketObject

Returns the value of attribute socket.



43
44
45
# File 'lib/http_streaming_client/client.rb', line 43

def socket
  @socket
end

Class Method Details

.get(uri, opts = {}, &block) ⇒ Object



82
83
84
85
# File 'lib/http_streaming_client/client.rb', line 82

def self.get(uri, opts = {}, &block)
  logger.debug("get:#{uri}")
  self.new.request("GET", uri, opts, &block)
end

.loggerObject



50
51
52
# File 'lib/http_streaming_client/client.rb', line 50

def self.logger
  HttpStreamingClient.logger
end

.post(uri, body, opts = {}, &block) ⇒ Object



97
98
99
100
# File 'lib/http_streaming_client/client.rb', line 97

def self.post(uri, body, opts = {}, &block)
  logger.debug("post:#{uri}")
  self.new.request("POST", uri, opts.merge({:body => body}), &block)
end

Instance Method Details

#get(uri, opts = {}, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/http_streaming_client/client.rb', line 87

def get(uri, opts = {}, &block)
  logger.debug("get(interrupt):#{uri}")
  @interrupted = false
  begin
	request("GET", uri, opts, &block)
  rescue IOError => e
	raise e unless @interrupted
  end
end

#interruptObject



112
113
114
115
116
# File 'lib/http_streaming_client/client.rb', line 112

def interrupt
  logger.debug("interrupt")
  @interrupted = true
  @socket.close unless @socket.nil?
end

#loggerObject



54
55
56
# File 'lib/http_streaming_client/client.rb', line 54

def logger
  HttpStreamingClient.logger
end

#post(uri, body, opts = {}, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/http_streaming_client/client.rb', line 102

def post(uri, body, opts = {}, &block)
  logger.debug("post(interrupt):#{uri}")
  @interrupted = false
  begin
	request("POST", uri, opts.merge({:body => body}), &block)
  rescue IOError => e
	raise e unless @interrupted
  end
end

#request(method, uri, opts = {}, &block) ⇒ Object



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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
208
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
258
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
294
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/http_streaming_client/client.rb', line 118

def request(method, uri, opts = {}, &block)

  logger.debug("Client::request:#{method}:#{uri}:#{opts}")

  if uri.is_a?(String)
	uri = URI.parse(uri)
  end

  default_headers = {
	"User-Agent" => opts["User-Agent"] || "HttpStreamingClient #{HttpStreamingClient::VERSION}",
	"Accept" => "*/*",
	"Accept-Charset" => "utf-8"
  }

  if method == "POST" || method == "PUT"
	default_headers["Content-Type"] = opts["Content-Type"] || "application/x-www-form-urlencoded;charset=UTF-8"
	body = opts.delete(:body)
	if body.is_a?(Hash)
	  body = body.keys.collect {|param| "#{URI.escape(param.to_s)}=#{URI.escape(body[param].to_s)}"}.join('&')
	end
	default_headers["Content-Length"] = body.length
  end

  unless uri.userinfo.nil?
	default_headers["Authorization"] = "Basic #{[uri.userinfo].pack('m').strip!}\r\n"
  end

  encodings = []
  encodings << "gzip" if (@compression_requested and opts[:compression].nil?) or opts[:compression]
  if encodings.any?
	default_headers["Accept-Encoding"] = "#{encodings.join(',')}"
  end

  headers = default_headers.merge(opts[:headers] || {})
  logger.debug "request headers: #{headers}"

  begin

	socket = initialize_socket(uri, opts)

	@reconnect_count = 0 if @reconnect_requested

	request = "#{method} #{uri.path}#{uri.query ? "?"+uri.query : nil} HTTP/1.1\r\n"
	request << "Host: #{uri.host}\r\n"
	headers.each do |k, v|
	  request << "#{k}: #{v}\r\n"
	end
	request << "\r\n"
	if method == "POST"
	  request << body
	end

	socket.write(request)

	response_head = {}
	response_head[:headers] = {}

	socket.each_line do |line|
	  if line == "\r\n" then
 break
	  else
 header = line.split(": ")
 if header.size == 1
   header = header[0].split(" ")
   response_head[:version] = header[0]
   response_head[:code] = header[1].to_i
   response_head[:msg] = header[2]
   logger.debug "HTTP response code is #{response_head[:code]}"
 else
   response_head[:headers][camelize_header_name(header[0])] = header[1].strip
 end
	  end
	end

	logger.debug "response headers:#{response_head[:headers]}"

	if response_head[:code] == 301 then
	  location = response_head[:headers]["Location"]
	  raise InvalidRedirect, "Unable to find Location header for HTTP 301 response" if location.nil?
	  logger.warn "Received HTTP 301 redirect to #{location}, following..."
	  socket.close if !socket.nil? and !socket.closed?
	  opts.delete(:socket)
	  return request(method, location, opts, &block)
	end

	content_length = response_head[:headers]["Content-Length"].to_i
	logger.debug "content-length: #{content_length}"

	content_type = response_head[:headers]["Content-Type"].split(';').first
	logger.debug "content-type: #{content_type}"

	response_compression = false

	if ALLOWED_MIME_TYPES.include?(content_type)
	  case response_head[:headers]["Content-Encoding"]
	  when "gzip"
 response_compression = true
	  end
	else
	  raise InvalidContentType, "invalid response MIME type: #{content_type}"
	end

	if (response_head[:code] != 200)
	  s = "Received HTTP #{response_head[:code]} response"
	  logger.debug "request: #{request}"
	  response = socket.read(content_length)
	  logger.debug "response: #{response}"
	  raise HttpError.new(response_head[:code], "Received HTTP #{response_head[:code]} response", response_head[:headers], response)
	end

	if response_head[:headers]["Transfer-Encoding"] == 'chunked'
	  partial = nil
	  decoder = nil
	  response = ""

	  if response_compression then
 logger.debug "response compression detected"
 if block_given? then
   decoder = HttpStreamingClient::Decoders::GZip.new { |line|
		logger.debug "read #{line.size} uncompressed bytes, decoder queue bytes:#{decoder.size}"
		block.call(line) unless @interrupted }
 else
   decoder = HttpStreamingClient::Decoders::GZip.new { |line|
		logger.debug "read #{line.size} uncompressed bytes, #{response.size} bytes total, decoder queue bytes:#{decoder.size}"
		response << line unless @interrupted }
 end
	  end

	  while !socket.eof? && (line = socket.gets)
 chunkLeft = 0

 if line.match /^0\r\n/ then
   logger.debug "received zero length chunk, chunked encoding EOF"
   logger.debug "EOF line: #{line}"
   break
 end

 next if line == "\r\n"

 size = line.hex
 logger.debug "chunk size:#{size}"

 partial = socket.read(size)
 next if partial.nil?

 remaining = size-partial.size
 logger.debug "read #{partial.size} bytes, #{remaining} bytes remaining"
 until remaining == 0
   partial << socket.read(remaining)
   remaining = size-partial.size
   logger.debug "read #{partial.size} bytes, #{remaining} bytes remaining"
 end

 if response_compression then
   return if @interrupted
   decoder << partial
 else
   if block_given? then
		yield partial
   else
     return response if @interrupted
		logger.debug "no block specified, returning chunk results and halting streaming response"
		response << partial
		return response
   end
 end
	  end

	  logger.debug "socket EOF detected" if socket.eof?
	  raise ReconnectRequest if @reconnect_requested
	  return response

	else
	  # Not chunked transfer encoding, but potentially gzip'd, and potentially streaming with content-length = 0

	  if content_length > 0 then
 bits = socket.read(content_length)
 logger.debug "read #{content_length} bytes"
 return bits if !response_compression
 logger.debug "response compression detected"
 begin
   decoder = Zlib::GzipReader.new(StringIO.new(bits))
   return decoder.read
 rescue Zlib::Error
   raise DecoderError
 end
	  end

	  if response_compression then

 logger.debug "response compression detected"
 decoder = nil
 response = ""

 if block_given? then
   decoder = HttpStreamingClient::Decoders::GZip.new { |line|
		logger.debug "read #{line.size} uncompressed bytes, decoder queue bytes:#{decoder.size}"
		block.call(line) unless @interrupted }
 else
   decoder = HttpStreamingClient::Decoders::GZip.new { |line|
		logger.debug "read #{line.size} uncompressed bytes, #{response.size} bytes total, decoder queue bytes:#{decoder.size}"
		response << line unless @interrupted }
 end

 while (!socket.eof? and !(line = socket.read_nonblock(2048)).nil?)
   logger.debug "read compressed line, #{line.size} bytes"
   decoder << line
   break response if @interrupted
 end

 logger.debug "EOF detected"
 raise ReconnectRequest if @reconnect_requested
 return response

	  else

 response = ""

 while (!socket.eof? and !(line = socket.readline).nil?)
   if block_given? then
		yield line
		logger.debug "read #{line.size} bytes"
   else
		logger.debug "read #{line.size} bytes, #{response.size} bytes total"
		response << line
   end
   break if @interrupted
 end

 raise ReconnectRequest if @reconnect_requested
 return response

	  end
	end
  rescue => e
	return if @interrupted
	logger.error "Error Detected: #{e}" unless e.instance_of? ReconnectRequest
	decoder.close if !decoder.nil?
	socket.close if !socket.nil? and !socket.closed?
	opts.delete(:socket)

	if @reconnect_requested then
	  logger.info "Connection closed. Reconnect requested. Trying..."
	  @reconnect_count = @reconnect_count + 1
	  sleep @reconnect_interval
	  retry if @reconnect_count < @reconnect_attempts
	  logger.info "Maximum number of failed reconnect attempts reached (#{@reconnect_attempts}). Exiting."
	end
	
	raise e unless e.instance_of? ReconnectRequest
  end
ensure
  logger.debug "ensure socket closed"
  decoder.close if !decoder.nil?
  socket.close if !socket.nil? and !socket.closed?
  opts.delete(:socket)
end