/
builder.cr
195 lines (168 loc) · 6.88 KB
/
builder.cr
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module HTTP::Multipart
# Builds a multipart MIME message.
#
# ### Example
#
# ```
# io = IO::Memory.new # This is a stub. Actually, any IO can be used.
# multipart = HTTP::Multipart::Builder.new(io)
# multipart.body_part HTTP::Headers{"Content-Type" => "text/plain"}, "hello!"
# multipart.finish
# io.to_s # => "----------------------------DTf61dRTHYzprx7rwVQhTWr7\r\nContent-Type: text/plain\r\n\r\nhello!\r\n----------------------------DTf61dRTHYzprx7rwVQhTWr7--"
# ```
class Builder
# Creates a new `Multipart::Builder` which writes the generated multipart
# message to *io*, using the multipart boundary *boundary*.
def initialize(@io : IO, @boundary = Multipart.generate_boundary)
@state = :START
end
getter boundary
# Finite State Machine diagram: https://gist.github.com/RX14/221c1edfa98d1196711515d4b5c264eb
# Returns a content type header with multipart subtype *subtype*, and
# boundary parameter added.
#
# ```
# io = IO::Memory.new # This is a stub. Actually, any IO can be used.
# builder = HTTP::Multipart::Builder.new(io, "a4VF")
# builder.content_type("mixed") # => "multipart/mixed; boundary=\"a4VF\""
# ```
def content_type(subtype = "mixed")
String.build do |str|
str << "multipart/"
str << subtype
str << "; boundary=\""
HTTP.quote_string(@boundary, str)
str << '"'
end
end
# Appends *string* to the preamble segment of the multipart message. Throws
# if `#body_part` is called before this method.
#
# Can be called multiple times to append to the preamble multiple times.
def preamble(string : String)
preamble { |io| string.to_s(io) }
end
# Appends *data* to the preamble segment of the multipart message. Throws
# if `#body_part` is called before this method.
#
# Can be called multiple times to append to the preamble multiple times.
def preamble(data : Bytes)
preamble { |io| io.write data }
end
# Appends *preamble_io* to the preamble segment of the multipart message.
# Throws if `#body_part` is called before this method.
#
# Can be called multiple times to append to the preamble multiple times.
def preamble(preamble_io : IO)
preamble { |io| IO.copy(preamble_io, io) }
end
# Yields an IO that can be used to append to the preamble of the multipart
# message. Throws if `#body_part` is called before this method.
#
# Can be called multiple times to append to the preamble multiple times.
def preamble
fail "Cannot generate preamble: body already started" if @state != :START && @state != :PREAMBLE
yield @io
@state = :PREAMBLE
end
# Appends a body part to the multipart message with the given *headers*
# and *string*. Throws if `#finish` or `#epilogue` is called before this
# method.
def body_part(headers : HTTP::Headers, string : String)
body_part_impl(headers) { |io| string.to_s(io) }
end
# Appends a body part to the multipart message with the given *headers*
# and *data*. Throws if `#finish` or `#epilogue` is called before this
# method.
def body_part(headers : HTTP::Headers, data : Bytes)
body_part_impl(headers) { |io| io.write data }
end
# Appends a body part to the multipart message with the given *headers*
# and data from *body_io*. Throws if `#finish` or `#epilogue` is called
# before this method.
def body_part(headers : HTTP::Headers, body_io : IO)
body_part_impl(headers) { |io| IO.copy(body_io, io) }
end
# Yields an IO that can be used to write to a body part which is appended
# to the multipart message with the given *headers*. Throws if `#finish` or
# `#epilogue` is called before this method.
def body_part(headers : HTTP::Headers)
body_part_impl(headers) { |io| yield io }
end
# Appends a body part to the multipart message with the given *headers*
# and no body data. Throws is `#finish` or `#epilogue` is called before
# this method.
def body_part(headers : HTTP::Headers)
body_part_impl(headers, empty: true) { }
end
private def body_part_impl(headers, empty = false)
fail "Cannot generate body part: already finished" if @state == :FINISHED
fail "Cannot generate body part: after epilogue" if @state == :EPILOGUE
# We don't add a crlf before the first boundary if this is the first body
# part and there is no preamble
@io << "\r\n" unless @state == :START
@io << "--" << @boundary
headers.each do |name, values|
values.each do |value|
@io << "\r\n" << name << ": " << value
end
end
@io << "\r\n\r\n" unless empty
yield @io
@state = :BODY_PART
end
# Appends *string* to the epilogue segment of the multipart message. Throws
# if `#finish` is called before this method, or no body parts have been
# appended.
#
# Can be called multiple times to append to the epilogue multiple times.
def epilogue(string : String)
epilogue { |io| string.to_s(io) }
end
# Appends *data* to the epilogue segment of the multipart message. Throws
# if `#finish` is called before this method, or no body parts have been
# appended.
#
# Can be called multiple times to append to the epilogue multiple times.
def epilogue(data : Bytes)
epilogue { |io| io.write data }
end
# Appends *preamble_io* to the epilogue segment of the multipart message.
# Throws if `#finish` is called before this method, or no body parts have
# been appended.
#
# Can be called multiple times to append to the epilogue multiple times.
def epilogue(epilogue_io : IO)
epilogue { |io| IO.copy(epilogue_io, io) }
end
# Yields an IO that can be used to append to the epilogue of the multipart
# message. Throws if `#finish` is called before this method, or no body
# parts have been appended.
#
# Can be called multiple times to append to the preamble multiple times.
def epilogue
fail "Cannot generate epilogue: already finished" if @state == :FINISHED
fail "Cannot generate epilogue: no body parts" if @state == :START || @state == :PREAMBLE
if @state != :EPILOGUE
# We need to send the end boundary
@io << "\r\n--" << @boundary << "--\r\n"
end
yield @io
@state = :EPILOGUE
end
# Finalizes the multipart message, this method must be called to properly
# end the multipart message.
def finish
fail "Cannot finish multipart: no body parts" if @state == :START || @state == :PREAMBLE
fail "Cannot finish multipart: already finished" if @state == :FINISHED
if @state == :BODY_PART
# We need to send the end boundary
@io << "\r\n--" << @boundary << "--"
end
@state = :FINISHED
end
private def fail(msg)
raise Multipart::Error.new msg
end
end
end