File: multipartable.rb

package info (click to toggle)
ruby-multipart-post 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144 kB
  • sloc: ruby: 247; makefile: 4
file content (70 lines) | stat: -rw-r--r-- 2,254 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
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2008, by McClain Looney.
# Copyright, 2008-2013, by Nick Sieger.
# Copyright, 2011, by Gerrit Riessen.
# Copyright, 2013, by Vincent Pellé.
# Copyright, 2013, by Gustav Ernberg.
# Copyright, 2013, by Socrates Vicente.
# Copyright, 2013, by Steffen Grunwald.
# Copyright, 2019, by Olle Jonsson.
# Copyright, 2019-2024, by Samuel Williams.
# Copyright, 2019, by Patrick Davey.
# Copyright, 2022, by Jason York.

require_relative 'parts'
require_relative 'composite_read_io'

require 'securerandom'

module Multipart
  module Post
    module Multipartable
      def self.secure_boundary
        # https://tools.ietf.org/html/rfc7230
        #      tchar          = "!" / "#" / "$" / "%" / "&" / "'" / "*"
        #                     / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
        #                     / DIGIT / ALPHA

        # https://tools.ietf.org/html/rfc2046
        #      bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
        #                       "+" / "_" / "," / "-" / "." /
        #                       "/" / ":" / "=" / "?"

        "--#{SecureRandom.uuid}"
      end

      def initialize(path, params, headers={}, boundary = Multipartable.secure_boundary)
        headers = headers.clone # don't want to modify the original variable
        parts_headers = symbolize_keys(headers.delete(:parts) || {})

        super(path, headers)
        parts = symbolize_keys(params).map do |k,v|
          case v
          when Array
            v.map {|item| Parts::Part.new(boundary, k, item, parts_headers[k]) }
          else
            Parts::Part.new(boundary, k, v, parts_headers[k])
          end
        end.flatten
        parts << Parts::EpiloguePart.new(boundary)
        ios = parts.map {|p| p.to_io }
        self.set_content_type(headers["Content-Type"] || "multipart/form-data",
                              { "boundary" => boundary })
        self.content_length = parts.inject(0) {|sum,i| sum + i.length }
        self.body_stream = CompositeReadIO.new(*ios)

        @boundary = boundary
      end

      attr :boundary

      private

      def symbolize_keys(hash)
        hash.transform_keys(&:to_sym)
      end
    end
  end
end