Class: Gitlab::Middleware::CompressedJson

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/middleware/compressed_json.rb

Constant Summary collapse

COLLECTOR_PATH =
'/api/v4/error_tracking/collector'
MAXIMUM_BODY_SIZE =
200.kilobytes.to_i

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CompressedJson

Returns a new instance of CompressedJson.


9
10
11
# File 'lib/gitlab/middleware/compressed_json.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gitlab/middleware/compressed_json.rb', line 13

def call(env)
  if compressed_et_request?(env)
    input = extract(env['rack.input'])

    if input.length > MAXIMUM_BODY_SIZE
      return too_large
    end

    env.delete('HTTP_CONTENT_ENCODING')
    env['CONTENT_LENGTH'] = input.length
    env['rack.input'] = StringIO.new(input)
  end

  @app.call(env)
end

#compressed_et_request?(env) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
32
33
34
# File 'lib/gitlab/middleware/compressed_json.rb', line 29

def compressed_et_request?(env)
  post_request?(env) &&
    gzip_encoding?(env) &&
    match_content_type?(env) &&
    match_path?(env)
end

#extract(input) ⇒ Object


44
45
46
# File 'lib/gitlab/middleware/compressed_json.rb', line 44

def extract(input)
  Zlib::GzipReader.new(input).read(MAXIMUM_BODY_SIZE + 1)
end

#gzip_encoding?(env) ⇒ Boolean

Returns:

  • (Boolean)

52
53
54
# File 'lib/gitlab/middleware/compressed_json.rb', line 52

def gzip_encoding?(env)
  env['HTTP_CONTENT_ENCODING'] == 'gzip'
end

#match_content_type?(env) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
59
# File 'lib/gitlab/middleware/compressed_json.rb', line 56

def match_content_type?(env)
  env['CONTENT_TYPE'] == 'application/json' ||
    env['CONTENT_TYPE'] == 'application/x-sentry-envelope'
end

#match_path?(env) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
# File 'lib/gitlab/middleware/compressed_json.rb', line 61

def match_path?(env)
  env['PATH_INFO'].start_with?((File.join(relative_url, COLLECTOR_PATH)))
end

#post_request?(env) ⇒ Boolean

Returns:

  • (Boolean)

48
49
50
# File 'lib/gitlab/middleware/compressed_json.rb', line 48

def post_request?(env)
  env['REQUEST_METHOD'] == 'POST'
end

#relative_urlObject


40
41
42
# File 'lib/gitlab/middleware/compressed_json.rb', line 40

def relative_url
  File.join('', Gitlab.config.gitlab.relative_url_root).chomp('/')
end

#too_largeObject


36
37
38
# File 'lib/gitlab/middleware/compressed_json.rb', line 36

def too_large
  [413, { 'Content-Type' => 'text/plain' }, ['Payload Too Large']]
end