mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 19:39:30 +08:00
DEV: Add a user agent to all HTTP requests that Discourse makes. (#31555)
This change standardises the `User-Agent` header that Discourse will send when talking to other sites. `Discourse.user_agent` is now the authority on what the user agent value should be. For Onebox requests, this changes the user agent from their existing value to match the new value (unless overridden). For all other requests, `Net::HTTPHeader` is monkey-patched to add a default `User-Agent` header when one hasn't been provided.
This commit is contained in:
15
lib/freedom_patches/net_http_header.rb
Normal file
15
lib/freedom_patches/net_http_header.rb
Normal file
@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module NetHTTPHeaderPatch
|
||||
def initialize_http_header(initheader)
|
||||
# If no user-agent is set, set it to the default
|
||||
initheader ||= {}
|
||||
user_agent_key =
|
||||
initheader.keys.find { |key| key.to_s.downcase == "user-agent" } || "User-Agent".to_sym
|
||||
initheader[user_agent_key] ||= Discourse.user_agent
|
||||
|
||||
super initheader
|
||||
end
|
||||
end
|
||||
|
||||
Net::HTTPHeader.prepend(NetHTTPHeaderPatch)
|
Reference in New Issue
Block a user