@@ -26,9 +26,7 @@ class HTTPTransport < Transport
2626
2727 def initialize ( *args )
2828 super
29- @endpoint = @dsn . envelope_endpoint
30-
31- log_debug ( "Sentry HTTP Transport will connect to #{ @dsn . server } " )
29+ log_debug ( "Sentry HTTP Transport will connect to #{ @dsn . server } " ) if @dsn
3230 end
3331
3432 def send_data ( data )
@@ -42,12 +40,14 @@ def send_data(data)
4240 headers = {
4341 'Content-Type' => CONTENT_TYPE ,
4442 'Content-Encoding' => encoding ,
45- 'X-Sentry-Auth' => generate_auth_header ,
4643 'User-Agent' => USER_AGENT
4744 }
4845
46+ auth_header = generate_auth_header
47+ headers [ 'X-Sentry-Auth' ] = auth_header if auth_header
48+
4949 response = conn . start do |http |
50- request = ::Net ::HTTP ::Post . new ( @ endpoint, headers )
50+ request = ::Net ::HTTP ::Post . new ( endpoint , headers )
5151 request . body = data
5252 http . request ( request )
5353 end
@@ -69,9 +69,53 @@ def send_data(data)
6969 raise Sentry ::ExternalError , error_info
7070 end
7171 rescue SocketError , *HTTP_ERRORS => e
72+ on_error if respond_to? ( :on_error )
7273 raise Sentry ::ExternalError . new ( e &.message )
7374 end
7475
76+ def endpoint
77+ @dsn . envelope_endpoint
78+ end
79+
80+ def generate_auth_header
81+ return nil unless @dsn
82+
83+ now = Sentry . utc_now . to_i
84+ fields = {
85+ 'sentry_version' => PROTOCOL_VERSION ,
86+ 'sentry_client' => USER_AGENT ,
87+ 'sentry_timestamp' => now ,
88+ 'sentry_key' => @dsn . public_key
89+ }
90+ fields [ 'sentry_secret' ] = @dsn . secret_key if @dsn . secret_key
91+ 'Sentry ' + fields . map { |key , value | "#{ key } =#{ value } " } . join ( ', ' )
92+ end
93+
94+ def conn
95+ server = URI ( @dsn . server )
96+
97+ # connection respects proxy setting from @transport_configuration, or environment variables (HTTP_PROXY, HTTPS_PROXY, NO_PROXY)
98+ # Net::HTTP will automatically read the env vars.
99+ # See https://ruby-doc.org/3.2.2/stdlibs/net/Net/HTTP.html#class-Net::HTTP-label-Proxies
100+ connection =
101+ if proxy = normalize_proxy ( @transport_configuration . proxy )
102+ ::Net ::HTTP . new ( server . hostname , server . port , proxy [ :uri ] . hostname , proxy [ :uri ] . port , proxy [ :user ] , proxy [ :password ] )
103+ else
104+ ::Net ::HTTP . new ( server . hostname , server . port )
105+ end
106+
107+ connection . use_ssl = server . scheme == "https"
108+ connection . read_timeout = @transport_configuration . timeout
109+ connection . write_timeout = @transport_configuration . timeout if connection . respond_to? ( :write_timeout )
110+ connection . open_timeout = @transport_configuration . open_timeout
111+
112+ ssl_configuration . each do |key , value |
113+ connection . send ( "#{ key } =" , value )
114+ end
115+
116+ connection
117+ end
118+
75119 private
76120
77121 def has_rate_limited_header? ( headers )
@@ -136,31 +180,6 @@ def should_compress?(data)
136180 @transport_configuration . encoding == GZIP_ENCODING && data . bytesize >= GZIP_THRESHOLD
137181 end
138182
139- def conn
140- server = URI ( @dsn . server )
141-
142- # connection respects proxy setting from @transport_configuration, or environment variables (HTTP_PROXY, HTTPS_PROXY, NO_PROXY)
143- # Net::HTTP will automatically read the env vars.
144- # See https://ruby-doc.org/3.2.2/stdlibs/net/Net/HTTP.html#class-Net::HTTP-label-Proxies
145- connection =
146- if proxy = normalize_proxy ( @transport_configuration . proxy )
147- ::Net ::HTTP . new ( server . hostname , server . port , proxy [ :uri ] . hostname , proxy [ :uri ] . port , proxy [ :user ] , proxy [ :password ] )
148- else
149- ::Net ::HTTP . new ( server . hostname , server . port )
150- end
151-
152- connection . use_ssl = server . scheme == "https"
153- connection . read_timeout = @transport_configuration . timeout
154- connection . write_timeout = @transport_configuration . timeout if connection . respond_to? ( :write_timeout )
155- connection . open_timeout = @transport_configuration . open_timeout
156-
157- ssl_configuration . each do |key , value |
158- connection . send ( "#{ key } =" , value )
159- end
160-
161- connection
162- end
163-
164183 # @param proxy [String, URI, Hash] Proxy config value passed into `config.transport`.
165184 # Accepts either a URI formatted string, URI, or a hash with the `uri`, `user`, and `password` keys.
166185 # @return [Hash] Normalized proxy config that will be passed into `Net::HTTP`
0 commit comments