Skip to content

Commit

Permalink
Add a user agent so hopefully ops don't block us at the laod balancer.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Aug 20, 2024
1 parent 9932059 commit d7b1fbc
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Style/StringLiterals:
RSpec/MultipleMemoizedHelpers:
Enabled: false

RSpec/MessageChain:
Enabled: false

Layout/EmptyLinesAroundAttributeAccessor:
Enabled: true
Layout/SpaceAroundMethodCallOperator:
Expand Down
5 changes: 4 additions & 1 deletion app/models/iiif_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def initialize(stacks_file:, transformation:, base_uri: Settings.imageserver.bas
def response
with_retries max_tries: 3, rescue: [HTTP::ConnectionError] do
benchmark "Fetch #{image_url}" do
HTTP.timeout(connect: 15).use({ normalize_uri: { normalizer: lambda(&:itself) } }).get(image_url)
HTTP.timeout(connect: 15)
.headers(user_agent: "#{HTTP::Request::USER_AGENT} (#{Settings.user_agent})")
.use({ normalize_uri: { normalizer: lambda(&:itself) } })
.get(image_url)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion app/services/iiif_metadata_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def retrieve
handle_response(
# Disable url normalization as an upstream bug in addressable causes issues for `+`
# https://github.com/sporkmonger/addressable/issues/386
HTTP.use({ normalize_uri: { normalizer: lambda(&:itself) } }).get(@url)
HTTP.timeout(connect: 15)
.headers(user_agent: "#{HTTP::Request::USER_AGENT} (#{Settings.user_agent})")
.use({ normalize_uri: { normalizer: lambda(&:itself) } }).get(@url)
)
end
end
Expand Down
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ cors:

token:
default_expiry_time: <%= 1.hour %>

user_agent: 'stacks.stanford.edu'
12 changes: 6 additions & 6 deletions spec/models/projection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
let(:options) { { size: 'max', region: 'full' } }

it 'allows the user to see the full-resolution image' do
allow(HTTP).to receive(:use).and_return(http_client)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
expect(http_client).to have_received(:get).with(%r{/full/max/0/default.jpg})
Expand All @@ -121,7 +121,7 @@
let(:options) { { size: '!850,700', region: 'full' } }

it 'returns original size when requested dimensions are larger' do
allow(HTTP).to receive(:use).and_return(http_client)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
expect(http_client).to have_received(:get).with(%r{/full/!800,600/0/default.jpg})
Expand All @@ -138,7 +138,7 @@
let(:options) { { size: 'max', region: 'full' } }

it 'limits users to a thumbnail' do
allow(HTTP).to receive(:use)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use)
.and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
Expand All @@ -150,7 +150,7 @@
let(:options) { { size: '!100,100', region: 'full' } }

it 'limits users to a thumbnail' do
allow(HTTP).to receive(:use)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use)
.and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
Expand All @@ -162,7 +162,7 @@
let(:options) { { size: '!800,880', region: 'full' } }

it 'limits users to a thumbnail' do
allow(HTTP).to receive(:use)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use)
.and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
Expand All @@ -174,7 +174,7 @@
let(:options) { { size: '100,100', region: 'square' } }

it 'limits users to a thumbnail' do
allow(HTTP).to receive(:use)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use)
.and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/iiif_auth_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
describe "#show" do
before do
allow_any_instance_of(Projection).to receive(:valid?).and_return(true)
allow(HTTP).to receive(:use).and_return(http_client)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client)
allow(http_client).to receive(:get).and_return(instance_double(HTTP::Response, status: 200, body: StringIO.new))
allow_any_instance_of(IiifController).to receive(:current_user).and_return(current_user)
allow_any_instance_of(IiifController).to receive(:current_image).and_return(current_image)
Expand Down
4 changes: 2 additions & 2 deletions spec/services/iiif_metadata_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
let(:response) { instance_double(HTTP::Response, code: 200, body: json) }

before do
allow(HTTP).to receive(:use).and_return(http_client)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use).and_return(http_client)
allow(http_client).to receive(:get)
.with("https://sul-imageserver-uat.stanford.edu/cantaloupe/iiif/2/#{image_server_path(druid, file_name)}/info.json")
.and_return(response)
Expand Down Expand Up @@ -57,7 +57,7 @@
let(:empty_json) { '' }
let(:bad_response) { instance_double(HTTP::Response, code: 200, body: empty_json) }
before do
allow(HTTP).to receive(:use)
allow(HTTP).to receive_message_chain(:timeout, :headers, :use)
.and_return(http_client)
allow(http_client).to receive(:get)
.with("https://sul-imageserver-uat.stanford.edu/cantaloupe/iiif/2/#{image_server_path(druid, file_name)}/info.json")
Expand Down

0 comments on commit d7b1fbc

Please sign in to comment.