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"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



55
56
57
58
59
60
61
# File 'lib/http_streaming_client/client.rb', line 55

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}")
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

#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



63
64
65
66
# File 'lib/http_streaming_client/client.rb', line 63

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

.loggerObject



47
48
49
# File 'lib/http_streaming_client/client.rb', line 47

def self.logger
  HttpStreamingClient.logger
end

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



78
79
80
81
# File 'lib/http_streaming_client/client.rb', line 78

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



68
69
70
71
72
73
74
75
76
# File 'lib/http_streaming_client/client.rb', line 68

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



93
94
95
96
97
# File 'lib/http_streaming_client/client.rb', line 93

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

#loggerObject



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

def logger
  HttpStreamingClient.logger
end

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



83
84
85
86
87
88
89
90
91
# File 'lib/http_streaming_client/client.rb', line 83

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
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
# File 'lib/http_streaming_client/client.rb', line 99

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}"

  socket = initialize_socket(uri, opts)
  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]}"

  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])
  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"
   block.call(line) unless @interrupted }
	  else
 decoder = HttpStreamingClient::Decoders::GZip.new { |line|
   logger.debug "read #{line.size} uncompressed bytes, #{response.size} bytes total"
   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"
 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

	  return if @interrupted

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

	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"
   block.call(line) unless @interrupted }
	  else
 decoder = HttpStreamingClient::Decoders::GZip.new { |line|
   logger.debug "read #{line.size} uncompressed bytes, #{response.size} bytes total"
   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"
	  decoder = nil

	  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

	  return response

	end
  end
ensure
  logger.debug "ensure socket closed"
  decoder.close if !decoder.nil?
  socket.close if !socket.nil? and !socket.closed?
end