moesif_rack 1.4.17 → 1.4.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b73acd14bb1af6f2d4ee8225fdd39ee19c6d1d9cf937ad37a9b10a7278842272
4
- data.tar.gz: c1e74c3f4afdbdbfe0cef5fc75dca534f8d289c0f0befdac9085b9f48d4575bf
3
+ metadata.gz: 4332e59f946cefea8c61e0e7f40136ba2fd45c4c96de9f133651622c734d62b7
4
+ data.tar.gz: 5f7e29ac2f016a4c97d56a1276aafd42cabedf316cbe0a5a6f82069dcd813f18
5
5
  SHA512:
6
- metadata.gz: 186263e17ebe464783f65297d205c6fea8dcde346ecf979bc1c79c0fd2468e31f474ab278ca94ff2664f6ebd5b9b5a8bc991f00c7f82532ccf709091d6db543e
7
- data.tar.gz: e7c29d009f3dc63464181025aa2679f45ee1b3972c72402934e43fa4fef0e778da300084d03b524f689c1b2dda81f3d16a351deb234c68d06cd01f21bd847111
6
+ metadata.gz: 25b4d5ea39d166c3aee2da057460c2fd9027bd4a7d1a1510e45ec0436890de5ea910b28353f00a034f735d99b7d5c840879e537b3022759216218d6cc351a1bf
7
+ data.tar.gz: d4e0e2014e5b78b1b1ca4be8cc8e10769ebe5d3ed3d6c91f96004587a6e474ac34470af7272da8922ccac5773206a8c44fe4f4d8657bdd90fa3f3fa444f09863
@@ -4,6 +4,7 @@ require 'time'
4
4
  require 'base64'
5
5
  require 'zlib'
6
6
  require 'stringio'
7
+ require 'rack'
7
8
  require_relative './client_ip.rb'
8
9
  require_relative './app_config.rb'
9
10
  require_relative './update_user.rb'
@@ -100,6 +101,24 @@ module MoesifRack
100
101
  end
101
102
  end
102
103
 
104
+ def parse_multipart(multipart_form_data, content_type)
105
+ @moesif_helpers.log_debug("try to parse multiple part #{content_type}")
106
+
107
+ sanitized_multipart_form_data = multipart_form_data.gsub(/\r?\n/, "\r\n")
108
+
109
+ io = StringIO.new(sanitized_multipart_form_data)
110
+ tempfile = Rack::Multipart::Parser::TEMPFILE_FACTORY
111
+ bufsize = Rack::Multipart::Parser::BUFSIZE
112
+ query_parser = Rack::Utils.default_query_parser
113
+ result = Rack::Multipart::Parser.parse(io, sanitized_multipart_form_data.length, content_type, tempfile, bufsize, query_parser)
114
+
115
+ @moesif_helpers.log_debug("multipart parse result")
116
+ @moesif_helpers.log_debug(result.inspect)
117
+
118
+ # this is a hash shold be treated as JSON down the road.
119
+ result.params
120
+ end
121
+
103
122
  def parse_body(body, headers)
104
123
  begin
105
124
  if (body.instance_of?(Hash) || body.instance_of?(Array))
@@ -108,6 +127,9 @@ module MoesifRack
108
127
  elsif start_with_json(body)
109
128
  parsed_body = JSON.parse(body)
110
129
  transfer_encoding = 'json'
130
+ elsif headers.key?('content-type') && ((headers['content-type'].downcase).include? 'multipart/form-data')
131
+ parsed_body = parse_multipart(body, headers['content-type'])
132
+ transfer_encoding = 'json'
111
133
  elsif headers.key?('content-encoding') && ((headers['content-encoding'].downcase).include? "gzip")
112
134
  uncompressed_string = decompress_body(body)
113
135
  parsed_body, transfer_encoding = base64_encode_body(uncompressed_string)
@@ -127,20 +149,20 @@ module MoesifRack
127
149
  begin
