File: transfer_encoding.rb

package info (click to toggle)
ruby-aws-sdk-core 3.212.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,232 kB
  • sloc: ruby: 17,533; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 1,682 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

module Aws
  module Plugins

    # For Streaming Input Operations, when `requiresLength` is enabled
    # checking whether `Content-Length` header can be set,
    # for `unsignedPayload` and `v4-unsigned-body` operations,
    # set `Transfer-Encoding` header.
    class TransferEncoding < Seahorse::Client::Plugin

      # @api private
      class Handler < Seahorse::Client::Handler
        def call(context)
          if streaming?(context.operation.input)
            # If it's an IO object and not a File / String / String IO
            unless context.http_request.body.respond_to?(:size)
              if requires_length?(context.operation.input)
                # if size of the IO is not available but required
                raise Aws::Errors::MissingContentLength
              elsif unsigned_payload?(context.operation)
                context.http_request.headers['Transfer-Encoding'] = 'chunked'
              end
            end
          end

          @handler.call(context)
        end

        private

        def streaming?(ref)
          if (payload = ref[:payload_member])
            payload['streaming'] || payload.shape['streaming']
          else
            false
          end
        end

        def unsigned_payload?(operation)
          operation['unsignedPayload'] ||
            operation['authtype'] == 'v4-unsigned-body'
        end

        def requires_length?(ref)
          if (payload = ref[:payload_member])
            payload['requiresLength'] || payload.shape['requiresLength']
          else
            false
          end
        end

      end

      handler(Handler, step: :sign)

    end

  end
end