lib/rodauth/oauth/http_extensions.rb in rodauth-oauth-1.0.0.pre.beta1 vs lib/rodauth/oauth/http_extensions.rb in rodauth-oauth-1.0.0.pre.beta2
- old
+ new
@@ -14,10 +14,13 @@
def http_request(uri, form_data = nil)
uri = URI(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
+ http.open_timeout = 15
+ http.read_timeout = 15
+ http.write_timeout = 15 if http.respond_to?(:write_timeout)
if form_data
request = Net::HTTP::Post.new(uri.request_uri)
request["content-type"] = "application/x-www-form-urlencoded"
request.set_form_data(form_data)
@@ -42,12 +45,22 @@
http_request_cache.set(uri) do
response = http_request(uri, *args)
ttl = if response.key?("cache-control")
cache_control = response["cache-control"]
- cache_control[/max-age=(\d+)/, 1].to_i
+ if cache_control.include?("no-cache")
+ nil
+ else
+ max_age = cache_control[/max-age=(\d+)/, 1].to_i
+ max_age.zero? ? nil : max_age
+ end
elsif response.key?("expires")
- Time.parse(response["expires"]).to_i - Time.now.to_i
+ expires = response["expires"]
+ begin
+ Time.parse(expires).to_i - Time.now.to_i
+ rescue ArgumentError
+ nil
+ end
end
[JSON.parse(response.body, symbolize_names: true), ttl]
end
end