File: multipart.rb

package info (click to toggle)
ruby-multipart-parser 0.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 128 kB
  • sloc: ruby: 602; makefile: 4
file content (137 lines) | stat: -rw-r--r-- 3,322 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Contains fixturues to
module MultipartParser::Fixtures
  # Returns all fixtures in the module
  def fixtures
    [Rfc1867.new, NoTrailingCRLF.new, EmptyHeader.new]
  end
  extend self

  class Rfc1867
    def boundary
      'AaB03x'
    end

    def expect_error
      false
    end

    def parts
      part1, part2 = {}, {}
      part1[:headers] = {'content-disposition' => 'form-data; name="field1"'}
      part1[:data] = "Joe Blow\r\nalmost tricked you!"
      part2[:headers] = {}
      part2[:headers]['content-disposition'] = 'form-data; name="pics"; ' +
                                                'filename="file1.txt"'
      part2[:headers]['Content-Type'] = 'text/plain'
      part2[:data] = "... contents of file1.txt ...\r"
      [part1, part2]
    end

    def raw
      ['--AaB03x',
        'content-disposition: form-data; name="field1"',
        '',
        "Joe Blow\r\nalmost tricked you!",
        '--AaB03x',
        'content-disposition: form-data; name="pics"; filename="file1.txt"',
        'Content-Type: text/plain',
        '',
        "... contents of file1.txt ...\r",
        '--AaB03x--',
        ''
      ].join("\r\n")
    end
  end

  class NoTrailingCRLF
    def boundary
      'AaB03x'
    end

    def expect_error
      false
    end

    def parts
      part1, part2 = {}, {}
      part1[:headers] = {'content-disposition' => 'form-data; name="field1"'}
      part1[:data] = "Joe Blow\r\nalmost tricked you!"
      part2[:headers] = {}
      part2[:headers]['content-disposition'] = 'form-data; name="pics"; ' +
                                                'filename="file1.txt"'
      part2[:headers]['Content-Type'] = 'text/plain'
      part2[:data] = "... contents of file1.txt ...\r"
      [part1, part2]
    end

    def raw
      ['--AaB03x',
        'content-disposition: form-data; name="field1"',
        '',
        "Joe Blow\r\nalmost tricked you!",
        '--AaB03x',
        'content-disposition: form-data; name="pics"; filename="file1.txt"',
        'Content-Type: text/plain',
        '',
        "... contents of file1.txt ...\r",
        '--AaB03x--'
      ].join("\r\n")
    end
  end

  class LongBoundary
    def boundary
      '----------------------------5c4dc587f69f'
    end

    def expect_error
      false
    end

    def parts
      part1 = {}
      part1[:headers] = {'content-disposition' => 'form-data; name="field1"'}
      part1[:data] = "Joe Blow\r\nalmost tricked you!"
      [part1]
    end

    def raw
      ['----------------------------5c4dc587f69f',
        'content-disposition: form-data; name="field1"',
        '',
        "Joe Blow\r\nalmost tricked you!",
        '----------------------------5c4dc587f69f--'
      ].join("\r\n")
    end
  end

  class EmptyHeader
    def boundary
      'AaB03x'
    end

    def expect_error
      true
    end

    def parts
      [] # Should never be called
    end

    def raw
      ['--AaB03x',
        'content-disposition: form-data; name="field1"',
        ': foo',
        '',
        "Joe Blow\r\nalmost tricked you!",
        '--AaB03x',
        'content-disposition: form-data; name="pics"; filename="file1.txt"',
        'Content-Type: text/plain',
        '',
        "... contents of file1.txt ...\r",
        '--AaB03x--',
        ''
      ].join("\r\n")
    end
  end
end