Skip to content

Commit 27170f6

Browse files
authored
fix: Use content_type.mime_type instead of direct header access (#36)
In some environments, retrieving the `content-type` header directly from the headers hash was returning an empty string. In those environments, the header was stored as `Content-Type`. By updating the code to use the provided `content_type` method provided on the response object, we can shield ourselves from casing issues like this in the future.
1 parent 33b5ee4 commit 27170f6

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/ld-eventsource/client.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Client
8585
# if you want to use something other than the default `TCPSocket`; it must implement
8686
# `open(uri, timeout)` to return a connected `Socket`
8787
# @yieldparam [Client] client the new client instance, before opening the connection
88-
#
88+
#
8989
def initialize(uri,
9090
headers: {},
9191
connect_timeout: DEFAULT_CONNECT_TIMEOUT,
@@ -107,7 +107,7 @@ def initialize(uri,
107107
if socket_factory
108108
http_client_options["socket_class"] = socket_factory
109109
end
110-
110+
111111
if proxy
112112
@proxy = proxy
113113
else
@@ -202,12 +202,12 @@ def closed?
202202
end
203203

204204
private
205-
205+
206206
def reset_http
207207
@http_client.close if !@http_client.nil?
208208
close_connection
209209
end
210-
210+
211211
def close_connection
212212
@lock.synchronize do
213213
@cxn.connection.close if !@cxn.nil?
@@ -258,7 +258,7 @@ def connect
258258
interval = @first_attempt ? 0 : @backoff.next_interval
259259
@first_attempt = false
260260
if interval > 0
261-
@logger.info { "Will retry connection after #{'%.3f' % interval} seconds" }
261+
@logger.info { "Will retry connection after #{'%.3f' % interval} seconds" }
262262
sleep(interval)
263263
end
264264
cxn = nil
@@ -268,14 +268,14 @@ def connect
268268
headers: build_headers
269269
})
270270
if cxn.status.code == 200
271-
content_type = cxn.headers["content-type"]
271+
content_type = cxn.content_type.mime_type
272272
if content_type && content_type.start_with?("text/event-stream")
273273
return cxn # we're good to proceed
274274
else
275275
reset_http
276-
err = Errors::HTTPContentTypeError.new(cxn.headers["content-type"])
276+
err = Errors::HTTPContentTypeError.new(content_type)
277277
@on[:error].call(err)
278-
@logger.warn { "Event source returned unexpected content type '#{cxn.headers["content-type"]}'" }
278+
@logger.warn { "Event source returned unexpected content type '#{content_type}'" }
279279
end
280280
else
281281
body = cxn.to_s # grab the whole response body in case it has error details
@@ -309,7 +309,7 @@ def read_stream(cxn)
309309
# readpartial gives us a string, which may not be a valid UTF-8 string because a
310310
# multi-byte character might not yet have been fully read, but BufferedLineReader
311311
# will handle that.
312-
rescue HTTP::TimeoutError
312+
rescue HTTP::TimeoutError
313313
# For historical reasons, we rethrow this as our own type
314314
raise Errors::ReadTimeoutError.new(@read_timeout)
315315
end
@@ -344,7 +344,7 @@ def log_and_dispatch_error(e, message)
344344
@logger.warn { "#{message}: #{e.inspect}"}
345345
@logger.debug { "Exception trace: #{e.backtrace}" }
346346
begin
347-
@on[:error].call(e)
347+
@on[:error].call(e)
348348
rescue StandardError => ee
349349
@logger.warn { "Error handler threw an exception: #{ee.inspect}"}
350350
@logger.debug { "Exception trace: #{ee.backtrace}" }

0 commit comments

Comments
 (0)