128
150
  until @events_queue.empty? do
129
151
  batch_events = []
130
- until batch_events.size == @batch_size || @events_queue.empty? do
152
+ until batch_events.size == @batch_size || @events_queue.empty? do
131
153
  batch_events << @events_queue.pop
132
- end
154
+ end
133
155
  @moesif_helpers.log_debug("Sending #{batch_events.size.to_s} events to Moesif")
134
156
  event_api_response = @api_controller.create_events_batch(batch_events)
135
157
  @event_response_config_etag = event_api_response[:x_moesif_config_etag]
136
158
  @moesif_helpers.log_debug(event_api_response.to_s)
137
159
  @moesif_helpers.log_debug("Events successfully sent to Moesif")
138
160
  end
139
-
161
+
140
162
  if @events_queue.empty?
141
163
  @moesif_helpers.log_debug("No events to read from the queue")
142
164
  end
143
-
165
+
144
166
  sleep @batch_max_time
145
167
  rescue MoesifApi::APIException => e
146
168
  if e.response_code.between?(401, 403)
@@ -177,6 +199,8 @@ module MoesifRack
177
199
  req_headers[new_key] = val
178
200
  end
179
201
 
202
+ # rewind first in case someone else already read the body
203
+ req.body.rewind
180
204
  req_body_string = req.body.read
181
205
  req.body.rewind
182
206
  req_body_transfer_encoding = nil
@@ -229,12 +253,12 @@ module MoesifRack
229
253
  end
230
254
 
231
255
  # Add Transaction Id to the Response Header
232
- if !transaction_id.nil?
256
+ if !transaction_id.nil?
233
257
  rsp_headers["X-Moesif-Transaction-Id"] = transaction_id
234
258
  end
235
259
 
236
260
  # Add Transaction Id to the Repsonse Header sent to the client
237
- if !transaction_id.nil?
261
+ if !transaction_id.nil?
238
262
  headers["X-Moesif-Transaction-Id"] = transaction_id
239
263
  end
240
264
 
@@ -254,7 +278,7 @@ module MoesifRack
254
278
  event_model.request = event_req
255
279
  event_model.response = event_rsp
256
280
  event_model.direction = "Incoming"
257
-
281
+
258
282
  if @identify_user
259
283
  @moesif_helpers.log_debug "calling identify user proc"
260
284
  event_model.user_id = @identify_user.call(env, headers, body)
@@ -285,7 +309,7 @@ module MoesifRack
285
309
  begin
286
310
  random_percentage = Random.rand(0.00..100.00)
287
311
 
288
- begin
312
+ begin
289
313
  sampling_percentage = @app_config.get_sampling_percentage(event_model, @config, event_model.user_id, event_model.company_id)
290
314
  @moesif_helpers.log_debug "Using sample rate #{sampling_percentage}"
291
315
  rescue => exception
@@ -294,7 +318,7 @@ module MoesifRack
294
318
  sampling_percentage = 100
295
319
  end
296
320
 
297
- if sampling_percentage > random_percentage
321
+ if sampling_percentage > random_percentage
298
322
  event_model.weight = @app_config.calculate_weight(sampling_percentage)
299
323
  # Add Event to the queue
300
324
  @events_queue << event_model
@@ -304,7 +328,7 @@ module MoesifRack
304
328
  end
305
329
 
306
330
  if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > (@last_config_download_time + 300)
307
- begin
331
+ begin
308
332
  new_config = @app_config.get_config(@api_controller)
309
333
  if !new_config.nil?
310
334
  @config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
@@ -334,7 +358,7 @@ module MoesifRack
334
358
  end
335
359
 
336
360
  if !should_skip
337
- begin
361
+ begin
338
362
  process_send.call
339
363
  rescue => exception
340
364
  @moesif_helpers.log_debug 'Error while logging event - '
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moesif_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.17
4
+ version: 1.4.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-08-23 00:00:00.000000000 Z
12
+ date: 2023-02-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit